source: tests/test.py@ 1805b1b

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

Tests now send SIGABRT instead and SIGTERM when a test takes too long

  • Property mode set to 100755
File size: 10.9 KB
RevLine 
[5b993e0]1#!/usr/bin/python3
[efc15918]2
[c07d724]3from pybin.tools import *
[0ad0c55]4from pybin.test_run import *
[bacc36c]5from pybin import settings
[efc15918]6
7import argparse
[122cac7]8import re
[efc15918]9import sys
[f806b61]10import tempfile
[ca54499]11import time
[efc15918]12
13################################################################################
14# help functions
15################################################################################
[f1231f2]16
[5bf1f3e]17def 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]35def 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]54def 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]81def 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')
[dcfedca]93 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]94 parser.add_argument('-j', '--jobs', help='Number of tests to run simultaneously', type=int)
[c07d724]95 parser.add_argument('--list-comp', help='List all valide arguments', action='store_true')
[0ad0c55]96 parser.add_argument('-I','--include', help='Directory of test to include, can be used multiple time, All if omitted', action='append')
97 parser.add_argument('-E','--exclude', help='Directory of test to exclude, can be used multiple time, None if omitted', action='append')
[c07d724]98 parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run')
99
[575a6e5]100 try:
101 options = parser.parse_args()
102 except:
103 print('ERROR: invalid arguments', file=sys.stderr)
104 parser.print_help(sys.stderr)
[5b993e0]105 sys.exit(1)
[c07d724]106
107 # script must have at least some tests to run or be listing
108 listing = options.list or options.list_comp
109 all_tests = options.all
110 some_tests = len(options.tests) > 0
[0ad0c55]111 some_dirs = len(options.include) > 0 if options.include else 0
[c07d724]112
113 # check that exactly one of the booleans is set to true
[0ad0c55]114 if not sum( (listing, all_tests, some_tests, some_dirs) ) > 0 :
[5b993e0]115 print('''ERROR: must have option '--all', '--list', '--include', '-I' or non-empty test list''', file=sys.stderr)
[c07d724]116 parser.print_help()
117 sys.exit(1)
118
119 return options
120
[efc15918]121################################################################################
122# running test functions
123################################################################################
[0c13238]124def success(val):
125 return val == 0 or settings.dry_run
[f85bc15]126
[5bf1f3e]127def no_rule(file, target):
128 return not settings.dry_run and file_contains_only(file, "make: *** No rule to make target `%s'. Stop." % target)
[f85bc15]129
[c07d724]130# logic to run a single test and return the result (No handling of printing or other test framework logic)
[209383b]131def run_single_test(test):
[3c1d702]132
[c07d724]133 # find the output file based on the test name and options flag
[f85bc15]134 exe_file = test.target_executable();
[bacc36c]135 out_file = test.target_output()
136 err_file = test.error_log()
137 cmp_file = test.expect()
138 in_file = test.input()
[0ad0c55]139
140 # prepare the proper directories
[bacc36c]141 test.prepare()
[efc15918]142
[c07d724]143 # build, skipping to next test on error
[0c13238]144 with Timed() as comp_dur:
[d65f92c]145 make_ret, _ = make( test.target(), output_file=subprocess.DEVNULL, error=out_file, error_file = err_file )
[efc15918]146
[0c13238]147 run_dur = None
[f806b61]148 # run everything in a temp directory to make sure core file are handled properly
149 with tempdir():
150 # if the make command succeds continue otherwise skip to diff
151 if success(make_ret):
152 with Timed() as run_dur:
153 if settings.dry_run or is_exe(exe_file):
154 # run test
[d65f92c]155 retcode, _ = sh(exe_file, output_file=out_file, input_file=in_file, timeout=True)
[f806b61]156 else :
157 # simply cat the result into the output
158 retcode = cat(exe_file, out_file)
159 else:
160 retcode = mv(err_file, out_file)
161
162 if success(retcode):
163 if settings.generating :
164 # if we are ounly generating the output we still need to check that the test actually exists
165 if no_rule(out_file, test.target()) :
166 retcode = 1
167 error = "\t\tNo make target for test %s!" % test.target()
168 rm(out_file)
169 else:
170 error = None
[0c13238]171 else :
[f806b61]172 # fetch return code and error from the diff command
173 retcode, error = diff(cmp_file, out_file)
174
175 else:
176 with open (out_file, "r") as myfile:
177 error = myfile.read()
178
179 ret, info = core_info(exe_file)
180 error = error + info if error else info
[0c13238]181
[dcfedca]182 if settings.archive:
183 error = error + '\n' + core_archive(settings.archive, test.target(), exe_file)
184
[0c13238]185
[b5f9829]186
[c07d724]187 # clean the executable
[0c13238]188 rm(exe_file)
[efc15918]189
[0c13238]190 return retcode, error, [comp_dur.duration, run_dur.duration if run_dur else None]
[efc15918]191
[c07d724]192# run a single test and handle the errors, outputs, printing, exception handling, etc.
[209383b]193def run_test_worker(t) :
[1bb2488]194 try :
[bacc36c]195 # print formated name
[5bf1f3e]196 name_txt = '{0:{width}} '.format(t.target(), width=settings.output_width)
[ced2e989]197
[ca54499]198 retcode, error, duration = run_single_test(t)
[0a1a680]199
[bacc36c]200 # update output based on current action
[ca54499]201 result_txt = TestResult.toString( retcode, duration )
[bacc36c]202
203 #print result with error if needed
[a45fc7b]204 text = '\t' + name_txt + result_txt
[bacc36c]205 out = sys.stdout
206 if error :
[2b10f95]207 text = text + '\n' + error
[bacc36c]208
[e791851]209 return retcode == TestResult.SUCCESS, text
[1bb2488]210 except KeyboardInterrupt:
[35a408b7]211 return False, ""
[8364209]212 except Exception as ex:
213 print("Unexpected error in worker thread: %s" % ex, file=sys.stderr)
[35a408b7]214 sys.stderr.flush()
215 return False, ""
216
[ced2e989]217
[911348cd]218# run the given list of tests with the given parameters
[209383b]219def run_tests(tests, jobs) :
[911348cd]220 # clean the sandbox from previous commands
[d65f92c]221 make('clean', output_file=subprocess.DEVNULL, error=subprocess.DEVNULL)
[efc15918]222
[c07d724]223 # create the executor for our jobs and handle the signal properly
[1bb2488]224 pool = multiprocessing.Pool(jobs)
[c07d724]225
[e791851]226 failed = False
227
[c07d724]228 # for each test to run
[ced2e989]229 try :
[e791851]230 num = len(tests)
231 fancy = sys.stdout.isatty()
[35a408b7]232 results = pool.imap_unordered(
[209383b]233 run_test_worker,
[bacc36c]234 tests,
235 chunksize = 1
[35a408b7]236 )
237
238 for i, (succ, txt) in enumerate(timed(results, timeout = settings.timeout.total), 1) :
[e791851]239 if not succ :
240 failed = True
241
242 print(" " + txt)
243
244 if(fancy and i != num):
245 print("%d/%d" % (i, num), end='\r')
246 sys.stdout.flush()
247
[ced2e989]248 except KeyboardInterrupt:
[e791851]249 print("Tests interrupted by user", file=sys.stderr)
[35a408b7]250 pool.terminate()
251 pool.join()
[e791851]252 failed = True
253 except multiprocessing.TimeoutError:
254 print("ERROR: Test suite timed out", file=sys.stderr)
[35a408b7]255 pool.terminate()
256 pool.join()
[e791851]257 failed = True
[35a408b7]258 killgroup() # needed to cleanly kill all children
259
[efc15918]260
[c07d724]261 # clean the workspace
[d65f92c]262 make('clean', output_file=subprocess.DEVNULL, error=subprocess.DEVNULL)
[efc15918]263
[e791851]264 return 1 if failed else 0
[efc15918]265
[6a1bdfd]266
[efc15918]267################################################################################
268# main loop
269################################################################################
[c07d724]270if __name__ == "__main__":
[f803a75]271
[c07d724]272 # parse the command line arguments
[5bf1f3e]273 options = parse_args()
[f1231f2]274
[bacc36c]275 # init global settings
276 settings.init( options )
277
[c07d724]278 # fetch the liest of all valid tests
[5bf1f3e]279 all_tests = list_tests( options.include, options.exclude )
[f1231f2]280
[0c13238]281
[c07d724]282 # if user wants all tests than no other treatement of the test list is required
[0ad0c55]283 if options.all or options.list or options.list_comp or options.include :
[5bf1f3e]284 tests = all_tests
[0534c3c]285
[f3b9efc]286 #otherwise we need to validate that the test list that was entered is valid
[c07d724]287 else :
[5bf1f3e]288 tests = valid_tests( options )
[0534c3c]289
[dd226e3]290 # make sure we have at least some test to run
291 if not tests :
292 print('ERROR: No valid test to run', file=sys.stderr)
293 sys.exit(1)
294
295
[c07d724]296 # sort the test alphabetically for convenience
[f3b9efc]297 tests.sort(key=lambda t: (t.arch if t.arch else '') + t.target())
[f1231f2]298
[c07d724]299 # users may want to simply list the tests
300 if options.list_comp :
[dcfedca]301 print("-h --help --debug --dry-run --list --arch --all --regenerate-expected --archive-errors --install --timeout --global-timeout -j --jobs ", end='')
[0ad0c55]302 print(" ".join(map(lambda t: "%s" % (t.target()), tests)))
[911348cd]303
[c07d724]304 elif options.list :
[f3b9efc]305 print("Listing for %s:%s"% (settings.arch.string, settings.debug.string))
[5b993e0]306 fancy_print("\n".join(map(lambda t: t.toString(), tests)))
[911348cd]307
[b98c913]308 else :
[28582b2]309 # check the build configuration works
[d3c1c6a]310 settings.prep_output(tests)
[28582b2]311 settings.validate()
312
[5bf1f3e]313 options.jobs, forceJobs = job_count( options, tests )
314 settings.update_make_cmd(forceJobs, options.jobs)
[b98c913]315
[2b10f95]316 print('%s %i tests on %i cores (%s:%s)' % (
317 'Regenerating' if settings.generating else 'Running',
318 len(tests),
319 options.jobs,
[f3b9efc]320 settings.arch.string,
[2b10f95]321 settings.debug.string
[bacc36c]322 ))
[efc15918]323
[c07d724]324 # otherwise run all tests and make sure to return the correct error code
[209383b]325 sys.exit( run_tests(tests, options.jobs) )
Note: See TracBrowser for help on using the repository browser.