~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
106
107
108
109
110
111
112
113
114
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Parallel test glue."""

__metaclass__ = type

from StringIO import StringIO
import subprocess
import tempfile

from fixtures import (
    PopenFixture,
    TestWithFixtures,
    )
from testtools import (
    TestCase,
    TestResult,
    )

from lp.services.testing.parallel import (
    find_load_list,
    find_tests,
    ListTestCase,
    prepare_argv,
    )


class TestListTestCase(TestCase, TestWithFixtures):

    def test_run(self):
        # ListTestCase.run should run bin/test adding in --subunit,
        # --load-list, with a list file containing the supplied list ids.
        def check_list_file(info):
            """Callback from subprocess.Popen for testing run()."""
            # A temp file should have been made.
            args = info['args']
            load_list = find_load_list(args)
            self.assertNotEqual(None, load_list)
            with open(load_list, 'rt') as testlist:
                contents = testlist.readlines()
            self.assertEqual(['foo\n', 'bar\n'], contents)
            return {'stdout': StringIO(''), 'stdin': StringIO()}
        popen = self.useFixture(PopenFixture(check_list_file))
        case = ListTestCase(['foo', 'bar'], ['bin/test'])
        self.assertEqual([], popen.procs)
        result = TestResult()
        case.run(result)
        self.assertEqual(0, result.testsRun)
        self.assertEqual(1, len(popen.procs))
        self.assertEqual(['bin/test', '--subunit', '--load-list'],
            popen.procs[0]._args['args'][:-1])


class TestUtilities(TestCase, TestWithFixtures):

    def test_prepare_argv_removes_subunit(self):
        self.assertEqual(
            ['bin/test', 'foo'],
            prepare_argv(['bin/test', '--subunit', 'foo']))

    def test_prepare_argv_removes_parallel(self):
        self.assertEqual(
            ['bin/test', 'foo'],
            prepare_argv(['bin/test', '--parallel', 'foo']))

    def test_prepare_argv_removes_load_list_with_equals(self):
        self.assertEqual(
            ['bin/test', 'foo'],
            prepare_argv(['bin/test', '--load-list=Foo', 'foo']))

    def test_prepare_argv_removes_load_2_arg_form(self):
        self.assertEqual(
            ['bin/test', 'foo'],
            prepare_argv(['bin/test', '--load-list', 'Foo', 'foo']))

    def test_find_tests_honours_list_list_equals(self):
        with tempfile.NamedTemporaryFile() as listfile:
            listfile.write('foo\nbar\n')
            listfile.flush()
            self.assertEqual(
                ['foo', 'bar'],
                find_tests(
                    ['bin/test', '--load-list=%s' % listfile.name, 'foo']))

    def test_find_tests_honours_list_list_two_arg_form(self):
        with tempfile.NamedTemporaryFile() as listfile:
            listfile.write('foo\nbar\n')
            listfile.flush()
            self.assertEqual(
                ['foo', 'bar'],
                find_tests(
                    ['bin/test', '--load-list', listfile.name, 'foo']))

    def test_find_tests_live(self):
        # When --load-tests wasn't supplied, find_tests needs to run bin/test
        # with --list-tests and --subunit, and parse the resulting subunit
        # stream.
        def inject_testlist(args):
            self.assertEqual(subprocess.PIPE, args['stdin'])
            self.assertEqual(subprocess.PIPE, args['stdout'])
            self.assertEqual(
                ['bin/test', '-vt', 'filter', '--list-tests', '--subunit'],
                args['args'])
            return {'stdin': StringIO(), 'stdout': StringIO(u"""\
test: quux
successful: quux
test: glom
successful: glom
""")}
        self.useFixture(PopenFixture(inject_testlist))
        self.assertEqual(
            ['quux', 'glom'],
            find_tests(['bin/test', '-vt', 'filter', '--parallel']))