test_job_timeout.py 5.8 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 13 14 15
from avocado.utils import process
from avocado.utils import script


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


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

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

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


38 39 40 41
class ParseXMLError(Exception):
    pass


42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
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()
        self.tmpdir = tempfile.mkdtemp()
        os.chdir(basedir)

58 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
    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)

100 101
    def test_sleep_longer_timeout(self):
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
102 103 104
                    '--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)
105 106 107

    def test_sleep_short_timeout(self):
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
108 109 110
                    '--xunit - --job-timeout=1 %s examples/tests/passtest.py' %
                    (self.tmpdir, self.script.path))
        self.run_and_check(cmd_line, 1, 2, 1, 0, 1)
111 112 113

    def test_sleep_short_timeout_with_test_methods(self):
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off '
114 115 116
                    '--xunit - --job-timeout=1 %s' %
                    (self.tmpdir, self.py.path))
        self.run_and_check(cmd_line, 1, 3, 1, 0, 2)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

    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)
        self.assertEqual(result.exit_status, 3)
        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)
        self.assertEqual(result.exit_status, 3)
        self.assertIn('Invalid number', result.stderr)

    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)
        self.assertEqual(result.exit_status, 0)
        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)
        self.assertEqual(result.exit_status, 0)
        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)
        self.assertEqual(result.exit_status, 0)
        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)
        self.assertEqual(result.exit_status, 0)

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

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