test_wrapper.py 3.7 KB
Newer Older
1
import os
2
import sys
3
import tempfile
4
import shutil
5

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

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

15
basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..')
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
basedir = os.path.abspath(basedir)


SCRIPT_CONTENT = """#!/bin/bash
touch %s
exec -- $@
"""

DUMMY_CONTENT = """#!/bin/bash
exec -- $@
"""


class WrapperTest(unittest.TestCase):

    def setUp(self):
32
        self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
33 34 35 36 37 38 39 40 41 42 43 44
        self.tmpfile = tempfile.mktemp()
        self.script = script.TemporaryScript(
            'success.sh',
            SCRIPT_CONTENT % self.tmpfile,
            'avocado_wrapper_functional')
        self.script.save()
        self.dummy = script.TemporaryScript(
            'dummy.sh',
            DUMMY_CONTENT,
            'avocado_wrapper_functional')
        self.dummy.save()

45
    @unittest.skip("Temporary plugin infrastructure removal")
46 47
    def test_global_wrapper(self):
        os.chdir(basedir)
48 49
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off --wrapper %s '
                    'examples/tests/datadir.py' % (self.tmpdir, self.script.path))
50
        result = process.run(cmd_line, ignore_status=True)
51
        expected_rc = exit_codes.AVOCADO_ALL_OK
52 53 54 55
        self.assertEqual(result.exit_status, expected_rc,
                         "Avocado did not return rc %d:\n%s" %
                         (expected_rc, result))
        self.assertTrue(os.path.exists(self.tmpfile),
56 57 58
                        "Wrapper did not touch the tmp file %s\nStdout: "
                        "%s\nCmdline: %s" %
                        (self.tmpfile, result.stdout, cmd_line))
59

60
    @unittest.skip("Temporary plugin infrastructure removal")
61 62
    def test_process_wrapper(self):
        os.chdir(basedir)
63 64
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off --wrapper %s:*/datadir '
                    'examples/tests/datadir.py' % (self.tmpdir, self.script.path))
65
        result = process.run(cmd_line, ignore_status=True)
66
        expected_rc = exit_codes.AVOCADO_ALL_OK
67 68 69 70
        self.assertEqual(result.exit_status, expected_rc,
                         "Avocado did not return rc %d:\n%s" %
                         (expected_rc, result))
        self.assertTrue(os.path.exists(self.tmpfile),
71 72 73
                        "Wrapper did not touch the tmp file %s\nStdout: "
                        "%s\nStdout: %s" %
                        (self.tmpfile, cmd_line, result.stdout))
74

75
    @unittest.skip("Temporary plugin infrastructure removal")
76 77
    def test_both_wrappers(self):
        os.chdir(basedir)
78 79
        cmd_line = ('./scripts/avocado run --job-results-dir %s --sysinfo=off --wrapper %s --wrapper %s:*/datadir '
                    'examples/tests/datadir.py' % (self.tmpdir, self.dummy.path,
80
                                                   self.script.path))
81
        result = process.run(cmd_line, ignore_status=True)
82
        expected_rc = exit_codes.AVOCADO_ALL_OK
83 84 85 86
        self.assertEqual(result.exit_status, expected_rc,
                         "Avocado did not return rc %d:\n%s" %
                         (expected_rc, result))
        self.assertTrue(os.path.exists(self.tmpfile),
87 88 89
                        "Wrapper did not touch the tmp file %s\nStdout: "
                        "%s\nStdout: %s" %
                        (self.tmpfile, cmd_line, result.stdout))
90 91 92 93 94 95 96 97

    def tearDown(self):
        self.script.remove()
        self.dummy.remove()
        try:
            os.remove(self.tmpfile)
        except OSError:
            pass
98
        shutil.rmtree(self.tmpdir)
99 100 101 102


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