1# Copyright (C) 2013, 2014 Red Hat, Inc.
2#
3# This work is licensed under the GNU GPLv2 or later.
4# See the COPYING file in the top-level directory.
5
6import os
7import sys
8
9
10_alldistros = {}
11
12DEVFEDORA_URL = "http://dl.fedoraproject.org/pub/fedora/linux/development/%s/Server/%s/os/"
13FEDORA_URL = "http://dl.fedoraproject.org/pub/fedora/linux/releases/%s/Server/%s/os/"
14
15(WARN_RHEL5,
16 WARN_DEBIAN,
17 WARN_FEDORA) = range(1, 4)
18
19
20def prompt():
21    sys.stdout.write("(press enter to continue)")
22    sys.stdout.flush()
23    return sys.stdin.readline()
24
25
26KSOLD = "tests/data/inject/old-kickstart.ks"
27KSNEW = "tests/data/inject/new-kickstart.ks"
28PRESEED = "tests/data/inject/preseed.cfg"
29
30
31class Distro(object):
32    def __init__(self, name, url, filename, warntype=WARN_FEDORA):
33        self.name = name
34        self.url = url
35        self.warntype = warntype
36        self.filename = filename
37
38        self.kernel = None
39        self.initrd = None
40
41
42def _add(*args, **kwargs):
43    _d = Distro(*args, **kwargs)
44    _alldistros[_d.name] = _d
45
46
47_add("centos-5.11", "http://vault.centos.org/5.11/os/x86_64/",
48     warntype=WARN_RHEL5, filename=KSOLD)
49_add("centos-6-latest", "http://ftp.linux.ncsu.edu/pub/CentOS/6/os/x86_64/",
50     warntype=WARN_RHEL5, filename=KSOLD)
51_add("centos-7-latest", "http://ftp.linux.ncsu.edu/pub/CentOS/7/os/x86_64/",
52     filename=KSNEW)
53_add("fedora-29", FEDORA_URL % ("29", "x86_64"), filename=KSNEW)
54_add("fedora-30", DEVFEDORA_URL % ("30", "x86_64"), filename=KSNEW)
55_add("debian-9",
56     "http://ftp.us.debian.org/debian/dists/stretch/main/installer-amd64/",
57     filename=PRESEED, warntype=WARN_DEBIAN)
58
59
60def _test_distro(distro):
61    os.system("clear")
62    print("\n")
63    if distro.warntype == WARN_RHEL5:
64        print("RHEL5, RHEL6, Fedora < 17: You'll get an error about a ")
65        print("bogus bootproto ITREADTHEKICKSTART. This means anaconda ")
66        print("read our busted kickstart.")
67    elif distro.warntype == WARN_DEBIAN:
68        print("Debian: Won't ask any questions, will autoconfig network, "
69              "then print a big red text box about a bad mirror config.")
70    elif distro.warntype == WARN_FEDORA:
71        print("RHEL7, Fedora >= 17: Chokes on the bogus URI in the early ")
72        print("console screen when fetching the installer squashfs image.")
73
74    os.environ.pop("VIRTINST_TEST_SUITE", None)
75    os.environ["VIRTINST_INITRD_TEST"] = "1"
76
77    if distro.warntype == WARN_DEBIAN:
78        append = "auto=true"
79    else:
80        append = "\"ks=file:/%s\"" % os.path.basename(distro.filename)
81    cmd = ("./virt-install --connect qemu:///system "
82        "--name __virtinst__test__initrd__ --ram 2048 "
83        "--transient --destroy-on-exit --disk none "
84        "--location %s --initrd-inject %s "
85        "--install kernel_args=%s,kernel_args_overwrite=yes" %
86        (distro.url, distro.filename, append))
87    print("\n\n" + cmd)
88    os.system(cmd)
89
90
91def _print_intro():
92    print("""
93
94
95This is an interactive test suite.
96
97We are going to launch various transient virt-installs, using initrd
98injections, that will cause installs to quickly fail. Look for the
99failure pattern to confirm that initrd injections are working as expected.
100
101""")
102    prompt()
103
104
105def _build_testfunc(dobj, do_setup):
106    def testfunc():
107        if do_setup:
108            _print_intro()
109        _test_distro(dobj)
110    return testfunc
111
112
113def _make_tests():
114    idx = 0
115    for dname, dobj in _alldistros.items():
116        idx += 1
117        name = "testInitrd%.3d_%s" % (idx, dname.replace("-", "_"))
118
119        do_setup = idx == 1
120        testfunc = _build_testfunc(dobj, do_setup)
121        globals()[name] = testfunc
122
123
124_make_tests()
125