Merge branch 'master' of ctdb into 'master' of samba
[sfrench/samba-autobuild/.git] / selftest / tests / test_run.py
1 # test_run.py -- Tests for selftest.run
2 # Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org>
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 3
7 # of the License or (at your option) any later version of
8 # the License.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA  02110-1301, USA.
19
20 """Tests for selftest.run."""
21
22 import datetime
23 import os
24 import subunit
25 import tempfile
26
27 from selftest.run import (
28     expand_command_list,
29     expand_environment_strings,
30     expand_command_run,
31     exported_envvars_str,
32     now,
33     run_testsuite_command,
34     )
35
36 from selftest.tests import TestCase
37
38
39 class ExpandEnvironmentStringsTests(TestCase):
40
41     def test_no_vars(self):
42         self.assertEquals("foo bar", expand_environment_strings("foo bar", {}))
43
44     def test_simple(self):
45         self.assertEquals("foo bar",
46             expand_environment_strings("foo $BLA", {"BLA": "bar"}))
47
48     def test_unknown(self):
49         self.assertEquals("foo $BLA",
50             expand_environment_strings("foo $BLA", {}))
51
52
53 class ExpandCommandListTests(TestCase):
54
55     def test_no_list(self):
56         self.assertIs(None, expand_command_list("test bla"))
57
58     def test_list(self):
59         self.assertEquals("test --list", expand_command_list("test $LISTOPT"))
60
61
62 class ExpandCommandRunTests(TestCase):
63
64     def test_idlist(self):
65         self.assertEquals(("test foo bar", None),
66             expand_command_run("test", False, True, subtests=["foo", "bar"]))
67
68     def test_idlist_all(self):
69         self.assertEquals(("test", None),
70             expand_command_run("test", False, True))
71
72     def test_loadlist(self):
73         (cmd, tmpf) = expand_command_run("test $LOADLIST", True, False,
74             subtests=["foo", "bar"])
75         self.addCleanup(os.remove, tmpf)
76         f = open(tmpf, 'r')
77         try:
78             self.assertEquals(f.read(), "foo\nbar\n")
79         finally:
80             f.close()
81         self.assertEquals("test --load-list=%s" % tmpf, cmd)
82
83     def test_loadlist_all(self):
84         self.assertEquals(("test ", None),
85             expand_command_run("test $LOADLIST", True, False))
86
87
88 class ExportedEnvvarsStrTests(TestCase):
89
90     def test_no_vars(self):
91         self.assertEquals("", exported_envvars_str({}, ["foo", "bar"]))
92
93     def test_vars(self):
94         self.assertEquals("foo=1\n",
95             exported_envvars_str({"foo": "1"}, ["foo", "bar"]))
96
97     def test_vars_unknown(self):
98         self.assertEquals("foo=1\n",
99             exported_envvars_str({"foo": "1", "bla": "2"}, ["foo", "bar"]))
100
101
102
103 class NowTests(TestCase):
104
105     def test_basic(self):
106         self.assertIsInstance(now(), datetime.datetime)
107         self.assertIsNot(now().tzinfo, None)
108
109
110 class MockSubunitOps(object):
111
112     def __init__(self):
113         self.calls = []
114
115     def start_testsuite(self, name):
116         self.calls.append(("start-testsuite", name))
117
118     def progress(self, count, whence):
119         self.calls.append(("progress", count, whence))
120
121     def time(self, t):
122         self.calls.append(("time", ))
123
124     def end_testsuite(self, name, result, details=None):
125         self.calls.append(("end-testsuite", name, result, details))
126
127
128 class RunTestsuiteCommandTests(TestCase):
129
130     def test_success_no_env(self):
131         outf = tempfile.TemporaryFile()
132         subunit_ops = MockSubunitOps()
133         exit_code = run_testsuite_command("thetestsuitename", "echo doing something", subunit_ops, outf=outf)
134         self.assertEquals([
135             ("start-testsuite", "thetestsuitename"),
136             ("progress", None, subunit.PROGRESS_PUSH),
137             ("time", ),
138             ("time", ),
139             ("progress", None, subunit.PROGRESS_POP),
140             ("end-testsuite", "thetestsuitename", "success", None),
141             ], subunit_ops.calls)
142         self.assertEquals(0, exit_code)
143         outf.seek(0)
144         self.assertEquals("""\
145 doing something
146 command: echo doing something
147 expanded command: echo doing something
148 """, outf.read())
149
150     def test_failure(self):
151         outf = tempfile.TemporaryFile()
152         subunit_ops = MockSubunitOps()
153         exit_code = run_testsuite_command("thetestsuitename", "exit 3", subunit_ops, outf=outf)
154         self.assertEquals([
155             ("start-testsuite", "thetestsuitename"),
156             ("progress", None, subunit.PROGRESS_PUSH),
157             ("time", ),
158             ("time", ),
159             ("progress", None, subunit.PROGRESS_POP),
160             ("end-testsuite", "thetestsuitename", "failure", "Exit code was 3"),
161             ], subunit_ops.calls)
162         self.assertEquals(3, exit_code)
163         outf.seek(0)
164         self.assertEquals("""\
165 command: exit 3
166 expanded command: exit 3
167 """, outf.read())
168
169     def test_error(self):
170         outf = tempfile.TemporaryFile()
171         subunit_ops = MockSubunitOps()
172         exit_code = run_testsuite_command("thetestsuitename",
173             "thisisacommandthatdoesnotexist 2>/dev/null", subunit_ops, outf=outf)
174         self.assertEquals([
175             ("start-testsuite", "thetestsuitename"),
176             ("progress", None, subunit.PROGRESS_PUSH),
177             ("time", ),
178             ("time", ),
179             ("progress", None, subunit.PROGRESS_POP),
180             ("end-testsuite", "thetestsuitename", "failure", "Exit code was 127"),
181             ], subunit_ops.calls)
182         self.assertEquals(127, exit_code)
183         outf.seek(0)
184         self.assertEquals("""\
185 command: thisisacommandthatdoesnotexist 2>/dev/null
186 expanded command: thisisacommandthatdoesnotexist 2>/dev/null
187 """, outf.read())