blob: 4644a89f14f85c9d7c35bf373fd21b294633af98 [file] [log] [blame]
Johnny Chene8d9dc62011-10-31 19:04:07 +00001#!/usr/bin/env python
2
3"""
4Run the test suite using a separate process for each test file.
Vince Harronede59652015-01-08 02:11:26 +00005
Siva Chandra2d7832e2015-05-08 23:08:53 +00006Each test will run with a time limit of 10 minutes by default.
Vince Harronede59652015-01-08 02:11:26 +00007
Siva Chandra2d7832e2015-05-08 23:08:53 +00008Override the default time limit of 10 minutes by setting
Vince Harronede59652015-01-08 02:11:26 +00009the environment variable LLDB_TEST_TIMEOUT.
10
11E.g., export LLDB_TEST_TIMEOUT=10m
12
13Override the time limit for individual tests by setting
14the environment variable LLDB_[TEST NAME]_TIMEOUT.
15
16E.g., export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=2m
17
18Set to "0" to run without time limit.
19
20E.g., export LLDB_TEST_TIMEOUT=0
21or export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=0
Vince Harrondcc2b9f2015-05-27 04:40:36 +000022
23To collect core files for timed out tests, do the following before running dosep.py
24
25OSX
26ulimit -c unlimited
27sudo sysctl -w kern.corefile=core.%P
28
29Linux:
30ulimit -c unlimited
31echo core.%p | sudo tee /proc/sys/kernel/core_pattern
Johnny Chene8d9dc62011-10-31 19:04:07 +000032"""
33
Greg Clayton2256d0d2014-03-24 23:01:57 +000034import multiprocessing
Todd Fiala3f0a3602014-07-08 06:42:37 +000035import os
Vince Harrondcc2b9f2015-05-27 04:40:36 +000036import fnmatch
Todd Fiala3f0a3602014-07-08 06:42:37 +000037import platform
Vince Harron06381732015-05-12 23:10:36 +000038import re
Vince Harronf8b9a1d2015-05-18 19:40:54 +000039import dotest_args
Vince Harron17f429f2014-12-13 00:08:19 +000040import shlex
41import subprocess
Todd Fiala3f0a3602014-07-08 06:42:37 +000042import sys
Steve Puccibefe2b12014-03-07 00:01:11 +000043
Johnny Chene8d9dc62011-10-31 19:04:07 +000044from optparse import OptionParser
45
Vince Harron17f429f2014-12-13 00:08:19 +000046def get_timeout_command():
Vince Harronede59652015-01-08 02:11:26 +000047 """Search for a suitable timeout command."""
Vince Harron17f429f2014-12-13 00:08:19 +000048 if sys.platform.startswith("win32"):
49 return None
50 try:
Chaoren Lin45c17ff2015-05-28 23:00:10 +000051 subprocess.call("timeout", stderr=subprocess.PIPE)
Vince Harron17f429f2014-12-13 00:08:19 +000052 return "timeout"
53 except OSError:
54 pass
55 try:
Chaoren Lin45c17ff2015-05-28 23:00:10 +000056 subprocess.call("gtimeout", stderr=subprocess.PIPE)
Vince Harron17f429f2014-12-13 00:08:19 +000057 return "gtimeout"
58 except OSError:
59 pass
60 return None
61
62timeout_command = get_timeout_command()
63
Siva Chandra2d7832e2015-05-08 23:08:53 +000064default_timeout = os.getenv("LLDB_TEST_TIMEOUT") or "10m"
Vince Harron17f429f2014-12-13 00:08:19 +000065
66# Status codes for running command with timeout.
67eTimedOut, ePassed, eFailed = 124, 0, 1
68
Chaoren Lin5e3ab2b2015-06-01 17:49:25 +000069output_lock = None
70test_counter = None
71total_tests = None
72
73def setup_lock_and_counter(lock, counter, total):
74 global output_lock, test_counter, total_tests
75 output_lock = lock
76 test_counter = counter
77 total_tests = total
78
Chaoren Lina4447b32015-06-07 18:50:40 +000079def update_status(name = None, command = None, output = None):
Chaoren Lin5e3ab2b2015-06-01 17:49:25 +000080 global output_lock, test_counter, total_tests
81 with output_lock:
82 if output is not None:
83 print >> sys.stderr
Chaoren Lina4447b32015-06-07 18:50:40 +000084 print >> sys.stderr, "Failed test suite: %s" % name
85 print >> sys.stderr, "Command invoked: %s" % ' '.join(command)
86 print >> sys.stderr, "stdout:\n%s" % output[0]
87 print >> sys.stderr, "stderr:\n%s" % output[1]
Chaoren Lin5e3ab2b2015-06-01 17:49:25 +000088 sys.stderr.write("\r%*d out of %d test suites processed" %
89 (len(str(total_tests)), test_counter.value, total_tests))
90 test_counter.value += 1
91
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +000092def parse_test_results(output):
93 passes = 0
94 failures = 0
95 for result in output:
96 pass_count = re.search("^RESULT:.*([0-9]+) passes", result, re.MULTILINE)
97 fail_count = re.search("^RESULT:.*([0-9]+) failures", result, re.MULTILINE)
98 error_count = re.search("^RESULT:.*([0-9]+) errors", result, re.MULTILINE)
99 this_fail_count = 0
100 this_error_count = 0
101 if pass_count != None:
102 passes = passes + int(pass_count.group(1))
103 if fail_count != None:
104 failures = failures + int(fail_count.group(1))
105 if error_count != None:
106 failures = failures + int(error_count.group(1))
107 pass
108 return passes, failures
109
Chaoren Lin5e3ab2b2015-06-01 17:49:25 +0000110def call_with_timeout(command, timeout, name):
Vince Harronede59652015-01-08 02:11:26 +0000111 """Run command with a timeout if possible."""
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000112 """-s QUIT will create a coredump if they are enabled on your system"""
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000113 process = None
114 if timeout_command and timeout != "0":
115 command = [timeout_command, '-s', 'QUIT', timeout] + command
116 # Specifying a value for close_fds is unsupported on Windows when using subprocess.PIPE
Zachary Turnerdc494d52015-02-07 00:14:55 +0000117 if os.name != "nt":
Chaoren Lin9d2b7f92015-05-29 18:43:46 +0000118 process = subprocess.Popen(command, stdin=subprocess.PIPE,
119 stdout=subprocess.PIPE,
120 stderr=subprocess.PIPE,
121 close_fds=True)
Zachary Turnerdc494d52015-02-07 00:14:55 +0000122 else:
Chaoren Lin9d2b7f92015-05-29 18:43:46 +0000123 process = subprocess.Popen(command, stdin=subprocess.PIPE,
124 stdout=subprocess.PIPE,
125 stderr=subprocess.PIPE)
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000126 output = process.communicate()
127 exit_status = process.returncode
128 passes, failures = parse_test_results(output)
Chaoren Lina4447b32015-06-07 18:50:40 +0000129 update_status(name, command, output if exit_status != 0 else None)
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000130 return exit_status, passes, failures
Johnny Chene8d9dc62011-10-31 19:04:07 +0000131
Vince Harron41657cc2015-05-21 18:15:09 +0000132def process_dir(root, files, test_root, dotest_argv):
Steve Puccibefe2b12014-03-07 00:01:11 +0000133 """Examine a directory for tests, and invoke any found within it."""
Vince Harron17f429f2014-12-13 00:08:19 +0000134 timed_out = []
Daniel Maleacbaef262013-02-15 21:31:37 +0000135 failed = []
136 passed = []
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000137 pass_sub_count = 0
138 fail_sub_count = 0
Steve Puccibefe2b12014-03-07 00:01:11 +0000139 for name in files:
140 path = os.path.join(root, name)
141
142 # We're only interested in the test file with the "Test*.py" naming pattern.
143 if not name.startswith("Test") or not name.endswith(".py"):
144 continue
145
146 # Neither a symbolically linked file.
147 if os.path.islink(path):
148 continue
149
Zachary Turnerf6896b02015-01-05 19:37:03 +0000150 script_file = os.path.join(test_root, "dotest.py")
Zachary Turnerf6896b02015-01-05 19:37:03 +0000151 command = ([sys.executable, script_file] +
Vince Harron41657cc2015-05-21 18:15:09 +0000152 dotest_argv +
Vince Harron17f429f2014-12-13 00:08:19 +0000153 ["-p", name, root])
154
155 timeout_name = os.path.basename(os.path.splitext(name)[0]).upper()
156
157 timeout = os.getenv("LLDB_%s_TIMEOUT" % timeout_name) or default_timeout
158
Chaoren Lin5e3ab2b2015-06-01 17:49:25 +0000159 exit_status, pass_count, fail_count = call_with_timeout(command, timeout, name)
Vince Harron17f429f2014-12-13 00:08:19 +0000160
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000161 pass_sub_count = pass_sub_count + pass_count
162 fail_sub_count = fail_sub_count + fail_count
163
164 if exit_status == ePassed:
Steve Puccibefe2b12014-03-07 00:01:11 +0000165 passed.append(name)
Vince Harron17f429f2014-12-13 00:08:19 +0000166 else:
167 if eTimedOut == exit_status:
168 timed_out.append(name)
169 failed.append(name)
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000170 return (timed_out, failed, passed, fail_sub_count, pass_sub_count)
Steve Puccibefe2b12014-03-07 00:01:11 +0000171
172in_q = None
173out_q = None
174
Todd Fiala3f0a3602014-07-08 06:42:37 +0000175def process_dir_worker(arg_tuple):
Steve Puccibefe2b12014-03-07 00:01:11 +0000176 """Worker thread main loop when in multithreaded mode.
177 Takes one directory specification at a time and works on it."""
Vince Harron41657cc2015-05-21 18:15:09 +0000178 (root, files, test_root, dotest_argv) = arg_tuple
179 return process_dir(root, files, test_root, dotest_argv)
Steve Puccibefe2b12014-03-07 00:01:11 +0000180
Vince Harron41657cc2015-05-21 18:15:09 +0000181def walk_and_invoke(test_directory, test_subdir, dotest_argv, num_threads):
Steve Puccibefe2b12014-03-07 00:01:11 +0000182 """Look for matched files and invoke test driver on each one.
183 In single-threaded mode, each test driver is invoked directly.
184 In multi-threaded mode, submit each test driver to a worker
Vince Harrone06a7a82015-05-12 23:12:19 +0000185 queue, and then wait for all to complete.
186
187 test_directory - lldb/test/ directory
188 test_subdir - lldb/test/ or a subfolder with the tests we're interested in running
189 """
Todd Fiala3f0a3602014-07-08 06:42:37 +0000190
191 # Collect the test files that we'll run.
192 test_work_items = []
Vince Harrone06a7a82015-05-12 23:12:19 +0000193 for root, dirs, files in os.walk(test_subdir, topdown=False):
Vince Harron41657cc2015-05-21 18:15:09 +0000194 test_work_items.append((root, files, test_directory, dotest_argv))
Todd Fiala3f0a3602014-07-08 06:42:37 +0000195
Chaoren Lin5e3ab2b2015-06-01 17:49:25 +0000196 global output_lock, test_counter, total_tests
197 output_lock = multiprocessing.Lock()
198 total_tests = len(test_work_items)
199 test_counter = multiprocessing.Value('i', 0)
200 print >> sys.stderr, "Testing: %d tests, %d threads" % (total_tests, num_threads)
201 update_status()
202
Todd Fiala3f0a3602014-07-08 06:42:37 +0000203 # Run the items, either in a pool (for multicore speedup) or
204 # calling each individually.
205 if num_threads > 1:
Chaoren Lin5e3ab2b2015-06-01 17:49:25 +0000206 pool = multiprocessing.Pool(num_threads,
207 initializer = setup_lock_and_counter,
208 initargs = (output_lock, test_counter, total_tests))
Todd Fiala3f0a3602014-07-08 06:42:37 +0000209 test_results = pool.map(process_dir_worker, test_work_items)
210 else:
211 test_results = []
212 for work_item in test_work_items:
213 test_results.append(process_dir_worker(work_item))
214
Vince Harron17f429f2014-12-13 00:08:19 +0000215 timed_out = []
Steve Puccibefe2b12014-03-07 00:01:11 +0000216 failed = []
217 passed = []
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000218 fail_sub_count = 0
219 pass_sub_count = 0
Todd Fiala3f0a3602014-07-08 06:42:37 +0000220
221 for test_result in test_results:
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000222 (dir_timed_out, dir_failed, dir_passed, dir_fail_sub_count, dir_pass_sub_count) = test_result
Vince Harron17f429f2014-12-13 00:08:19 +0000223 timed_out += dir_timed_out
Todd Fiala3f0a3602014-07-08 06:42:37 +0000224 failed += dir_failed
225 passed += dir_passed
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000226 fail_sub_count = fail_sub_count + dir_fail_sub_count
227 pass_sub_count = pass_sub_count + dir_pass_sub_count
Todd Fiala3f0a3602014-07-08 06:42:37 +0000228
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000229 return (timed_out, failed, passed, fail_sub_count, pass_sub_count)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000230
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000231def getExpectedTimeouts(platform_name):
Vince Harron06381732015-05-12 23:10:36 +0000232 # returns a set of test filenames that might timeout
233 # are we running against a remote target?
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000234 if platform_name is None:
Vince Harron06381732015-05-12 23:10:36 +0000235 target = sys.platform
236 remote = False
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000237 else:
238 m = re.search('remote-(\w+)', platform_name)
239 target = m.group(1)
240 remote = True
Vince Harron06381732015-05-12 23:10:36 +0000241
242 expected_timeout = set()
243
244 if target.startswith("linux"):
245 expected_timeout |= {
Vince Harronde92b522015-05-13 23:59:03 +0000246 "TestAttachDenied.py",
Vince Harron06381732015-05-12 23:10:36 +0000247 "TestAttachResume.py",
248 "TestConnectRemote.py",
249 "TestCreateAfterAttach.py",
Tamas Berghammer0d0ec9f2015-05-19 10:49:40 +0000250 "TestEvents.py",
Vince Harron06381732015-05-12 23:10:36 +0000251 "TestExitDuringStep.py",
Tamas Berghammer0b11db52015-06-02 14:45:25 +0000252 "TestHelloWorld.py", # Times out in ~10% of the times on the build bot
Vince Harron06381732015-05-12 23:10:36 +0000253 "TestThreadStepOut.py",
254 }
255 elif target.startswith("android"):
256 expected_timeout |= {
257 "TestExitDuringStep.py",
258 "TestHelloWorld.py",
259 }
Ed Maste4dd8fba2015-05-14 16:25:52 +0000260 elif target.startswith("freebsd"):
261 expected_timeout |= {
262 "TestBreakpointConditions.py",
Ed Maste08948132015-05-28 18:45:30 +0000263 "TestChangeProcessGroup.py",
Ed Mastebfd05632015-05-27 19:11:29 +0000264 "TestValueObjectRecursion.py",
Ed Maste4dd8fba2015-05-14 16:25:52 +0000265 "TestWatchpointConditionAPI.py",
266 }
Vince Harron0f173ac2015-05-18 19:36:33 +0000267 elif target.startswith("darwin"):
268 expected_timeout |= {
269 "TestThreadSpecificBreakpoint.py", # times out on MBP Retina, Mid 2012
270 }
Vince Harron06381732015-05-12 23:10:36 +0000271 return expected_timeout
272
Vince Harron0b9dbb52015-05-21 18:18:52 +0000273def touch(fname, times=None):
274 with open(fname, 'a'):
275 os.utime(fname, times)
276
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000277def find(pattern, path):
278 result = []
279 for root, dirs, files in os.walk(path):
280 for name in files:
281 if fnmatch.fnmatch(name, pattern):
282 result.append(os.path.join(root, name))
283 return result
284
Johnny Chene8d9dc62011-10-31 19:04:07 +0000285def main():
Vince Harrond5fa1022015-05-10 15:24:12 +0000286 # We can't use sys.path[0] to determine the script directory
287 # because it doesn't work under a debugger
Vince Harrone06a7a82015-05-12 23:12:19 +0000288 test_directory = os.path.dirname(os.path.realpath(__file__))
Johnny Chene8d9dc62011-10-31 19:04:07 +0000289 parser = OptionParser(usage="""\
290Run lldb test suite using a separate process for each test file.
Vince Harronede59652015-01-08 02:11:26 +0000291
Siva Chandra2d7832e2015-05-08 23:08:53 +0000292 Each test will run with a time limit of 10 minutes by default.
Vince Harronede59652015-01-08 02:11:26 +0000293
Siva Chandra2d7832e2015-05-08 23:08:53 +0000294 Override the default time limit of 10 minutes by setting
Vince Harronede59652015-01-08 02:11:26 +0000295 the environment variable LLDB_TEST_TIMEOUT.
296
297 E.g., export LLDB_TEST_TIMEOUT=10m
298
299 Override the time limit for individual tests by setting
300 the environment variable LLDB_[TEST NAME]_TIMEOUT.
301
302 E.g., export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=2m
303
304 Set to "0" to run without time limit.
305
306 E.g., export LLDB_TEST_TIMEOUT=0
307 or export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=0
Johnny Chene8d9dc62011-10-31 19:04:07 +0000308""")
309 parser.add_option('-o', '--options',
310 type='string', action='store',
311 dest='dotest_options',
312 help="""The options passed to 'dotest.py' if specified.""")
313
Greg Clayton2256d0d2014-03-24 23:01:57 +0000314 parser.add_option('-t', '--threads',
315 type='int',
316 dest='num_threads',
Ed Mastecec2a5b2014-11-21 02:41:25 +0000317 help="""The number of threads to use when running tests separately.""")
Greg Clayton2256d0d2014-03-24 23:01:57 +0000318
Johnny Chene8d9dc62011-10-31 19:04:07 +0000319 opts, args = parser.parse_args()
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000320 dotest_option_string = opts.dotest_options
321
Vince Harron41657cc2015-05-21 18:15:09 +0000322 is_posix = (os.name == "posix")
323 dotest_argv = shlex.split(dotest_option_string, posix=is_posix) if dotest_option_string else []
Vince Harron8994fed2015-05-22 19:49:23 +0000324
325 parser = dotest_args.create_parser()
326 dotest_options = dotest_args.parse_args(parser, dotest_argv)
327
Vince Harron41657cc2015-05-21 18:15:09 +0000328 if not dotest_options.s:
329 # no session log directory, we need to add this to prevent
330 # every dotest invocation from creating its own directory
331 import datetime
332 # The windows platforms don't like ':' in the pathname.
333 timestamp_started = datetime.datetime.now().strftime("%Y-%m-%d-%H_%M_%S")
334 dotest_argv.append('-s')
335 dotest_argv.append(timestamp_started)
Vince Harron0b9dbb52015-05-21 18:18:52 +0000336 dotest_options.s = timestamp_started
337
338 session_dir = os.path.join(os.getcwd(), dotest_options.s)
Ed Mastecec2a5b2014-11-21 02:41:25 +0000339
Vince Harrone06a7a82015-05-12 23:12:19 +0000340 # The root directory was specified on the command line
341 if len(args) == 0:
342 test_subdir = test_directory
343 else:
344 test_subdir = os.path.join(test_directory, args[0])
345
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000346 # clean core files in test tree from previous runs (Linux)
347 cores = find('core.*', test_subdir)
348 for core in cores:
349 os.unlink(core)
350
Ed Mastecec2a5b2014-11-21 02:41:25 +0000351 if opts.num_threads:
352 num_threads = opts.num_threads
353 else:
Greg Clayton2256d0d2014-03-24 23:01:57 +0000354 num_threads_str = os.environ.get("LLDB_TEST_THREADS")
355 if num_threads_str:
356 num_threads = int(num_threads_str)
Greg Clayton2256d0d2014-03-24 23:01:57 +0000357 else:
Ed Mastecec2a5b2014-11-21 02:41:25 +0000358 num_threads = multiprocessing.cpu_count()
359 if num_threads < 1:
360 num_threads = 1
Johnny Chene8d9dc62011-10-31 19:04:07 +0000361
Daniel Maleab42556f2013-04-19 18:32:53 +0000362 system_info = " ".join(platform.uname())
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000363 (timed_out, failed, passed, all_fails, all_passes) = walk_and_invoke(test_directory, test_subdir, dotest_argv, num_threads)
364
Vince Harron17f429f2014-12-13 00:08:19 +0000365 timed_out = set(timed_out)
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000366 num_test_files = len(failed) + len(passed)
367 num_tests = all_fails + all_passes
Daniel Maleab42556f2013-04-19 18:32:53 +0000368
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000369 # move core files into session dir
370 cores = find('core.*', test_subdir)
371 for core in cores:
372 dst = core.replace(test_directory, "")[1:]
373 dst = dst.replace(os.path.sep, "-")
374 os.rename(core, os.path.join(session_dir, dst))
375
Vince Harron06381732015-05-12 23:10:36 +0000376 # remove expected timeouts from failures
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000377 expected_timeout = getExpectedTimeouts(dotest_options.lldb_platform_name)
Vince Harron06381732015-05-12 23:10:36 +0000378 for xtime in expected_timeout:
379 if xtime in timed_out:
380 timed_out.remove(xtime)
381 failed.remove(xtime)
Vince Harron0b9dbb52015-05-21 18:18:52 +0000382 result = "ExpectedTimeout"
383 elif xtime in passed:
384 result = "UnexpectedCompletion"
385 else:
386 result = None # failed
387
388 if result:
389 test_name = os.path.splitext(xtime)[0]
390 touch(os.path.join(session_dir, "{}-{}".format(result, test_name)))
Vince Harron06381732015-05-12 23:10:36 +0000391
Chaoren Lin5e3ab2b2015-06-01 17:49:25 +0000392 print
Chaoren Lin273aea82015-06-01 19:06:01 +0000393 print "Ran %d test suites (%d failed) (%f%%)" % (num_test_files, len(failed),
394 (100.0 * len(failed) / num_test_files) if num_test_files > 0 else float('NaN'))
395 print "Ran %d test cases (%d failed) (%f%%)" % (num_tests, all_fails,
396 (100.0 * all_fails / num_tests) if num_tests > 0 else float('NaN'))
Daniel Maleacbaef262013-02-15 21:31:37 +0000397 if len(failed) > 0:
Shawn Best13491c42014-10-22 19:29:00 +0000398 failed.sort()
Ying Chen10ed1a92015-05-28 23:51:49 +0000399 print "Failing Tests (%d)" % len(failed)
Daniel Maleacbaef262013-02-15 21:31:37 +0000400 for f in failed:
Vince Harron17f429f2014-12-13 00:08:19 +0000401 print "%s: LLDB (suite) :: %s (%s)" % (
402 "TIMEOUT" if f in timed_out else "FAIL", f, system_info
403 )
Daniel Maleacbaef262013-02-15 21:31:37 +0000404 sys.exit(1)
405 sys.exit(0)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000406
407if __name__ == '__main__':
408 main()