source: tests/test.py@ 40c81e5

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 40c81e5 was e6cfb4e2, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

synteax only tests now cp error log into target file.
Small refactoring.
Added comments to test.py

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