test_loader.py 13.0 KB
Newer Older
1
import os
2
import json
3
import subprocess
4
import time
5
import stat
6 7
import tempfile
import shutil
8
import signal
9
import unittest
10

11
from avocado.core import exit_codes
12 13 14 15
from avocado.utils import script
from avocado.utils import process


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

19 20
AVOCADO = os.environ.get("UNITTEST_AVOCADO_CMD", "./scripts/avocado")

21

22
AVOCADO_TEST_OK = """#!/usr/bin/env python
23
from avocado import Test
24
from avocado import main
25

26
class PassTest(Test):
27
    def test(self):
28 29 30
        pass

if __name__ == "__main__":
31
    main()
32 33
"""

34

35
AVOCADO_TEST_SLEEP_ELEVEN = """#!/usr/bin/env python
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
import time

from avocado import Test
from avocado import main

class SleepEleven(Test):
    def test(self):
        time.sleep(10)
    def test_2(self):
        time.sleep(1)

time.sleep(11)

if __name__ == "__main__":
    main()
"""


54
AVOCADO_TEST_MULTIPLE_CLASSES = """#!/usr/bin/env python
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
import time

from avocado import Test
from avocado import main

class First(Test):
    def test(self):
        pass

class Second(Test):
    def test(self):
        pass

if __name__ == "__main__":
    main()
"""

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
AVOCADO_TEST_MULTIPLE_METHODS_SAME_NAME = """#!/usr/bin/env python
from avocado import Test
from avocado import main

class Multiple(Test):
    def test(self):
        raise

    def test(self):
        pass

if __name__ == "__main__":
    main()
"""

87 88
NOT_A_TEST = """
def hello():
89
    print('Hello World!')
90 91
"""

92
PY_SIMPLE_TEST = """#!/usr/bin/env python
93
def hello():
94
    print('Hello World!')
95 96 97 98 99 100 101 102 103

if __name__ == "__main__":
    hello()
"""

SIMPLE_TEST = """#!/bin/sh
true
"""

104 105 106 107 108 109 110 111
AVOCADO_SIMPLE_PYTHON_LIKE_MULTIPLE_FILES = """#!/usr/bin/env python
# A simple test (executable bit set when saved to file) that looks like
# an Avocado instrumented test, with base class on separate file
from avocado import Test
from avocado import main
from test2 import *

class BasicTestSuite(SuperTest):
112 113 114
    '''
    :avocado: disable
    '''
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

    def test1(self):
        self.xxx()
        self.assertTrue(True)

if __name__ == '__main__':
    main()
"""

AVOCADO_SIMPLE_PYTHON_LIKE_MULTIPLE_FILES_LIB = """
#!/usr/bin/python

from avocado import Test

class SuperTest(Test):
    def xxx(self):
        print "ahoj"
"""

AVOCADO_TEST_SIMPLE_USING_MAIN = """#!/usr/bin/env python
from avocado import main

if __name__ == "__main__":
    main()
"""

141 142 143

class LoaderTestFunctional(unittest.TestCase):

A
Amador Pahim 已提交
144 145 146 147
    MODE_0644 = (stat.S_IRUSR | stat.S_IWUSR |
                 stat.S_IRGRP |
                 stat.S_IROTH)

148 149 150 151 152 153 154 155
    MODE_0664 = (stat.S_IRUSR | stat.S_IWUSR |
                 stat.S_IRGRP | stat.S_IWGRP |
                 stat.S_IROTH)

    MODE_0775 = (stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
                 stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP |
                 stat.S_IROTH | stat.S_IXOTH)

156
    def setUp(self):
157
        os.chdir(basedir)
158
        self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
159

160
    def _test(self, name, content, exp_str, mode=MODE_0664, count=1):
161 162 163 164
        test_script = script.TemporaryScript(name, content,
                                             'avocado_loader_test',
                                             mode=mode)
        test_script.save()
165
        cmd_line = ('%s list -V %s' % (AVOCADO, test_script.path))
166
        result = process.run(cmd_line)
167
        self.assertIn('%s: %s' % (exp_str, count), result.stdout_text)
168 169
        test_script.remove()

170 171 172 173
    def _run_with_timeout(self, cmd_line, timeout):
        current_time = time.time()
        deadline = current_time + timeout
        test_process = subprocess.Popen(cmd_line, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
174
                                        preexec_fn=os.setsid, shell=True)
175 176 177 178 179
        while not test_process.poll():
            if time.time() > deadline:
                os.killpg(os.getpgid(test_process.pid), signal.SIGKILL)
                self.fail("Failed to run test under %s seconds" % timeout)
            time.sleep(0.05)
180
        self.assertEqual(test_process.returncode, exit_codes.AVOCADO_TESTS_FAIL)
181

182
    def test_simple(self):
183
        self._test('simpletest.sh', SIMPLE_TEST, 'SIMPLE', self.MODE_0775)
184 185

    def test_simple_not_exec(self):
186
        self._test('simpletest.sh', SIMPLE_TEST, 'NOT_A_TEST')
187 188

    def test_pass(self):
189
        self._test('passtest.py', AVOCADO_TEST_OK, 'INSTRUMENTED')
190

191 192 193
    def test_not_python_module(self):
        self._test('passtest', AVOCADO_TEST_OK, 'NOT_A_TEST')

194 195 196
    @unittest.skipIf(int(os.environ.get("AVOCADO_CHECK_LEVEL", 0)) < 1,
                     "Skipping test that take a long time to run, are "
                     "resource intensive or time sensitve")
197 198 199 200 201 202 203 204
    def test_sleep_a_lot(self):
        """
        Verifies that the test loader, at list time, does not load the Python
        module and thus executes its contents.
        """
        test_script = script.TemporaryScript('sleepeleven.py',
                                             AVOCADO_TEST_SLEEP_ELEVEN,
                                             'avocado_loader_test',
205
                                             mode=self.MODE_0664)
206
        test_script.save()
207
        cmd_line = ('%s list -V %s' % (AVOCADO, test_script.path))
208 209 210 211 212 213 214 215
        initial_time = time.time()
        result = process.run(cmd_line, ignore_status=True)
        test_script.remove()
        actual_time = time.time() - initial_time
        self.assertLess(actual_time, 3.0,
                        ("Took more than 3 seconds to list tests. Loader "
                         "probably loaded/executed Python code and slept for "
                         "eleven seconds."))
216
        self.assertIn(b'INSTRUMENTED: 2', result.stdout)
217

218 219
    def test_multiple_class(self):
        self._test('multipleclasses.py', AVOCADO_TEST_MULTIPLE_CLASSES,
220
                   'INSTRUMENTED', self.MODE_0664, 2)
221

222 223
    def test_multiple_methods_same_name(self):
        self._test('multiplemethods.py', AVOCADO_TEST_MULTIPLE_METHODS_SAME_NAME,
224
                   'INSTRUMENTED', self.MODE_0664, 1)
225

226
    def test_load_not_a_test(self):
227
        self._test('notatest.py', NOT_A_TEST, 'SIMPLE', self.MODE_0775)
228 229

    def test_load_not_a_test_not_exec(self):
230
        self._test('notatest.py', NOT_A_TEST, 'NOT_A_TEST')
231

232
    @unittest.skipIf(int(os.environ.get("AVOCADO_CHECK_LEVEL", 0)) < 2,
233 234
                     "Skipping test that take a long time to run, are "
                     "resource intensive or time sensitve")
235 236 237 238 239
    def test_runner_simple_python_like_multiple_files(self):
        mylib = script.TemporaryScript(
            'test2.py',
            AVOCADO_SIMPLE_PYTHON_LIKE_MULTIPLE_FILES_LIB,
            'avocado_simpletest_functional',
240
            self.MODE_0644)
241 242 243 244 245 246
        mylib.save()
        mytest = script.Script(
            os.path.join(os.path.dirname(mylib.path), 'test.py'),
            AVOCADO_SIMPLE_PYTHON_LIKE_MULTIPLE_FILES)
        os.chdir(basedir)
        mytest.save()
247
        cmd_line = "%s list -V %s" % (AVOCADO, mytest)
248
        result = process.run(cmd_line)
249
        self.assertIn(b'SIMPLE: 1', result.stdout)
250 251
        # job should be able to finish under 5 seconds. If this fails, it's
        # possible that we hit the "simple test fork bomb" bug
252 253
        cmd_line = ("%s run --sysinfo=off --job-results-dir '%s' -- '%s'"
                    % (AVOCADO, self.tmpdir, mytest))
254 255
        self._run_with_timeout(cmd_line, 5)

256 257 258
    @unittest.skipIf(int(os.environ.get("AVOCADO_CHECK_LEVEL", 0)) < 1,
                     "Skipping test that take a long time to run, are "
                     "resource intensive or time sensitve")
259 260 261 262 263 264 265 266
    def test_simple_using_main(self):
        mytest = script.TemporaryScript("simple_using_main.py",
                                        AVOCADO_TEST_SIMPLE_USING_MAIN,
                                        'avocado_simpletest_functional')
        mytest.save()
        os.chdir(basedir)
        # job should be able to finish under 5 seconds. If this fails, it's
        # possible that we hit the "simple test fork bomb" bug
267 268
        cmd_line = ("%s run --sysinfo=off --job-results-dir '%s' -- '%s'"
                    % (AVOCADO, self.tmpdir, mytest))
269 270
        self._run_with_timeout(cmd_line, 5)

271 272 273 274 275 276 277 278 279 280 281 282
    @unittest.skipUnless(os.path.exists("/bin/true"), "/bin/true not "
                         "available")
    @unittest.skipUnless(os.path.exists("/bin/echo"), "/bin/echo not "
                         "available")
    def test_yaml_loader_list(self):
        # Verifies that yaml_loader list won't crash and is able to detect
        # various test types
        result = process.run("%s list -V --loaders yaml_testsuite -- "
                             "examples/yaml_to_mux_loader/loaders.yaml"
                             % AVOCADO)
        # This has to be defined like this as pep8 complains about tailing
        # empty spaces when using """
283
        self.assertRegexpMatches(result.stdout_text, r"Type *Test *Tag\(s\)\n"
284 285 286 287 288 289 290 291 292 293
                                 r"INSTRUMENTED *passtest.py:PassTest.test *"
                                 "fast\n"
                                 r"SIMPLE.*passtest.sh *\n"
                                 r"EXTERNAL *external_echo *\n"
                                 r"EXTERNAL *external_false *\n")
        # Also check whether list without loaders won't crash
        result = process.run("%s list -V -- "
                             "examples/yaml_to_mux_loader/loaders.yaml"
                             % AVOCADO)

294 295 296 297 298 299 300 301 302 303 304
    def test_yaml_loader_run(self):
        # Checks that yaml_loader supplies correct params and that
        # --mux-suite-only filters the test suite
        result = process.run("%s --show test run --dry-run --mux-suite-only "
                             "/run/tests/sleeptest -- examples/yaml_to_mux_"
                             "loader/advanced.yaml" % AVOCADO)
        test = -1
        exp_timeouts = [2] * 4 + [6] * 4 + [None] * 4
        exp_timeout = None
        exp_sleep_lengths = [0.5, 1, 5, 10] * 3
        exp_sleep_length = None
305
        for line in result.stdout_text.splitlines():
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
            if line.startswith("START "):
                self.assertFalse(exp_timeout, "%s was not found in test %ss "
                                 "output:\n%s" % (exp_timeout, test, result))
                self.assertFalse(exp_timeout, "%s was not found in test %ss "
                                 "output:\n%s" % (exp_sleep_length, test,
                                                  result))
                self.assertLess(test, 12, "Number of tests is greater than "
                                "12:\n%s" % result)
                test += 1
                timeout = exp_timeouts[test]
                if timeout:
                    exp_timeout = "timeout ==> %s" % timeout
                else:
                    exp_timeout = "(key=timeout, path=*, default=None) => None"
                exp_sleep_length = ("sleep_length ==> %s"
                                    % exp_sleep_lengths[test])
            elif exp_timeout and exp_timeout in line:
                exp_timeout = None
            elif exp_sleep_length and exp_sleep_length in line:
                exp_sleep_length = None
        self.assertEqual(test, 11, "Number of tests is not 12 (%s):\n%s"
                         % (test, result))

329 330 331 332 333
    def test_python_unittest(self):
        test_path = os.path.join(basedir, "selftests", ".data", "unittests.py")
        cmd = ("%s run --sysinfo=off --job-results-dir %s --json - -- %s"
               % (AVOCADO, self.tmpdir, test_path))
        result = process.run(cmd, ignore_status=True)
334
        jres = json.loads(result.stdout_text)
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
        self.assertEqual(result.exit_status, 1, result)
        exps = [("unittests.Second.test_fail", "FAIL"),
                ("unittests.Second.test_error", "ERROR"),
                ("unittests.Second.test_skip", "CANCEL"),
                ("unittests.First.test_pass", "PASS")]
        for test in jres["tests"]:
            for exp in exps:
                if exp[0] in test["id"]:
                    self.assertEqual(test["status"], exp[1], "Status of %s not"
                                     " as expected\n%s" % (exp, result))
                    exps.remove(exp)
                    break
            else:
                self.fail("No expected result for %s\n%s\n\nexps = %s"
                          % (test["id"], result, exps))
        self.assertFalse(exps, "Some expected result not matched to actual"
                         "results:\n%s\n\nexps = %s" % (result, exps))

353 354 355 356 357 358 359
    def test_list_subtests_filter(self):
        """
        Check whether the subtests filter works for both INSTRUMENTED
        and SIMPLE in a directory list.
        """
        cmd = "%s list examples/tests/:fail" % AVOCADO
        result = process.run(cmd)
360 361 362 363
        expected = (b"INSTRUMENTED examples/tests/doublefail.py:DoubleFail.test\n"
                    b"INSTRUMENTED examples/tests/fail_on_exception.py:FailOnException.test\n"
                    b"INSTRUMENTED examples/tests/failtest.py:FailTest.test\n"
                    b"SIMPLE       examples/tests/failtest.sh\n")
364 365
        self.assertEqual(expected, result.stdout)

366 367 368 369
    def tearDown(self):
        shutil.rmtree(self.tmpdir)


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