test_multiplex.py 5.9 KB
Newer Older
1 2 3 4
#!/usr/bin/env python

import os
import sys
5 6
import tempfile
import shutil
7

8 9 10 11 12
if sys.version_info[:2] == (2, 6):
    import unittest2 as unittest
else:
    import unittest

13
from avocado.core import exit_codes
14 15 16
from avocado.utils import process


17
basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..')
18 19 20
basedir = os.path.abspath(basedir)


21
DEBUG_OUT = """Variant 16:    amd@examples/mux-environment.yaml, virtio@examples/mux-environment.yaml, mint@examples/mux-environment.yaml, debug@examples/mux-environment.yaml
22 23 24 25 26 27 28
    /distro/mint:init         => systemv@examples/mux-environment.yaml:/distro/mint
    /env/debug:opt_CFLAGS     => -O0 -g@examples/mux-environment.yaml:/env/debug
    /hw/cpu/amd:cpu_CFLAGS    => -march=athlon64@examples/mux-environment.yaml:/hw/cpu/amd
    /hw/cpu/amd:joinlist      => ['first_item']@examples/mux-selftest.yaml:/hw/cpu + ['second', 'third']@examples/mux-selftest.yaml:/hw/cpu/amd
    /hw/disk/virtio:disk_type => virtio@examples/mux-environment.yaml:/hw/disk/virtio
    /hw/disk:corruptlist      => nonlist@examples/mux-selftest.yaml:/hw/disk
    /hw:corruptlist           => ['upper_node_list']@examples/mux-selftest.yaml:/hw
29 30
"""

31 32 33

class MultiplexTests(unittest.TestCase):

34
    def setUp(self):
35
        self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
36

37
    def run_and_check(self, cmd_line, expected_rc):
38 39 40 41 42
        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))
43
        return result
44 45

    def test_mplex_plugin(self):
46
        cmd_line = './scripts/avocado multiplex -m examples/tests/sleeptest.py.data/sleeptest.yaml'
47
        expected_rc = exit_codes.AVOCADO_ALL_OK
48 49 50
        self.run_and_check(cmd_line, expected_rc)

    def test_mplex_plugin_nonexistent(self):
51
        cmd_line = './scripts/avocado multiplex -m nonexist'
52
        expected_rc = exit_codes.AVOCADO_JOB_FAIL
53
        result = self.run_and_check(cmd_line, expected_rc)
54
        self.assertIn('No such file or directory', result.stderr)
55

56
    def test_mplex_debug(self):
57
        cmd_line = ('./scripts/avocado multiplex -c -d -m '
58 59 60 61
                    '/:examples/mux-selftest.yaml '
                    '/:examples/mux-environment.yaml '
                    '/:examples/mux-selftest.yaml '
                    '/:examples/mux-environment.yaml')
62
        expected_rc = exit_codes.AVOCADO_ALL_OK
63 64
        result = self.run_and_check(cmd_line, expected_rc)
        self.assertIn(DEBUG_OUT, result.stdout)
65

66
    def test_run_mplex_noid(self):
67 68
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
                    '--multiplex examples/tests/sleeptest.py.data/sleeptest.yaml' % self.tmpdir)
69
        expected_rc = exit_codes.AVOCADO_JOB_FAIL
70
        self.run_and_check(cmd_line, expected_rc)
71

72
    def test_run_mplex_passtest(self):
73 74 75 76
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
                    'passtest.py --multiplex '
                    'examples/tests/sleeptest.py.data/sleeptest.yaml'
                    % self.tmpdir)
77
        expected_rc = exit_codes.AVOCADO_ALL_OK
78
        self.run_and_check(cmd_line, expected_rc)
79

80
    def test_run_mplex_doublepass(self):
81 82 83 84
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
                    'passtest.py passtest.py --multiplex '
                    'examples/tests/sleeptest.py.data/sleeptest.yaml'
                    % self.tmpdir)
85
        self.run_and_check(cmd_line, expected_rc=0)
86 87

    def test_run_mplex_failtest(self):
88 89 90 91
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
                    'passtest.py failtest.py --multiplex '
                    'examples/tests/sleeptest.py.data/sleeptest.yaml'
                    % self.tmpdir)
92
        expected_rc = exit_codes.AVOCADO_TESTS_FAIL
93 94
        self.run_and_check(cmd_line, expected_rc)

95
    def test_run_double_mplex(self):
96 97
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
                    'passtest.py --multiplex '
98
                    'examples/tests/sleeptest.py.data/sleeptest.yaml '
99 100
                    'examples/tests/sleeptest.py.data/sleeptest.yaml'
                    % self.tmpdir)
101
        expected_rc = exit_codes.AVOCADO_ALL_OK
102
        self.run_and_check(cmd_line, expected_rc)
103

104
    def test_run_mplex_params(self):
105 106 107 108 109 110 111 112 113 114 115 116
        for variant_msg in (('/run/short', 'A'),
                            ('/run/medium', 'ASDFASDF'),
                            ('/run/long', 'This is very long\nmultiline\ntext.')):
            variant, msg = variant_msg
            cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off examples/tests/env_variables.sh '
                        '--multiplex examples/tests/env_variables.sh.data/env_variables.yaml '
                        '--filter-only %s --show-job-log' % (self.tmpdir, variant))
            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]
117
            self.assertIn(msg_header, result.stdout,
118
                          "Multiplexed variable should produce:"
119
                          "\n  %s\nwhich is not present in the output:\n  %s"
120
                          % (msg_header, "\n  ".join(result.stdout.splitlines())))
121
            for msg_remain in msg_lines[1:]:
122
                self.assertIn('[stdout] %s' % msg_remain, result.stdout,
123 124
                              "Multiplexed variable should produce:"
                              "\n  %s\nwhich is not present in the output:\n  %s"
125
                              % (msg_remain, "\n  ".join(result.stdout.splitlines())))
126

127 128 129
    def tearDown(self):
        shutil.rmtree(self.tmpdir)

130 131
if __name__ == '__main__':
    unittest.main()