1 # test_run.py -- Tests for selftest.run
2 # Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org>
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
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.
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,
20 """Tests for selftest.run."""
24 from samba.subunit import (
30 from selftest.run import (
32 expand_environment_strings,
36 run_testsuite_command,
39 from unittest import TestCase
42 class ExpandEnvironmentStringsTests(TestCase):
44 def test_no_vars(self):
45 self.assertEquals("foo bar", expand_environment_strings("foo bar", {}))
47 def test_simple(self):
48 self.assertEquals("foo bar",
49 expand_environment_strings("foo $BLA", {"BLA": "bar"}))
51 def test_unknown(self):
52 self.assertEquals("foo $BLA",
53 expand_environment_strings("foo $BLA", {}))
56 class ExpandCommandListTests(TestCase):
58 def test_no_list(self):
59 self.assertIs(None, expand_command_list("test bla"))
62 self.assertEquals("test --list", expand_command_list("test $LISTOPT"))
65 class ExpandCommandRunTests(TestCase):
67 def test_idlist(self):
68 self.assertEquals(("test foo bar", None),
69 expand_command_run("test", False, True, subtests=["foo", "bar"]))
71 def test_idlist_all(self):
72 self.assertEquals(("test", None),
73 expand_command_run("test", False, True))
75 def test_loadlist(self):
76 (cmd, tmpf) = expand_command_run("test $LOADLIST", True, False,
77 subtests=["foo", "bar"])
78 self.addCleanup(os.remove, tmpf)
81 self.assertEquals(f.read(), "foo\nbar\n")
84 self.assertEquals("test --load-list=%s" % tmpf, cmd)
86 def test_loadlist_all(self):
87 self.assertEquals(("test ", None),
88 expand_command_run("test $LOADLIST", True, False))
91 class ExportedEnvvarsStrTests(TestCase):
93 def test_no_vars(self):
94 self.assertEquals("", exported_envvars_str({}, ["foo", "bar"]))
97 self.assertEquals("foo=1\n",
98 exported_envvars_str({"foo": "1"}, ["foo", "bar"]))
100 def test_vars_unknown(self):
101 self.assertEquals("foo=1\n",
102 exported_envvars_str({"foo": "1", "bla": "2"}, ["foo", "bar"]))
106 class NowTests(TestCase):
108 def test_basic(self):
109 self.assertIsInstance(now(), datetime.datetime)
110 self.assertIsNot(now().tzinfo, None)
113 class MockSubunitOps(object):
118 def start_testsuite(self, name):
119 self.calls.append(("start-testsuite", name))
121 def progress(self, count, whence):
122 self.calls.append(("progress", count, whence))
125 self.calls.append(("time", ))
127 def end_testsuite(self, name, result, details=None):
128 self.calls.append(("end-testsuite", name, result, details))
131 class RunTestsuiteCommandTests(TestCase):
133 def test_success_no_env(self):
134 outf = tempfile.TemporaryFile()
135 subunit_ops = MockSubunitOps()
136 exit_code = run_testsuite_command("thetestsuitename", "echo doing something", subunit_ops, outf=outf)
138 ("start-testsuite", "thetestsuitename"),
139 ("progress", None, PROGRESS_PUSH),
142 ("progress", None, PROGRESS_POP),
143 ("end-testsuite", "thetestsuitename", "success", None),
144 ], subunit_ops.calls)
145 self.assertEquals(0, exit_code)
147 self.assertEquals("""\
149 command: echo doing something
150 expanded command: echo doing something
153 def test_failure(self):
154 outf = tempfile.TemporaryFile()
155 subunit_ops = MockSubunitOps()
156 exit_code = run_testsuite_command("thetestsuitename", "exit 3", subunit_ops, outf=outf)
158 ("start-testsuite", "thetestsuitename"),
159 ("progress", None, PROGRESS_PUSH),
162 ("progress", None, PROGRESS_POP),
163 ("end-testsuite", "thetestsuitename", "failure", "Exit code was 3"),
164 ], subunit_ops.calls)
165 self.assertEquals(3, exit_code)
167 self.assertEquals("""\
169 expanded command: exit 3
172 def test_error(self):
173 outf = tempfile.TemporaryFile()
174 subunit_ops = MockSubunitOps()
175 exit_code = run_testsuite_command("thetestsuitename",
176 "thisisacommandthatdoesnotexist 2>/dev/null", subunit_ops, outf=outf)
178 ("start-testsuite", "thetestsuitename"),
179 ("progress", None, PROGRESS_PUSH),
182 ("progress", None, PROGRESS_POP),
183 ("end-testsuite", "thetestsuitename", "failure", "Exit code was 127"),
184 ], subunit_ops.calls)
185 self.assertEquals(127, exit_code)
187 self.assertEquals("""\
188 command: thisisacommandthatdoesnotexist 2>/dev/null
189 expanded command: thisisacommandthatdoesnotexist 2>/dev/null