blob: de42ea0c0e6c65d06cb12bdd5661c3e48cd08ea1 [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:
51 subprocess.call("timeout")
52 return "timeout"
53 except OSError:
54 pass
55 try:
56 subprocess.call("gtimeout")
57 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
69def call_with_timeout(command, timeout):
Vince Harronede59652015-01-08 02:11:26 +000070 """Run command with a timeout if possible."""
Vince Harrondcc2b9f2015-05-27 04:40:36 +000071 """-s QUIT will create a coredump if they are enabled on your system"""
Zachary Turnerdc494d52015-02-07 00:14:55 +000072 if os.name != "nt":
73 if timeout_command and timeout != "0":
Vince Harrondcc2b9f2015-05-27 04:40:36 +000074 return subprocess.call([timeout_command, '-s', 'QUIT', timeout] + command,
Zachary Turnerdc494d52015-02-07 00:14:55 +000075 stdin=subprocess.PIPE, close_fds=True)
76 return (ePassed if subprocess.call(command, stdin=subprocess.PIPE, close_fds=True) == 0
77 else eFailed)
78 else:
79 if timeout_command and timeout != "0":
Vince Harrondcc2b9f2015-05-27 04:40:36 +000080 return subprocess.call([timeout_command, '-s', 'QUIT', timeout] + command,
Zachary Turnerdc494d52015-02-07 00:14:55 +000081 stdin=subprocess.PIPE)
82 return (ePassed if subprocess.call(command, stdin=subprocess.PIPE) == 0
83 else eFailed)
Johnny Chene8d9dc62011-10-31 19:04:07 +000084
Vince Harron41657cc2015-05-21 18:15:09 +000085def process_dir(root, files, test_root, dotest_argv):
Steve Puccibefe2b12014-03-07 00:01:11 +000086 """Examine a directory for tests, and invoke any found within it."""
Vince Harron17f429f2014-12-13 00:08:19 +000087 timed_out = []
Daniel Maleacbaef262013-02-15 21:31:37 +000088 failed = []
89 passed = []
Steve Puccibefe2b12014-03-07 00:01:11 +000090 for name in files:
91 path = os.path.join(root, name)
92
93 # We're only interested in the test file with the "Test*.py" naming pattern.
94 if not name.startswith("Test") or not name.endswith(".py"):
95 continue
96
97 # Neither a symbolically linked file.
98 if os.path.islink(path):
99 continue
100
Zachary Turnerf6896b02015-01-05 19:37:03 +0000101 script_file = os.path.join(test_root, "dotest.py")
Zachary Turnerf6896b02015-01-05 19:37:03 +0000102 command = ([sys.executable, script_file] +
Vince Harron41657cc2015-05-21 18:15:09 +0000103 dotest_argv +
Vince Harron17f429f2014-12-13 00:08:19 +0000104 ["-p", name, root])
105
106 timeout_name = os.path.basename(os.path.splitext(name)[0]).upper()
107
108 timeout = os.getenv("LLDB_%s_TIMEOUT" % timeout_name) or default_timeout
109
110 exit_status = call_with_timeout(command, timeout)
111
112 if ePassed == exit_status:
Steve Puccibefe2b12014-03-07 00:01:11 +0000113 passed.append(name)
Vince Harron17f429f2014-12-13 00:08:19 +0000114 else:
115 if eTimedOut == exit_status:
116 timed_out.append(name)
117 failed.append(name)
118 return (timed_out, failed, passed)
Steve Puccibefe2b12014-03-07 00:01:11 +0000119
120in_q = None
121out_q = None
122
Todd Fiala3f0a3602014-07-08 06:42:37 +0000123def process_dir_worker(arg_tuple):
Steve Puccibefe2b12014-03-07 00:01:11 +0000124 """Worker thread main loop when in multithreaded mode.
125 Takes one directory specification at a time and works on it."""
Vince Harron41657cc2015-05-21 18:15:09 +0000126 (root, files, test_root, dotest_argv) = arg_tuple
127 return process_dir(root, files, test_root, dotest_argv)
Steve Puccibefe2b12014-03-07 00:01:11 +0000128
Vince Harron41657cc2015-05-21 18:15:09 +0000129def walk_and_invoke(test_directory, test_subdir, dotest_argv, num_threads):
Steve Puccibefe2b12014-03-07 00:01:11 +0000130 """Look for matched files and invoke test driver on each one.
131 In single-threaded mode, each test driver is invoked directly.
132 In multi-threaded mode, submit each test driver to a worker
Vince Harrone06a7a82015-05-12 23:12:19 +0000133 queue, and then wait for all to complete.
134
135 test_directory - lldb/test/ directory
136 test_subdir - lldb/test/ or a subfolder with the tests we're interested in running
137 """
Todd Fiala3f0a3602014-07-08 06:42:37 +0000138
139 # Collect the test files that we'll run.
140 test_work_items = []
Vince Harrone06a7a82015-05-12 23:12:19 +0000141 for root, dirs, files in os.walk(test_subdir, topdown=False):
Vince Harron41657cc2015-05-21 18:15:09 +0000142 test_work_items.append((root, files, test_directory, dotest_argv))
Todd Fiala3f0a3602014-07-08 06:42:37 +0000143
144 # Run the items, either in a pool (for multicore speedup) or
145 # calling each individually.
146 if num_threads > 1:
147 pool = multiprocessing.Pool(num_threads)
148 test_results = pool.map(process_dir_worker, test_work_items)
149 else:
150 test_results = []
151 for work_item in test_work_items:
152 test_results.append(process_dir_worker(work_item))
153
Vince Harron17f429f2014-12-13 00:08:19 +0000154 timed_out = []
Steve Puccibefe2b12014-03-07 00:01:11 +0000155 failed = []
156 passed = []
Todd Fiala3f0a3602014-07-08 06:42:37 +0000157
158 for test_result in test_results:
Vince Harron17f429f2014-12-13 00:08:19 +0000159 (dir_timed_out, dir_failed, dir_passed) = test_result
160 timed_out += dir_timed_out
Todd Fiala3f0a3602014-07-08 06:42:37 +0000161 failed += dir_failed
162 passed += dir_passed
163
Vince Harron17f429f2014-12-13 00:08:19 +0000164 return (timed_out, failed, passed)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000165
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000166def getExpectedTimeouts(platform_name):
Vince Harron06381732015-05-12 23:10:36 +0000167 # returns a set of test filenames that might timeout
168 # are we running against a remote target?
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000169 if platform_name is None:
Vince Harron06381732015-05-12 23:10:36 +0000170 target = sys.platform
171 remote = False
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000172 else:
173 m = re.search('remote-(\w+)', platform_name)
174 target = m.group(1)
175 remote = True
Vince Harron06381732015-05-12 23:10:36 +0000176
177 expected_timeout = set()
178
179 if target.startswith("linux"):
180 expected_timeout |= {
Vince Harronde92b522015-05-13 23:59:03 +0000181 "TestAttachDenied.py",
Vince Harron06381732015-05-12 23:10:36 +0000182 "TestAttachResume.py",
183 "TestConnectRemote.py",
184 "TestCreateAfterAttach.py",
Tamas Berghammer0d0ec9f2015-05-19 10:49:40 +0000185 "TestEvents.py",
Vince Harron06381732015-05-12 23:10:36 +0000186 "TestExitDuringStep.py",
187 "TestThreadStepOut.py",
188 }
189 elif target.startswith("android"):
190 expected_timeout |= {
191 "TestExitDuringStep.py",
192 "TestHelloWorld.py",
193 }
Ed Maste4dd8fba2015-05-14 16:25:52 +0000194 elif target.startswith("freebsd"):
195 expected_timeout |= {
196 "TestBreakpointConditions.py",
Ed Maste08948132015-05-28 18:45:30 +0000197 "TestChangeProcessGroup.py",
Ed Mastebfd05632015-05-27 19:11:29 +0000198 "TestValueObjectRecursion.py",
Ed Maste4dd8fba2015-05-14 16:25:52 +0000199 "TestWatchpointConditionAPI.py",
200 }
Vince Harron0f173ac2015-05-18 19:36:33 +0000201 elif target.startswith("darwin"):
202 expected_timeout |= {
203 "TestThreadSpecificBreakpoint.py", # times out on MBP Retina, Mid 2012
204 }
Vince Harron06381732015-05-12 23:10:36 +0000205 return expected_timeout
206
Vince Harron0b9dbb52015-05-21 18:18:52 +0000207def touch(fname, times=None):
208 with open(fname, 'a'):
209 os.utime(fname, times)
210
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000211def find(pattern, path):
212 result = []
213 for root, dirs, files in os.walk(path):
214 for name in files:
215 if fnmatch.fnmatch(name, pattern):
216 result.append(os.path.join(root, name))
217 return result
218
Johnny Chene8d9dc62011-10-31 19:04:07 +0000219def main():
Vince Harrond5fa1022015-05-10 15:24:12 +0000220 # We can't use sys.path[0] to determine the script directory
221 # because it doesn't work under a debugger
Vince Harrone06a7a82015-05-12 23:12:19 +0000222 test_directory = os.path.dirname(os.path.realpath(__file__))
Johnny Chene8d9dc62011-10-31 19:04:07 +0000223 parser = OptionParser(usage="""\
224Run lldb test suite using a separate process for each test file.
Vince Harronede59652015-01-08 02:11:26 +0000225
Siva Chandra2d7832e2015-05-08 23:08:53 +0000226 Each test will run with a time limit of 10 minutes by default.
Vince Harronede59652015-01-08 02:11:26 +0000227
Siva Chandra2d7832e2015-05-08 23:08:53 +0000228 Override the default time limit of 10 minutes by setting
Vince Harronede59652015-01-08 02:11:26 +0000229 the environment variable LLDB_TEST_TIMEOUT.
230
231 E.g., export LLDB_TEST_TIMEOUT=10m
232
233 Override the time limit for individual tests by setting
234 the environment variable LLDB_[TEST NAME]_TIMEOUT.
235
236 E.g., export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=2m
237
238 Set to "0" to run without time limit.
239
240 E.g., export LLDB_TEST_TIMEOUT=0
241 or export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=0
Johnny Chene8d9dc62011-10-31 19:04:07 +0000242""")
243 parser.add_option('-o', '--options',
244 type='string', action='store',
245 dest='dotest_options',
246 help="""The options passed to 'dotest.py' if specified.""")
247
Greg Clayton2256d0d2014-03-24 23:01:57 +0000248 parser.add_option('-t', '--threads',
249 type='int',
250 dest='num_threads',
Ed Mastecec2a5b2014-11-21 02:41:25 +0000251 help="""The number of threads to use when running tests separately.""")
Greg Clayton2256d0d2014-03-24 23:01:57 +0000252
Johnny Chene8d9dc62011-10-31 19:04:07 +0000253 opts, args = parser.parse_args()
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000254 dotest_option_string = opts.dotest_options
255
Vince Harron41657cc2015-05-21 18:15:09 +0000256 is_posix = (os.name == "posix")
257 dotest_argv = shlex.split(dotest_option_string, posix=is_posix) if dotest_option_string else []
Vince Harron8994fed2015-05-22 19:49:23 +0000258
259 parser = dotest_args.create_parser()
260 dotest_options = dotest_args.parse_args(parser, dotest_argv)
261
Vince Harron41657cc2015-05-21 18:15:09 +0000262 if not dotest_options.s:
263 # no session log directory, we need to add this to prevent
264 # every dotest invocation from creating its own directory
265 import datetime
266 # The windows platforms don't like ':' in the pathname.
267 timestamp_started = datetime.datetime.now().strftime("%Y-%m-%d-%H_%M_%S")
268 dotest_argv.append('-s')
269 dotest_argv.append(timestamp_started)
Vince Harron0b9dbb52015-05-21 18:18:52 +0000270 dotest_options.s = timestamp_started
271
272 session_dir = os.path.join(os.getcwd(), dotest_options.s)
Ed Mastecec2a5b2014-11-21 02:41:25 +0000273
Vince Harrone06a7a82015-05-12 23:12:19 +0000274 # The root directory was specified on the command line
275 if len(args) == 0:
276 test_subdir = test_directory
277 else:
278 test_subdir = os.path.join(test_directory, args[0])
279
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000280 # clean core files in test tree from previous runs (Linux)
281 cores = find('core.*', test_subdir)
282 for core in cores:
283 os.unlink(core)
284
Ed Mastecec2a5b2014-11-21 02:41:25 +0000285 if opts.num_threads:
286 num_threads = opts.num_threads
287 else:
Greg Clayton2256d0d2014-03-24 23:01:57 +0000288 num_threads_str = os.environ.get("LLDB_TEST_THREADS")
289 if num_threads_str:
290 num_threads = int(num_threads_str)
Greg Clayton2256d0d2014-03-24 23:01:57 +0000291 else:
Ed Mastecec2a5b2014-11-21 02:41:25 +0000292 num_threads = multiprocessing.cpu_count()
293 if num_threads < 1:
294 num_threads = 1
Johnny Chene8d9dc62011-10-31 19:04:07 +0000295
Daniel Maleab42556f2013-04-19 18:32:53 +0000296 system_info = " ".join(platform.uname())
Vince Harron41657cc2015-05-21 18:15:09 +0000297 (timed_out, failed, passed) = walk_and_invoke(test_directory, test_subdir, dotest_argv,
Vince Harron17f429f2014-12-13 00:08:19 +0000298 num_threads)
299 timed_out = set(timed_out)
Daniel Maleacbaef262013-02-15 21:31:37 +0000300 num_tests = len(failed) + len(passed)
Daniel Maleab42556f2013-04-19 18:32:53 +0000301
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000302 # move core files into session dir
303 cores = find('core.*', test_subdir)
304 for core in cores:
305 dst = core.replace(test_directory, "")[1:]
306 dst = dst.replace(os.path.sep, "-")
307 os.rename(core, os.path.join(session_dir, dst))
308
Vince Harron06381732015-05-12 23:10:36 +0000309 # remove expected timeouts from failures
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000310 expected_timeout = getExpectedTimeouts(dotest_options.lldb_platform_name)
Vince Harron06381732015-05-12 23:10:36 +0000311 for xtime in expected_timeout:
312 if xtime in timed_out:
313 timed_out.remove(xtime)
314 failed.remove(xtime)
Vince Harron0b9dbb52015-05-21 18:18:52 +0000315 result = "ExpectedTimeout"
316 elif xtime in passed:
317 result = "UnexpectedCompletion"
318 else:
319 result = None # failed
320
321 if result:
322 test_name = os.path.splitext(xtime)[0]
323 touch(os.path.join(session_dir, "{}-{}".format(result, test_name)))
Vince Harron06381732015-05-12 23:10:36 +0000324
Daniel Maleacbaef262013-02-15 21:31:37 +0000325 print "Ran %d tests." % num_tests
326 if len(failed) > 0:
Shawn Best13491c42014-10-22 19:29:00 +0000327 failed.sort()
Daniel Maleacbaef262013-02-15 21:31:37 +0000328 print "Failing Tests (%d)" % len(failed)
329 for f in failed:
Vince Harron17f429f2014-12-13 00:08:19 +0000330 print "%s: LLDB (suite) :: %s (%s)" % (
331 "TIMEOUT" if f in timed_out else "FAIL", f, system_info
332 )
Daniel Maleacbaef262013-02-15 21:31:37 +0000333 sys.exit(1)
334 sys.exit(0)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000335
336if __name__ == '__main__':
337 main()