test_remote.py 4.7 KB
Newer Older
1 2
import argparse
import shutil
3
import unittest
4

5 6 7 8
try:
    from unittest import mock
except ImportError:
    import mock
9

10 11 12
from avocado.core.job import Job
from avocado.core import version
from avocado.utils import process
13
import avocado_runner_remote
14

15

16
JSON_RESULTS = ('Something other than json\n'
A
Amador Pahim 已提交
17
                '{"tests": [{"id": "1-sleeptest;0",'
18
                '"reference": "sleeptest", '
19
                '"fail_reason": "None", '
20 21 22
                '"status": "PASS", "time": 1.23, "start": 0, "end": 1.23}],'
                '"debuglog": "/home/user/avocado/logs/run-2014-05-26-15.45.'
                '37/debug.log", "errors": 0, "skip": 0, "time": 1.4, '
23 24
                '"logdir": "/local/path/test-results/sleeptest", '
                '"logdir": "/local/path/test-results/sleeptest", '
25
                '"start": 0, "end": 1.4, "pass": 1, "failures": 0, "total": '
26
                '1}\nAdditional stuff other than json')
27

28

29
class RemoteTestRunnerTest(unittest.TestCase):
30

31
    """ Tests RemoteTestRunner """
32

33
    def test_run_suite(self):
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 100 101 102 103 104 105
        """
        Test RemoteTestRunner.run_suite()

        The general idea of this test is to:

        1) Create the machinery necessary to get a RemoteTestRunner
           setup inside a job, or looking at it the other way around, to
           have a runner that is created with a valid job.

        2) Mock the interactions with a remote host.  This is done here
           basically by mocking 'Remote' and 'fabric' usage.

        3) Provide a polluted JSON to be parsed by the RemoteTestRunner

        4) Assert that those results are properly parsed into the
           job's result
        """
        job_args = argparse.Namespace(test_result_total=1,
                                      remote_username='username',
                                      remote_hostname='hostname',
                                      remote_port=22,
                                      remote_password='password',
                                      remote_key_file=None,
                                      remote_timeout=60,
                                      show_job_log=False,
                                      mux_yaml=['~/avocado/tests/foo.yaml',
                                                '~/avocado/tests/bar/baz.yaml'],
                                      dry_run=True,
                                      env_keep=None,
                                      reference=['/tests/sleeptest.py',
                                                 '/tests/other/test',
                                                 'passtest.py'])

        try:
            job = Job(job_args)
            runner = avocado_runner_remote.RemoteTestRunner(job, job.result)
            return_value = (True, (version.MAJOR, version.MINOR))
            runner.check_remote_avocado = mock.Mock(return_value=return_value)

            # These are mocked at their source, and will prevent fabric from
            # trying to contact remote hosts
            with mock.patch('avocado_runner_remote.Remote'):
                runner.remote = avocado_runner_remote.Remote(job_args.remote_hostname)

                # This is the result that the run_suite() will get from remote.run
                remote_run_result = process.CmdResult()
                remote_run_result.stdout = JSON_RESULTS
                remote_run_result.exit_status = 0
                runner.remote.run = mock.Mock(return_value=remote_run_result)

                # We have to fake the uncompressing and removal of the zip
                # archive that was never generated on the "remote" end
                # This test could be expand by mocking creating an actual
                # zip file instead, but it's really overkill
                with mock.patch('avocado_runner_remote.archive.uncompress'):
                    with mock.patch('avocado_runner_remote.os.remove'):
                        runner.run_suite(None, None, 61)

            # The job was created with dry_run so it should have a zeroed id
            self.assertEqual(job.result.job_unique_id, '0' * 40)
            self.assertEqual(job.result.tests_run, 1)
            self.assertEqual(job.result.passed, 1)
            cmd_line = ('avocado run --force-job-id '
                        '0000000000000000000000000000000000000000 --json - '
                        '--archive /tests/sleeptest.py /tests/other/test '
                        'passtest.py -m ~/avocado/tests/foo.yaml '
                        '~/avocado/tests/bar/baz.yaml --dry-run')
            runner.remote.run.assert_called_with(cmd_line,
                                                 ignore_status=True,
                                                 timeout=61)
        finally:
            shutil.rmtree(job.args.base_logdir)
106

L
Lukáš Doktor 已提交
107

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