blob: 62b12b778953272fe8cfd90fc625d8580e5da2be [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 Harron17f429f2014-12-13 00:08:19 +000027import shlex
28import subprocess
Todd Fiala3f0a3602014-07-08 06:42:37 +000029import sys
Steve Puccibefe2b12014-03-07 00:01:11 +000030
Johnny Chene8d9dc62011-10-31 19:04:07 +000031from optparse import OptionParser
32
Vince Harron17f429f2014-12-13 00:08:19 +000033def get_timeout_command():
Vince Harronede59652015-01-08 02:11:26 +000034 """Search for a suitable timeout command."""
Vince Harron17f429f2014-12-13 00:08:19 +000035 if sys.platform.startswith("win32"):
36 return None
37 try:
38 subprocess.call("timeout")
39 return "timeout"
40 except OSError:
41 pass
42 try:
43 subprocess.call("gtimeout")
44 return "gtimeout"
45 except OSError:
46 pass
47 return None
48
49timeout_command = get_timeout_command()
50
Siva Chandra2d7832e2015-05-08 23:08:53 +000051default_timeout = os.getenv("LLDB_TEST_TIMEOUT") or "10m"
Vince Harron17f429f2014-12-13 00:08:19 +000052
53# Status codes for running command with timeout.
54eTimedOut, ePassed, eFailed = 124, 0, 1
55
56def call_with_timeout(command, timeout):
Vince Harronede59652015-01-08 02:11:26 +000057 """Run command with a timeout if possible."""
Zachary Turnerdc494d52015-02-07 00:14:55 +000058 if os.name != "nt":
59 if timeout_command and timeout != "0":
60 return subprocess.call([timeout_command, timeout] + command,
61 stdin=subprocess.PIPE, close_fds=True)
62 return (ePassed if subprocess.call(command, stdin=subprocess.PIPE, close_fds=True) == 0
63 else eFailed)
64 else:
65 if timeout_command and timeout != "0":
66 return subprocess.call([timeout_command, timeout] + command,
67 stdin=subprocess.PIPE)
68 return (ePassed if subprocess.call(command, stdin=subprocess.PIPE) == 0
69 else eFailed)
Johnny Chene8d9dc62011-10-31 19:04:07 +000070
Steve Puccibefe2b12014-03-07 00:01:11 +000071def process_dir(root, files, test_root, dotest_options):
72 """Examine a directory for tests, and invoke any found within it."""
Vince Harron17f429f2014-12-13 00:08:19 +000073 timed_out = []
Daniel Maleacbaef262013-02-15 21:31:37 +000074 failed = []
75 passed = []
Steve Puccibefe2b12014-03-07 00:01:11 +000076 for name in files:
77 path = os.path.join(root, name)
78
79 # We're only interested in the test file with the "Test*.py" naming pattern.
80 if not name.startswith("Test") or not name.endswith(".py"):
81 continue
82
83 # Neither a symbolically linked file.
84 if os.path.islink(path):
85 continue
86
Zachary Turnerf6896b02015-01-05 19:37:03 +000087 script_file = os.path.join(test_root, "dotest.py")
88 is_posix = (os.name == "posix")
89 split_args = shlex.split(dotest_options, posix=is_posix) if dotest_options else []
90 command = ([sys.executable, script_file] +
91 split_args +
Vince Harron17f429f2014-12-13 00:08:19 +000092 ["-p", name, root])
93
94 timeout_name = os.path.basename(os.path.splitext(name)[0]).upper()
95
96 timeout = os.getenv("LLDB_%s_TIMEOUT" % timeout_name) or default_timeout
97
98 exit_status = call_with_timeout(command, timeout)
99
100 if ePassed == exit_status:
Steve Puccibefe2b12014-03-07 00:01:11 +0000101 passed.append(name)
Vince Harron17f429f2014-12-13 00:08:19 +0000102 else:
103 if eTimedOut == exit_status:
104 timed_out.append(name)
105 failed.append(name)
106 return (timed_out, failed, passed)
Steve Puccibefe2b12014-03-07 00:01:11 +0000107
108in_q = None
109out_q = None
110
Todd Fiala3f0a3602014-07-08 06:42:37 +0000111def process_dir_worker(arg_tuple):
Steve Puccibefe2b12014-03-07 00:01:11 +0000112 """Worker thread main loop when in multithreaded mode.
113 Takes one directory specification at a time and works on it."""
Todd Fiala3f0a3602014-07-08 06:42:37 +0000114 (root, files, test_root, dotest_options) = arg_tuple
115 return process_dir(root, files, test_root, dotest_options)
Steve Puccibefe2b12014-03-07 00:01:11 +0000116
117def walk_and_invoke(test_root, dotest_options, num_threads):
118 """Look for matched files and invoke test driver on each one.
119 In single-threaded mode, each test driver is invoked directly.
120 In multi-threaded mode, submit each test driver to a worker
121 queue, and then wait for all to complete."""
Todd Fiala3f0a3602014-07-08 06:42:37 +0000122
123 # Collect the test files that we'll run.
124 test_work_items = []
125 for root, dirs, files in os.walk(test_root, topdown=False):
126 test_work_items.append((root, files, test_root, dotest_options))
127
128 # Run the items, either in a pool (for multicore speedup) or
129 # calling each individually.
130 if num_threads > 1:
131 pool = multiprocessing.Pool(num_threads)
132 test_results = pool.map(process_dir_worker, test_work_items)
133 else:
134 test_results = []
135 for work_item in test_work_items:
136 test_results.append(process_dir_worker(work_item))
137
Vince Harron17f429f2014-12-13 00:08:19 +0000138 timed_out = []
Steve Puccibefe2b12014-03-07 00:01:11 +0000139 failed = []
140 passed = []
Todd Fiala3f0a3602014-07-08 06:42:37 +0000141
142 for test_result in test_results:
Vince Harron17f429f2014-12-13 00:08:19 +0000143 (dir_timed_out, dir_failed, dir_passed) = test_result
144 timed_out += dir_timed_out
Todd Fiala3f0a3602014-07-08 06:42:37 +0000145 failed += dir_failed
146 passed += dir_passed
147
Vince Harron17f429f2014-12-13 00:08:19 +0000148 return (timed_out, failed, passed)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000149
150def main():
Vince Harrond5fa1022015-05-10 15:24:12 +0000151 # We can't use sys.path[0] to determine the script directory
152 # because it doesn't work under a debugger
153 test_root = os.path.dirname(os.path.realpath(__file__))
Johnny Chene8d9dc62011-10-31 19:04:07 +0000154
155 parser = OptionParser(usage="""\
156Run lldb test suite using a separate process for each test file.
Vince Harronede59652015-01-08 02:11:26 +0000157
Siva Chandra2d7832e2015-05-08 23:08:53 +0000158 Each test will run with a time limit of 10 minutes by default.
Vince Harronede59652015-01-08 02:11:26 +0000159
Siva Chandra2d7832e2015-05-08 23:08:53 +0000160 Override the default time limit of 10 minutes by setting
Vince Harronede59652015-01-08 02:11:26 +0000161 the environment variable LLDB_TEST_TIMEOUT.
162
163 E.g., export LLDB_TEST_TIMEOUT=10m
164
165 Override the time limit for individual tests by setting
166 the environment variable LLDB_[TEST NAME]_TIMEOUT.
167
168 E.g., export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=2m
169
170 Set to "0" to run without time limit.
171
172 E.g., export LLDB_TEST_TIMEOUT=0
173 or export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=0
Johnny Chene8d9dc62011-10-31 19:04:07 +0000174""")
175 parser.add_option('-o', '--options',
176 type='string', action='store',
177 dest='dotest_options',
178 help="""The options passed to 'dotest.py' if specified.""")
179
Greg Clayton2256d0d2014-03-24 23:01:57 +0000180 parser.add_option('-t', '--threads',
181 type='int',
182 dest='num_threads',
Ed Mastecec2a5b2014-11-21 02:41:25 +0000183 help="""The number of threads to use when running tests separately.""")
Greg Clayton2256d0d2014-03-24 23:01:57 +0000184
Johnny Chene8d9dc62011-10-31 19:04:07 +0000185 opts, args = parser.parse_args()
186 dotest_options = opts.dotest_options
Ed Mastecec2a5b2014-11-21 02:41:25 +0000187
188 if opts.num_threads:
189 num_threads = opts.num_threads
190 else:
Greg Clayton2256d0d2014-03-24 23:01:57 +0000191 num_threads_str = os.environ.get("LLDB_TEST_THREADS")
192 if num_threads_str:
193 num_threads = int(num_threads_str)
Greg Clayton2256d0d2014-03-24 23:01:57 +0000194 else:
Ed Mastecec2a5b2014-11-21 02:41:25 +0000195 num_threads = multiprocessing.cpu_count()
196 if num_threads < 1:
197 num_threads = 1
Johnny Chene8d9dc62011-10-31 19:04:07 +0000198
Daniel Maleab42556f2013-04-19 18:32:53 +0000199 system_info = " ".join(platform.uname())
Vince Harron17f429f2014-12-13 00:08:19 +0000200 (timed_out, failed, passed) = walk_and_invoke(test_root, dotest_options,
201 num_threads)
202 timed_out = set(timed_out)
Daniel Maleacbaef262013-02-15 21:31:37 +0000203 num_tests = len(failed) + len(passed)
Daniel Maleab42556f2013-04-19 18:32:53 +0000204
Daniel Maleacbaef262013-02-15 21:31:37 +0000205 print "Ran %d tests." % num_tests
206 if len(failed) > 0:
Shawn Best13491c42014-10-22 19:29:00 +0000207 failed.sort()
Daniel Maleacbaef262013-02-15 21:31:37 +0000208 print "Failing Tests (%d)" % len(failed)
209 for f in failed:
Vince Harron17f429f2014-12-13 00:08:19 +0000210 print "%s: LLDB (suite) :: %s (%s)" % (
211 "TIMEOUT" if f in timed_out else "FAIL", f, system_info
212 )
Daniel Maleacbaef262013-02-15 21:31:37 +0000213 sys.exit(1)
214 sys.exit(0)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000215
216if __name__ == '__main__':
217 main()