test_multiplex.py 9.6 KB
Newer Older
1
import os
2 3
import tempfile
import shutil
4
import unittest
5
import sys
6

7
from avocado.core import exit_codes
8 9
from avocado.utils import process

10

11
basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', '..')
12 13
basedir = os.path.abspath(basedir)

14
AVOCADO = os.environ.get("UNITTEST_AVOCADO_CMD", "./scripts/avocado")
15

16
DEBUG_OUT = b"""
17
Variant mint-debug-amd-virtio-022a:    amd@optional_plugins/varianter_yaml_to_mux/tests/.data/mux-environment.yaml, virtio@optional_plugins/varianter_yaml_to_mux/tests/.data/mux-environment.yaml, mint@optional_plugins/varianter_yaml_to_mux/tests/.data/mux-environment.yaml, debug@optional_plugins/varianter_yaml_to_mux/tests/.data/mux-environment.yaml
18 19 20 21 22 23 24
    /distro/mint:init         => systemv@optional_plugins/varianter_yaml_to_mux/tests/.data/mux-environment.yaml:/distro/mint
    /env/debug:opt_CFLAGS     => -O0 -g@optional_plugins/varianter_yaml_to_mux/tests/.data/mux-environment.yaml:/env/debug
    /hw/cpu/amd:cpu_CFLAGS    => -march=athlon64@optional_plugins/varianter_yaml_to_mux/tests/.data/mux-environment.yaml:/hw/cpu/amd
    /hw/cpu/amd:joinlist      => ['first_item']@optional_plugins/varianter_yaml_to_mux/tests/.data/mux-selftest.yaml:/hw/cpu + ['second', 'third']@optional_plugins/varianter_yaml_to_mux/tests/.data/mux-selftest.yaml:/hw/cpu/amd
    /hw/disk/virtio:disk_type => virtio@optional_plugins/varianter_yaml_to_mux/tests/.data/mux-environment.yaml:/hw/disk/virtio
    /hw/disk:corruptlist      => nonlist@optional_plugins/varianter_yaml_to_mux/tests/.data/mux-selftest.yaml:/hw/disk
    /hw:corruptlist           => ['upper_node_list']@optional_plugins/varianter_yaml_to_mux/tests/.data/mux-selftest.yaml:/hw
25 26
"""

27 28 29

class MultiplexTests(unittest.TestCase):

30
    def setUp(self):
31
        self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
32

33
    def run_and_check(self, cmd_line, expected_rc, tests=None):
34 35 36 37 38
        os.chdir(basedir)
        result = process.run(cmd_line, ignore_status=True)
        self.assertEqual(result.exit_status, expected_rc,
                         "Command %s did not return rc "
                         "%d:\n%s" % (cmd_line, expected_rc, result))
39
        if tests is not None:
40 41
            exp = ("PASS %s | ERROR 0 | FAIL %s | SKIP 0 | WARN 0 | "
                   "INTERRUPT 0" % tests)
42
            self.assertIn(exp, result.stdout_text, "%s not in stdout:\n%s"
43
                          % (exp, result))
44
        return result
45 46

    def test_mplex_plugin(self):
47
        cmd_line = ('%s variants -m examples/tests/sleeptest.py.data/'
48
                    'sleeptest.yaml' % AVOCADO)
49
        expected_rc = exit_codes.AVOCADO_ALL_OK
50 51 52
        self.run_and_check(cmd_line, expected_rc)

    def test_mplex_plugin_nonexistent(self):
53
        cmd_line = '%s variants -m nonexist' % AVOCADO
54
        expected_rc = exit_codes.AVOCADO_FAIL
55
        result = self.run_and_check(cmd_line, expected_rc)
56
        self.assertIn('No such file or directory', result.stderr_text)
57

58 59 60 61 62 63 64
    def test_mplex_plugin_using(self):
        cmd_line = ('%s variants -m /:optional_plugins/varianter_yaml_to_mux/'
                    'tests/.data/mux-selftest-using.yaml' % AVOCADO)
        expected_rc = exit_codes.AVOCADO_ALL_OK
        result = self.run_and_check(cmd_line, expected_rc)
        self.assertIn(b' /foo/baz/bar', result.stdout)

65 66
    @unittest.skipIf(sys.version_info[0] == 3,
                     "Test currently broken on Python 3")
67
    def test_mplex_debug(self):
68
        cmd_line = ('%s variants -c -d -m '
69 70 71 72 73
                    '/:optional_plugins/varianter_yaml_to_mux/tests/.data/mux-selftest.yaml '
                    '/:optional_plugins/varianter_yaml_to_mux/tests/.data/mux-environment.yaml '
                    '/:optional_plugins/varianter_yaml_to_mux/tests/.data/mux-selftest.yaml '
                    '/:optional_plugins/varianter_yaml_to_mux/tests/.data/mux-environment.yaml'
                    % AVOCADO)
74
        expected_rc = exit_codes.AVOCADO_ALL_OK
75 76
        result = self.run_and_check(cmd_line, expected_rc)
        self.assertIn(DEBUG_OUT, result.stdout)
77

78
    def test_run_mplex_noid(self):
79 80 81
        cmd_line = ('%s run --job-results-dir %s --sysinfo=off '
                    '-m examples/tests/sleeptest.py.data/sleeptest.yaml'
                    % (AVOCADO, self.tmpdir))
82
        expected_rc = exit_codes.AVOCADO_JOB_FAIL
83
        self.run_and_check(cmd_line, expected_rc)
84

85
    def test_run_mplex_passtest(self):
86
        cmd_line = ('%s run --job-results-dir %s --sysinfo=off '
87
                    'passtest.py -m '
88
                    'examples/tests/sleeptest.py.data/sleeptest.yaml'
89
                    % (AVOCADO, self.tmpdir))
90
        expected_rc = exit_codes.AVOCADO_ALL_OK
91
        self.run_and_check(cmd_line, expected_rc, (4, 0))
92
        # Also check whether jobdata contains correct parameter paths
93 94 95
        with open(os.path.join(self.tmpdir, "latest", "jobdata",
                  "variants.json")) as variants_file:
            variants = variants_file.read()
96 97
        self.assertIn('["/run/*"]', variants, "parameter paths stored in "
                      "jobdata does not contains [\"/run/*\"]\n%s" % variants)
98

99
    def test_run_mplex_doublepass(self):
100
        cmd_line = ('%s run --job-results-dir %s --sysinfo=off '
101
                    'passtest.py passtest.py -m '
102 103
                    'examples/tests/sleeptest.py.data/sleeptest.yaml '
                    '--mux-path /foo/\\* /bar/\\* /baz/\\*'
104
                    % (AVOCADO, self.tmpdir))
105
        self.run_and_check(cmd_line, exit_codes.AVOCADO_ALL_OK, (8, 0))
106
        # Also check whether jobdata contains correct parameter paths
107 108 109
        with open(os.path.join(self.tmpdir, "latest", "jobdata",
                  "variants.json")) as variants_file:
            variants = variants_file.read()
110
        exp = '["/foo/*", "/bar/*", "/baz/*"]'
111
        self.assertIn(exp, variants, "parameter paths stored in jobdata "
112
                      "does not contains %s\n%s" % (exp, variants))
113 114

    def test_run_mplex_failtest(self):
115
        cmd_line = ('%s run --job-results-dir %s --sysinfo=off '
116
                    'passtest.py failtest.py -m '
117
                    'examples/tests/sleeptest.py.data/sleeptest.yaml'
118
                    % (AVOCADO, self.tmpdir))
119
        expected_rc = exit_codes.AVOCADO_TESTS_FAIL
120
        result = self.run_and_check(cmd_line, expected_rc, (4, 4))
121 122 123
        self.assertIn(b"(1/8) passtest.py:PassTest.test;short", result.stdout)
        self.assertIn(b"(2/8) passtest.py:PassTest.test;medium", result.stdout)
        self.assertIn(b"(8/8) failtest.py:FailTest.test;longest",
124 125 126 127 128 129 130 131 132 133
                      result.stdout)

    def test_run_mplex_failtest_tests_per_variant(self):
        cmd_line = ("%s run --job-results-dir %s --sysinfo=off "
                    "passtest.py failtest.py -m "
                    "examples/tests/sleeptest.py.data/sleeptest.yaml "
                    "--execution-order tests-per-variant"
                    % (AVOCADO, self.tmpdir))
        expected_rc = exit_codes.AVOCADO_TESTS_FAIL
        result = self.run_and_check(cmd_line, expected_rc, (4, 4))
134 135 136
        self.assertIn(b"(1/8) passtest.py:PassTest.test;short", result.stdout)
        self.assertIn(b"(2/8) failtest.py:FailTest.test;short", result.stdout)
        self.assertIn(b"(8/8) failtest.py:FailTest.test;longest",
137
                      result.stdout)
138

139
    def test_run_double_mplex(self):
140
        cmd_line = ('%s run --job-results-dir %s --sysinfo=off '
141
                    'passtest.py -m '
142
                    'examples/tests/sleeptest.py.data/sleeptest.yaml '
143
                    'examples/tests/sleeptest.py.data/sleeptest.yaml'
144
                    % (AVOCADO, self.tmpdir))
145
        expected_rc = exit_codes.AVOCADO_ALL_OK
146
        self.run_and_check(cmd_line, expected_rc, (4, 0))
147

148
    def test_empty_file(self):
149 150 151
        cmd_line = ("%s run --job-results-dir %s -m optional_plugins/"
                    "varianter_yaml_to_mux/tests/.data/empty_file -- "
                    "passtest.py" % (AVOCADO, self.tmpdir))
152
        self.run_and_check(cmd_line, exit_codes.AVOCADO_ALL_OK, (1, 0))
153

154 155
    @unittest.skipIf(sys.version_info[0] == 3,
                     "Test currently broken on Python 3")
156
    def test_run_mplex_params(self):
157 158 159 160
        for variant_msg in (('/run/short', 'A'),
                            ('/run/medium', 'ASDFASDF'),
                            ('/run/long', 'This is very long\nmultiline\ntext.')):
            variant, msg = variant_msg
161 162
            cmd_line = ('%s run --job-results-dir %s --sysinfo=off '
                        'examples/tests/env_variables.sh '
163
                        '-m examples/tests/env_variables.sh.data/env_variables.yaml '
164
                        '--mux-filter-only %s --show-job-log'
165
                        % (AVOCADO, self.tmpdir, variant))
166 167 168 169 170
            expected_rc = exit_codes.AVOCADO_ALL_OK
            result = self.run_and_check(cmd_line, expected_rc)

            msg_lines = msg.splitlines()
            msg_header = '[stdout] Custom variable: %s' % msg_lines[0]
171
            self.assertIn(msg_header, result.stdout_text,
172
                          "Multiplexed variable should produce:"
173
                          "\n  %s\nwhich is not present in the output:\n  %s"
174
                          % (msg_header, "\n  ".join(result.stdout_text.splitlines())))
175
            for msg_remain in msg_lines[1:]:
176
                self.assertIn('[stdout] %s' % msg_remain, result.stdout_text,
177 178
                              "Multiplexed variable should produce:"
                              "\n  %s\nwhich is not present in the output:\n  %s"
179
                              % (msg_remain, "\n  ".join(result.stdout_text.splitlines())))
180

181 182 183
    def tearDown(self):
        shutil.rmtree(self.tmpdir)

L
Lukáš Doktor 已提交
184

185 186
if __name__ == '__main__':
    unittest.main()