blob: abf32f2a13e4bdc1e7c4f798020d0ec1fe734517 [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 Mastebfd05632015-05-27 19:11:29 +0000197 "TestValueObjectRecursion.py",
Ed Maste4dd8fba2015-05-14 16:25:52 +0000198 "TestWatchpointConditionAPI.py",
199 }
Vince Harron0f173ac2015-05-18 19:36:33 +0000200 elif target.startswith("darwin"):
201 expected_timeout |= {
202 "TestThreadSpecificBreakpoint.py", # times out on MBP Retina, Mid 2012
203 }
Vince Harron06381732015-05-12 23:10:36 +0000204 return expected_timeout
205
Vince Harron0b9dbb52015-05-21 18:18:52 +0000206def touch(fname, times=None):
207 with open(fname, 'a'):
208 os.utime(fname, times)
209
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000210def find(pattern, path):
211 result = []
212 for root, dirs, files in os.walk(path):
213 for name in files:
214 if fnmatch.fnmatch(name, pattern):
215 result.append(os.path.join(root, name))
216 return result
217
Johnny Chene8d9dc62011-10-31 19:04:07 +0000218def main():
Vince Harrond5fa1022015-05-10 15:24:12 +0000219 # We can't use sys.path[0] to determine the script directory
220 # because it doesn't work under a debugger
Vince Harrone06a7a82015-05-12 23:12:19 +0000221 test_directory = os.path.dirname(os.path.realpath(__file__))
Johnny Chene8d9dc62011-10-31 19:04:07 +0000222 parser = OptionParser(usage="""\
223Run lldb test suite using a separate process for each test file.
Vince Harronede59652015-01-08 02:11:26 +0000224
Siva Chandra2d7832e2015-05-08 23:08:53 +0000225 Each test will run with a time limit of 10 minutes by default.
Vince Harronede59652015-01-08 02:11:26 +0000226
Siva Chandra2d7832e2015-05-08 23:08:53 +0000227 Override the default time limit of 10 minutes by setting
Vince Harronede59652015-01-08 02:11:26 +0000228 the environment variable LLDB_TEST_TIMEOUT.
229
230 E.g., export LLDB_TEST_TIMEOUT=10m
231
232 Override the time limit for individual tests by setting
233 the environment variable LLDB_[TEST NAME]_TIMEOUT.
234
235 E.g., export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=2m
236
237 Set to "0" to run without time limit.
238
239 E.g., export LLDB_TEST_TIMEOUT=0
240 or export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=0
Johnny Chene8d9dc62011-10-31 19:04:07 +0000241""")
242 parser.add_option('-o', '--options',
243 type='string', action='store',
244 dest='dotest_options',
245 help="""The options passed to 'dotest.py' if specified.""")
246
Greg Clayton2256d0d2014-03-24 23:01:57 +0000247 parser.add_option('-t', '--threads',
248 type='int',
249 dest='num_threads',
Ed Mastecec2a5b2014-11-21 02:41:25 +0000250 help="""The number of threads to use when running tests separately.""")
Greg Clayton2256d0d2014-03-24 23:01:57 +0000251
Johnny Chene8d9dc62011-10-31 19:04:07 +0000252 opts, args = parser.parse_args()
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000253 dotest_option_string = opts.dotest_options
254
Vince Harron41657cc2015-05-21 18:15:09 +0000255 is_posix = (os.name == "posix")
256 dotest_argv = shlex.split(dotest_option_string, posix=is_posix) if dotest_option_string else []
Vince Harron8994fed2015-05-22 19:49:23 +0000257
258 parser = dotest_args.create_parser()
259 dotest_options = dotest_args.parse_args(parser, dotest_argv)
260
Vince Harron41657cc2015-05-21 18:15:09 +0000261 if not dotest_options.s:
262 # no session log directory, we need to add this to prevent
263 # every dotest invocation from creating its own directory
264 import datetime
265 # The windows platforms don't like ':' in the pathname.
266 timestamp_started = datetime.datetime.now().strftime("%Y-%m-%d-%H_%M_%S")
267 dotest_argv.append('-s')
268 dotest_argv.append(timestamp_started)
Vince Harron0b9dbb52015-05-21 18:18:52 +0000269 dotest_options.s = timestamp_started
270
271 session_dir = os.path.join(os.getcwd(), dotest_options.s)
Ed Mastecec2a5b2014-11-21 02:41:25 +0000272
Vince Harrone06a7a82015-05-12 23:12:19 +0000273 # The root directory was specified on the command line
274 if len(args) == 0:
275 test_subdir = test_directory
276 else:
277 test_subdir = os.path.join(test_directory, args[0])
278
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000279 # clean core files in test tree from previous runs (Linux)
280 cores = find('core.*', test_subdir)
281 for core in cores:
282 os.unlink(core)
283
Ed Mastecec2a5b2014-11-21 02:41:25 +0000284 if opts.num_threads:
285 num_threads = opts.num_threads
286 else:
Greg Clayton2256d0d2014-03-24 23:01:57 +0000287 num_threads_str = os.environ.get("LLDB_TEST_THREADS")
288 if num_threads_str:
289 num_threads = int(num_threads_str)
Greg Clayton2256d0d2014-03-24 23:01:57 +0000290 else:
Ed Mastecec2a5b2014-11-21 02:41:25 +0000291 num_threads = multiprocessing.cpu_count()
292 if num_threads < 1:
293 num_threads = 1
Johnny Chene8d9dc62011-10-31 19:04:07 +0000294
Daniel Maleab42556f2013-04-19 18:32:53 +0000295 system_info = " ".join(platform.uname())
Vince Harron41657cc2015-05-21 18:15:09 +0000296 (timed_out, failed, passed) = walk_and_invoke(test_directory, test_subdir, dotest_argv,
Vince Harron17f429f2014-12-13 00:08:19 +0000297 num_threads)
298 timed_out = set(timed_out)
Daniel Maleacbaef262013-02-15 21:31:37 +0000299 num_tests = len(failed) + len(passed)
Daniel Maleab42556f2013-04-19 18:32:53 +0000300
Vince Harrondcc2b9f2015-05-27 04:40:36 +0000301 # move core files into session dir
302 cores = find('core.*', test_subdir)
303 for core in cores:
304 dst = core.replace(test_directory, "")[1:]
305 dst = dst.replace(os.path.sep, "-")
306 os.rename(core, os.path.join(session_dir, dst))
307
Vince Harron06381732015-05-12 23:10:36 +0000308 # remove expected timeouts from failures
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000309 expected_timeout = getExpectedTimeouts(dotest_options.lldb_platform_name)
Vince Harron06381732015-05-12 23:10:36 +0000310 for xtime in expected_timeout:
311 if xtime in timed_out:
312 timed_out.remove(xtime)
313 failed.remove(xtime)
Vince Harron0b9dbb52015-05-21 18:18:52 +0000314 result = "ExpectedTimeout"
315 elif xtime in passed:
316 result = "UnexpectedCompletion"
317 else:
318 result = None # failed
319
320 if result:
321 test_name = os.path.splitext(xtime)[0]
322 touch(os.path.join(session_dir, "{}-{}".format(result, test_name)))
Vince Harron06381732015-05-12 23:10:36 +0000323
Daniel Maleacbaef262013-02-15 21:31:37 +0000324 print "Ran %d tests." % num_tests
325 if len(failed) > 0:
Shawn Best13491c42014-10-22 19:29:00 +0000326 failed.sort()
Daniel Maleacbaef262013-02-15 21:31:37 +0000327 print "Failing Tests (%d)" % len(failed)
328 for f in failed:
Vince Harron17f429f2014-12-13 00:08:19 +0000329 print "%s: LLDB (suite) :: %s (%s)" % (
330 "TIMEOUT" if f in timed_out else "FAIL", f, system_info
331 )
Daniel Maleacbaef262013-02-15 21:31:37 +0000332 sys.exit(1)
333 sys.exit(0)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000334
335if __name__ == '__main__':
336 main()