blob: 3c50f90f827919650c55ffd08aaae6c5eca2b652 [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":
Chaoren Lin9d2b7f92015-05-29 18:43:46 +000095 process = subprocess.Popen(command, stdin=subprocess.PIPE,
96 stdout=subprocess.PIPE,
97 stderr=subprocess.PIPE,
98 close_fds=True)
Zachary Turnerdc494d52015-02-07 00:14:55 +000099 else:
Chaoren Lin9d2b7f92015-05-29 18:43:46 +0000100 process = subprocess.Popen(command, stdin=subprocess.PIPE,
101 stdout=subprocess.PIPE,
102 stderr=subprocess.PIPE)
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000103 output = process.communicate()
104 exit_status = process.returncode
105 passes, failures = parse_test_results(output)
106 return exit_status, passes, failures
Johnny Chene8d9dc62011-10-31 19:04:07 +0000107
Vince Harron41657cc2015-05-21 18:15:09 +0000108def process_dir(root, files, test_root, dotest_argv):
Steve Puccibefe2b12014-03-07 00:01:11 +0000109 """Examine a directory for tests, and invoke any found within it."""
Vince Harron17f429f2014-12-13 00:08:19 +0000110 timed_out = []
Daniel Maleacbaef262013-02-15 21:31:37 +0000111 failed = []
112 passed = []
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000113 pass_sub_count = 0
114 fail_sub_count = 0
Steve Puccibefe2b12014-03-07 00:01:11 +0000115 for name in files:
116 path = os.path.join(root, name)
117
118 # We're only interested in the test file with the "Test*.py" naming pattern.
119 if not name.startswith("Test") or not name.endswith(".py"):
120 continue
121
122 # Neither a symbolically linked file.
123 if os.path.islink(path):
124 continue
125
Zachary Turnerf6896b02015-01-05 19:37:03 +0000126 script_file = os.path.join(test_root, "dotest.py")
Zachary Turnerf6896b02015-01-05 19:37:03 +0000127 command = ([sys.executable, script_file] +
Vince Harron41657cc2015-05-21 18:15:09 +0000128 dotest_argv +
Vince Harron17f429f2014-12-13 00:08:19 +0000129 ["-p", name, root])
130
131 timeout_name = os.path.basename(os.path.splitext(name)[0]).upper()
132
133 timeout = os.getenv("LLDB_%s_TIMEOUT" % timeout_name) or default_timeout
134
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000135 exit_status, pass_count, fail_count = call_with_timeout(command, timeout)
Vince Harron17f429f2014-12-13 00:08:19 +0000136
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000137 pass_sub_count = pass_sub_count + pass_count
138 fail_sub_count = fail_sub_count + fail_count
139
140 if exit_status == ePassed:
Steve Puccibefe2b12014-03-07 00:01:11 +0000141 passed.append(name)
Vince Harron17f429f2014-12-13 00:08:19 +0000142 else:
143 if eTimedOut == exit_status:
144 timed_out.append(name)
145 failed.append(name)
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000146 return (timed_out, failed, passed, fail_sub_count, pass_sub_count)
Steve Puccibefe2b12014-03-07 00:01:11 +0000147
148in_q = None
149out_q = None
150
Todd Fiala3f0a3602014-07-08 06:42:37 +0000151def process_dir_worker(arg_tuple):
Steve Puccibefe2b12014-03-07 00:01:11 +0000152 """Worker thread main loop when in multithreaded mode.
153 Takes one directory specification at a time and works on it."""
Vince Harron41657cc2015-05-21 18:15:09 +0000154 (root, files, test_root, dotest_argv) = arg_tuple
155 return process_dir(root, files, test_root, dotest_argv)
Steve Puccibefe2b12014-03-07 00:01:11 +0000156
Vince Harron41657cc2015-05-21 18:15:09 +0000157def walk_and_invoke(test_directory, test_subdir, dotest_argv, num_threads):
Steve Puccibefe2b12014-03-07 00:01:11 +0000158 """Look for matched files and invoke test driver on each one.
159 In single-threaded mode, each test driver is invoked directly.
160 In multi-threaded mode, submit each test driver to a worker
Vince Harrone06a7a82015-05-12 23:12:19 +0000161 queue, and then wait for all to complete.
162
163 test_directory - lldb/test/ directory
164 test_subdir - lldb/test/ or a subfolder with the tests we're interested in running
165 """
Todd Fiala3f0a3602014-07-08 06:42:37 +0000166
167 # Collect the test files that we'll run.
168 test_work_items = []
Vince Harrone06a7a82015-05-12 23:12:19 +0000169 for root, dirs, files in os.walk(test_subdir, topdown=False):
Vince Harron41657cc2015-05-21 18:15:09 +0000170 test_work_items.append((root, files, test_directory, dotest_argv))
Todd Fiala3f0a3602014-07-08 06:42:37 +0000171
172 # Run the items, either in a pool (for multicore speedup) or
173 # calling each individually.
174 if num_threads > 1:
175 pool = multiprocessing.Pool(num_threads)
176 test_results = pool.map(process_dir_worker, test_work_items)
177 else:
178 test_results = []
179 for work_item in test_work_items:
180 test_results.append(process_dir_worker(work_item))
181
Vince Harron17f429f2014-12-13 00:08:19 +0000182 timed_out = []
Steve Puccibefe2b12014-03-07 00:01:11 +0000183 failed = []
184 passed = []
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000185 fail_sub_count = 0
186 pass_sub_count = 0
Todd Fiala3f0a3602014-07-08 06:42:37 +0000187
188 for test_result in test_results:
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000189 (dir_timed_out, dir_failed, dir_passed, dir_fail_sub_count, dir_pass_sub_count) = test_result
Vince Harron17f429f2014-12-13 00:08:19 +0000190 timed_out += dir_timed_out
Todd Fiala3f0a3602014-07-08 06:42:37 +0000191 failed += dir_failed
192 passed += dir_passed
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000193 fail_sub_count = fail_sub_count + dir_fail_sub_count
194 pass_sub_count = pass_sub_count + dir_pass_sub_count
Todd Fiala3f0a3602014-07-08 06:42:37 +0000195
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000196 return (timed_out, failed, passed, fail_sub_count, pass_sub_count)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000197
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000198def getExpectedTimeouts(platform_name):
Vince Harron06381732015-05-12 23:10:36 +0000199 # returns a set of test filenames that might timeout
200 # are we running against a remote target?
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000201 if platform_name is None:
Vince Harron06381732015-05-12 23:10:36 +0000202 target = sys.platform
203 remote = False
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000204 else:
205 m = re.search('remote-(\w+)', platform_name)
206 target = m.group(1)
207 remote = True
Vince Harron06381732015-05-12 23:10:36 +0000208
209 expected_timeout = set()
210
211 if target.startswith("linux"):
212 expected_timeout |= {
Vince Harronde92b522015-05-13 23:59:03 +0000213 "TestAttachDenied.py",
Vince Harron06381732015-05-12 23:10:36 +0000214 "TestAttachResume.py",
215 "TestConnectRemote.py",
216 "TestCreateAfterAttach.py",
Tamas Berghammer0d0ec9f2015-05-19 10:49:40 +0000217 "TestEvents.py",
Vince Harron06381732015-05-12 23:10:36 +0000218 "TestExitDuringStep.py",
219 "TestThreadStepOut.py",
220 }
221 elif target.startswith("android"):
222 expected_timeout |= {
223 "TestExitDuringStep.py",
224 "TestHelloWorld.py",
225 }
Ed Maste4dd8fba2015-05-14 16:25:52 +0000226 elif target.startswith("freebsd"):
227 expected_timeout |= {
228 "TestBreakpointConditions.py",
Ed Maste08948132015-05-28 18:45:30 +0000229 "TestChangeProcessGroup.py",
Ed Mastebfd05632015-05-27 19:11:29 +0000230 "TestValueObjectRecursion.py",
Ed Maste4dd8fba2015-05-14 16:25:52 +0000231 "TestWatchpointConditionAPI.py",
232 }
Vince Harron0f173ac2015-05-18 19:36:33 +0000233 elif target.startswith("darwin"):
234 expected_timeout |= {
235 "TestThreadSpecificBreakpoint.py", # times out on MBP Retina, Mid 2012
236 }
Vince Harron06381732015-05-12 23:10:36 +0000237 return expected_timeout
238
Vince Harron0b9dbb52015-05-21 18:18:52 +0000239def touch(fname, times=None):
240 with open(fname, 'a'):
241 os.utime(fname, times)
242
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000243def find(pattern, path):
244 result = []
245 for root, dirs, files in os.walk(path):
246 for name in files:
247 if fnmatch.fnmatch(name, pattern):
248 result.append(os.path.join(root, name))
249 return result
250
Johnny Chene8d9dc62011-10-31 19:04:07 +0000251def main():
Vince Harrond5fa1022015-05-10 15:24:12 +0000252 # We can't use sys.path[0] to determine the script directory
253 # because it doesn't work under a debugger
Vince Harrone06a7a82015-05-12 23:12:19 +0000254 test_directory = os.path.dirname(os.path.realpath(__file__))
Johnny Chene8d9dc62011-10-31 19:04:07 +0000255 parser = OptionParser(usage="""\
256Run lldb test suite using a separate process for each test file.
Vince Harronede59652015-01-08 02:11:26 +0000257
Siva Chandra2d7832e2015-05-08 23:08:53 +0000258 Each test will run with a time limit of 10 minutes by default.
Vince Harronede59652015-01-08 02:11:26 +0000259
Siva Chandra2d7832e2015-05-08 23:08:53 +0000260 Override the default time limit of 10 minutes by setting
Vince Harronede59652015-01-08 02:11:26 +0000261 the environment variable LLDB_TEST_TIMEOUT.
262
263 E.g., export LLDB_TEST_TIMEOUT=10m
264
265 Override the time limit for individual tests by setting
266 the environment variable LLDB_[TEST NAME]_TIMEOUT.
267
268 E.g., export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=2m
269
270 Set to "0" to run without time limit.
271
272 E.g., export LLDB_TEST_TIMEOUT=0
273 or export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=0
Johnny Chene8d9dc62011-10-31 19:04:07 +0000274""")
275 parser.add_option('-o', '--options',
276 type='string', action='store',
277 dest='dotest_options',
278 help="""The options passed to 'dotest.py' if specified.""")
279
Greg Clayton2256d0d2014-03-24 23:01:57 +0000280 parser.add_option('-t', '--threads',
281 type='int',
282 dest='num_threads',
Ed Mastecec2a5b2014-11-21 02:41:25 +0000283 help="""The number of threads to use when running tests separately.""")
Greg Clayton2256d0d2014-03-24 23:01:57 +0000284
Johnny Chene8d9dc62011-10-31 19:04:07 +0000285 opts, args = parser.parse_args()
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000286 dotest_option_string = opts.dotest_options
287
Vince Harron41657cc2015-05-21 18:15:09 +0000288 is_posix = (os.name == "posix")
289 dotest_argv = shlex.split(dotest_option_string, posix=is_posix) if dotest_option_string else []
Vince Harron8994fed2015-05-22 19:49:23 +0000290
291 parser = dotest_args.create_parser()
292 dotest_options = dotest_args.parse_args(parser, dotest_argv)
293
Vince Harron41657cc2015-05-21 18:15:09 +0000294 if not dotest_options.s:
295 # no session log directory, we need to add this to prevent
296 # every dotest invocation from creating its own directory
297 import datetime
298 # The windows platforms don't like ':' in the pathname.
299 timestamp_started = datetime.datetime.now().strftime("%Y-%m-%d-%H_%M_%S")
300 dotest_argv.append('-s')
301 dotest_argv.append(timestamp_started)
Vince Harron0b9dbb52015-05-21 18:18:52 +0000302 dotest_options.s = timestamp_started
303
304 session_dir = os.path.join(os.getcwd(), dotest_options.s)
Ed Mastecec2a5b2014-11-21 02:41:25 +0000305
Vince Harrone06a7a82015-05-12 23:12:19 +0000306 # The root directory was specified on the command line
307 if len(args) == 0:
308 test_subdir = test_directory
309 else:
310 test_subdir = os.path.join(test_directory, args[0])
311
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000312 # clean core files in test tree from previous runs (Linux)
313 cores = find('core.*', test_subdir)
314 for core in cores:
315 os.unlink(core)
316
Ed Mastecec2a5b2014-11-21 02:41:25 +0000317 if opts.num_threads:
318 num_threads = opts.num_threads
319 else:
Greg Clayton2256d0d2014-03-24 23:01:57 +0000320 num_threads_str = os.environ.get("LLDB_TEST_THREADS")
321 if num_threads_str:
322 num_threads = int(num_threads_str)
Greg Clayton2256d0d2014-03-24 23:01:57 +0000323 else:
Ed Mastecec2a5b2014-11-21 02:41:25 +0000324 num_threads = multiprocessing.cpu_count()
325 if num_threads < 1:
326 num_threads = 1
Johnny Chene8d9dc62011-10-31 19:04:07 +0000327
Daniel Maleab42556f2013-04-19 18:32:53 +0000328 system_info = " ".join(platform.uname())
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000329 (timed_out, failed, passed, all_fails, all_passes) = walk_and_invoke(test_directory, test_subdir, dotest_argv, num_threads)
330
Vince Harron17f429f2014-12-13 00:08:19 +0000331 timed_out = set(timed_out)
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000332 num_test_files = len(failed) + len(passed)
333 num_tests = all_fails + all_passes
Daniel Maleab42556f2013-04-19 18:32:53 +0000334
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000335 # move core files into session dir
336 cores = find('core.*', test_subdir)
337 for core in cores:
338 dst = core.replace(test_directory, "")[1:]
339 dst = dst.replace(os.path.sep, "-")
340 os.rename(core, os.path.join(session_dir, dst))
341
Vince Harron06381732015-05-12 23:10:36 +0000342 # remove expected timeouts from failures
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000343 expected_timeout = getExpectedTimeouts(dotest_options.lldb_platform_name)
Vince Harron06381732015-05-12 23:10:36 +0000344 for xtime in expected_timeout:
345 if xtime in timed_out:
346 timed_out.remove(xtime)
347 failed.remove(xtime)
Vince Harron0b9dbb52015-05-21 18:18:52 +0000348 result = "ExpectedTimeout"
349 elif xtime in passed:
350 result = "UnexpectedCompletion"
351 else:
352 result = None # failed
353
354 if result:
355 test_name = os.path.splitext(xtime)[0]
356 touch(os.path.join(session_dir, "{}-{}".format(result, test_name)))
Vince Harron06381732015-05-12 23:10:36 +0000357
Zachary Turnerc7a7c8a2015-05-28 19:56:26 +0000358 print "Ran %d test suites (%d failed) (%f%%)" % (num_test_files, len(failed), 100.0*len(failed)/num_test_files)
359 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 +0000360 if len(failed) > 0:
Shawn Best13491c42014-10-22 19:29:00 +0000361 failed.sort()
Ying Chen10ed1a92015-05-28 23:51:49 +0000362 print "Failing Tests (%d)" % len(failed)
Daniel Maleacbaef262013-02-15 21:31:37 +0000363 for f in failed:
Vince Harron17f429f2014-12-13 00:08:19 +0000364 print "%s: LLDB (suite) :: %s (%s)" % (
365 "TIMEOUT" if f in timed_out else "FAIL", f, system_info
366 )
Daniel Maleacbaef262013-02-15 21:31:37 +0000367 sys.exit(1)
368 sys.exit(0)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000369
370if __name__ == '__main__':
371 main()