blob: dedc6097fb0bd4ae77b4a628ab5ffc23f00ab119 [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
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +000069def parse_test_results(output):
70 passes = 0
71 failures = 0
72 for result in output:
73 pass_count = re.search("^RESULT:.*([0-9]+) passes", result, re.MULTILINE)
74 fail_count = re.search("^RESULT:.*([0-9]+) failures", result, re.MULTILINE)
75 error_count = re.search("^RESULT:.*([0-9]+) errors", result, re.MULTILINE)
76 this_fail_count = 0
77 this_error_count = 0
78 if pass_count != None:
79 passes = passes + int(pass_count.group(1))
80 if fail_count != None:
81 failures = failures + int(fail_count.group(1))
82 if error_count != None:
83 failures = failures + int(error_count.group(1))
84 pass
85 return passes, failures
86
Vince Harron17f429f2014-12-13 00:08:19 +000087def call_with_timeout(command, timeout):
Vince Harronede59652015-01-08 02:11:26 +000088 """Run command with a timeout if possible."""
Vince Harrondcc2b9f2015-05-27 04:40:36 +000089 """-s QUIT will create a coredump if they are enabled on your system"""
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +000090 process = None
91 if timeout_command and timeout != "0":
92 command = [timeout_command, '-s', 'QUIT', timeout] + command
93 # Specifying a value for close_fds is unsupported on Windows when using subprocess.PIPE
Zachary Turnerdc494d52015-02-07 00:14:55 +000094 if os.name != "nt":
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +000095 process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
Zachary Turnerdc494d52015-02-07 00:14:55 +000096 else:
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +000097 process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
98 output = process.communicate()
99 exit_status = process.returncode
100 passes, failures = parse_test_results(output)
101 return exit_status, passes, failures
Johnny Chene8d9dc62011-10-31 19:04:07 +0000102
Vince Harron41657cc2015-05-21 18:15:09 +0000103def process_dir(root, files, test_root, dotest_argv):
Steve Puccibefe2b12014-03-07 00:01:11 +0000104 """Examine a directory for tests, and invoke any found within it."""
Vince Harron17f429f2014-12-13 00:08:19 +0000105 timed_out = []
Daniel Maleacbaef262013-02-15 21:31:37 +0000106 failed = []
107 passed = []
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000108 pass_sub_count = 0
109 fail_sub_count = 0
Steve Puccibefe2b12014-03-07 00:01:11 +0000110 for name in files:
111 path = os.path.join(root, name)
112
113 # We're only interested in the test file with the "Test*.py" naming pattern.
114 if not name.startswith("Test") or not name.endswith(".py"):
115 continue
116
117 # Neither a symbolically linked file.
118 if os.path.islink(path):
119 continue
120
Zachary Turnerf6896b02015-01-05 19:37:03 +0000121 script_file = os.path.join(test_root, "dotest.py")
Zachary Turnerf6896b02015-01-05 19:37:03 +0000122 command = ([sys.executable, script_file] +
Vince Harron41657cc2015-05-21 18:15:09 +0000123 dotest_argv +
Vince Harron17f429f2014-12-13 00:08:19 +0000124 ["-p", name, root])
125
126 timeout_name = os.path.basename(os.path.splitext(name)[0]).upper()
127
128 timeout = os.getenv("LLDB_%s_TIMEOUT" % timeout_name) or default_timeout
129
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000130 exit_status, pass_count, fail_count = call_with_timeout(command, timeout)
Vince Harron17f429f2014-12-13 00:08:19 +0000131
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000132 pass_sub_count = pass_sub_count + pass_count
133 fail_sub_count = fail_sub_count + fail_count
134
135 if exit_status == ePassed:
Steve Puccibefe2b12014-03-07 00:01:11 +0000136 passed.append(name)
Vince Harron17f429f2014-12-13 00:08:19 +0000137 else:
138 if eTimedOut == exit_status:
139 timed_out.append(name)
140 failed.append(name)
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000141 return (timed_out, failed, passed, fail_sub_count, pass_sub_count)
Steve Puccibefe2b12014-03-07 00:01:11 +0000142
143in_q = None
144out_q = None
145
Todd Fiala3f0a3602014-07-08 06:42:37 +0000146def process_dir_worker(arg_tuple):
Steve Puccibefe2b12014-03-07 00:01:11 +0000147 """Worker thread main loop when in multithreaded mode.
148 Takes one directory specification at a time and works on it."""
Vince Harron41657cc2015-05-21 18:15:09 +0000149 (root, files, test_root, dotest_argv) = arg_tuple
150 return process_dir(root, files, test_root, dotest_argv)
Steve Puccibefe2b12014-03-07 00:01:11 +0000151
Vince Harron41657cc2015-05-21 18:15:09 +0000152def walk_and_invoke(test_directory, test_subdir, dotest_argv, num_threads):
Steve Puccibefe2b12014-03-07 00:01:11 +0000153 """Look for matched files and invoke test driver on each one.
154 In single-threaded mode, each test driver is invoked directly.
155 In multi-threaded mode, submit each test driver to a worker
Vince Harrone06a7a82015-05-12 23:12:19 +0000156 queue, and then wait for all to complete.
157
158 test_directory - lldb/test/ directory
159 test_subdir - lldb/test/ or a subfolder with the tests we're interested in running
160 """
Todd Fiala3f0a3602014-07-08 06:42:37 +0000161
162 # Collect the test files that we'll run.
163 test_work_items = []
Vince Harrone06a7a82015-05-12 23:12:19 +0000164 for root, dirs, files in os.walk(test_subdir, topdown=False):
Vince Harron41657cc2015-05-21 18:15:09 +0000165 test_work_items.append((root, files, test_directory, dotest_argv))
Todd Fiala3f0a3602014-07-08 06:42:37 +0000166
167 # Run the items, either in a pool (for multicore speedup) or
168 # calling each individually.
169 if num_threads > 1:
170 pool = multiprocessing.Pool(num_threads)
171 test_results = pool.map(process_dir_worker, test_work_items)
172 else:
173 test_results = []
174 for work_item in test_work_items:
175 test_results.append(process_dir_worker(work_item))
176
Vince Harron17f429f2014-12-13 00:08:19 +0000177 timed_out = []
Steve Puccibefe2b12014-03-07 00:01:11 +0000178 failed = []
179 passed = []
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000180 fail_sub_count = 0
181 pass_sub_count = 0
Todd Fiala3f0a3602014-07-08 06:42:37 +0000182
183 for test_result in test_results:
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000184 (dir_timed_out, dir_failed, dir_passed, dir_fail_sub_count, dir_pass_sub_count) = test_result
Vince Harron17f429f2014-12-13 00:08:19 +0000185 timed_out += dir_timed_out
Todd Fiala3f0a3602014-07-08 06:42:37 +0000186 failed += dir_failed
187 passed += dir_passed
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000188 fail_sub_count = fail_sub_count + dir_fail_sub_count
189 pass_sub_count = pass_sub_count + dir_pass_sub_count
Todd Fiala3f0a3602014-07-08 06:42:37 +0000190
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000191 return (timed_out, failed, passed, fail_sub_count, pass_sub_count)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000192
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000193def getExpectedTimeouts(platform_name):
Vince Harron06381732015-05-12 23:10:36 +0000194 # returns a set of test filenames that might timeout
195 # are we running against a remote target?
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000196 if platform_name is None:
Vince Harron06381732015-05-12 23:10:36 +0000197 target = sys.platform
198 remote = False
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000199 else:
200 m = re.search('remote-(\w+)', platform_name)
201 target = m.group(1)
202 remote = True
Vince Harron06381732015-05-12 23:10:36 +0000203
204 expected_timeout = set()
205
206 if target.startswith("linux"):
207 expected_timeout |= {
Vince Harronde92b522015-05-13 23:59:03 +0000208 "TestAttachDenied.py",
Vince Harron06381732015-05-12 23:10:36 +0000209 "TestAttachResume.py",
210 "TestConnectRemote.py",
211 "TestCreateAfterAttach.py",
Tamas Berghammer0d0ec9f2015-05-19 10:49:40 +0000212 "TestEvents.py",
Vince Harron06381732015-05-12 23:10:36 +0000213 "TestExitDuringStep.py",
214 "TestThreadStepOut.py",
215 }
216 elif target.startswith("android"):
217 expected_timeout |= {
218 "TestExitDuringStep.py",
219 "TestHelloWorld.py",
220 }
Ed Maste4dd8fba2015-05-14 16:25:52 +0000221 elif target.startswith("freebsd"):
222 expected_timeout |= {
223 "TestBreakpointConditions.py",
Ed Maste08948132015-05-28 18:45:30 +0000224 "TestChangeProcessGroup.py",
Ed Mastebfd05632015-05-27 19:11:29 +0000225 "TestValueObjectRecursion.py",
Ed Maste4dd8fba2015-05-14 16:25:52 +0000226 "TestWatchpointConditionAPI.py",
227 }
Vince Harron0f173ac2015-05-18 19:36:33 +0000228 elif target.startswith("darwin"):
229 expected_timeout |= {
230 "TestThreadSpecificBreakpoint.py", # times out on MBP Retina, Mid 2012
231 }
Vince Harron06381732015-05-12 23:10:36 +0000232 return expected_timeout
233
Vince Harron0b9dbb52015-05-21 18:18:52 +0000234def touch(fname, times=None):
235 with open(fname, 'a'):
236 os.utime(fname, times)
237
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000238def find(pattern, path):
239 result = []
240 for root, dirs, files in os.walk(path):
241 for name in files:
242 if fnmatch.fnmatch(name, pattern):
243 result.append(os.path.join(root, name))
244 return result
245
Johnny Chene8d9dc62011-10-31 19:04:07 +0000246def main():
Vince Harrond5fa1022015-05-10 15:24:12 +0000247 # We can't use sys.path[0] to determine the script directory
248 # because it doesn't work under a debugger
Vince Harrone06a7a82015-05-12 23:12:19 +0000249 test_directory = os.path.dirname(os.path.realpath(__file__))
Johnny Chene8d9dc62011-10-31 19:04:07 +0000250 parser = OptionParser(usage="""\
251Run lldb test suite using a separate process for each test file.
Vince Harronede59652015-01-08 02:11:26 +0000252
Siva Chandra2d7832e2015-05-08 23:08:53 +0000253 Each test will run with a time limit of 10 minutes by default.
Vince Harronede59652015-01-08 02:11:26 +0000254
Siva Chandra2d7832e2015-05-08 23:08:53 +0000255 Override the default time limit of 10 minutes by setting
Vince Harronede59652015-01-08 02:11:26 +0000256 the environment variable LLDB_TEST_TIMEOUT.
257
258 E.g., export LLDB_TEST_TIMEOUT=10m
259
260 Override the time limit for individual tests by setting
261 the environment variable LLDB_[TEST NAME]_TIMEOUT.
262
263 E.g., export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=2m
264
265 Set to "0" to run without time limit.
266
267 E.g., export LLDB_TEST_TIMEOUT=0
268 or export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=0
Johnny Chene8d9dc62011-10-31 19:04:07 +0000269""")
270 parser.add_option('-o', '--options',
271 type='string', action='store',
272 dest='dotest_options',
273 help="""The options passed to 'dotest.py' if specified.""")
274
Greg Clayton2256d0d2014-03-24 23:01:57 +0000275 parser.add_option('-t', '--threads',
276 type='int',
277 dest='num_threads',
Ed Mastecec2a5b2014-11-21 02:41:25 +0000278 help="""The number of threads to use when running tests separately.""")
Greg Clayton2256d0d2014-03-24 23:01:57 +0000279
Johnny Chene8d9dc62011-10-31 19:04:07 +0000280 opts, args = parser.parse_args()
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000281 dotest_option_string = opts.dotest_options
282
Vince Harron41657cc2015-05-21 18:15:09 +0000283 is_posix = (os.name == "posix")
284 dotest_argv = shlex.split(dotest_option_string, posix=is_posix) if dotest_option_string else []
Vince Harron8994fed2015-05-22 19:49:23 +0000285
286 parser = dotest_args.create_parser()
287 dotest_options = dotest_args.parse_args(parser, dotest_argv)
288
Vince Harron41657cc2015-05-21 18:15:09 +0000289 if not dotest_options.s:
290 # no session log directory, we need to add this to prevent
291 # every dotest invocation from creating its own directory
292 import datetime
293 # The windows platforms don't like ':' in the pathname.
294 timestamp_started = datetime.datetime.now().strftime("%Y-%m-%d-%H_%M_%S")
295 dotest_argv.append('-s')
296 dotest_argv.append(timestamp_started)
Vince Harron0b9dbb52015-05-21 18:18:52 +0000297 dotest_options.s = timestamp_started
298
299 session_dir = os.path.join(os.getcwd(), dotest_options.s)
Ed Mastecec2a5b2014-11-21 02:41:25 +0000300
Vince Harrone06a7a82015-05-12 23:12:19 +0000301 # The root directory was specified on the command line
302 if len(args) == 0:
303 test_subdir = test_directory
304 else:
305 test_subdir = os.path.join(test_directory, args[0])
306
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000307 # clean core files in test tree from previous runs (Linux)
308 cores = find('core.*', test_subdir)
309 for core in cores:
310 os.unlink(core)
311
Ed Mastecec2a5b2014-11-21 02:41:25 +0000312 if opts.num_threads:
313 num_threads = opts.num_threads
314 else:
Greg Clayton2256d0d2014-03-24 23:01:57 +0000315 num_threads_str = os.environ.get("LLDB_TEST_THREADS")
316 if num_threads_str:
317 num_threads = int(num_threads_str)
Greg Clayton2256d0d2014-03-24 23:01:57 +0000318 else:
Ed Mastecec2a5b2014-11-21 02:41:25 +0000319 num_threads = multiprocessing.cpu_count()
320 if num_threads < 1:
321 num_threads = 1
Johnny Chene8d9dc62011-10-31 19:04:07 +0000322
Daniel Maleab42556f2013-04-19 18:32:53 +0000323 system_info = " ".join(platform.uname())
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000324 (timed_out, failed, passed, all_fails, all_passes) = walk_and_invoke(test_directory, test_subdir, dotest_argv, num_threads)
325
Vince Harron17f429f2014-12-13 00:08:19 +0000326 timed_out = set(timed_out)
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000327 num_test_files = len(failed) + len(passed)
328 num_tests = all_fails + all_passes
Daniel Maleab42556f2013-04-19 18:32:53 +0000329
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000330 # move core files into session dir
331 cores = find('core.*', test_subdir)
332 for core in cores:
333 dst = core.replace(test_directory, "")[1:]
334 dst = dst.replace(os.path.sep, "-")
335 os.rename(core, os.path.join(session_dir, dst))
336
Vince Harron06381732015-05-12 23:10:36 +0000337 # remove expected timeouts from failures
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000338 expected_timeout = getExpectedTimeouts(dotest_options.lldb_platform_name)
Vince Harron06381732015-05-12 23:10:36 +0000339 for xtime in expected_timeout:
340 if xtime in timed_out:
341 timed_out.remove(xtime)
342 failed.remove(xtime)
Vince Harron0b9dbb52015-05-21 18:18:52 +0000343 result = "ExpectedTimeout"
344 elif xtime in passed:
345 result = "UnexpectedCompletion"
346 else:
347 result = None # failed
348
349 if result:
350 test_name = os.path.splitext(xtime)[0]
351 touch(os.path.join(session_dir, "{}-{}".format(result, test_name)))
Vince Harron06381732015-05-12 23:10:36 +0000352
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000353 print "Ran %d test suites (%d failed) (%f%%)" % (num_test_files, len(failed), 100.0*len(failed)/num_test_files)
354 print "Ran %d test cases (%d failed) (%f%%)" % (num_tests, all_fails, 100.0*all_fails/num_tests)
Daniel Maleacbaef262013-02-15 21:31:37 +0000355 if len(failed) > 0:
Shawn Best13491c42014-10-22 19:29:00 +0000356 failed.sort()
Daniel Maleacbaef262013-02-15 21:31:37 +0000357 for f in failed:
Vince Harron17f429f2014-12-13 00:08:19 +0000358 print "%s: LLDB (suite) :: %s (%s)" % (
359 "TIMEOUT" if f in timed_out else "FAIL", f, system_info
360 )
Daniel Maleacbaef262013-02-15 21:31:37 +0000361 sys.exit(1)
362 sys.exit(0)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000363
364if __name__ == '__main__':
365 main()