1 | #!/usr/bin/python3 |
---|
2 | |
---|
3 | from pybin.tools import * |
---|
4 | from pybin.test_run import * |
---|
5 | from pybin import settings |
---|
6 | |
---|
7 | import argparse |
---|
8 | import itertools |
---|
9 | import re |
---|
10 | import sys |
---|
11 | import tempfile |
---|
12 | import time |
---|
13 | |
---|
14 | import os |
---|
15 | import signal |
---|
16 | |
---|
17 | ################################################################################ |
---|
18 | # help functions |
---|
19 | ################################################################################ |
---|
20 | |
---|
21 | def find_tests(): |
---|
22 | expected = [] |
---|
23 | |
---|
24 | def match_test(path): |
---|
25 | match = re.search("^%s\/([\w\/\-_]*).expect\/([\w\-_]+)(\.nast|\.oast)?(\.[\w\-_]+)?\.txt$" % settings.SRCDIR, path) |
---|
26 | if match : |
---|
27 | test = Test() |
---|
28 | test.name = match.group(2) |
---|
29 | test.path = match.group(1) |
---|
30 | test.arch = match.group(4)[1:] if match.group(4) else None |
---|
31 | |
---|
32 | astv = match.group(3)[1:] if match.group(3) else None |
---|
33 | if astv == 'oast': |
---|
34 | test.astv = 'old' |
---|
35 | elif astv == 'nast': |
---|
36 | test.astv = 'new' |
---|
37 | elif astv: |
---|
38 | print('ERROR: "%s", expect file has astv but it is not "nast" or "oast"' % testname, file=sys.stderr) |
---|
39 | sys.exit(1) |
---|
40 | |
---|
41 | expected.append(test) |
---|
42 | |
---|
43 | path_walk( match_test ) |
---|
44 | |
---|
45 | return expected |
---|
46 | |
---|
47 | # reads the directory ./.expect and indentifies the tests |
---|
48 | def list_tests( includes, excludes ): |
---|
49 | # tests directly in the .expect folder will always be processed |
---|
50 | test_list = find_tests() |
---|
51 | |
---|
52 | # if we have a limited number of includes, filter by them |
---|
53 | if includes: |
---|
54 | test_list = [x for x in test_list if |
---|
55 | x.target().startswith( tuple(includes) ) |
---|
56 | ] |
---|
57 | |
---|
58 | # # if we have a folders to excludes, filter by them |
---|
59 | if excludes: |
---|
60 | test_list = [x for x in test_list if not |
---|
61 | x.target().startswith( tuple(excludes) ) |
---|
62 | ] |
---|
63 | |
---|
64 | # sort the test alphabetically for convenience |
---|
65 | test_list.sort(key=lambda t: ('~' if t.arch else '') + t.target() + (t.arch if t.arch else '')) |
---|
66 | |
---|
67 | return test_list |
---|
68 | |
---|
69 | # from the found tests, filter all the valid tests/desired tests |
---|
70 | def valid_tests( options ): |
---|
71 | tests = [] |
---|
72 | |
---|
73 | # if we are regenerating the tests we need to find the information of the |
---|
74 | # already existing tests and create new info for the new tests |
---|
75 | if options.regenerate_expected : |
---|
76 | for testname in options.tests : |
---|
77 | testname = os.path.normpath( os.path.join(settings.SRCDIR, testname) ) |
---|
78 | |
---|
79 | # first check if this is a valid name to regenerate |
---|
80 | if Test.valid_name(testname): |
---|
81 | # this is a valid name, let's check if it already exists |
---|
82 | found = [test for test in all_tests if canonical_path( test.target() ) == testname] |
---|
83 | setup = itertools.product(settings.all_arch if options.arch else [None], settings.all_ast if options.ast else [None]) |
---|
84 | if not found: |
---|
85 | # it's a new name, create it according to the name and specified architecture/ast version |
---|
86 | tests.extend( [Test.new_target(testname, arch, ast) for arch, ast in setup] ) |
---|
87 | elif len(found) == 1 and not found[0].arch: |
---|
88 | # we found a single test, the user better be wanting to create a cross platform test |
---|
89 | if options.arch: |
---|
90 | print('ERROR: "%s", test has no specified architecture but --arch was specified, ignoring it' % testname, file=sys.stderr) |
---|
91 | elif options.ast: |
---|
92 | print('ERROR: "%s", test has no specified ast version but --ast was specified, ignoring it' % testname, file=sys.stderr) |
---|
93 | else: |
---|
94 | tests.append( found[0] ) |
---|
95 | else: |
---|
96 | # this test is already cross platform, just add a test for each platform the user asked |
---|
97 | tests.extend( [Test.new_target(testname, arch, ast) for arch, ast in setup] ) |
---|
98 | |
---|
99 | # print a warning if it users didn't ask for a specific architecture |
---|
100 | found_arch = [f.arch for f in found if f.arch] |
---|
101 | if found_arch and not options.arch: |
---|
102 | print('WARNING: "%s", test has architecture specific expected files but --arch was not specified, regenerating only for current host' % testname, file=sys.stderr) |
---|
103 | |
---|
104 | |
---|
105 | # print a warning if it users didn't ask for a specific ast version |
---|
106 | found_astv = [f.astv for f in found if f.astv] |
---|
107 | if found_astv and not options.ast: |
---|
108 | print('WARNING: "%s", test has ast version specific expected files but --ast was not specified, regenerating only for current ast' % testname, file=sys.stderr) |
---|
109 | |
---|
110 | else : |
---|
111 | print('ERROR: "%s", tests are not allowed to end with a C/C++/CFA extension, ignoring it' % testname, file=sys.stderr) |
---|
112 | |
---|
113 | else : |
---|
114 | # otherwise we only need to validate that all tests are present in the complete list |
---|
115 | for testname in options.tests: |
---|
116 | test = [t for t in all_tests if path_cmp( t.target(), testname )] |
---|
117 | |
---|
118 | if test : |
---|
119 | tests.extend( test ) |
---|
120 | else : |
---|
121 | print('ERROR: No expected file for test %s, ignoring it' % testname, file=sys.stderr) |
---|
122 | |
---|
123 | return tests |
---|
124 | |
---|
125 | # parses the option |
---|
126 | def parse_args(): |
---|
127 | # create a parser with the arguments for the tests script |
---|
128 | parser = argparse.ArgumentParser(description='Script which runs cforall tests') |
---|
129 | parser.add_argument('--ast', help='Test for specific ast', type=comma_separated(str), default=None) |
---|
130 | parser.add_argument('--arch', help='Test for specific architecture', type=comma_separated(str), default=None) |
---|
131 | parser.add_argument('--debug', help='Run all tests in debug or release', type=comma_separated(yes_no), default='yes') |
---|
132 | parser.add_argument('--install', help='Run all tests based on installed binaries or tree binaries', type=comma_separated(yes_no), default='no') |
---|
133 | parser.add_argument('--continue', help='When multiple specifications are passed (debug/install/arch), sets whether or not to continue if the last specification failed', type=yes_no, default='yes', dest='continue_') |
---|
134 | parser.add_argument('--timeout', help='Maximum duration in seconds after a single test is considered to have timed out', type=int, default=180) |
---|
135 | 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) |
---|
136 | parser.add_argument('--timeout-with-gdb', help='Instead of killing the command when it times out, orphan it and print process id to allow gdb to attach', type=yes_no, default="no") |
---|
137 | parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true') |
---|
138 | parser.add_argument('--list', help='List all test available', action='store_true') |
---|
139 | parser.add_argument('--all', help='Run all test available', action='store_true') |
---|
140 | parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true') |
---|
141 | 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='') |
---|
142 | parser.add_argument('-j', '--jobs', help='Number of tests to run simultaneously, 0 (default) for unlimited', nargs='?', const=0, type=int) |
---|
143 | parser.add_argument('--list-comp', help='List all valide arguments', action='store_true') |
---|
144 | parser.add_argument('--list-dist', help='List all tests for distribution', action='store_true') |
---|
145 | parser.add_argument('-I','--include', help='Directory of test to include, can be used multiple time, All if omitted', action='append') |
---|
146 | parser.add_argument('-E','--exclude', help='Directory of test to exclude, can be used multiple time, None if omitted', action='append') |
---|
147 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run') |
---|
148 | |
---|
149 | try: |
---|
150 | options = parser.parse_args() |
---|
151 | except: |
---|
152 | print('ERROR: invalid arguments', file=sys.stderr) |
---|
153 | parser.print_help(sys.stderr) |
---|
154 | sys.exit(1) |
---|
155 | |
---|
156 | # script must have at least some tests to run or be listing |
---|
157 | listing = options.list or options.list_comp or options.list_dist |
---|
158 | all_tests = options.all |
---|
159 | some_tests = len(options.tests) > 0 |
---|
160 | some_dirs = len(options.include) > 0 if options.include else 0 |
---|
161 | |
---|
162 | # check that exactly one of the booleans is set to true |
---|
163 | if not sum( (listing, all_tests, some_tests, some_dirs) ) > 0 : |
---|
164 | print('''ERROR: must have option '--all', '--list', '--include', '-I' or non-empty test list''', file=sys.stderr) |
---|
165 | parser.print_help() |
---|
166 | sys.exit(1) |
---|
167 | |
---|
168 | return options |
---|
169 | |
---|
170 | ################################################################################ |
---|
171 | # running test functions |
---|
172 | ################################################################################ |
---|
173 | def success(val): |
---|
174 | return val == 0 or settings.dry_run |
---|
175 | |
---|
176 | def no_rule(file, target): |
---|
177 | return not settings.dry_run and file_contains_only(file, "make: *** No rule to make target `%s'. Stop." % target) |
---|
178 | |
---|
179 | # logic to run a single test and return the result (No handling of printing or other test framework logic) |
---|
180 | def run_single_test(test): |
---|
181 | |
---|
182 | # find the output file based on the test name and options flag |
---|
183 | exe_file = test.target_executable(); |
---|
184 | out_file = test.target_output() |
---|
185 | err_file = test.error_log() |
---|
186 | cmp_file = test.expect() |
---|
187 | in_file = test.input() |
---|
188 | |
---|
189 | # prepare the proper directories |
---|
190 | test.prepare() |
---|
191 | |
---|
192 | # ---------- |
---|
193 | # MAKE |
---|
194 | # ---------- |
---|
195 | # build, skipping to next test on error |
---|
196 | with Timed() as comp_dur: |
---|
197 | make_ret, _, _ = make( test.target(), output_file=subprocess.DEVNULL, error=out_file, error_file = err_file ) |
---|
198 | |
---|
199 | # ---------- |
---|
200 | # RUN |
---|
201 | # ---------- |
---|
202 | # run everything in a temp directory to make sure core file are handled properly |
---|
203 | run_dur = None |
---|
204 | with tempdir(): |
---|
205 | # if the make command succeeds continue otherwise skip to diff |
---|
206 | if success(make_ret): |
---|
207 | with Timed() as run_dur: |
---|
208 | if settings.dry_run or is_exe(exe_file): |
---|
209 | # run test |
---|
210 | retcode, _, _ = sh(exe_file, output_file=out_file, input_file=in_file, timeout=True) |
---|
211 | else : |
---|
212 | # simply cat the result into the output |
---|
213 | retcode = cat(exe_file, out_file) |
---|
214 | else: |
---|
215 | retcode = mv(err_file, out_file) |
---|
216 | |
---|
217 | if success(retcode): |
---|
218 | if settings.generating : |
---|
219 | # if we are only generating the output we still need to check that the test actually exists |
---|
220 | if no_rule(out_file, test.target()) : |
---|
221 | retcode = 1 |
---|
222 | error = "\t\tNo make target for test %s!" % test.target() |
---|
223 | rm(out_file) |
---|
224 | else: |
---|
225 | error = None |
---|
226 | else : |
---|
227 | # fetch return code and error from the diff command |
---|
228 | retcode, error, _ = diff(cmp_file, out_file) |
---|
229 | |
---|
230 | else: |
---|
231 | if os.stat(out_file).st_size < 1048576: |
---|
232 | with open (out_file, "r", encoding='latin-1') as myfile: # use latin-1 so all chars mean something. |
---|
233 | error = myfile.read() |
---|
234 | else: |
---|
235 | error = "Output log can't be read, file is bigger than 1MB, see {} for actual error\n".format(out_file) |
---|
236 | |
---|
237 | ret, info = core_info(exe_file) |
---|
238 | error = error + info if error else info |
---|
239 | |
---|
240 | if settings.archive: |
---|
241 | error = error + '\n' + core_archive(settings.archive, test.target(), exe_file) |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | # clean the executable |
---|
246 | rm(exe_file) |
---|
247 | |
---|
248 | return retcode, error, [comp_dur.duration, run_dur.duration if run_dur else None] |
---|
249 | |
---|
250 | # run a single test and handle the errors, outputs, printing, exception handling, etc. |
---|
251 | def run_test_worker(t) : |
---|
252 | try : |
---|
253 | # print formated name |
---|
254 | name_txt = t.format_target(width=settings.output_width) + ' ' |
---|
255 | |
---|
256 | retcode, error, duration = run_single_test(t) |
---|
257 | |
---|
258 | # update output based on current action |
---|
259 | result_key, result_txt = TestResult.toString( retcode, duration ) |
---|
260 | |
---|
261 | #print result with error if needed |
---|
262 | text = '\t' + name_txt + result_txt |
---|
263 | out = sys.stdout |
---|
264 | if error : |
---|
265 | text = text + '\n' + error |
---|
266 | |
---|
267 | return retcode == TestResult.SUCCESS, result_key, text |
---|
268 | except KeyboardInterrupt: |
---|
269 | return False, 'keybrd', "" |
---|
270 | # except Exception as ex: |
---|
271 | # print("Unexpected error in worker thread running {}: {}".format(t.target(), ex), file=sys.stderr) |
---|
272 | # sys.stderr.flush() |
---|
273 | # return False, "" |
---|
274 | |
---|
275 | |
---|
276 | # run the given list of tests with the given parameters |
---|
277 | def run_tests(tests, jobs) : |
---|
278 | # clean the sandbox from previous commands |
---|
279 | make('clean', output_file=subprocess.DEVNULL, error=subprocess.DEVNULL) |
---|
280 | |
---|
281 | # create the executor for our jobs |
---|
282 | pool = multiprocessing.Pool(jobs) |
---|
283 | |
---|
284 | failed = False |
---|
285 | rescnts = { 'pass': 0, 'fail': 0, 'time': 0, 'keybrd': 0 } |
---|
286 | other = 0 |
---|
287 | |
---|
288 | # for each test to run |
---|
289 | try : |
---|
290 | num = len(tests) |
---|
291 | fancy = sys.stdout.isatty() |
---|
292 | results = pool.imap_unordered( |
---|
293 | run_test_worker, |
---|
294 | tests, |
---|
295 | chunksize = 1 |
---|
296 | ) |
---|
297 | |
---|
298 | for i, (succ, code, txt) in enumerate(timed(results, timeout = settings.timeout.total), 1) : |
---|
299 | if code in rescnts.keys(): |
---|
300 | rescnts[code] += 1 |
---|
301 | else: |
---|
302 | other += 1 |
---|
303 | |
---|
304 | if not succ : |
---|
305 | failed = True |
---|
306 | |
---|
307 | print(" " + txt) |
---|
308 | |
---|
309 | if(fancy and i != num): |
---|
310 | print("%d/%d" % (i, num), end='\r') |
---|
311 | sys.stdout.flush() |
---|
312 | |
---|
313 | except KeyboardInterrupt: |
---|
314 | print("Tests interrupted by user", file=sys.stderr) |
---|
315 | pool.terminate() |
---|
316 | pool.join() |
---|
317 | failed = True |
---|
318 | except multiprocessing.TimeoutError: |
---|
319 | print("ERROR: Test suite timed out", file=sys.stderr) |
---|
320 | pool.terminate() |
---|
321 | pool.join() |
---|
322 | failed = True |
---|
323 | killgroup() # needed to cleanly kill all children |
---|
324 | |
---|
325 | |
---|
326 | # clean the workspace |
---|
327 | make('clean', output_file=subprocess.DEVNULL, error=subprocess.DEVNULL) |
---|
328 | |
---|
329 | print("{} passes, {} failures, {} timeouts, {} cancelled, {} other".format(rescnts['pass'], rescnts['fail'], rescnts['time'], rescnts['keybrd'], other)) |
---|
330 | |
---|
331 | return failed |
---|
332 | |
---|
333 | |
---|
334 | ################################################################################ |
---|
335 | # main loop |
---|
336 | ################################################################################ |
---|
337 | if __name__ == "__main__": |
---|
338 | |
---|
339 | # parse the command line arguments |
---|
340 | options = parse_args() |
---|
341 | |
---|
342 | # init global settings |
---|
343 | settings.init( options ) |
---|
344 | |
---|
345 | # -------------------------------------------------- |
---|
346 | # list all the test for auto completion programs |
---|
347 | # not pretty, single line, with the command line options |
---|
348 | if options.list_comp : |
---|
349 | # fetch the liest of all valid tests |
---|
350 | tests = list_tests( None, None ) |
---|
351 | |
---|
352 | # print the possible options |
---|
353 | print("-h --help --debug --dry-run --list --arch --all --regenerate-expected --archive-errors --install --timeout --global-timeout --timeout-with-gdb -j --jobs -I --include -E --exclude --continue ", end='') |
---|
354 | print(" ".join(map(lambda t: "%s" % (t.target()), tests))) |
---|
355 | |
---|
356 | # done |
---|
357 | sys.exit(0) |
---|
358 | |
---|
359 | # -------------------------------------------------- |
---|
360 | # list all the test for auto completion programs |
---|
361 | if options.list_dist : |
---|
362 | # fetch the liest of all valid tests |
---|
363 | tests = list_tests( None, None ) |
---|
364 | |
---|
365 | for t in tests: |
---|
366 | print(os.path.relpath(t.expect(), settings.SRCDIR), end=' ') |
---|
367 | print(os.path.relpath(t.input() , settings.SRCDIR), end=' ') |
---|
368 | code, out, err = make_recon(t.target()) |
---|
369 | |
---|
370 | if code != 0: |
---|
371 | print('ERROR: recond failed for test {}: {} \'{}\''.format(t.target(), code, err), file=sys.stderr) |
---|
372 | sys.exit(1) |
---|
373 | |
---|
374 | print(' '.join(re.findall('([^\s]+\.cfa)', out)), end=' ') |
---|
375 | |
---|
376 | print('') |
---|
377 | |
---|
378 | # done |
---|
379 | sys.exit(0) |
---|
380 | |
---|
381 | |
---|
382 | # -------------------------------------------------- |
---|
383 | # list all the tests for users, in a pretty format |
---|
384 | if options.list : |
---|
385 | # fetch the liest of all valid tests |
---|
386 | tests = list_tests( options.include, options.exclude ) |
---|
387 | |
---|
388 | # print the available tests |
---|
389 | fancy_print("\n".join(map(lambda t: t.toString(), tests))) |
---|
390 | |
---|
391 | # done |
---|
392 | sys.exit(0) |
---|
393 | |
---|
394 | # fetch the liest of all valid tests |
---|
395 | all_tests = list_tests( options.include, options.exclude ) |
---|
396 | |
---|
397 | # if user wants all tests than no other treatement of the test list is required |
---|
398 | if options.all or options.include : |
---|
399 | tests = all_tests |
---|
400 | |
---|
401 | #otherwise we need to validate that the test list that was entered is valid |
---|
402 | else : |
---|
403 | tests = valid_tests( options ) |
---|
404 | |
---|
405 | # make sure we have at least some test to run |
---|
406 | if not tests : |
---|
407 | print('ERROR: No valid test to run', file=sys.stderr) |
---|
408 | sys.exit(1) |
---|
409 | |
---|
410 | # prep invariants |
---|
411 | settings.prep_output(tests) |
---|
412 | failed = 0 |
---|
413 | |
---|
414 | # check if the expected files aren't empty |
---|
415 | if not options.regenerate_expected: |
---|
416 | for t in tests: |
---|
417 | if is_empty(t.expect()): |
---|
418 | print('WARNING: test "{}" has empty .expect file'.format(t.target()), file=sys.stderr) |
---|
419 | |
---|
420 | options.jobs = job_count( options ) |
---|
421 | |
---|
422 | # for each build configurations, run the test |
---|
423 | with Timed() as total_dur: |
---|
424 | for ast, arch, debug, install in itertools.product(settings.all_ast, settings.all_arch, settings.all_debug, settings.all_install): |
---|
425 | settings.ast = ast |
---|
426 | settings.arch = arch |
---|
427 | settings.debug = debug |
---|
428 | settings.install = install |
---|
429 | |
---|
430 | # filter out the tests for a different architecture |
---|
431 | # tests are the same across debug/install |
---|
432 | local_tests = settings.ast.filter( tests ) |
---|
433 | local_tests = settings.arch.filter( local_tests ) |
---|
434 | |
---|
435 | # check the build configuration works |
---|
436 | settings.validate() |
---|
437 | jobs = min(options.jobs, len(local_tests)) |
---|
438 | |
---|
439 | # print configuration |
---|
440 | print('%s %i tests on %i cores (%s:%s - %s)' % ( |
---|
441 | 'Regenerating' if settings.generating else 'Running', |
---|
442 | len(local_tests), |
---|
443 | jobs, |
---|
444 | settings.ast.string, |
---|
445 | settings.arch.string, |
---|
446 | settings.debug.string |
---|
447 | )) |
---|
448 | if not local_tests : |
---|
449 | print('WARNING: No tests for this configuration') |
---|
450 | continue |
---|
451 | |
---|
452 | # otherwise run all tests and make sure to return the correct error code |
---|
453 | failed = run_tests(local_tests, jobs) |
---|
454 | if failed: |
---|
455 | if not settings.continue_: |
---|
456 | break |
---|
457 | |
---|
458 | print('Tests took %s' % fmtDur( total_dur.duration )) |
---|
459 | sys.exit( failed ) |
---|