blob: 8a4814bae0747d138ca6af069ce9ad82879bf77a [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
Johnny Chene8d9dc62011-10-31 19:04:07 +000022"""
23
Greg Clayton2256d0d2014-03-24 23:01:57 +000024import multiprocessing
Todd Fiala3f0a3602014-07-08 06:42:37 +000025import os
26import platform
Vince Harron06381732015-05-12 23:10:36 +000027import re
Vince Harronf8b9a1d2015-05-18 19:40:54 +000028import dotest_args
Vince Harron17f429f2014-12-13 00:08:19 +000029import shlex
30import subprocess
Todd Fiala3f0a3602014-07-08 06:42:37 +000031import sys
Steve Puccibefe2b12014-03-07 00:01:11 +000032
Johnny Chene8d9dc62011-10-31 19:04:07 +000033from optparse import OptionParser
34
Vince Harron17f429f2014-12-13 00:08:19 +000035def get_timeout_command():
Vince Harronede59652015-01-08 02:11:26 +000036 """Search for a suitable timeout command."""
Vince Harron17f429f2014-12-13 00:08:19 +000037 if sys.platform.startswith("win32"):
38 return None
39 try:
40 subprocess.call("timeout")
41 return "timeout"
42 except OSError:
43 pass
44 try:
45 subprocess.call("gtimeout")
46 return "gtimeout"
47 except OSError:
48 pass
49 return None
50
51timeout_command = get_timeout_command()
52
Siva Chandra2d7832e2015-05-08 23:08:53 +000053default_timeout = os.getenv("LLDB_TEST_TIMEOUT") or "10m"
Vince Harron17f429f2014-12-13 00:08:19 +000054
55# Status codes for running command with timeout.
56eTimedOut, ePassed, eFailed = 124, 0, 1
57
58def call_with_timeout(command, timeout):
Vince Harronede59652015-01-08 02:11:26 +000059 """Run command with a timeout if possible."""
Zachary Turnerdc494d52015-02-07 00:14:55 +000060 if os.name != "nt":
61 if timeout_command and timeout != "0":
62 return subprocess.call([timeout_command, timeout] + command,
63 stdin=subprocess.PIPE, close_fds=True)
64 return (ePassed if subprocess.call(command, stdin=subprocess.PIPE, close_fds=True) == 0
65 else eFailed)
66 else:
67 if timeout_command and timeout != "0":
68 return subprocess.call([timeout_command, timeout] + command,
69 stdin=subprocess.PIPE)
70 return (ePassed if subprocess.call(command, stdin=subprocess.PIPE) == 0
71 else eFailed)
Johnny Chene8d9dc62011-10-31 19:04:07 +000072
Steve Puccibefe2b12014-03-07 00:01:11 +000073def process_dir(root, files, test_root, dotest_options):
74 """Examine a directory for tests, and invoke any found within it."""
Vince Harron17f429f2014-12-13 00:08:19 +000075 timed_out = []
Daniel Maleacbaef262013-02-15 21:31:37 +000076 failed = []
77 passed = []
Steve Puccibefe2b12014-03-07 00:01:11 +000078 for name in files:
79 path = os.path.join(root, name)
80
81 # We're only interested in the test file with the "Test*.py" naming pattern.
82 if not name.startswith("Test") or not name.endswith(".py"):
83 continue
84
85 # Neither a symbolically linked file.
86 if os.path.islink(path):
87 continue
88
Zachary Turnerf6896b02015-01-05 19:37:03 +000089 script_file = os.path.join(test_root, "dotest.py")
90 is_posix = (os.name == "posix")
91 split_args = shlex.split(dotest_options, posix=is_posix) if dotest_options else []
92 command = ([sys.executable, script_file] +
93 split_args +
Vince Harron17f429f2014-12-13 00:08:19 +000094 ["-p", name, root])
95
96 timeout_name = os.path.basename(os.path.splitext(name)[0]).upper()
97
98 timeout = os.getenv("LLDB_%s_TIMEOUT" % timeout_name) or default_timeout
99
100 exit_status = call_with_timeout(command, timeout)
101
102 if ePassed == exit_status:
Steve Puccibefe2b12014-03-07 00:01:11 +0000103 passed.append(name)
Vince Harron17f429f2014-12-13 00:08:19 +0000104 else:
105 if eTimedOut == exit_status:
106 timed_out.append(name)
107 failed.append(name)
108 return (timed_out, failed, passed)
Steve Puccibefe2b12014-03-07 00:01:11 +0000109
110in_q = None
111out_q = None
112
Todd Fiala3f0a3602014-07-08 06:42:37 +0000113def process_dir_worker(arg_tuple):
Steve Puccibefe2b12014-03-07 00:01:11 +0000114 """Worker thread main loop when in multithreaded mode.
115 Takes one directory specification at a time and works on it."""
Todd Fiala3f0a3602014-07-08 06:42:37 +0000116 (root, files, test_root, dotest_options) = arg_tuple
117 return process_dir(root, files, test_root, dotest_options)
Steve Puccibefe2b12014-03-07 00:01:11 +0000118
Vince Harrone06a7a82015-05-12 23:12:19 +0000119def walk_and_invoke(test_directory, test_subdir, dotest_options, num_threads):
Steve Puccibefe2b12014-03-07 00:01:11 +0000120 """Look for matched files and invoke test driver on each one.
121 In single-threaded mode, each test driver is invoked directly.
122 In multi-threaded mode, submit each test driver to a worker
Vince Harrone06a7a82015-05-12 23:12:19 +0000123 queue, and then wait for all to complete.
124
125 test_directory - lldb/test/ directory
126 test_subdir - lldb/test/ or a subfolder with the tests we're interested in running
127 """
Todd Fiala3f0a3602014-07-08 06:42:37 +0000128
129 # Collect the test files that we'll run.
130 test_work_items = []
Vince Harrone06a7a82015-05-12 23:12:19 +0000131 for root, dirs, files in os.walk(test_subdir, topdown=False):
132 test_work_items.append((root, files, test_directory, dotest_options))
Todd Fiala3f0a3602014-07-08 06:42:37 +0000133
134 # Run the items, either in a pool (for multicore speedup) or
135 # calling each individually.
136 if num_threads > 1:
137 pool = multiprocessing.Pool(num_threads)
138 test_results = pool.map(process_dir_worker, test_work_items)
139 else:
140 test_results = []
141 for work_item in test_work_items:
142 test_results.append(process_dir_worker(work_item))
143
Vince Harron17f429f2014-12-13 00:08:19 +0000144 timed_out = []
Steve Puccibefe2b12014-03-07 00:01:11 +0000145 failed = []
146 passed = []
Todd Fiala3f0a3602014-07-08 06:42:37 +0000147
148 for test_result in test_results:
Vince Harron17f429f2014-12-13 00:08:19 +0000149 (dir_timed_out, dir_failed, dir_passed) = test_result
150 timed_out += dir_timed_out
Todd Fiala3f0a3602014-07-08 06:42:37 +0000151 failed += dir_failed
152 passed += dir_passed
153
Vince Harron17f429f2014-12-13 00:08:19 +0000154 return (timed_out, failed, passed)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000155
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000156def getExpectedTimeouts(platform_name):
Vince Harron06381732015-05-12 23:10:36 +0000157 # returns a set of test filenames that might timeout
158 # are we running against a remote target?
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000159 if platform_name is None:
Vince Harron06381732015-05-12 23:10:36 +0000160 target = sys.platform
161 remote = False
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000162 else:
163 m = re.search('remote-(\w+)', platform_name)
164 target = m.group(1)
165 remote = True
Vince Harron06381732015-05-12 23:10:36 +0000166
167 expected_timeout = set()
168
169 if target.startswith("linux"):
170 expected_timeout |= {
Vince Harronde92b522015-05-13 23:59:03 +0000171 "TestAttachDenied.py",
Vince Harron06381732015-05-12 23:10:36 +0000172 "TestAttachResume.py",
173 "TestConnectRemote.py",
174 "TestCreateAfterAttach.py",
Tamas Berghammer0d0ec9f2015-05-19 10:49:40 +0000175 "TestEvents.py",
Vince Harron06381732015-05-12 23:10:36 +0000176 "TestExitDuringStep.py",
177 "TestThreadStepOut.py",
178 }
179 elif target.startswith("android"):
180 expected_timeout |= {
181 "TestExitDuringStep.py",
182 "TestHelloWorld.py",
183 }
Ed Maste4dd8fba2015-05-14 16:25:52 +0000184 elif target.startswith("freebsd"):
185 expected_timeout |= {
186 "TestBreakpointConditions.py",
187 "TestWatchpointConditionAPI.py",
188 }
Vince Harron0f173ac2015-05-18 19:36:33 +0000189 elif target.startswith("darwin"):
190 expected_timeout |= {
191 "TestThreadSpecificBreakpoint.py", # times out on MBP Retina, Mid 2012
192 }
Vince Harron06381732015-05-12 23:10:36 +0000193 return expected_timeout
194
Johnny Chene8d9dc62011-10-31 19:04:07 +0000195def main():
Vince Harrond5fa1022015-05-10 15:24:12 +0000196 # We can't use sys.path[0] to determine the script directory
197 # because it doesn't work under a debugger
Vince Harrone06a7a82015-05-12 23:12:19 +0000198 test_directory = os.path.dirname(os.path.realpath(__file__))
Johnny Chene8d9dc62011-10-31 19:04:07 +0000199 parser = OptionParser(usage="""\
200Run lldb test suite using a separate process for each test file.
Vince Harronede59652015-01-08 02:11:26 +0000201
Siva Chandra2d7832e2015-05-08 23:08:53 +0000202 Each test will run with a time limit of 10 minutes by default.
Vince Harronede59652015-01-08 02:11:26 +0000203
Siva Chandra2d7832e2015-05-08 23:08:53 +0000204 Override the default time limit of 10 minutes by setting
Vince Harronede59652015-01-08 02:11:26 +0000205 the environment variable LLDB_TEST_TIMEOUT.
206
207 E.g., export LLDB_TEST_TIMEOUT=10m
208
209 Override the time limit for individual tests by setting
210 the environment variable LLDB_[TEST NAME]_TIMEOUT.
211
212 E.g., export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=2m
213
214 Set to "0" to run without time limit.
215
216 E.g., export LLDB_TEST_TIMEOUT=0
217 or export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=0
Johnny Chene8d9dc62011-10-31 19:04:07 +0000218""")
219 parser.add_option('-o', '--options',
220 type='string', action='store',
221 dest='dotest_options',
222 help="""The options passed to 'dotest.py' if specified.""")
223
Greg Clayton2256d0d2014-03-24 23:01:57 +0000224 parser.add_option('-t', '--threads',
225 type='int',
226 dest='num_threads',
Ed Mastecec2a5b2014-11-21 02:41:25 +0000227 help="""The number of threads to use when running tests separately.""")
Greg Clayton2256d0d2014-03-24 23:01:57 +0000228
Johnny Chene8d9dc62011-10-31 19:04:07 +0000229 opts, args = parser.parse_args()
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000230 dotest_option_string = opts.dotest_options
231
232 dotest_argv = shlex.split(dotest_option_string)
233 dotest_options = dotest_args.getArguments(dotest_argv)
Ed Mastecec2a5b2014-11-21 02:41:25 +0000234
Vince Harrone06a7a82015-05-12 23:12:19 +0000235 # The root directory was specified on the command line
236 if len(args) == 0:
237 test_subdir = test_directory
238 else:
239 test_subdir = os.path.join(test_directory, args[0])
240
Ed Mastecec2a5b2014-11-21 02:41:25 +0000241 if opts.num_threads:
242 num_threads = opts.num_threads
243 else:
Greg Clayton2256d0d2014-03-24 23:01:57 +0000244 num_threads_str = os.environ.get("LLDB_TEST_THREADS")
245 if num_threads_str:
246 num_threads = int(num_threads_str)
Greg Clayton2256d0d2014-03-24 23:01:57 +0000247 else:
Ed Mastecec2a5b2014-11-21 02:41:25 +0000248 num_threads = multiprocessing.cpu_count()
249 if num_threads < 1:
250 num_threads = 1
Johnny Chene8d9dc62011-10-31 19:04:07 +0000251
Daniel Maleab42556f2013-04-19 18:32:53 +0000252 system_info = " ".join(platform.uname())
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000253 (timed_out, failed, passed) = walk_and_invoke(test_directory, test_subdir, dotest_option_string,
Vince Harron17f429f2014-12-13 00:08:19 +0000254 num_threads)
255 timed_out = set(timed_out)
Daniel Maleacbaef262013-02-15 21:31:37 +0000256 num_tests = len(failed) + len(passed)
Daniel Maleab42556f2013-04-19 18:32:53 +0000257
Vince Harron06381732015-05-12 23:10:36 +0000258 # remove expected timeouts from failures
Vince Harronf8b9a1d2015-05-18 19:40:54 +0000259 expected_timeout = getExpectedTimeouts(dotest_options.lldb_platform_name)
Vince Harron06381732015-05-12 23:10:36 +0000260 for xtime in expected_timeout:
261 if xtime in timed_out:
262 timed_out.remove(xtime)
263 failed.remove(xtime)
264
Daniel Maleacbaef262013-02-15 21:31:37 +0000265 print "Ran %d tests." % num_tests
266 if len(failed) > 0:
Shawn Best13491c42014-10-22 19:29:00 +0000267 failed.sort()
Daniel Maleacbaef262013-02-15 21:31:37 +0000268 print "Failing Tests (%d)" % len(failed)
269 for f in failed:
Vince Harron17f429f2014-12-13 00:08:19 +0000270 print "%s: LLDB (suite) :: %s (%s)" % (
271 "TIMEOUT" if f in timed_out else "FAIL", f, system_info
272 )
Daniel Maleacbaef262013-02-15 21:31:37 +0000273 sys.exit(1)
274 sys.exit(0)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000275
276if __name__ == '__main__':
277 main()