[5b993e0] | 1 | #!/usr/bin/python3 |
---|
[efc15918] | 2 | |
---|
[c07d724] | 3 | from pybin.tools import * |
---|
[0ad0c55] | 4 | from pybin.test_run import * |
---|
[bacc36c] | 5 | from pybin import settings |
---|
[efc15918] | 6 | |
---|
| 7 | import argparse |
---|
[122cac7] | 8 | import re |
---|
[efc15918] | 9 | import sys |
---|
[f806b61] | 10 | import tempfile |
---|
[ca54499] | 11 | import time |
---|
[efc15918] | 12 | |
---|
[2cd949b] | 13 | import os |
---|
| 14 | import psutil |
---|
| 15 | import signal |
---|
| 16 | |
---|
[efc15918] | 17 | ################################################################################ |
---|
| 18 | # help functions |
---|
| 19 | ################################################################################ |
---|
[f1231f2] | 20 | |
---|
[5bf1f3e] | 21 | def find_tests(): |
---|
[0ad0c55] | 22 | expected = [] |
---|
[f1231f2] | 23 | |
---|
[5bf1f3e] | 24 | def match_test(path): |
---|
[6044200] | 25 | match = re.search("^%s\/([\w\/\-_]*).expect\/([\w\-_]+)(\.[\w\-_]+)?\.txt$" % settings.SRCDIR, path) |
---|
[bacc36c] | 26 | if match : |
---|
| 27 | test = Test() |
---|
| 28 | test.name = match.group(2) |
---|
| 29 | test.path = match.group(1) |
---|
| 30 | test.arch = match.group(3)[1:] if match.group(3) else None |
---|
[f3b9efc] | 31 | if settings.arch.match(test.arch): |
---|
| 32 | expected.append(test) |
---|
[f803a75] | 33 | |
---|
[5bf1f3e] | 34 | path_walk( match_test ) |
---|
[c07d724] | 35 | |
---|
[0ad0c55] | 36 | return expected |
---|
[efc15918] | 37 | |
---|
[be65cca] | 38 | # reads the directory ./.expect and indentifies the tests |
---|
[5bf1f3e] | 39 | def list_tests( includes, excludes ): |
---|
[be65cca] | 40 | # tests directly in the .expect folder will always be processed |
---|
[5bf1f3e] | 41 | test_list = find_tests() |
---|
[be65cca] | 42 | |
---|
[0ad0c55] | 43 | # if we have a limited number of includes, filter by them |
---|
| 44 | if includes: |
---|
| 45 | test_list = [x for x in test_list if |
---|
[a85e44c] | 46 | x.target().startswith( tuple(includes) ) |
---|
[0ad0c55] | 47 | ] |
---|
[be65cca] | 48 | |
---|
[0ad0c55] | 49 | # # if we have a folders to excludes, filter by them |
---|
| 50 | if excludes: |
---|
| 51 | test_list = [x for x in test_list if not |
---|
[a85e44c] | 52 | x.target().startswith( tuple(excludes) ) |
---|
[0ad0c55] | 53 | ] |
---|
[f1231f2] | 54 | |
---|
[0ad0c55] | 55 | return test_list |
---|
[efc15918] | 56 | |
---|
[c07d724] | 57 | # from the found tests, filter all the valid tests/desired tests |
---|
[5bf1f3e] | 58 | def valid_tests( options ): |
---|
[c07d724] | 59 | tests = [] |
---|
| 60 | |
---|
| 61 | # if we are regenerating the tests we need to find the information of the |
---|
| 62 | # already existing tests and create new info for the new tests |
---|
| 63 | if options.regenerate_expected : |
---|
| 64 | for testname in options.tests : |
---|
[5bf1f3e] | 65 | testname = canonical_path( testname ) |
---|
[bacc36c] | 66 | if Test.valid_name(testname): |
---|
[5bf1f3e] | 67 | found = [test for test in all_tests if canonical_path( test.target() ) == testname] |
---|
[bacc36c] | 68 | tests.append( found[0] if len(found) == 1 else Test.from_target(testname) ) |
---|
[c07d724] | 69 | else : |
---|
[bacc36c] | 70 | print('ERROR: "%s", tests are not allowed to end with a C/C++/CFA extension, ignoring it' % testname, file=sys.stderr) |
---|
[c07d724] | 71 | |
---|
| 72 | else : |
---|
| 73 | # otherwise we only need to validate that all tests are present in the complete list |
---|
| 74 | for testname in options.tests: |
---|
[5bf1f3e] | 75 | test = [t for t in all_tests if path_cmp( t.target(), testname )] |
---|
[c07d724] | 76 | |
---|
[bacc36c] | 77 | if test : |
---|
[c07d724] | 78 | tests.append( test[0] ) |
---|
| 79 | else : |
---|
| 80 | print('ERROR: No expected file for test %s, ignoring it' % testname, file=sys.stderr) |
---|
| 81 | |
---|
| 82 | return tests |
---|
| 83 | |
---|
| 84 | # parses the option |
---|
[5bf1f3e] | 85 | def parse_args(): |
---|
[c07d724] | 86 | # create a parser with the arguments for the tests script |
---|
| 87 | parser = argparse.ArgumentParser(description='Script which runs cforall tests') |
---|
[28582b2] | 88 | parser.add_argument('--debug', help='Run all tests in debug or release', type=yes_no, default='yes') |
---|
[a5121bf] | 89 | parser.add_argument('--install', help='Run all tests based on installed binaries or tree binaries', type=yes_no, default='no') |
---|
[bacc36c] | 90 | parser.add_argument('--arch', help='Test for specific architecture', type=str, default='') |
---|
[afe8882] | 91 | parser.add_argument('--timeout', help='Maximum duration in seconds after a single test is considered to have timed out', type=int, default=60) |
---|
| 92 | parser.add_argument('--global-timeout', help='Maximum cumulative duration in seconds after the ALL tests are considered to have timed out', type=int, default=7200) |
---|
[c07d724] | 93 | parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true') |
---|
| 94 | parser.add_argument('--list', help='List all test available', action='store_true') |
---|
| 95 | parser.add_argument('--all', help='Run all test available', action='store_true') |
---|
| 96 | parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true') |
---|
[dcfedca] | 97 | parser.add_argument('--archive-errors', help='If called with a valid path, on test crashes the test script will copy the core dump and the executable to the specified path.', type=str, default='') |
---|
[d142ec5] | 98 | parser.add_argument('-j', '--jobs', help='Number of tests to run simultaneously', type=int) |
---|
[c07d724] | 99 | parser.add_argument('--list-comp', help='List all valide arguments', action='store_true') |
---|
[0ad0c55] | 100 | parser.add_argument('-I','--include', help='Directory of test to include, can be used multiple time, All if omitted', action='append') |
---|
| 101 | parser.add_argument('-E','--exclude', help='Directory of test to exclude, can be used multiple time, None if omitted', action='append') |
---|
[c07d724] | 102 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run') |
---|
| 103 | |
---|
[575a6e5] | 104 | try: |
---|
| 105 | options = parser.parse_args() |
---|
| 106 | except: |
---|
| 107 | print('ERROR: invalid arguments', file=sys.stderr) |
---|
| 108 | parser.print_help(sys.stderr) |
---|
[5b993e0] | 109 | sys.exit(1) |
---|
[c07d724] | 110 | |
---|
| 111 | # script must have at least some tests to run or be listing |
---|
| 112 | listing = options.list or options.list_comp |
---|
| 113 | all_tests = options.all |
---|
| 114 | some_tests = len(options.tests) > 0 |
---|
[0ad0c55] | 115 | some_dirs = len(options.include) > 0 if options.include else 0 |
---|
[c07d724] | 116 | |
---|
| 117 | # check that exactly one of the booleans is set to true |
---|
[0ad0c55] | 118 | if not sum( (listing, all_tests, some_tests, some_dirs) ) > 0 : |
---|
[5b993e0] | 119 | print('''ERROR: must have option '--all', '--list', '--include', '-I' or non-empty test list''', file=sys.stderr) |
---|
[c07d724] | 120 | parser.print_help() |
---|
| 121 | sys.exit(1) |
---|
| 122 | |
---|
| 123 | return options |
---|
| 124 | |
---|
[efc15918] | 125 | ################################################################################ |
---|
| 126 | # running test functions |
---|
| 127 | ################################################################################ |
---|
[0c13238] | 128 | def success(val): |
---|
| 129 | return val == 0 or settings.dry_run |
---|
[f85bc15] | 130 | |
---|
[5bf1f3e] | 131 | def no_rule(file, target): |
---|
| 132 | return not settings.dry_run and file_contains_only(file, "make: *** No rule to make target `%s'. Stop." % target) |
---|
[f85bc15] | 133 | |
---|
[c07d724] | 134 | # logic to run a single test and return the result (No handling of printing or other test framework logic) |
---|
[209383b] | 135 | def run_single_test(test): |
---|
[3c1d702] | 136 | |
---|
[c07d724] | 137 | # find the output file based on the test name and options flag |
---|
[f85bc15] | 138 | exe_file = test.target_executable(); |
---|
[bacc36c] | 139 | out_file = test.target_output() |
---|
| 140 | err_file = test.error_log() |
---|
| 141 | cmp_file = test.expect() |
---|
| 142 | in_file = test.input() |
---|
[0ad0c55] | 143 | |
---|
| 144 | # prepare the proper directories |
---|
[bacc36c] | 145 | test.prepare() |
---|
[efc15918] | 146 | |
---|
[c07d724] | 147 | # build, skipping to next test on error |
---|
[0c13238] | 148 | with Timed() as comp_dur: |
---|
[d65f92c] | 149 | make_ret, _ = make( test.target(), output_file=subprocess.DEVNULL, error=out_file, error_file = err_file ) |
---|
[efc15918] | 150 | |
---|
[0c13238] | 151 | run_dur = None |
---|
[f806b61] | 152 | # run everything in a temp directory to make sure core file are handled properly |
---|
| 153 | with tempdir(): |
---|
[103c292] | 154 | # if the make command succeeds continue otherwise skip to diff |
---|
[f806b61] | 155 | if success(make_ret): |
---|
| 156 | with Timed() as run_dur: |
---|
| 157 | if settings.dry_run or is_exe(exe_file): |
---|
| 158 | # run test |
---|
[d65f92c] | 159 | retcode, _ = sh(exe_file, output_file=out_file, input_file=in_file, timeout=True) |
---|
[f806b61] | 160 | else : |
---|
| 161 | # simply cat the result into the output |
---|
| 162 | retcode = cat(exe_file, out_file) |
---|
| 163 | else: |
---|
| 164 | retcode = mv(err_file, out_file) |
---|
| 165 | |
---|
| 166 | if success(retcode): |
---|
| 167 | if settings.generating : |
---|
| 168 | # if we are ounly generating the output we still need to check that the test actually exists |
---|
| 169 | if no_rule(out_file, test.target()) : |
---|
| 170 | retcode = 1 |
---|
| 171 | error = "\t\tNo make target for test %s!" % test.target() |
---|
| 172 | rm(out_file) |
---|
| 173 | else: |
---|
| 174 | error = None |
---|
[0c13238] | 175 | else : |
---|
[f806b61] | 176 | # fetch return code and error from the diff command |
---|
| 177 | retcode, error = diff(cmp_file, out_file) |
---|
| 178 | |
---|
| 179 | else: |
---|
| 180 | with open (out_file, "r") as myfile: |
---|
| 181 | error = myfile.read() |
---|
| 182 | |
---|
| 183 | ret, info = core_info(exe_file) |
---|
| 184 | error = error + info if error else info |
---|
[0c13238] | 185 | |
---|
[dcfedca] | 186 | if settings.archive: |
---|
| 187 | error = error + '\n' + core_archive(settings.archive, test.target(), exe_file) |
---|
| 188 | |
---|
[0c13238] | 189 | |
---|
[b5f9829] | 190 | |
---|
[c07d724] | 191 | # clean the executable |
---|
[0c13238] | 192 | rm(exe_file) |
---|
[efc15918] | 193 | |
---|
[0c13238] | 194 | return retcode, error, [comp_dur.duration, run_dur.duration if run_dur else None] |
---|
[efc15918] | 195 | |
---|
[c07d724] | 196 | # run a single test and handle the errors, outputs, printing, exception handling, etc. |
---|
[209383b] | 197 | def run_test_worker(t) : |
---|
[1bb2488] | 198 | try : |
---|
[bacc36c] | 199 | # print formated name |
---|
[5bf1f3e] | 200 | name_txt = '{0:{width}} '.format(t.target(), width=settings.output_width) |
---|
[ced2e989] | 201 | |
---|
[ca54499] | 202 | retcode, error, duration = run_single_test(t) |
---|
[0a1a680] | 203 | |
---|
[bacc36c] | 204 | # update output based on current action |
---|
[ca54499] | 205 | result_txt = TestResult.toString( retcode, duration ) |
---|
[bacc36c] | 206 | |
---|
| 207 | #print result with error if needed |
---|
[a45fc7b] | 208 | text = '\t' + name_txt + result_txt |
---|
[bacc36c] | 209 | out = sys.stdout |
---|
| 210 | if error : |
---|
[2b10f95] | 211 | text = text + '\n' + error |
---|
[bacc36c] | 212 | |
---|
[e791851] | 213 | return retcode == TestResult.SUCCESS, text |
---|
[1bb2488] | 214 | except KeyboardInterrupt: |
---|
[35a408b7] | 215 | return False, "" |
---|
[8364209] | 216 | except Exception as ex: |
---|
| 217 | print("Unexpected error in worker thread: %s" % ex, file=sys.stderr) |
---|
[35a408b7] | 218 | sys.stderr.flush() |
---|
| 219 | return False, "" |
---|
| 220 | |
---|
[ced2e989] | 221 | |
---|
[911348cd] | 222 | # run the given list of tests with the given parameters |
---|
[209383b] | 223 | def run_tests(tests, jobs) : |
---|
[911348cd] | 224 | # clean the sandbox from previous commands |
---|
[d65f92c] | 225 | make('clean', output_file=subprocess.DEVNULL, error=subprocess.DEVNULL) |
---|
[efc15918] | 226 | |
---|
[2cd949b] | 227 | # since python prints stacks by default on a interrupt, redo the interrupt handling to be silent |
---|
| 228 | def worker_init(): |
---|
| 229 | def sig_int(signal_num, frame): |
---|
| 230 | pass |
---|
| 231 | |
---|
| 232 | signal.signal(signal.SIGINT, sig_int) |
---|
| 233 | |
---|
[c07d724] | 234 | # create the executor for our jobs and handle the signal properly |
---|
[2cd949b] | 235 | pool = multiprocessing.Pool(jobs, worker_init) |
---|
[c07d724] | 236 | |
---|
[e791851] | 237 | failed = False |
---|
| 238 | |
---|
[2cd949b] | 239 | def stop(x, y): |
---|
| 240 | print("Tests interrupted by user", file=sys.stderr) |
---|
| 241 | sys.exit(1) |
---|
| 242 | signal.signal(signal.SIGINT, stop) |
---|
| 243 | |
---|
[c07d724] | 244 | # for each test to run |
---|
[ced2e989] | 245 | try : |
---|
[e791851] | 246 | num = len(tests) |
---|
| 247 | fancy = sys.stdout.isatty() |
---|
[35a408b7] | 248 | results = pool.imap_unordered( |
---|
[209383b] | 249 | run_test_worker, |
---|
[bacc36c] | 250 | tests, |
---|
| 251 | chunksize = 1 |
---|
[35a408b7] | 252 | ) |
---|
| 253 | |
---|
| 254 | for i, (succ, txt) in enumerate(timed(results, timeout = settings.timeout.total), 1) : |
---|
[e791851] | 255 | if not succ : |
---|
| 256 | failed = True |
---|
| 257 | |
---|
| 258 | print(" " + txt) |
---|
| 259 | |
---|
| 260 | if(fancy and i != num): |
---|
| 261 | print("%d/%d" % (i, num), end='\r') |
---|
| 262 | sys.stdout.flush() |
---|
| 263 | |
---|
[ced2e989] | 264 | except KeyboardInterrupt: |
---|
[e791851] | 265 | print("Tests interrupted by user", file=sys.stderr) |
---|
[35a408b7] | 266 | pool.terminate() |
---|
| 267 | pool.join() |
---|
[e791851] | 268 | failed = True |
---|
| 269 | except multiprocessing.TimeoutError: |
---|
| 270 | print("ERROR: Test suite timed out", file=sys.stderr) |
---|
[35a408b7] | 271 | pool.terminate() |
---|
| 272 | pool.join() |
---|
[e791851] | 273 | failed = True |
---|
[35a408b7] | 274 | killgroup() # needed to cleanly kill all children |
---|
| 275 | |
---|
[efc15918] | 276 | |
---|
[c07d724] | 277 | # clean the workspace |
---|
[d65f92c] | 278 | make('clean', output_file=subprocess.DEVNULL, error=subprocess.DEVNULL) |
---|
[efc15918] | 279 | |
---|
[e791851] | 280 | return 1 if failed else 0 |
---|
[efc15918] | 281 | |
---|
[6a1bdfd] | 282 | |
---|
[efc15918] | 283 | ################################################################################ |
---|
| 284 | # main loop |
---|
| 285 | ################################################################################ |
---|
[c07d724] | 286 | if __name__ == "__main__": |
---|
[f803a75] | 287 | |
---|
[c07d724] | 288 | # parse the command line arguments |
---|
[5bf1f3e] | 289 | options = parse_args() |
---|
[f1231f2] | 290 | |
---|
[bacc36c] | 291 | # init global settings |
---|
| 292 | settings.init( options ) |
---|
| 293 | |
---|
[c07d724] | 294 | # fetch the liest of all valid tests |
---|
[5bf1f3e] | 295 | all_tests = list_tests( options.include, options.exclude ) |
---|
[f1231f2] | 296 | |
---|
[0c13238] | 297 | |
---|
[c07d724] | 298 | # if user wants all tests than no other treatement of the test list is required |
---|
[0ad0c55] | 299 | if options.all or options.list or options.list_comp or options.include : |
---|
[5bf1f3e] | 300 | tests = all_tests |
---|
[0534c3c] | 301 | |
---|
[f3b9efc] | 302 | #otherwise we need to validate that the test list that was entered is valid |
---|
[c07d724] | 303 | else : |
---|
[5bf1f3e] | 304 | tests = valid_tests( options ) |
---|
[0534c3c] | 305 | |
---|
[dd226e3] | 306 | # make sure we have at least some test to run |
---|
| 307 | if not tests : |
---|
| 308 | print('ERROR: No valid test to run', file=sys.stderr) |
---|
| 309 | sys.exit(1) |
---|
| 310 | |
---|
| 311 | |
---|
[c07d724] | 312 | # sort the test alphabetically for convenience |
---|
[f3b9efc] | 313 | tests.sort(key=lambda t: (t.arch if t.arch else '') + t.target()) |
---|
[f1231f2] | 314 | |
---|
[c07d724] | 315 | # users may want to simply list the tests |
---|
| 316 | if options.list_comp : |
---|
[dcfedca] | 317 | print("-h --help --debug --dry-run --list --arch --all --regenerate-expected --archive-errors --install --timeout --global-timeout -j --jobs ", end='') |
---|
[0ad0c55] | 318 | print(" ".join(map(lambda t: "%s" % (t.target()), tests))) |
---|
[911348cd] | 319 | |
---|
[c07d724] | 320 | elif options.list : |
---|
[f3b9efc] | 321 | print("Listing for %s:%s"% (settings.arch.string, settings.debug.string)) |
---|
[5b993e0] | 322 | fancy_print("\n".join(map(lambda t: t.toString(), tests))) |
---|
[911348cd] | 323 | |
---|
[b98c913] | 324 | else : |
---|
[28582b2] | 325 | # check the build configuration works |
---|
[d3c1c6a] | 326 | settings.prep_output(tests) |
---|
[28582b2] | 327 | settings.validate() |
---|
| 328 | |
---|
[5bf1f3e] | 329 | options.jobs, forceJobs = job_count( options, tests ) |
---|
| 330 | settings.update_make_cmd(forceJobs, options.jobs) |
---|
[b98c913] | 331 | |
---|
[2b10f95] | 332 | print('%s %i tests on %i cores (%s:%s)' % ( |
---|
| 333 | 'Regenerating' if settings.generating else 'Running', |
---|
| 334 | len(tests), |
---|
| 335 | options.jobs, |
---|
[f3b9efc] | 336 | settings.arch.string, |
---|
[2b10f95] | 337 | settings.debug.string |
---|
[bacc36c] | 338 | )) |
---|
[efc15918] | 339 | |
---|
[c07d724] | 340 | # otherwise run all tests and make sure to return the correct error code |
---|
[209383b] | 341 | sys.exit( run_tests(tests, options.jobs) ) |
---|