test_replay_basic.py 8.0 KB
Newer Older
1 2 3 4
import glob
import os
import tempfile
import shutil
5
import unittest
6 7 8 9 10 11 12 13

from avocado.core import exit_codes
from avocado.utils import process


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

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

16 17 18 19 20

class ReplayTests(unittest.TestCase):

    def setUp(self):
        self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
21
        cmd_line = ('%s run passtest.py '
22
                    '-m examples/tests/sleeptest.py.data/sleeptest.yaml '
23 24
                    '--job-results-dir %s --sysinfo=off --json -'
                    % (AVOCADO, self.tmpdir))
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
        expected_rc = exit_codes.AVOCADO_ALL_OK
        self.run_and_check(cmd_line, expected_rc)
        self.jobdir = ''.join(glob.glob(os.path.join(self.tmpdir, 'job-*')))
        idfile = ''.join(os.path.join(self.jobdir, 'id'))
        with open(idfile, 'r') as f:
            self.jobid = f.read().strip('\n')

    def run_and_check(self, cmd_line, expected_rc):
        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))
        return result

    def test_run_replay_noid(self):
41 42 43
        """
        Runs a replay job with an invalid jobid.
        """
44 45 46
        cmd_line = ('%s run --replay %s '
                    '--job-results-dir %s --sysinfo=off'
                    % (AVOCADO, 'foo', self.tmpdir))
47
        expected_rc = exit_codes.AVOCADO_FAIL
48 49
        self.run_and_check(cmd_line, expected_rc)

50
    def test_run_replay_latest(self):
51 52 53
        """
        Runs a replay job using the 'latest' keyword.
        """
54 55
        cmd_line = ('%s run --replay latest --job-results-dir %s --sysinfo=off'
                    % (AVOCADO, self.tmpdir))
56 57 58
        expected_rc = exit_codes.AVOCADO_ALL_OK
        self.run_and_check(cmd_line, expected_rc)

59
    def test_run_replay_data(self):
60 61 62
        """
        Checks if all expected files are there.
        """
63
        file_list = ['variants.json', 'config', 'test_references', 'pwd',
L
Lukáš Doktor 已提交
64
                     'args.json', 'cmdline']
65
        for filename in file_list:
66
            path = os.path.join(self.jobdir, 'jobdata', filename)
67 68 69
            self.assertTrue(glob.glob(path))

    def test_run_replay(self):
70 71 72
        """
        Runs a replay job.
        """
73
        cmd_line = ('%s run --replay %s '
74
                    '--job-results-dir %s --sysinfo=off'
75
                    % (AVOCADO, self.jobid, self.tmpdir))
76 77 78 79
        expected_rc = exit_codes.AVOCADO_ALL_OK
        self.run_and_check(cmd_line, expected_rc)

    def test_run_replay_partialid(self):
80 81 82
        """
        Runs a replay job with a partial jobid.
        """
83
        partial_id = self.jobid[:5]
84
        cmd_line = ('%s run --replay %s '
85
                    '--job-results-dir %s --sysinfo=off'
86
                    % (AVOCADO, partial_id, self.tmpdir))
87 88 89 90 91 92 93
        expected_rc = exit_codes.AVOCADO_ALL_OK
        self.run_and_check(cmd_line, expected_rc)

    def test_run_replay_results_as_jobid(self):
        """
        Runs a replay job identifying the job by its results directory.
        """
94
        cmd_line = ('%s run --replay %s '
95
                    '--job-results-dir %s --sysinfo=off'
96
                    % (AVOCADO, self.jobdir, self.tmpdir))
97 98 99 100
        expected_rc = exit_codes.AVOCADO_ALL_OK
        self.run_and_check(cmd_line, expected_rc)

    def test_run_replay_invalidignore(self):
101 102 103
        """
        Runs a replay job with an invalid option for '--replay-ignore'
        """
104
        cmd_line = ('%s run --replay %s --replay-ignore foo'
105
                    '--job-results-dir %s --sysinfo=off'
106
                    % (AVOCADO, self.jobid, self.tmpdir))
107
        expected_rc = exit_codes.AVOCADO_FAIL
108
        result = self.run_and_check(cmd_line, expected_rc)
109 110
        msg = (b'Invalid --replay-ignore option. Valid options are '
               b'(more than one allowed): variants,config')
111 112
        self.assertIn(msg, result.stderr)

113
    def test_run_replay_ignorevariants(self):
114
        """
115
        Runs a replay job ignoring the variants.
116
        """
117
        cmd_line = ('%s run --replay %s --replay-ignore variants '
118
                    '--job-results-dir %s --sysinfo=off'
119
                    % (AVOCADO, self.jobid, self.tmpdir))
120 121
        expected_rc = exit_codes.AVOCADO_ALL_OK
        result = self.run_and_check(cmd_line, expected_rc)
122
        msg = b'Ignoring variants from source job with --replay-ignore.'
123
        self.assertIn(msg, result.stderr)
124 125

    def test_run_replay_invalidstatus(self):
126 127 128
        """
        Runs a replay job with an invalid option for '--replay-test-status'
        """
129
        cmd_line = ('%s run --replay %s --replay-test-status E '
130
                    '--job-results-dir %s --sysinfo=off'
131
                    % (AVOCADO, self.jobid, self.tmpdir))
132
        expected_rc = exit_codes.AVOCADO_FAIL
133
        result = self.run_and_check(cmd_line, expected_rc)
134 135
        msg = (b'Invalid --replay-test-status option. Valid options are (more '
               b'than one allowed): SKIP,ERROR,FAIL,WARN,PASS,INTERRUPTED')
136 137 138
        self.assertIn(msg, result.stderr)

    def test_run_replay_statusfail(self):
139 140 141
        """
        Runs a replay job only with tests that failed.
        """
142 143 144
        cmd_line = ('%s run --replay %s --replay-test-status '
                    'FAIL --job-results-dir %s --sysinfo=off'
                    % (AVOCADO, self.jobid, self.tmpdir))
145 146
        expected_rc = exit_codes.AVOCADO_ALL_OK
        result = self.run_and_check(cmd_line, expected_rc)
147 148
        msg = (b'RESULTS    : PASS 0 | ERROR 0 | FAIL 0 | SKIP 4 | WARN 0 | '
               b'INTERRUPT 0')
149 150 151
        self.assertIn(msg, result.stdout)

    def test_run_replay_remotefail(self):
152 153 154
        """
        Runs a replay job using remote plugin (not supported).
        """
155 156 157
        cmd_line = ('%s run --replay %s --remote-hostname '
                    'localhost --job-results-dir %s --sysinfo=off'
                    % (AVOCADO, self.jobid, self.tmpdir))
158
        expected_rc = exit_codes.AVOCADO_FAIL
159
        result = self.run_and_check(cmd_line, expected_rc)
160
        msg = b"Currently we don't replay jobs in remote hosts."
161 162
        self.assertIn(msg, result.stderr)

163
    def test_run_replay_status_and_variants(self):
164
        """
165
        Runs a replay job with custom variants using '--replay-test-status'
166
        """
167
        cmd_line = ('%s run --replay %s --replay-ignore variants '
168
                    '--replay-test-status FAIL --job-results-dir %s '
169
                    '--sysinfo=off' % (AVOCADO, self.jobid, self.tmpdir))
170 171
        expected_rc = exit_codes.AVOCADO_FAIL
        result = self.run_and_check(cmd_line, expected_rc)
172 173
        msg = (b"Option `--replay-test-status` is incompatible with "
               b"`--replay-ignore variants`")
174 175
        self.assertIn(msg, result.stderr)

176
    def test_run_replay_status_and_references(self):
177
        """
178
        Runs a replay job with custom test references and --replay-test-status
179
        """
180
        cmd_line = ('%s run sleeptest --replay %s '
181
                    '--replay-test-status FAIL --job-results-dir %s '
182
                    '--sysinfo=off' % (AVOCADO, self.jobid, self.tmpdir))
183 184
        expected_rc = exit_codes.AVOCADO_FAIL
        result = self.run_and_check(cmd_line, expected_rc)
185 186
        msg = (b"Option --replay-test-status is incompatible with "
               b"test references given on the command line.")
187 188
        self.assertIn(msg, result.stderr)

189 190 191 192 193
    def test_run_replay_and_mux(self):
        """
        Runs a replay job and specifies multiplex file (which should be
        ignored)
        """
194
        cmdline = ("%s run --replay %s --job-results-dir %s "
195
                   "--sysinfo=off -m selftests/.data/mux-selftest.yaml"
196
                   % (AVOCADO, self.jobid, self.tmpdir))
197 198
        self.run_and_check(cmdline, exit_codes.AVOCADO_ALL_OK)

199 200 201 202 203 204
    def tearDown(self):
        shutil.rmtree(self.tmpdir)


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