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