make perftest: for performance testing
[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 from samba.subunit import (
25     PROGRESS_PUSH,
26     PROGRESS_POP,
27     )
28 from samba.tests import TestCase
29 import tempfile
30
31 from selftest.run import (
32     expand_command_list,
33     expand_environment_strings,
34     expand_command_run,
35     exported_envvars_str,
36     now,
37     run_testsuite_command,
38     )
39
40
41
42 class ExpandEnvironmentStringsTests(TestCase):
43
44     def test_no_vars(self):
45         self.assertEquals("foo bar", expand_environment_strings("foo bar", {}))
46
47     def test_simple(self):
48         self.assertEquals("foo bar",
49             expand_environment_strings("foo $BLA", {"BLA": "bar"}))
50
51     def test_unknown(self):
52         self.assertEquals("foo $BLA",
53             expand_environment_strings("foo $BLA", {}))
54
55
56 class ExpandCommandListTests(TestCase):
57
58     def test_no_list(self):
59         self.assertIs(None, expand_command_list("test bla"))
60
61     def test_list(self):
62         self.assertEquals("test --list", expand_command_list("test $LISTOPT"))
63
64
65 class ExpandCommandRunTests(TestCase):
66
67     def test_idlist(self):
68         self.assertEquals(("test foo bar", None),
69             expand_command_run("test", False, True, subtests=["foo", "bar"]))
70
71     def test_idlist_all(self):
72         self.assertEquals(("test", None),
73             expand_command_run("test", False, True))
74
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)
79         f = open(tmpf, 'r')
80         try:
81             self.assertEquals(f.read(), "foo\nbar\n")
82         finally:
83             f.close()
84         self.assertEquals("test --load-list=%s" % tmpf, cmd)
85
86     def test_loadlist_all(self):
87         self.assertEquals(("test ", None),
88             expand_command_run("test $LOADLIST", True, False))
89
90
91 class ExportedEnvvarsStrTests(TestCase):
92
93     def test_no_vars(self):
94         self.assertEquals("", exported_envvars_str({}, ["foo", "bar"]))
95
96     def test_vars(self):
97         self.assertEquals("foo=1\n",
98             exported_envvars_str({"foo": "1"}, ["foo", "bar"]))
99
100     def test_vars_unknown(self):
101         self.assertEquals("foo=1\n",
102             exported_envvars_str({"foo": "1", "bla": "2"}, ["foo", "bar"]))
103
104
105
106 class NowTests(TestCase):
107
108     def test_basic(self):
109         self.assertIsInstance(now(), datetime.datetime)
110         self.assertIsNot(now().tzinfo, None)
111
112
113 class MockSubunitOps(object):
114
115     def __init__(self):
116         self.calls = []
117
118     def start_testsuite(self, name):
119         self.calls.append(("start-testsuite", name))
120
121     def progress(self, count, whence):
122         self.calls.append(("progress", count, whence))
123
124     def time(self, t):
125         self.calls.append(("time", ))
126
127     def end_testsuite(self, name, result, details=None):
128         self.calls.append(("end-testsuite", name, result, details))
129
130
131 class RunTestsuiteCommandTests(TestCase):
132
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)
137         self.assertEquals([
138             ("start-testsuite", "thetestsuitename"),
139             ("progress", None, PROGRESS_PUSH),
140             ("time", ),
141             ("time", ),
142             ("progress", None, PROGRESS_POP),
143             ("end-testsuite", "thetestsuitename", "success", None),
144             ], subunit_ops.calls)
145         self.assertEquals(0, exit_code)
146         outf.seek(0)
147         self.assertEquals("""\
148 doing something
149 command: echo doing something
150 expanded command: echo doing something
151 """, outf.read())
152
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)
157         self.assertEquals([
158             ("start-testsuite", "thetestsuitename"),
159             ("progress", None, PROGRESS_PUSH),
160             ("time", ),
161             ("time", ),
162             ("progress", None, PROGRESS_POP),
163             ("end-testsuite", "thetestsuitename", "failure", "Exit code was 3"),
164             ], subunit_ops.calls)
165         self.assertEquals(3, exit_code)
166         outf.seek(0)
167         self.assertEquals("""\
168 command: exit 3
169 expanded command: exit 3
170 """, outf.read())
171
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)
177         self.assertEquals([
178             ("start-testsuite", "thetestsuitename"),
179             ("progress", None, PROGRESS_PUSH),
180             ("time", ),
181             ("time", ),
182             ("progress", None, PROGRESS_POP),
183             ("end-testsuite", "thetestsuitename", "failure", "Exit code was 127"),
184             ], subunit_ops.calls)
185         self.assertEquals(127, exit_code)
186         outf.seek(0)
187         self.assertEquals("""\
188 command: thisisacommandthatdoesnotexist 2>/dev/null
189 expanded command: thisisacommandthatdoesnotexist 2>/dev/null
190 """, outf.read())