[945047e] | 1 | #!/usr/bin/python |
---|
[efc15918] | 2 | from __future__ import print_function |
---|
| 3 | |
---|
[c07d724] | 4 | from pybin.tools import * |
---|
[0ad0c55] | 5 | from pybin.test_run import * |
---|
[bacc36c] | 6 | from pybin import settings |
---|
[efc15918] | 7 | |
---|
| 8 | import argparse |
---|
[122cac7] | 9 | import re |
---|
[efc15918] | 10 | import sys |
---|
[ca54499] | 11 | import time |
---|
[efc15918] | 12 | |
---|
| 13 | ################################################################################ |
---|
| 14 | # help functions |
---|
| 15 | ################################################################################ |
---|
[f1231f2] | 16 | |
---|
[209383b] | 17 | def findTests(): |
---|
[0ad0c55] | 18 | expected = [] |
---|
[f1231f2] | 19 | |
---|
[f3b9efc] | 20 | def matchTest(path): |
---|
[f85bc15] | 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 | |
---|
[f3b9efc] | 30 | pathWalk( matchTest ) |
---|
[c07d724] | 31 | |
---|
[0ad0c55] | 32 | return expected |
---|
[efc15918] | 33 | |
---|
[be65cca] | 34 | # reads the directory ./.expect and indentifies the tests |
---|
[0ad0c55] | 35 | def listTests( includes, excludes ): |
---|
[bacc36c] | 36 | includes = [canonicalPath( i ) for i in includes] if includes else None |
---|
| 37 | excludes = [canonicalPath( i ) for i in excludes] if excludes else None |
---|
[be65cca] | 38 | |
---|
| 39 | # tests directly in the .expect folder will always be processed |
---|
[209383b] | 40 | test_list = findTests() |
---|
[be65cca] | 41 | |
---|
[0ad0c55] | 42 | # if we have a limited number of includes, filter by them |
---|
| 43 | if includes: |
---|
| 44 | test_list = [x for x in test_list if |
---|
[a85e44c] | 45 | x.target().startswith( tuple(includes) ) |
---|
[0ad0c55] | 46 | ] |
---|
[be65cca] | 47 | |
---|
[0ad0c55] | 48 | # # if we have a folders to excludes, filter by them |
---|
| 49 | if excludes: |
---|
| 50 | test_list = [x for x in test_list if not |
---|
[a85e44c] | 51 | x.target().startswith( tuple(excludes) ) |
---|
[0ad0c55] | 52 | ] |
---|
[f1231f2] | 53 | |
---|
[0ad0c55] | 54 | return test_list |
---|
[efc15918] | 55 | |
---|
[c07d724] | 56 | # from the found tests, filter all the valid tests/desired tests |
---|
| 57 | def validTests( options ): |
---|
| 58 | tests = [] |
---|
| 59 | |
---|
| 60 | # if we are regenerating the tests we need to find the information of the |
---|
| 61 | # already existing tests and create new info for the new tests |
---|
| 62 | if options.regenerate_expected : |
---|
| 63 | for testname in options.tests : |
---|
[16a63a78] | 64 | testname = canonicalPath( testname ) |
---|
[bacc36c] | 65 | if Test.valid_name(testname): |
---|
[a95c117] | 66 | found = [test for test in allTests if canonicalPath( test.target() ) == testname] |
---|
[bacc36c] | 67 | tests.append( found[0] if len(found) == 1 else Test.from_target(testname) ) |
---|
[c07d724] | 68 | else : |
---|
[bacc36c] | 69 | print('ERROR: "%s", tests are not allowed to end with a C/C++/CFA extension, ignoring it' % testname, file=sys.stderr) |
---|
[c07d724] | 70 | |
---|
| 71 | else : |
---|
| 72 | # otherwise we only need to validate that all tests are present in the complete list |
---|
| 73 | for testname in options.tests: |
---|
[bacc36c] | 74 | test = [t for t in allTests if pathCmp( t.target(), testname )] |
---|
[c07d724] | 75 | |
---|
[bacc36c] | 76 | if test : |
---|
[c07d724] | 77 | tests.append( test[0] ) |
---|
| 78 | else : |
---|
| 79 | print('ERROR: No expected file for test %s, ignoring it' % testname, file=sys.stderr) |
---|
| 80 | |
---|
| 81 | return tests |
---|
| 82 | |
---|
| 83 | # parses the option |
---|
| 84 | def getOptions(): |
---|
| 85 | # create a parser with the arguments for the tests script |
---|
| 86 | parser = argparse.ArgumentParser(description='Script which runs cforall tests') |
---|
[28582b2] | 87 | parser.add_argument('--debug', help='Run all tests in debug or release', type=yes_no, default='yes') |
---|
[a5121bf] | 88 | parser.add_argument('--install', help='Run all tests based on installed binaries or tree binaries', type=yes_no, default='no') |
---|
[bacc36c] | 89 | parser.add_argument('--arch', help='Test for specific architecture', type=str, default='') |
---|
[afe8882] | 90 | parser.add_argument('--timeout', help='Maximum duration in seconds after a single test is considered to have timed out', type=int, default=60) |
---|
| 91 | 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] | 92 | parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true') |
---|
| 93 | parser.add_argument('--list', help='List all test available', action='store_true') |
---|
| 94 | parser.add_argument('--all', help='Run all test available', action='store_true') |
---|
| 95 | parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true') |
---|
[d142ec5] | 96 | parser.add_argument('-j', '--jobs', help='Number of tests to run simultaneously', type=int) |
---|
[c07d724] | 97 | parser.add_argument('--list-comp', help='List all valide arguments', action='store_true') |
---|
[0ad0c55] | 98 | parser.add_argument('-I','--include', help='Directory of test to include, can be used multiple time, All if omitted', action='append') |
---|
| 99 | parser.add_argument('-E','--exclude', help='Directory of test to exclude, can be used multiple time, None if omitted', action='append') |
---|
[c07d724] | 100 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run') |
---|
| 101 | |
---|
[575a6e5] | 102 | try: |
---|
| 103 | options = parser.parse_args() |
---|
| 104 | except: |
---|
| 105 | print('ERROR: invalid arguments', file=sys.stderr) |
---|
| 106 | parser.print_help(sys.stderr) |
---|
| 107 | sys.exit(1) |
---|
[c07d724] | 108 | |
---|
| 109 | # script must have at least some tests to run or be listing |
---|
| 110 | listing = options.list or options.list_comp |
---|
| 111 | all_tests = options.all |
---|
| 112 | some_tests = len(options.tests) > 0 |
---|
[0ad0c55] | 113 | some_dirs = len(options.include) > 0 if options.include else 0 |
---|
[c07d724] | 114 | |
---|
| 115 | # check that exactly one of the booleans is set to true |
---|
[0ad0c55] | 116 | if not sum( (listing, all_tests, some_tests, some_dirs) ) > 0 : |
---|
| 117 | print('ERROR: must have option \'--all\', \'--list\', \'--include\', \'-I\' or non-empty test list', file=sys.stderr) |
---|
[c07d724] | 118 | parser.print_help() |
---|
| 119 | sys.exit(1) |
---|
| 120 | |
---|
| 121 | return options |
---|
| 122 | |
---|
[efc15918] | 123 | ################################################################################ |
---|
| 124 | # running test functions |
---|
| 125 | ################################################################################ |
---|
[f85bc15] | 126 | # fix the absolute paths in the output |
---|
| 127 | def fixoutput( fname ): |
---|
| 128 | if not is_ascii(fname): |
---|
| 129 | return |
---|
| 130 | |
---|
| 131 | file_replace(fname, "%s/" % settings.SRCDIR, "") |
---|
| 132 | |
---|
| 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 |
---|
[ca54499] | 148 | before = time.time() |
---|
[bacc36c] | 149 | make_ret, _ = make( test.target(), |
---|
| 150 | redirects = "2> %s 1> /dev/null" % out_file, |
---|
| 151 | error_file = err_file |
---|
| 152 | ) |
---|
[ca54499] | 153 | after = time.time() |
---|
| 154 | |
---|
| 155 | comp_dur = after - before |
---|
| 156 | |
---|
| 157 | run_dur = None |
---|
[efc15918] | 158 | |
---|
[c07d724] | 159 | # if the make command succeds continue otherwise skip to diff |
---|
[bacc36c] | 160 | if make_ret == 0 or settings.dry_run: |
---|
[ca54499] | 161 | before = time.time() |
---|
[f85bc15] | 162 | if settings.dry_run or fileIsExecutable(exe_file) : |
---|
[c07d724] | 163 | # run test |
---|
[afe8882] | 164 | retcode, _ = sh("timeout %d %s > %s 2>&1" % (settings.timeout.single, exe_file, out_file), input = in_file) |
---|
[9fcb5e4] | 165 | else : |
---|
[c07d724] | 166 | # simply cat the result into the output |
---|
[f85bc15] | 167 | retcode, _ = sh("cat %s > %s" % (exe_file, out_file)) |
---|
[ca54499] | 168 | |
---|
| 169 | after = time.time() |
---|
| 170 | run_dur = after - before |
---|
[0ad0c55] | 171 | else: |
---|
[f3b9efc] | 172 | retcode, _ = sh("mv %s %s" % (err_file, out_file)) |
---|
[122cac7] | 173 | |
---|
[0ad0c55] | 174 | |
---|
[c2d5e28] | 175 | if retcode == 0: |
---|
[f7d3215] | 176 | # fixoutput(out_file) |
---|
[bacc36c] | 177 | if settings.generating : |
---|
[c2d5e28] | 178 | # if we are ounly generating the output we still need to check that the test actually exists |
---|
[bacc36c] | 179 | if not settings.dry_run and fileContainsOnly(out_file, "make: *** No rule to make target `%s'. Stop." % test.target()) : |
---|
[c2d5e28] | 180 | retcode = 1; |
---|
[0ad0c55] | 181 | error = "\t\tNo make target for test %s!" % test.target() |
---|
[c2d5e28] | 182 | sh("rm %s" % out_file, False) |
---|
[f3b9efc] | 183 | else: |
---|
| 184 | error = None |
---|
[c2d5e28] | 185 | else : |
---|
| 186 | # fetch return code and error from the diff command |
---|
[bacc36c] | 187 | retcode, error = diff(cmp_file, out_file) |
---|
[ac032b5] | 188 | |
---|
| 189 | else: |
---|
| 190 | with open (out_file, "r") as myfile: |
---|
| 191 | error = myfile.read() |
---|
| 192 | |
---|
[b5f9829] | 193 | |
---|
[c07d724] | 194 | # clean the executable |
---|
[bacc36c] | 195 | sh("rm -f %s > /dev/null 2>&1" % test.target()) |
---|
[efc15918] | 196 | |
---|
[ca54499] | 197 | return retcode, error, [comp_dur, run_dur] |
---|
[efc15918] | 198 | |
---|
[c07d724] | 199 | # run a single test and handle the errors, outputs, printing, exception handling, etc. |
---|
[209383b] | 200 | def run_test_worker(t) : |
---|
[ced2e989] | 201 | |
---|
[bacc36c] | 202 | with SignalHandling(): |
---|
| 203 | # print formated name |
---|
| 204 | name_txt = "%20s " % t.name |
---|
[ced2e989] | 205 | |
---|
[ca54499] | 206 | retcode, error, duration = run_single_test(t) |
---|
[0a1a680] | 207 | |
---|
[bacc36c] | 208 | # update output based on current action |
---|
[ca54499] | 209 | result_txt = TestResult.toString( retcode, duration ) |
---|
[bacc36c] | 210 | |
---|
| 211 | #print result with error if needed |
---|
| 212 | text = name_txt + result_txt |
---|
| 213 | out = sys.stdout |
---|
| 214 | if error : |
---|
| 215 | text = text + "\n" + error |
---|
| 216 | out = sys.stderr |
---|
| 217 | |
---|
| 218 | print(text, file = out) |
---|
| 219 | sys.stdout.flush() |
---|
| 220 | sys.stderr.flush() |
---|
[9fcb5e4] | 221 | |
---|
[c2d5e28] | 222 | return retcode != TestResult.SUCCESS |
---|
[ced2e989] | 223 | |
---|
[911348cd] | 224 | # run the given list of tests with the given parameters |
---|
[209383b] | 225 | def run_tests(tests, jobs) : |
---|
[911348cd] | 226 | # clean the sandbox from previous commands |
---|
[bacc36c] | 227 | make('clean', redirects = '> /dev/null 2>&1') |
---|
[efc15918] | 228 | |
---|
[c07d724] | 229 | # create the executor for our jobs and handle the signal properly |
---|
[bacc36c] | 230 | pool = setupPool(jobs) |
---|
[c07d724] | 231 | |
---|
| 232 | # for each test to run |
---|
[ced2e989] | 233 | try : |
---|
[bacc36c] | 234 | results = pool.map_async( |
---|
[209383b] | 235 | run_test_worker, |
---|
[bacc36c] | 236 | tests, |
---|
| 237 | chunksize = 1 |
---|
[afe8882] | 238 | ).get(settings.timeout.total) |
---|
[ced2e989] | 239 | except KeyboardInterrupt: |
---|
| 240 | pool.terminate() |
---|
| 241 | print("Tests interrupted by user") |
---|
| 242 | sys.exit(1) |
---|
[efc15918] | 243 | |
---|
[c07d724] | 244 | # clean the workspace |
---|
[bacc36c] | 245 | make('clean', redirects = '> /dev/null 2>&1') |
---|
[efc15918] | 246 | |
---|
[ced2e989] | 247 | for failed in results: |
---|
| 248 | if failed : |
---|
| 249 | return 1 |
---|
| 250 | |
---|
| 251 | return 0 |
---|
[efc15918] | 252 | |
---|
[6a1bdfd] | 253 | |
---|
[efc15918] | 254 | ################################################################################ |
---|
| 255 | # main loop |
---|
| 256 | ################################################################################ |
---|
[c07d724] | 257 | if __name__ == "__main__": |
---|
[f803a75] | 258 | |
---|
[c07d724] | 259 | # parse the command line arguments |
---|
| 260 | options = getOptions() |
---|
[f1231f2] | 261 | |
---|
[bacc36c] | 262 | # init global settings |
---|
| 263 | settings.init( options ) |
---|
| 264 | |
---|
[c07d724] | 265 | # fetch the liest of all valid tests |
---|
[0ad0c55] | 266 | allTests = listTests( options.include, options.exclude ) |
---|
[f1231f2] | 267 | |
---|
[c07d724] | 268 | # if user wants all tests than no other treatement of the test list is required |
---|
[0ad0c55] | 269 | if options.all or options.list or options.list_comp or options.include : |
---|
[c07d724] | 270 | tests = allTests |
---|
[0534c3c] | 271 | |
---|
[f3b9efc] | 272 | #otherwise we need to validate that the test list that was entered is valid |
---|
[c07d724] | 273 | else : |
---|
| 274 | tests = validTests( options ) |
---|
[0534c3c] | 275 | |
---|
[dd226e3] | 276 | # make sure we have at least some test to run |
---|
| 277 | if not tests : |
---|
| 278 | print('ERROR: No valid test to run', file=sys.stderr) |
---|
| 279 | sys.exit(1) |
---|
| 280 | |
---|
| 281 | |
---|
[c07d724] | 282 | # sort the test alphabetically for convenience |
---|
[f3b9efc] | 283 | tests.sort(key=lambda t: (t.arch if t.arch else '') + t.target()) |
---|
[f1231f2] | 284 | |
---|
[c07d724] | 285 | # users may want to simply list the tests |
---|
| 286 | if options.list_comp : |
---|
[afe8882] | 287 | print("-h --help --debug --dry-run --list --arch --all --regenerate-expected --install --timeout --global-timeout -j --jobs ", end='') |
---|
[0ad0c55] | 288 | print(" ".join(map(lambda t: "%s" % (t.target()), tests))) |
---|
[911348cd] | 289 | |
---|
[c07d724] | 290 | elif options.list : |
---|
[f3b9efc] | 291 | print("Listing for %s:%s"% (settings.arch.string, settings.debug.string)) |
---|
[ed45af6] | 292 | fancy_print("\n".join(map(lambda t: "%s" % (t.toString()), tests))) |
---|
[911348cd] | 293 | |
---|
[b98c913] | 294 | else : |
---|
[28582b2] | 295 | # check the build configuration works |
---|
| 296 | settings.validate() |
---|
| 297 | |
---|
[bacc36c] | 298 | options.jobs, forceJobs = jobCount( options, tests ) |
---|
| 299 | settings.updateMakeCmd(forceJobs, options.jobs) |
---|
[b98c913] | 300 | |
---|
[bacc36c] | 301 | print('%s (%s:%s) on %i cores' % ( |
---|
| 302 | 'Regenerate tests' if settings.generating else 'Running', |
---|
[f3b9efc] | 303 | settings.arch.string, |
---|
| 304 | settings.debug.string, |
---|
[bacc36c] | 305 | options.jobs |
---|
| 306 | )) |
---|
[efc15918] | 307 | |
---|
[c07d724] | 308 | # otherwise run all tests and make sure to return the correct error code |
---|
[209383b] | 309 | sys.exit( run_tests(tests, options.jobs) ) |
---|