Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | Run the test suite using a separate process for each test file. |
Vince Harron | ede5965 | 2015-01-08 02:11:26 +0000 | [diff] [blame] | 5 | |
Siva Chandra | 2d7832e | 2015-05-08 23:08:53 +0000 | [diff] [blame] | 6 | Each test will run with a time limit of 10 minutes by default. |
Vince Harron | ede5965 | 2015-01-08 02:11:26 +0000 | [diff] [blame] | 7 | |
Siva Chandra | 2d7832e | 2015-05-08 23:08:53 +0000 | [diff] [blame] | 8 | Override the default time limit of 10 minutes by setting |
Vince Harron | ede5965 | 2015-01-08 02:11:26 +0000 | [diff] [blame] | 9 | the environment variable LLDB_TEST_TIMEOUT. |
| 10 | |
| 11 | E.g., export LLDB_TEST_TIMEOUT=10m |
| 12 | |
| 13 | Override the time limit for individual tests by setting |
| 14 | the environment variable LLDB_[TEST NAME]_TIMEOUT. |
| 15 | |
| 16 | E.g., export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=2m |
| 17 | |
| 18 | Set to "0" to run without time limit. |
| 19 | |
| 20 | E.g., export LLDB_TEST_TIMEOUT=0 |
| 21 | or export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=0 |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 22 | """ |
| 23 | |
Greg Clayton | 2256d0d | 2014-03-24 23:01:57 +0000 | [diff] [blame] | 24 | import multiprocessing |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 25 | import os |
| 26 | import platform |
Vince Harron | 0638173 | 2015-05-12 23:10:36 +0000 | [diff] [blame^] | 27 | import re |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 28 | import shlex |
| 29 | import subprocess |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 30 | import sys |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 31 | |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 32 | from optparse import OptionParser |
| 33 | |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 34 | def get_timeout_command(): |
Vince Harron | ede5965 | 2015-01-08 02:11:26 +0000 | [diff] [blame] | 35 | """Search for a suitable timeout command.""" |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 36 | if sys.platform.startswith("win32"): |
| 37 | return None |
| 38 | try: |
| 39 | subprocess.call("timeout") |
| 40 | return "timeout" |
| 41 | except OSError: |
| 42 | pass |
| 43 | try: |
| 44 | subprocess.call("gtimeout") |
| 45 | return "gtimeout" |
| 46 | except OSError: |
| 47 | pass |
| 48 | return None |
| 49 | |
| 50 | timeout_command = get_timeout_command() |
| 51 | |
Siva Chandra | 2d7832e | 2015-05-08 23:08:53 +0000 | [diff] [blame] | 52 | default_timeout = os.getenv("LLDB_TEST_TIMEOUT") or "10m" |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 53 | |
| 54 | # Status codes for running command with timeout. |
| 55 | eTimedOut, ePassed, eFailed = 124, 0, 1 |
| 56 | |
| 57 | def call_with_timeout(command, timeout): |
Vince Harron | ede5965 | 2015-01-08 02:11:26 +0000 | [diff] [blame] | 58 | """Run command with a timeout if possible.""" |
Zachary Turner | dc494d5 | 2015-02-07 00:14:55 +0000 | [diff] [blame] | 59 | if os.name != "nt": |
| 60 | if timeout_command and timeout != "0": |
| 61 | return subprocess.call([timeout_command, timeout] + command, |
| 62 | stdin=subprocess.PIPE, close_fds=True) |
| 63 | return (ePassed if subprocess.call(command, stdin=subprocess.PIPE, close_fds=True) == 0 |
| 64 | else eFailed) |
| 65 | else: |
| 66 | if timeout_command and timeout != "0": |
| 67 | return subprocess.call([timeout_command, timeout] + command, |
| 68 | stdin=subprocess.PIPE) |
| 69 | return (ePassed if subprocess.call(command, stdin=subprocess.PIPE) == 0 |
| 70 | else eFailed) |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 71 | |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 72 | def process_dir(root, files, test_root, dotest_options): |
| 73 | """Examine a directory for tests, and invoke any found within it.""" |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 74 | timed_out = [] |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 75 | failed = [] |
| 76 | passed = [] |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 77 | for name in files: |
| 78 | path = os.path.join(root, name) |
| 79 | |
| 80 | # We're only interested in the test file with the "Test*.py" naming pattern. |
| 81 | if not name.startswith("Test") or not name.endswith(".py"): |
| 82 | continue |
| 83 | |
| 84 | # Neither a symbolically linked file. |
| 85 | if os.path.islink(path): |
| 86 | continue |
| 87 | |
Zachary Turner | f6896b0 | 2015-01-05 19:37:03 +0000 | [diff] [blame] | 88 | script_file = os.path.join(test_root, "dotest.py") |
| 89 | is_posix = (os.name == "posix") |
| 90 | split_args = shlex.split(dotest_options, posix=is_posix) if dotest_options else [] |
| 91 | command = ([sys.executable, script_file] + |
| 92 | split_args + |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 93 | ["-p", name, root]) |
| 94 | |
| 95 | timeout_name = os.path.basename(os.path.splitext(name)[0]).upper() |
| 96 | |
| 97 | timeout = os.getenv("LLDB_%s_TIMEOUT" % timeout_name) or default_timeout |
| 98 | |
| 99 | exit_status = call_with_timeout(command, timeout) |
| 100 | |
| 101 | if ePassed == exit_status: |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 102 | passed.append(name) |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 103 | else: |
| 104 | if eTimedOut == exit_status: |
| 105 | timed_out.append(name) |
| 106 | failed.append(name) |
| 107 | return (timed_out, failed, passed) |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 108 | |
| 109 | in_q = None |
| 110 | out_q = None |
| 111 | |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 112 | def process_dir_worker(arg_tuple): |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 113 | """Worker thread main loop when in multithreaded mode. |
| 114 | Takes one directory specification at a time and works on it.""" |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 115 | (root, files, test_root, dotest_options) = arg_tuple |
| 116 | return process_dir(root, files, test_root, dotest_options) |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 117 | |
| 118 | def walk_and_invoke(test_root, dotest_options, num_threads): |
| 119 | """Look for matched files and invoke test driver on each one. |
| 120 | In single-threaded mode, each test driver is invoked directly. |
| 121 | In multi-threaded mode, submit each test driver to a worker |
| 122 | queue, and then wait for all to complete.""" |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 123 | |
| 124 | # Collect the test files that we'll run. |
| 125 | test_work_items = [] |
| 126 | for root, dirs, files in os.walk(test_root, topdown=False): |
| 127 | test_work_items.append((root, files, test_root, dotest_options)) |
| 128 | |
| 129 | # Run the items, either in a pool (for multicore speedup) or |
| 130 | # calling each individually. |
| 131 | if num_threads > 1: |
| 132 | pool = multiprocessing.Pool(num_threads) |
| 133 | test_results = pool.map(process_dir_worker, test_work_items) |
| 134 | else: |
| 135 | test_results = [] |
| 136 | for work_item in test_work_items: |
| 137 | test_results.append(process_dir_worker(work_item)) |
| 138 | |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 139 | timed_out = [] |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 140 | failed = [] |
| 141 | passed = [] |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 142 | |
| 143 | for test_result in test_results: |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 144 | (dir_timed_out, dir_failed, dir_passed) = test_result |
| 145 | timed_out += dir_timed_out |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 146 | failed += dir_failed |
| 147 | passed += dir_passed |
| 148 | |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 149 | return (timed_out, failed, passed) |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 150 | |
Vince Harron | 0638173 | 2015-05-12 23:10:36 +0000 | [diff] [blame^] | 151 | def getExpectedTimeouts(dotest_options): |
| 152 | # returns a set of test filenames that might timeout |
| 153 | # are we running against a remote target? |
| 154 | m = re.search('\sremote-(\w+)', dotest_options) |
| 155 | if m: |
| 156 | target = m.group(1) |
| 157 | remote = True |
| 158 | else: |
| 159 | target = sys.platform |
| 160 | remote = False |
| 161 | |
| 162 | expected_timeout = set() |
| 163 | |
| 164 | if target.startswith("linux"): |
| 165 | expected_timeout |= { |
| 166 | "TestAttachResume.py", |
| 167 | "TestConnectRemote.py", |
| 168 | "TestCreateAfterAttach.py", |
| 169 | "TestExitDuringStep.py", |
| 170 | "TestThreadStepOut.py", |
| 171 | } |
| 172 | elif target.startswith("android"): |
| 173 | expected_timeout |= { |
| 174 | "TestExitDuringStep.py", |
| 175 | "TestHelloWorld.py", |
| 176 | } |
| 177 | return expected_timeout |
| 178 | |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 179 | def main(): |
Vince Harron | d5fa102 | 2015-05-10 15:24:12 +0000 | [diff] [blame] | 180 | # We can't use sys.path[0] to determine the script directory |
| 181 | # because it doesn't work under a debugger |
| 182 | test_root = os.path.dirname(os.path.realpath(__file__)) |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 183 | |
| 184 | parser = OptionParser(usage="""\ |
| 185 | Run lldb test suite using a separate process for each test file. |
Vince Harron | ede5965 | 2015-01-08 02:11:26 +0000 | [diff] [blame] | 186 | |
Siva Chandra | 2d7832e | 2015-05-08 23:08:53 +0000 | [diff] [blame] | 187 | Each test will run with a time limit of 10 minutes by default. |
Vince Harron | ede5965 | 2015-01-08 02:11:26 +0000 | [diff] [blame] | 188 | |
Siva Chandra | 2d7832e | 2015-05-08 23:08:53 +0000 | [diff] [blame] | 189 | Override the default time limit of 10 minutes by setting |
Vince Harron | ede5965 | 2015-01-08 02:11:26 +0000 | [diff] [blame] | 190 | the environment variable LLDB_TEST_TIMEOUT. |
| 191 | |
| 192 | E.g., export LLDB_TEST_TIMEOUT=10m |
| 193 | |
| 194 | Override the time limit for individual tests by setting |
| 195 | the environment variable LLDB_[TEST NAME]_TIMEOUT. |
| 196 | |
| 197 | E.g., export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=2m |
| 198 | |
| 199 | Set to "0" to run without time limit. |
| 200 | |
| 201 | E.g., export LLDB_TEST_TIMEOUT=0 |
| 202 | or export LLDB_TESTCONCURRENTEVENTS_TIMEOUT=0 |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 203 | """) |
| 204 | parser.add_option('-o', '--options', |
| 205 | type='string', action='store', |
| 206 | dest='dotest_options', |
| 207 | help="""The options passed to 'dotest.py' if specified.""") |
| 208 | |
Greg Clayton | 2256d0d | 2014-03-24 23:01:57 +0000 | [diff] [blame] | 209 | parser.add_option('-t', '--threads', |
| 210 | type='int', |
| 211 | dest='num_threads', |
Ed Maste | cec2a5b | 2014-11-21 02:41:25 +0000 | [diff] [blame] | 212 | help="""The number of threads to use when running tests separately.""") |
Greg Clayton | 2256d0d | 2014-03-24 23:01:57 +0000 | [diff] [blame] | 213 | |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 214 | opts, args = parser.parse_args() |
| 215 | dotest_options = opts.dotest_options |
Ed Maste | cec2a5b | 2014-11-21 02:41:25 +0000 | [diff] [blame] | 216 | |
| 217 | if opts.num_threads: |
| 218 | num_threads = opts.num_threads |
| 219 | else: |
Greg Clayton | 2256d0d | 2014-03-24 23:01:57 +0000 | [diff] [blame] | 220 | num_threads_str = os.environ.get("LLDB_TEST_THREADS") |
| 221 | if num_threads_str: |
| 222 | num_threads = int(num_threads_str) |
Greg Clayton | 2256d0d | 2014-03-24 23:01:57 +0000 | [diff] [blame] | 223 | else: |
Ed Maste | cec2a5b | 2014-11-21 02:41:25 +0000 | [diff] [blame] | 224 | num_threads = multiprocessing.cpu_count() |
| 225 | if num_threads < 1: |
| 226 | num_threads = 1 |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 227 | |
Daniel Malea | b42556f | 2013-04-19 18:32:53 +0000 | [diff] [blame] | 228 | system_info = " ".join(platform.uname()) |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 229 | (timed_out, failed, passed) = walk_and_invoke(test_root, dotest_options, |
| 230 | num_threads) |
| 231 | timed_out = set(timed_out) |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 232 | num_tests = len(failed) + len(passed) |
Daniel Malea | b42556f | 2013-04-19 18:32:53 +0000 | [diff] [blame] | 233 | |
Vince Harron | 0638173 | 2015-05-12 23:10:36 +0000 | [diff] [blame^] | 234 | # remove expected timeouts from failures |
| 235 | expected_timeout = getExpectedTimeouts(dotest_options) |
| 236 | for xtime in expected_timeout: |
| 237 | if xtime in timed_out: |
| 238 | timed_out.remove(xtime) |
| 239 | failed.remove(xtime) |
| 240 | |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 241 | print "Ran %d tests." % num_tests |
| 242 | if len(failed) > 0: |
Shawn Best | 13491c4 | 2014-10-22 19:29:00 +0000 | [diff] [blame] | 243 | failed.sort() |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 244 | print "Failing Tests (%d)" % len(failed) |
| 245 | for f in failed: |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 246 | print "%s: LLDB (suite) :: %s (%s)" % ( |
| 247 | "TIMEOUT" if f in timed_out else "FAIL", f, system_info |
| 248 | ) |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 249 | sys.exit(1) |
| 250 | sys.exit(0) |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 251 | |
| 252 | if __name__ == '__main__': |
| 253 | main() |