Debian Codegraph @

testrunner/test/test_runner.py

raw symbols SCIP
1 import py, sys, os, signal, cStringIO, tempfile
2
3 import runner
4 import pypy
5
6 pytest_script = py.path.local(pypy.__file__).dirpath('test_all.py')
7
8
9 def test_busywait():
10 class FakeProcess:
11 def poll(self):
12 if timers[0] >= timers[1]:
13 return 42
14 return None
15 class FakeTime:
16 def sleep(self, delay):
17 timers[0] += delay
18 def time(self):
19 timers[2] += 1
20 return 12345678.9 + timers[0]
21 p = FakeProcess()
22 prevtime = runner.time
23 try:
24 runner.time = FakeTime()
25 #
26 timers = [0.0, 0.0, 0]
27 returncode = runner.busywait(p, 10)
28 assert returncode == 42 and 0.0 <= timers[0] <= 1.0
29 #
30 timers = [0.0, 3.0, 0]
31 returncode = runner.busywait(p, 10)
32 assert returncode == 42 and 3.0 <= timers[0] <= 5.0 and timers[2] <= 10
33 #
34 timers = [0.0, 500.0, 0]
35 returncode = runner.busywait(p, 1000)
36 assert returncode == 42 and 500.0<=timers[0]<=510.0 and timers[2]<=100
37 #
38 timers = [0.0, 500.0, 0]
39 returncode = runner.busywait(p, 100) # get a timeout
40 assert returncode == None and 100.0 <= timers[0] <= 110.0
41 #
42 finally:
43 runner.time = prevtime
44
45 def test_should_report_failure():
46 should_report_failure = runner.should_report_failure
47 assert should_report_failure("")
48 assert should_report_failure(". Abc\n. Def\n")
49 assert should_report_failure("s Ghi\n")
50 assert not should_report_failure(". Abc\nF Def\n")
51 assert not should_report_failure(". Abc\nE Def\n")
52 assert not should_report_failure(". Abc\nP Def\n")
53 assert not should_report_failure("F Def\n. Ghi\n. Jkl\n")
54
55
56
57 class TestRunHelper(object):
58 def pytest_funcarg__out(self, request):
59 tmpdir = request.getfuncargvalue('tmpdir')
60 return tmpdir.ensure('out')
61
62 def test_run(self, out):
63 res = runner.run([sys.executable, "-c", "print 42"], '.', out)
64 assert res == 0
65 assert out.read() == "42\n"
66
67 def test_error(self, out):
68 res = runner.run([sys.executable, "-c", "import sys; sys.exit(3)"], '.', out)
69 assert res == 3
70
71 def test_signal(self, out):
72 if sys.platform == 'win32':
73 py.test.skip("no death by signal on windows")
74 res = runner.run([sys.executable, "-c", "import os; os.kill(os.getpid(), 9)"], '.', out)
75 assert res == -9
76
77 def test_timeout(self, out):
78 res = runner.run([sys.executable, "-c", "while True: pass"], '.', out, timeout=3)
79 assert res == -999
80
81 def test_timeout_lock(self, out):
82 res = runner.run([sys.executable, "-c", "import threading; l=threading.Lock(); l.acquire(); l.acquire()"], '.', out, timeout=3)
83 assert res == -999
84
85 def test_timeout_syscall(self, out):
86 res = runner.run([sys.executable, "-c", "import socket; s=s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.bind(('', 0)); s.recv(1000)"], '.', out, timeout=3)
87 assert res == -999
88
89 def test_timeout_success(self, out):
90 res = runner.run([sys.executable, "-c", "print 42"], '.',
91 out, timeout=2)
92 assert res == 0
93 out = out.read()
94 assert out == "42\n"
95
96
97 class TestExecuteTest(object):
98
99 def setup_class(cls):
100 cls.real_run = (runner.run,)
101 cls.called = []
102 cls.exitcode = [0]
103
104 def fake_run(args, cwd, out, timeout):
105 cls.called = (args, cwd, out, timeout)
106 return cls.exitcode[0]
107 runner.run = fake_run
108
109 def teardown_class(cls):
110 runner.run = cls.real_run[0]
111
112 def test_explicit(self):
113 res = runner.execute_test('/wd', 'test_one', 'out', 'LOGFILE',
114 interp=['INTERP', 'IARG'],
115 test_driver=['driver', 'darg'],
116 timeout='secs')
117
118 expected = ['INTERP', 'IARG',
119 'driver', 'darg',
120 '-p', 'resultlog',
121 '--resultlog=LOGFILE',
122 #'--junitxml=LOGFILE.junit',
123
124 'test_one']
125
126 assert self.called == (expected, '/wd', 'out', 'secs')
127 assert res == 0
128
129 def test_explicit_win32(self):
130 res = runner.execute_test('/wd', 'test_one', 'out', 'LOGFILE',
131 interp=['./INTERP', 'IARG'],
132 test_driver=['driver', 'darg'],
133 timeout='secs',
134 _win32=True
135 )
136
137 expected = ['/wd' + os.sep + './INTERP', 'IARG',
138 'driver', 'darg',
139 '-p', 'resultlog',
140 '--resultlog=LOGFILE',
141 #'--junitxml=LOGFILE.junit',
142 'test_one']
143 assert self.called[0] == expected
144 assert self.called == (expected, '/wd', 'out', 'secs')
145 assert res == 0
146
147 def test_error(self):
148 self.exitcode[:] = [1]
149 res = runner.execute_test('/wd', 'test_one', 'out', 'LOGFILE',
150 interp=['INTERP', 'IARG'],
151 test_driver=['driver', 'darg'])
152 assert res == 1
153
154
155 self.exitcode[:] = [-signal.SIGSEGV]
156 res = runner.execute_test('/wd', 'test_one', 'out', 'LOGFILE',
157 interp=['INTERP', 'IARG'],
158 test_driver=['driver', 'darg'])
159 assert res == -signal.SIGSEGV
160
161 def test_interpret_exitcode(self):
162 failure, extralog = runner.interpret_exitcode(0, "test_foo")
163 assert not failure
164 assert extralog == ""
165
166 failure, extralog = runner.interpret_exitcode(1, "test_foo", "")
167 assert failure
168 assert extralog == """! test_foo
169 Exit code 1.
170 """
171
172 failure, extralog = runner.interpret_exitcode(1, "test_foo", "F Foo\n")
173 assert failure
174 assert extralog == " (somefailed=True in test_foo)\n"
175
176 failure, extralog = runner.interpret_exitcode(2, "test_foo")
177 assert failure
178 assert extralog == """! test_foo
179 Exit code 2.
180 """
181
182 failure, extralog = runner.interpret_exitcode(-signal.SIGSEGV,
183 "test_foo")
184 assert failure
185 assert extralog == """! test_foo
186 Killed by SIGSEGV.
187 """
188
189 class RunnerTests(object):
190 with_thread = True
191
192 def setup_class(cls):
193 cls.real_invoke_in_thread = (runner.invoke_in_thread,)
194 if not cls.with_thread:
195 runner.invoke_in_thread = lambda func, args: func(*args)
196
197 cls.udir = py.path.local.make_numbered_dir(prefix='usession-runner-',
198 keep=3)
199 cls.manydir = cls.udir.join('many').ensure(dir=1)
200
201 cls.udir.join("conftest.py").write("pytest_plugins = 'resultlog'\n")
202
203 def fill_test_dir(test_dir, fromdir='normal'):
204 for p in py.path.local(__file__).dirpath(
205 'examples', fromdir).listdir("*.py"):
206 p.copy(test_dir.join('test_'+p.basename))
207
208
209 test_normal_dir0 = cls.manydir.join('one', 'test_normal').ensure(dir=1)
210 cls.one_test_dir = cls.manydir.join('one')
211
212 fill_test_dir(test_normal_dir0)
213
214
215 test_normal_dir1 = cls.manydir.join('two', 'test_normal1').ensure(dir=1)
216 test_normal_dir2 = cls.manydir.join('two', 'pkg',
217 'test_normal2').ensure(dir=1)
218 cls.two_test_dir = cls.manydir.join('two')
219
220 fill_test_dir(test_normal_dir1)
221 fill_test_dir(test_normal_dir2)
222
223 cls.test_stall_dir = cls.udir.join('stall').ensure(dir=1)
224 test_stall_dir0 = cls.test_stall_dir.join('zero').ensure(dir=1)
225 fill_test_dir(test_stall_dir0, 'stall')
226
227 def teardown_class(cls):
228 runner.invoke_in_thread = cls.real_invoke_in_thread[0]
229
230 def test_one_dir(self):
231 test_driver = [pytest_script]
232
233 log = cStringIO.StringIO()
234 out = cStringIO.StringIO()
235
236 run_param = runner.RunParam(self.one_test_dir)
237 run_param.test_driver = test_driver
238 run_param.parallel_runs = 3
239
240 res = runner.execute_tests(run_param, ['test_normal'], log, out)
241
242 assert res
243
244 out = out.getvalue()
245 assert out
246 assert '\r\n' not in out
247 assert '\n' in out
248
249 log = log.getvalue()
250 assert '\r\n' not in log
251 assert '\n' in log
252 log_lines = log.splitlines()
253
254 assert ". test_example.py::test_one" in log_lines
255 nfailures = 0
256 noutcomes = 0
257 for line in log_lines:
258 if line[0] != ' ':
259 noutcomes += 1
260 if line[0] != '.':
261 nfailures += 1
262
263 assert noutcomes == 107
264 assert nfailures == 6
265
266 def test_one_dir_dry_run(self):
267 test_driver = [pytest_script]
268
269 log = cStringIO.StringIO()
270 out = cStringIO.StringIO()
271
272 run_param = runner.RunParam(self.one_test_dir)
273 run_param.test_driver = test_driver
274 run_param.parallel_runs = 3
275 run_param.dry_run = True
276
277 res = runner.execute_tests(run_param, ['test_normal'], log, out)
278
279 assert not res
280
281 assert log.getvalue() == ""
282
283 out_lines = out.getvalue().splitlines()
284
285 assert len(out_lines) == 7
286
287 assert 'starting' in out_lines[4]
288 assert out_lines[6].startswith("run [")
289 for line in out_lines[4:]:
290 assert "test_normal" in line
291
292 def test_many_dirs(self):
293 test_driver = [pytest_script]
294
295 log = cStringIO.StringIO()
296 out = cStringIO.StringIO()
297
298 cleanedup = []
299 def cleanup(testdir):
300 cleanedup.append(testdir)
301
302 run_param = runner.RunParam(self.manydir)
303 run_param.test_driver = test_driver
304 run_param.parallel_runs = 3
305 run_param.cleanup = cleanup
306
307 testdirs = []
308 run_param.collect_testdirs(testdirs)
309 alltestdirs = testdirs[:]
310
311 res = runner.execute_tests(run_param, testdirs, log, out)
312
313 assert res
314
315 assert out.getvalue()
316
317 log_lines = log.getvalue().splitlines()
318
319 nfailures = 0
320 noutcomes = 0
321 for line in log_lines:
322 if line[0] != ' ':
323 noutcomes += 1
324 if line[0] != '.':
325 nfailures += 1
326
327 assert noutcomes == 3*107
328 assert nfailures == 3*6
329
330 assert set(cleanedup) == set(alltestdirs)
331
332 def test_timeout(self):
333 test_driver = [pytest_script]
334
335 log = cStringIO.StringIO()
336 out = cStringIO.StringIO()
337
338 run_param = runner.RunParam(self.test_stall_dir)
339 run_param.test_driver = test_driver
340 run_param.parallel_runs = 3
341 run_param.timeout = 3
342
343 testdirs = []
344 run_param.collect_testdirs(testdirs)
345 res = runner.execute_tests(run_param, testdirs, log, out)
346 assert res
347
348 log_lines = log.getvalue().splitlines()
349 assert log_lines[1] == ' TIMEOUT'
350
351 def test_run_wrong_interp(self):
352 log = cStringIO.StringIO()
353 out = cStringIO.StringIO()
354
355 run_param = runner.RunParam(self.one_test_dir)
356 run_param.interp = ['wrong-interp']
357 run_param.parallel_runs = 3
358
359 testdirs = []
360 run_param.collect_testdirs(testdirs)
361 res = runner.execute_tests(run_param, testdirs, log, out)
362 assert res
363
364 log_lines = log.getvalue().splitlines()
365 assert log_lines[1] == ' Failed to run interp'
366
367 def test_run_bad_get_test_driver(self):
368 test_driver = [pytest_script]
369
370 log = cStringIO.StringIO()
371 out = cStringIO.StringIO()
372
373 run_param = runner.RunParam(self.one_test_dir)
374 run_param.parallel_runs = 3
375 def boom(testdir):
376 raise RuntimeError("Boom")
377 run_param.get_test_driver = boom
378
379 testdirs = []
380 run_param.collect_testdirs(testdirs)
381 res = runner.execute_tests(run_param, testdirs, log, out)
382 assert res
383
384 log_lines = log.getvalue().splitlines()
385 assert log_lines[1] == ' Failed with exception in execute-test'
386
387
388 class TestRunnerNoThreads(RunnerTests):
389 with_thread = False
390
391 def test_collect_testdirs(self):
392 res = []
393 seen = []
394 run_param = runner.RunParam(self.one_test_dir)
395 real_collect_one_testdir = run_param.collect_one_testdir
396
397 def witness_collect_one_testdir(testdirs, reldir, tests):
398 seen.append((reldir, sorted(map(str, tests))))
399 real_collect_one_testdir(testdirs, reldir, tests)
400
401 run_param.collect_one_testdir = witness_collect_one_testdir
402
403 run_param.collect_testdirs(res)
404
405 assert res == ['test_normal']
406 assert len(seen) == 1
407 reldir, tests = seen[0]
408 assert reldir == 'test_normal'
409 for test in tests:
410 assert test.startswith('test_normal/')
411
412 run_param.collect_one_testdir = real_collect_one_testdir
413 res = []
414 run_param = runner.RunParam(self.two_test_dir)
415
416 run_param.collect_testdirs(res)
417
418 assert sorted(res) == ['pkg/test_normal2', 'test_normal1']
419
420
421 class TestRunner(RunnerTests):
422 pass
423