test_job_timeout.py 6.3 KB
Newer Older
1 2 3 4
import os
import sys
import tempfile
import shutil
5
import xml.dom.minidom
6 7 8 9 10 11

if sys.version_info[:2] == (2, 6):
    import unittest2 as unittest
else:
    import unittest

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


17
basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..')
18 19 20 21 22 23 24 25 26
basedir = os.path.abspath(basedir)


SCRIPT_CONTENT = """#!/bin/bash
sleep 2
"""

PYTHON_CONTENT = """#!/usr/bin/env python
import time
27
from avocado import Test
28

29
class Dummy(Test):
30 31 32 33 34 35 36 37 38
    def test00sleep(self):
        time.sleep(2)
    def test01pass(self):
        pass
    def test02pass(self):
        pass
"""


39 40 41 42
class ParseXMLError(Exception):
    pass


43 44 45 46 47 48 49 50 51 52 53 54 55
class JobTimeOutTest(unittest.TestCase):

    def setUp(self):
        self.script = script.TemporaryScript(
            'sleep.sh',
            SCRIPT_CONTENT,
            'avocado_timeout_functional')
        self.script.save()
        self.py = script.TemporaryScript(
            'sleep_test.py',
            PYTHON_CONTENT,
            'avocado_timeout_functional')
        self.py.save()
56
        self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
57 58
        os.chdir(basedir)

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    def run_and_check(self, cmd_line, e_rc, e_ntests, e_nerrors, e_nfailures,
                      e_nskip):
        os.chdir(basedir)
        result = process.run(cmd_line, ignore_status=True)
        xml_output = result.stdout
        self.assertEqual(result.exit_status, e_rc,
                         "Avocado did not return rc %d:\n%s" %
                         (e_rc, result))
        try:
            xunit_doc = xml.dom.minidom.parseString(xml_output)
        except Exception, detail:
            raise ParseXMLError("Failed to parse content: %s\n%s" %
                                (detail, xml_output))

        testsuite_list = xunit_doc.getElementsByTagName('testsuite')
        self.assertEqual(len(testsuite_list), 1, 'More than one testsuite tag')

        testsuite_tag = testsuite_list[0]
        self.assertEqual(len(testsuite_tag.attributes), 7,
                         'The testsuite tag does not have 7 attributes. '
                         'XML:\n%s' % xml_output)

        n_tests = int(testsuite_tag.attributes['tests'].value)
        self.assertEqual(n_tests, e_ntests,
                         "Unexpected number of executed tests, "
                         "XML:\n%s" % xml_output)

        n_errors = int(testsuite_tag.attributes['errors'].value)
        self.assertEqual(n_errors, e_nerrors,
                         "Unexpected number of test errors, "
                         "XML:\n%s" % xml_output)

        n_failures = int(testsuite_tag.attributes['failures'].value)
        self.assertEqual(n_failures, e_nfailures,
                         "Unexpected number of test failures, "
                         "XML:\n%s" % xml_output)

        n_skip = int(testsuite_tag.attributes['skip'].value)
        self.assertEqual(n_skip, e_nskip,
                         "Unexpected number of test skips, "
                         "XML:\n%s" % xml_output)

101
    @unittest.skip("Temporary plugin infrastructure removal")
102 103
    def test_sleep_longer_timeout(self):
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
104 105 106
                    '--xunit - --job-timeout=5 %s examples/tests/passtest.py' %
                    (self.tmpdir, self.script.path))
        self.run_and_check(cmd_line, 0, 2, 0, 0, 0)
107

108
    @unittest.skip("Temporary plugin infrastructure removal")
109 110
    def test_sleep_short_timeout(self):
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
111 112
                    '--xunit - --job-timeout=1 %s examples/tests/passtest.py' %
                    (self.tmpdir, self.script.path))
113
        self.run_and_check(cmd_line, exit_codes.AVOCADO_TESTS_FAIL, 2, 1, 0, 1)
114

115
    @unittest.skip("Temporary plugin infrastructure removal")
116 117
    def test_sleep_short_timeout_with_test_methods(self):
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
118 119
                    '--xunit - --job-timeout=1 %s' %
                    (self.tmpdir, self.py.path))
120
        self.run_and_check(cmd_line, exit_codes.AVOCADO_TESTS_FAIL, 3, 1, 0, 2)
121

122
    @unittest.skip("Temporary plugin infrastructure removal")
123 124 125 126
    def test_invalid_values(self):
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
                    '--job-timeout=0 examples/tests/passtest.py' % self.tmpdir)
        result = process.run(cmd_line, ignore_status=True)
127
        self.assertEqual(result.exit_status, exit_codes.AVOCADO_FAIL)
128 129 130 131
        self.assertIn('Invalid number', result.stderr)
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
                    '--job-timeout=123x examples/tests/passtest.py' % self.tmpdir)
        result = process.run(cmd_line, ignore_status=True)
132
        self.assertEqual(result.exit_status, exit_codes.AVOCADO_FAIL)
133 134
        self.assertIn('Invalid number', result.stderr)

135
    @unittest.skip("Temporary plugin infrastructure removal")
136 137 138 139
    def test_valid_values(self):
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
                    '--job-timeout=123 examples/tests/passtest.py' % self.tmpdir)
        result = process.run(cmd_line, ignore_status=True)
140
        self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
141 142 143
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
                    '--job-timeout=123s examples/tests/passtest.py' % self.tmpdir)
        result = process.run(cmd_line, ignore_status=True)
144
        self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
145 146 147
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
                    '--job-timeout=123m examples/tests/passtest.py' % self.tmpdir)
        result = process.run(cmd_line, ignore_status=True)
148
        self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
149 150 151
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
                    '--job-timeout=123h examples/tests/passtest.py' % self.tmpdir)
        result = process.run(cmd_line, ignore_status=True)
152
        self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
153 154 155 156 157 158 159 160

    def tearDown(self):
        self.script.remove()
        self.py.remove()
        shutil.rmtree(self.tmpdir)

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