blob: cc487759e2741599733c365f57edf02799897d69 [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
79def update_status(name = None, output = None):
80 global output_lock, test_counter, total_tests
81 with output_lock:
82 if output is not None:
83 print >> sys.stderr
84 print >> sys.stderr, 'Test suite %s failed' % name
85 print >> sys.stderr, 'stdout:\n' + output[0]
86 print >> sys.stderr, 'stderr:\n' + output[1]
87 sys.stderr.write("\r%*d out of %d test suites processed" %
88 (len(str(total_tests)), test_counter.value, total_tests))
89 test_counter.value += 1
90
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +000091def parse_test_results(output):
92 passes = 0
93 failures = 0
94 for result in output:
95 pass_count = re.search("^RESULT:.*([0-9]+) passes", result, re.MULTILINE)
96 fail_count = re.search("^RESULT:.*([0-9]+) failures", result, re.MULTILINE)
97 error_count = re.search("^RESULT:.*([0-9]+) errors", result, re.MULTILINE)
98 this_fail_count = 0
99 this_error_count = 0
100 if pass_count != None:
101 passes = passes + int(pass_count.group(1))
102 if fail_count != None:
103 failures = failures + int(fail_count.group(1))
104 if error_count != None:
105 failures = failures + int(error_count.group(1))
106 pass
107 return passes, failures
108
Chaoren Lin5e3ab2b2015-06-01 17:49:25 +0000109def call_with_timeout(command, timeout, name):
Vince Harronede59652015-01-08 02:11:26 +0000110 """Run command with a timeout if possible."""
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000111 """-s QUIT will create a coredump if they are enabled on your system"""
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000112 process = None
113 if timeout_command and timeout != "0":
114 command = [timeout_command, '-s', 'QUIT', timeout] + command
115 # Specifying a value for close_fds is unsupported on Windows when using subprocess.PIPE
Zachary Turnerdc494d52015-02-07 00:14:55 +0000116 if os.name != "nt":
Chaoren Lin9d2b7f92015-05-29 18:43:46 +0000117 process = subprocess.Popen(command, stdin=subprocess.PIPE,
118 stdout=subprocess.PIPE,
119 stderr=subprocess.PIPE,
120 close_fds=True)
Zachary Turnerdc494d52015-02-07 00:14:55 +0000121 else:
Chaoren Lin9d2b7f92015-05-29 18:43:46 +0000122 process = subprocess.Popen(command, stdin=subprocess.PIPE,
123 stdout=subprocess.PIPE,
124 stderr=subprocess.PIPE)
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000125 output = process.communicate()
126 exit_status = process.returncode
127 passes, failures = parse_test_results(output)
Chaoren Lin273aea82015-06-01 19:06:01 +0000128 update_status(name, output if exit_status != 0 else None)
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000129 return exit_status, passes, failures
Johnny Chene8d9dc62011-10-31 19:04:07 +0000130
Vince Harron41657cc2015-05-21 18:15:09 +0000131def process_dir(root, files, test_root, dotest_argv):
Steve Puccibefe2b12014-03-07 00:01:11 +0000132 """Examine a directory for tests, and invoke any found within it."""
Vince Harron17f429f2014-12-13 00:08:19 +0000133 timed_out = []
Daniel Maleacbaef262013-02-15 21:31:37 +0000134 failed = []
135 passed = []
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000136 pass_sub_count = 0
137 fail_sub_count = 0
Steve Puccibefe2b12014-03-07 00:01:11 +0000138 for name in files:
139 path = os.path.join(root, name)
140
141 # We're only interested in the test file with the "Test*.py" naming pattern.
142 if not name.startswith("Test") or not name.endswith(".py"):
143 continue
144
145 # Neither a symbolically linked file.
146 if os.path.islink(path):
147 continue
148
Zachary Turnerf6896b02015-01-05 19:37:03 +0000149 script_file = os.path.join(test_root, "dotest.py")
Zachary Turnerf6896b02015-01-05 19:37:03 +0000150 command = ([sys.executable, script_file] +
Vince Harron41657cc2015-05-21 18:15:09 +0000151 dotest_argv +
Vince Harron17f429f2014-12-13 00:08:19 +0000152 ["-p", name, root])
153
154 timeout_name = os.path.basename(os.path.splitext(name)[0]).upper()
155
156 timeout = os.getenv("LLDB_%s_TIMEOUT" % timeout_name) or default_timeout
157
Chaoren Lin5e3ab2b2015-06-01 17:49:25 +0000158 exit_status, pass_count, fail_count = call_with_timeout(command, timeout, name)
Vince Harron17f429f2014-12-13 00:08:19 +0000159
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000160 pass_sub_count = pass_sub_count + pass_count
161 fail_sub_count = fail_sub_count + fail_count
162
163 if exit_status == ePassed:
Steve Puccibefe2b12014-03-07 00:01:11 +0000164 passed.append(name)
Vince Harron17f429f2014-12-13 00:08:19 +0000165 else:
166 if eTimedOut == exit_status:
167 timed_out.append(name)
168 failed.append(name)
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000169 return (timed_out, failed, passed, fail_sub_count, pass_sub_count)
Steve Puccibefe2b12014-03-07 00:01:11 +0000170
171in_q = None
172out_q = None
173
Todd Fiala3f0a3602014-07-08 06:42:37 +0000174def process_dir_worker(arg_tuple):
Steve Puccibefe2b12014-03-07 00:01:11 +0000175 """Worker thread main loop when in multithreaded mode.
176 Takes one directory specification at a time and works on it."""
Vince Harron41657cc2015-05-21 18:15:09 +0000177 (root, files, test_root, dotest_argv) = arg_tuple
178 return process_dir(root, files, test_root, dotest_argv)
Steve Puccibefe2b12014-03-07 00:01:11 +0000179
Vince Harron41657cc2015-05-21 18:15:09 +0000180def walk_and_invoke(test_directory, test_subdir, dotest_argv, num_threads):
Steve Puccibefe2b12014-03-07 00:01:11 +0000181 """Look for matched files and invoke test driver on each one.
182 In single-threaded mode, each test driver is invoked directly.
183 In multi-threaded mode, submit each test driver to a worker
Vince Harrone06a7a82015-05-12 23:12:19 +0000184 queue, and then wait for all to complete.
185
186 test_directory - lldb/test/ directory
187 test_subdir - lldb/test/ or a subfolder with the tests we're interested in running
188 """
Todd Fiala3f0a3602014-07-08 06:42:37 +0000189
190 # Collect the test files that we'll run.
191 test_work_items = []
Vince Harrone06a7a82015-05-12 23:12:19 +0000192 for root, dirs, files in os.walk(test_subdir, topdown=False):
Vince Harron41657cc2015-05-21 18:15:09 +0000193 test_work_items.append((root, files, test_directory, dotest_argv))
Todd Fiala3f0a3602014-07-08 06:42:37 +0000194
Chaoren Lin5e3ab2b2015-06-01 17:49:25 +0000195 global output_lock, test_counter, total_tests
196 output_lock = multiprocessing.Lock()
197 total_tests = len(test_work_items)
198 test_counter = multiprocessing.Value('i', 0)
199 print >> sys.stderr, "Testing: %d tests, %d threads" % (total_tests, num_threads)
200 update_status()
201
Todd Fiala3f0a3602014-07-08 06:42:37 +0000202 # Run the items, either in a pool (for multicore speedup) or
203 # calling each individually.
204 if num_threads > 1:
Chaoren Lin5e3ab2b2015-06-01 17:49:25 +0000205 pool = multiprocessing.Pool(num_threads,
206 initializer = setup_lock_and_counter,
207 initargs = (output_lock, test_counter, total_tests))
Todd Fiala3f0a3602014-07-08 06:42:37 +0000208 test_results = pool.map(process_dir_worker, test_work_items)
209 else:
210 test_results = []
211 for work_item in test_work_items:
212 test_results.append(process_dir_worker(work_item))
213
Vince Harron17f429f2014-12-13 00:08:19 +0000214 timed_out = []
Steve Puccibefe2b12014-03-07 00:01:11 +0000215 failed = []
216 passed = []
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000217 fail_sub_count = 0
218 pass_sub_count = 0
Todd Fiala3f0a3602014-07-08 06:42:37 +0000219
220 for test_result in test_results:
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000221 (dir_timed_out, dir_failed, dir_passed, dir_fail_sub_count, dir_pass_sub_count) = test_result
Vince Harron17f429f2014-12-13 00:08:19 +0000222 timed_out += dir_timed_out
Todd Fiala3f0a3602014-07-08 06:42:37 +0000223 failed += dir_failed
224 passed += dir_passed
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000225 fail_sub_count = fail_sub_count + dir_fail_sub_count
226 pass_sub_count = pass_sub_count + dir_pass_sub_count
Todd Fiala3f0a3602014-07-08 06:42:37 +0000227
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000228 return (timed_out, failed, passed, fail_sub_count, pass_sub_count)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000229
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000230def getExpectedTimeouts(platform_name):
Vince Harron06381732015-05-12 23:10:36 +0000231 # returns a set of test filenames that might timeout
232 # are we running against a remote target?
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000233 if platform_name is None:
Vince Harron06381732015-05-12 23:10:36 +0000234 target = sys.platform
235 remote = False
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000236 else:
237 m = re.search('remote-(\w+)', platform_name)
238 target = m.group(1)
239 remote = True
Vince Harron06381732015-05-12 23:10:36 +0000240
241 expected_timeout = set()
242
243 if target.startswith("linux"):
244 expected_timeout |= {
Vince Harronde92b522015-05-13 23:59:03 +0000245 "TestAttachDenied.py",
Vince Harron06381732015-05-12 23:10:36 +0000246 "TestAttachResume.py",
247 "TestConnectRemote.py",
248 "TestCreateAfterAttach.py",
Tamas Berghammer0d0ec9f2015-05-19 10:49:40 +0000249 "TestEvents.py",
Vince Harron06381732015-05-12 23:10:36 +0000250 "TestExitDuringStep.py",
Tamas Berghammer0b11db52015-06-02 14:45:25 +0000251 "TestHelloWorld.py", # Times out in ~10% of the times on the build bot
Vince Harron06381732015-05-12 23:10:36 +0000252 "TestThreadStepOut.py",
253 }
254 elif target.startswith("android"):
255 expected_timeout |= {
256 "TestExitDuringStep.py",
257 "TestHelloWorld.py",
258 }
Ed Maste4dd8fba2015-05-14 16:25:52 +0000259 elif target.startswith("freebsd"):
260 expected_timeout |= {
261 "TestBreakpointConditions.py",
Ed Maste08948132015-05-28 18:45:30 +0000262 "TestChangeProcessGroup.py",
Ed Mastebfd05632015-05-27 19:11:29 +0000263 "TestValueObjectRecursion.py",
Ed Maste4dd8fba2015-05-14 16:25:52 +0000264 "TestWatchpointConditionAPI.py",
265 }
Vince Harron0f173ac2015-05-18 19:36:33 +0000266 elif target.startswith("darwin"):
267 expected_timeout |= {
268 "TestThreadSpecificBreakpoint.py", # times out on MBP Retina, Mid 2012
269 }
Vince Harron06381732015-05-12 23:10:36 +0000270 return expected_timeout
271
Vince Harron0b9dbb52015-05-21 18:18:52 +0000272def touch(fname, times=None):
273 with open(fname, 'a'):
274 os.utime(fname, times)
275
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000276def find(pattern, path):
277 result = []
278 for root, dirs, files in os.walk(path):
279 for name in files:
280 if fnmatch.fnmatch(name, pattern):
281 result.append(os.path.join(root, name))
282 return result
283
Johnny Chene8d9dc62011-10-31 19:04:07 +0000284def main():
Vince Harrond5fa1022015-05-10 15:24:12 +0000285 # We can't use sys.path[0] to determine the script directory
286 # because it doesn't work under a debugger
Vince Harrone06a7a82015-05-12 23:12:19 +0000287 test_directory = os.path.dirname(os.path.realpath(__file__))
Johnny Chene8d9dc62011-10-31 19:04:07 +0000288 parser = OptionParser(usage="""\
289Run lldb test suite using a separate process for each test file.
Vince Harronede59652015-01-08 02:11:26 +0000290
Siva Chandra2d7832e2015-05-08 23:08:53 +0000291 Each test will run with a time limit of 10 minutes by default.
Vince Harronede59652015-01-08 02:11:26 +0000292
Siva Chandra2d7832e2015-05-08 23:08:53 +0000293 Override the default time limit of 10 minutes by setting
Vince Harronede59652015-01-08 02:11:26 +0000294 the environment variable LLDB_TEST_TIMEOUT.
295
296 E.g., export LLDB_TEST_TIMEOUT=10m
297
298 Override the time limit for individual tests by setting
299 the environment variable LLDB_[TEST NAME]_TIMEOUT.
300
301 E.g., export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=2m
302
303 Set to "0" to run without time limit.
304
305 E.g., export LLDB_TEST_TIMEOUT=0
306 or export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=0
Johnny Chene8d9dc62011-10-31 19:04:07 +0000307""")
308 parser.add_option('-o', '--options',
309 type='string', action='store',
310 dest='dotest_options',
311 help="""The options passed to 'dotest.py' if specified.""")
312
Greg Clayton2256d0d2014-03-24 23:01:57 +0000313 parser.add_option('-t', '--threads',
314 type='int',
315 dest='num_threads',
Ed Mastecec2a5b2014-11-21 02:41:25 +0000316 help="""The number of threads to use when running tests separately.""")
Greg Clayton2256d0d2014-03-24 23:01:57 +0000317
Johnny Chene8d9dc62011-10-31 19:04:07 +0000318 opts, args = parser.parse_args()
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000319 dotest_option_string = opts.dotest_options
320
Vince Harron41657cc2015-05-21 18:15:09 +0000321 is_posix = (os.name == "posix")
322 dotest_argv = shlex.split(dotest_option_string, posix=is_posix) if dotest_option_string else []
Vince Harron8994fed2015-05-22 19:49:23 +0000323
324 parser = dotest_args.create_parser()
325 dotest_options = dotest_args.parse_args(parser, dotest_argv)
326
Vince Harron41657cc2015-05-21 18:15:09 +0000327 if not dotest_options.s:
328 # no session log directory, we need to add this to prevent
329 # every dotest invocation from creating its own directory
330 import datetime
331 # The windows platforms don't like ':' in the pathname.
332 timestamp_started = datetime.datetime.now().strftime("%Y-%m-%d-%H_%M_%S")
333 dotest_argv.append('-s')
334 dotest_argv.append(timestamp_started)
Vince Harron0b9dbb52015-05-21 18:18:52 +0000335 dotest_options.s = timestamp_started
336
337 session_dir = os.path.join(os.getcwd(), dotest_options.s)
Ed Mastecec2a5b2014-11-21 02:41:25 +0000338
Vince Harrone06a7a82015-05-12 23:12:19 +0000339 # The root directory was specified on the command line
340 if len(args) == 0:
341 test_subdir = test_directory
342 else:
343 test_subdir = os.path.join(test_directory, args[0])
344
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000345 # clean core files in test tree from previous runs (Linux)
346 cores = find('core.*', test_subdir)
347 for core in cores:
348 os.unlink(core)
349
Ed Mastecec2a5b2014-11-21 02:41:25 +0000350 if opts.num_threads:
351 num_threads = opts.num_threads
352 else:
Greg Clayton2256d0d2014-03-24 23:01:57 +0000353 num_threads_str = os.environ.get("LLDB_TEST_THREADS")
354 if num_threads_str:
355 num_threads = int(num_threads_str)
Greg Clayton2256d0d2014-03-24 23:01:57 +0000356 else:
Ed Mastecec2a5b2014-11-21 02:41:25 +0000357 num_threads = multiprocessing.cpu_count()
358 if num_threads < 1:
359 num_threads = 1
Johnny Chene8d9dc62011-10-31 19:04:07 +0000360
Daniel Maleab42556f2013-04-19 18:32:53 +0000361 system_info = " ".join(platform.uname())
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000362 (timed_out, failed, passed, all_fails, all_passes) = walk_and_invoke(test_directory, test_subdir, dotest_argv, num_threads)
363
Vince Harron17f429f2014-12-13 00:08:19 +0000364 timed_out = set(timed_out)
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000365 num_test_files = len(failed) + len(passed)
366 num_tests = all_fails + all_passes
Daniel Maleab42556f2013-04-19 18:32:53 +0000367
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000368 # move core files into session dir
369 cores = find('core.*', test_subdir)
370 for core in cores:
371 dst = core.replace(test_directory, "")[1:]
372 dst = dst.replace(os.path.sep, "-")
373 os.rename(core, os.path.join(session_dir, dst))
374
Vince Harron06381732015-05-12 23:10:36 +0000375 # remove expected timeouts from failures
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000376 expected_timeout = getExpectedTimeouts(dotest_options.lldb_platform_name)
Vince Harron06381732015-05-12 23:10:36 +0000377 for xtime in expected_timeout:
378 if xtime in timed_out:
379 timed_out.remove(xtime)
380 failed.remove(xtime)
Vince Harron0b9dbb52015-05-21 18:18:52 +0000381 result = "ExpectedTimeout"
382 elif xtime in passed:
383 result = "UnexpectedCompletion"
384 else:
385 result = None # failed
386
387 if result:
388 test_name = os.path.splitext(xtime)[0]
389 touch(os.path.join(session_dir, "{}-{}".format(result, test_name)))
Vince Harron06381732015-05-12 23:10:36 +0000390
Chaoren Lin5e3ab2b2015-06-01 17:49:25 +0000391 print
Chaoren Lin273aea82015-06-01 19:06:01 +0000392 print "Ran %d test suites (%d failed) (%f%%)" % (num_test_files, len(failed),
393 (100.0 * len(failed) / num_test_files) if num_test_files > 0 else float('NaN'))
394 print "Ran %d test cases (%d failed) (%f%%)" % (num_tests, all_fails,
395 (100.0 * all_fails / num_tests) if num_tests > 0 else float('NaN'))
Daniel Maleacbaef262013-02-15 21:31:37 +0000396 if len(failed) > 0:
Shawn Best13491c42014-10-22 19:29:00 +0000397 failed.sort()
Ying Chen10ed1a92015-05-28 23:51:49 +0000398 print "Failing Tests (%d)" % len(failed)
Daniel Maleacbaef262013-02-15 21:31:37 +0000399 for f in failed:
Vince Harron17f429f2014-12-13 00:08:19 +0000400 print "%s: LLDB (suite) :: %s (%s)" % (
401 "TIMEOUT" if f in timed_out else "FAIL", f, system_info
402 )
Daniel Maleacbaef262013-02-15 21:31:37 +0000403 sys.exit(1)
404 sys.exit(0)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000405
406if __name__ == '__main__':
407 main()