| 1 | #!/usr/bin/python
|
|---|
| 2 | from __future__ import print_function
|
|---|
| 3 |
|
|---|
| 4 | from functools import partial
|
|---|
| 5 | from multiprocessing import Pool
|
|---|
| 6 | from os import listdir, environ
|
|---|
| 7 | from os.path import isfile, join, splitext
|
|---|
| 8 | from pybin.tools import *
|
|---|
| 9 | from pybin.test_run import *
|
|---|
| 10 |
|
|---|
| 11 | import argparse
|
|---|
| 12 | import multiprocessing
|
|---|
| 13 | import os
|
|---|
| 14 | import re
|
|---|
| 15 | import signal
|
|---|
| 16 | import sys
|
|---|
| 17 |
|
|---|
| 18 | ################################################################################
|
|---|
| 19 | # help functions
|
|---|
| 20 | ################################################################################
|
|---|
| 21 |
|
|---|
| 22 | def list_expected():
|
|---|
| 23 | expected = []
|
|---|
| 24 |
|
|---|
| 25 | def step(_, dirname, names):
|
|---|
| 26 | for name in names:
|
|---|
| 27 | path = os.path.join(dirname, name)
|
|---|
| 28 |
|
|---|
| 29 | match = re.search("(\.[\w\/\-_]*)\/.expect\/([\w\-_]+)(\.[\w\-_]+)?\.txt", path)
|
|---|
| 30 | if match :
|
|---|
| 31 | test = Test()
|
|---|
| 32 | test.name = match.group(2)
|
|---|
| 33 | test.path = match.group(1)
|
|---|
| 34 | test.arch = match.group(3)[1:] if match.group(3) else None
|
|---|
| 35 | expected.append(test)
|
|---|
| 36 |
|
|---|
| 37 | # Start the walk
|
|---|
| 38 | os.path.walk('.', step, '')
|
|---|
| 39 |
|
|---|
| 40 | return expected
|
|---|
| 41 |
|
|---|
| 42 | # reads the directory ./.expect and indentifies the tests
|
|---|
| 43 | def listTests( includes, excludes ):
|
|---|
| 44 | includes = [os.path.normpath( os.path.join('.',i) ) for i in includes] if includes else None
|
|---|
| 45 | excludes = [os.path.normpath( os.path.join('.',i) ) for i in excludes] if excludes else None
|
|---|
| 46 |
|
|---|
| 47 | # tests directly in the .expect folder will always be processed
|
|---|
| 48 | test_list = list_expected()
|
|---|
| 49 |
|
|---|
| 50 | # if we have a limited number of includes, filter by them
|
|---|
| 51 | if includes:
|
|---|
| 52 | test_list = [x for x in test_list if
|
|---|
| 53 | os.path.normpath( x.path ).startswith( tuple(includes) )
|
|---|
| 54 | ]
|
|---|
| 55 |
|
|---|
| 56 | # # if we have a folders to excludes, filter by them
|
|---|
| 57 | if excludes:
|
|---|
| 58 | test_list = [x for x in test_list if not
|
|---|
| 59 | os.path.normpath( x.path ).startswith( tuple(excludes) )
|
|---|
| 60 | ]
|
|---|
| 61 |
|
|---|
| 62 | return test_list
|
|---|
| 63 |
|
|---|
| 64 | # from the found tests, filter all the valid tests/desired tests
|
|---|
| 65 | def validTests( options ):
|
|---|
| 66 | tests = []
|
|---|
| 67 |
|
|---|
| 68 | # if we are regenerating the tests we need to find the information of the
|
|---|
| 69 | # already existing tests and create new info for the new tests
|
|---|
| 70 | if options.regenerate_expected :
|
|---|
| 71 | for testname in options.tests :
|
|---|
| 72 | if testname.endswith( (".c", ".cc", ".cpp") ):
|
|---|
| 73 | print('ERROR: "%s", tests are not allowed to end with a C/C++/CFA extension, ignoring it' % testname, file=sys.stderr)
|
|---|
| 74 | else :
|
|---|
| 75 | found = [test for test in allTests if test.name == testname]
|
|---|
| 76 | tests.append( found[0] if len(found) == 1 else Test(testname, testname) )
|
|---|
| 77 |
|
|---|
| 78 | else :
|
|---|
| 79 | # otherwise we only need to validate that all tests are present in the complete list
|
|---|
| 80 | for testname in options.tests:
|
|---|
| 81 | test = [t for t in allTests if os.path.normpath( t.target() ) == os.path.normpath( testname )]
|
|---|
| 82 |
|
|---|
| 83 | if len(test) != 0 :
|
|---|
| 84 | tests.append( test[0] )
|
|---|
| 85 | else :
|
|---|
| 86 | print('ERROR: No expected file for test %s, ignoring it' % testname, file=sys.stderr)
|
|---|
| 87 |
|
|---|
| 88 | # make sure we have at least some test to run
|
|---|
| 89 | if len(tests) == 0 :
|
|---|
| 90 | print('ERROR: No valid test to run', file=sys.stderr)
|
|---|
| 91 | sys.exit(1)
|
|---|
| 92 |
|
|---|
| 93 | return tests
|
|---|
| 94 |
|
|---|
| 95 | class TestResult:
|
|---|
| 96 | SUCCESS = 0
|
|---|
| 97 | FAILURE = 1
|
|---|
| 98 | TIMEOUT = 124
|
|---|
| 99 |
|
|---|
| 100 | # parses the option
|
|---|
| 101 | def getOptions():
|
|---|
| 102 | # create a parser with the arguments for the tests script
|
|---|
| 103 | parser = argparse.ArgumentParser(description='Script which runs cforall tests')
|
|---|
| 104 | parser.add_argument('--debug', help='Run all tests in debug or release', type=yes_no, default='no')
|
|---|
| 105 | parser.add_argument('--arch', help='Test for specific architecture', type=str, default=getMachineType())
|
|---|
| 106 | parser.add_argument('--dry-run', help='Don\'t run the tests, only output the commands', action='store_true')
|
|---|
| 107 | parser.add_argument('--list', help='List all test available', action='store_true')
|
|---|
| 108 | parser.add_argument('--all', help='Run all test available', action='store_true')
|
|---|
| 109 | parser.add_argument('--regenerate-expected', help='Regenerate the .expect by running the specified tets, can be used with --all option', action='store_true')
|
|---|
| 110 | parser.add_argument('-j', '--jobs', help='Number of tests to run simultaneously', type=int, default='8')
|
|---|
| 111 | parser.add_argument('--list-comp', help='List all valide arguments', action='store_true')
|
|---|
| 112 | parser.add_argument('-I','--include', help='Directory of test to include, can be used multiple time, All if omitted', action='append')
|
|---|
| 113 | parser.add_argument('-E','--exclude', help='Directory of test to exclude, can be used multiple time, None if omitted', action='append')
|
|---|
| 114 | parser.add_argument('tests', metavar='test', type=str, nargs='*', help='a list of tests to run')
|
|---|
| 115 |
|
|---|
| 116 | options = parser.parse_args()
|
|---|
| 117 |
|
|---|
| 118 | # script must have at least some tests to run or be listing
|
|---|
| 119 | listing = options.list or options.list_comp
|
|---|
| 120 | all_tests = options.all
|
|---|
| 121 | some_tests = len(options.tests) > 0
|
|---|
| 122 | some_dirs = len(options.include) > 0 if options.include else 0
|
|---|
| 123 |
|
|---|
| 124 | # check that exactly one of the booleans is set to true
|
|---|
| 125 | if not sum( (listing, all_tests, some_tests, some_dirs) ) > 0 :
|
|---|
| 126 | print('ERROR: must have option \'--all\', \'--list\', \'--include\', \'-I\' or non-empty test list', file=sys.stderr)
|
|---|
| 127 | parser.print_help()
|
|---|
| 128 | sys.exit(1)
|
|---|
| 129 |
|
|---|
| 130 | return options
|
|---|
| 131 |
|
|---|
| 132 | def jobCount( options ):
|
|---|
| 133 | # check if the user already passed in a number of jobs for multi-threading
|
|---|
| 134 | make_flags = environ.get('MAKEFLAGS')
|
|---|
| 135 | make_jobs_fds = re.search("--jobserver-(auth|fds)=\s*([0-9]+),([0-9]+)", make_flags) if make_flags else None
|
|---|
| 136 | if make_jobs_fds :
|
|---|
| 137 | tokens = os.read(int(make_jobs_fds.group(2)), 1024)
|
|---|
| 138 | options.jobs = len(tokens)
|
|---|
| 139 | os.write(int(make_jobs_fds.group(3)), tokens)
|
|---|
| 140 | else :
|
|---|
| 141 | options.jobs = multiprocessing.cpu_count()
|
|---|
| 142 |
|
|---|
| 143 | # make sure we have a valid number of jobs that corresponds to user input
|
|---|
| 144 | if options.jobs <= 0 :
|
|---|
| 145 | print('ERROR: Invalid number of jobs', file=sys.stderr)
|
|---|
| 146 | sys.exit(1)
|
|---|
| 147 |
|
|---|
| 148 | return min( options.jobs, len(tests) ), True if make_flags else False
|
|---|
| 149 |
|
|---|
| 150 | ################################################################################
|
|---|
| 151 | # running test functions
|
|---|
| 152 | ################################################################################
|
|---|
| 153 | # logic to run a single test and return the result (No handling of printing or other test framework logic)
|
|---|
| 154 | def run_single_test(test, generate, dry_run, debug):
|
|---|
| 155 |
|
|---|
| 156 | # find the output file based on the test name and options flag
|
|---|
| 157 | out_file = test.output_file() if not generate else test.expect_file()
|
|---|
| 158 | err_file = test.error_file()
|
|---|
| 159 | cmp_file = test.expect_file()
|
|---|
| 160 | in_file = test.input_file()
|
|---|
| 161 |
|
|---|
| 162 | # prepare the proper directories
|
|---|
| 163 | test.prepare( dry_run )
|
|---|
| 164 |
|
|---|
| 165 | # remove any outputs from the previous tests to prevent side effects
|
|---|
| 166 | rm( (out_file, err_file, test.target()), dry_run )
|
|---|
| 167 |
|
|---|
| 168 | options = "-debug" if debug else "-nodebug"
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 | # build, skipping to next test on error
|
|---|
| 172 | make_ret, _ = sh("""%s DEBUG_FLAGS="%s" %s test="%s" 2> %s 1> /dev/null""" % (make_cmd, options, test.target(), err_file, out_file), dry_run)
|
|---|
| 173 |
|
|---|
| 174 | retcode = 0
|
|---|
| 175 | error = None
|
|---|
| 176 |
|
|---|
| 177 | # if the make command succeds continue otherwise skip to diff
|
|---|
| 178 | if make_ret == 0 or dry_run:
|
|---|
| 179 | # fetch optional input
|
|---|
| 180 | stdinput = "< %s" % in_file if isfile(in_file) else ""
|
|---|
| 181 |
|
|---|
| 182 | if dry_run or fileIsExecutable(test.target()) :
|
|---|
| 183 | # run test
|
|---|
| 184 | retcode, _ = sh("timeout 60 ./%s %s > %s 2>&1" % (test.target(), stdinput, out_file), dry_run)
|
|---|
| 185 | else :
|
|---|
| 186 | # simply cat the result into the output
|
|---|
| 187 | sh("cat %s > %s" % (test.target(), out_file), dry_run)
|
|---|
| 188 | else:
|
|---|
| 189 | sh("mv %s %s" % (err_file, out_file), dry_run)
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 | if retcode == 0:
|
|---|
| 193 | if generate :
|
|---|
| 194 | # if we are ounly generating the output we still need to check that the test actually exists
|
|---|
| 195 | if not dry_run and fileContainsOnly(out_file, "make: *** No rule to make target `%s'. Stop." % test.target()) :
|
|---|
| 196 | retcode = 1;
|
|---|
| 197 | error = "\t\tNo make target for test %s!" % test.target()
|
|---|
| 198 | sh("rm %s" % out_file, False)
|
|---|
| 199 | else :
|
|---|
| 200 | # fetch return code and error from the diff command
|
|---|
| 201 | retcode, error = diff(cmp_file, out_file, dry_run)
|
|---|
| 202 |
|
|---|
| 203 | else:
|
|---|
| 204 | with open (out_file, "r") as myfile:
|
|---|
| 205 | error = myfile.read()
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 | # clean the executable
|
|---|
| 209 | sh("rm -f %s > /dev/null 2>&1" % test.target(), dry_run)
|
|---|
| 210 |
|
|---|
| 211 | return retcode, error
|
|---|
| 212 |
|
|---|
| 213 | # run a single test and handle the errors, outputs, printing, exception handling, etc.
|
|---|
| 214 | def run_test_worker(t, generate, dry_run, debug) :
|
|---|
| 215 |
|
|---|
| 216 | signal.signal(signal.SIGINT, signal.SIG_DFL)
|
|---|
| 217 | # print formated name
|
|---|
| 218 | name_txt = "%20s " % t.name
|
|---|
| 219 |
|
|---|
| 220 | retcode, error = run_single_test(t, generate, dry_run, debug)
|
|---|
| 221 |
|
|---|
| 222 | # update output based on current action
|
|---|
| 223 | if generate :
|
|---|
| 224 | if retcode == TestResult.SUCCESS: result_txt = "Done"
|
|---|
| 225 | elif retcode == TestResult.TIMEOUT: result_txt = "TIMEOUT"
|
|---|
| 226 | else : result_txt = "ERROR code %d" % retcode
|
|---|
| 227 | else :
|
|---|
| 228 | if retcode == TestResult.SUCCESS: result_txt = "PASSED"
|
|---|
| 229 | elif retcode == TestResult.TIMEOUT: result_txt = "TIMEOUT"
|
|---|
| 230 | else : result_txt = "FAILED with code %d" % retcode
|
|---|
| 231 |
|
|---|
| 232 | #print result with error if needed
|
|---|
| 233 | text = name_txt + result_txt
|
|---|
| 234 | out = sys.stdout
|
|---|
| 235 | if error :
|
|---|
| 236 | text = text + "\n" + error
|
|---|
| 237 | out = sys.stderr
|
|---|
| 238 |
|
|---|
| 239 | print(text, file = out)
|
|---|
| 240 | sys.stdout.flush()
|
|---|
| 241 | sys.stderr.flush()
|
|---|
| 242 | signal.signal(signal.SIGINT, signal.SIG_IGN)
|
|---|
| 243 |
|
|---|
| 244 | return retcode != TestResult.SUCCESS
|
|---|
| 245 |
|
|---|
| 246 | # run the given list of tests with the given parameters
|
|---|
| 247 | def run_tests(tests, generate, dry_run, jobs, debug) :
|
|---|
| 248 | # clean the sandbox from previous commands
|
|---|
| 249 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run)
|
|---|
| 250 |
|
|---|
| 251 | if generate :
|
|---|
| 252 | print( "Regenerate tests for: " )
|
|---|
| 253 |
|
|---|
| 254 | # create the executor for our jobs and handle the signal properly
|
|---|
| 255 | original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
|
|---|
| 256 | pool = Pool(jobs)
|
|---|
| 257 | signal.signal(signal.SIGINT, original_sigint_handler)
|
|---|
| 258 |
|
|---|
| 259 | # for each test to run
|
|---|
| 260 | try :
|
|---|
| 261 | results = pool.map_async(partial(run_test_worker, generate=generate, dry_run=dry_run, debug=debug), tests, chunksize = 1 ).get(7200)
|
|---|
| 262 | except KeyboardInterrupt:
|
|---|
| 263 | pool.terminate()
|
|---|
| 264 | print("Tests interrupted by user")
|
|---|
| 265 | sys.exit(1)
|
|---|
| 266 |
|
|---|
| 267 | # clean the workspace
|
|---|
| 268 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run)
|
|---|
| 269 |
|
|---|
| 270 | for failed in results:
|
|---|
| 271 | if failed :
|
|---|
| 272 | return 1
|
|---|
| 273 |
|
|---|
| 274 | return 0
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 | ################################################################################
|
|---|
| 278 | # main loop
|
|---|
| 279 | ################################################################################
|
|---|
| 280 | if __name__ == "__main__":
|
|---|
| 281 | #always run from same folder
|
|---|
| 282 | chdir()
|
|---|
| 283 |
|
|---|
| 284 | # parse the command line arguments
|
|---|
| 285 | options = getOptions()
|
|---|
| 286 |
|
|---|
| 287 | # fetch the liest of all valid tests
|
|---|
| 288 | allTests = listTests( options.include, options.exclude )
|
|---|
| 289 |
|
|---|
| 290 | # if user wants all tests than no other treatement of the test list is required
|
|---|
| 291 | if options.all or options.list or options.list_comp or options.include :
|
|---|
| 292 | tests = allTests
|
|---|
| 293 |
|
|---|
| 294 | else :
|
|---|
| 295 | #otherwise we need to validate that the test list that was entered is valid
|
|---|
| 296 | tests = validTests( options )
|
|---|
| 297 |
|
|---|
| 298 | # sort the test alphabetically for convenience
|
|---|
| 299 | tests.sort(key=lambda t: os.path.join(t.path, t.name))
|
|---|
| 300 |
|
|---|
| 301 | # users may want to simply list the tests
|
|---|
| 302 | if options.list_comp :
|
|---|
| 303 | print("-h --help --debug --dry-run --list --all --regenerate-expected -j --jobs ", end='')
|
|---|
| 304 | print(" ".join(map(lambda t: "%s" % (t.target()), tests)))
|
|---|
| 305 |
|
|---|
| 306 | elif options.list :
|
|---|
| 307 | print("Listing for %s:%s"% (options.arch, "debug" if options.debug else "no debug"))
|
|---|
| 308 | print("\n".join(map(lambda t: "%s" % (t.toString()), tests)))
|
|---|
| 309 |
|
|---|
| 310 | else :
|
|---|
| 311 | options.jobs, forceJobs = jobCount( options )
|
|---|
| 312 |
|
|---|
| 313 | print('Running (%s:%s) on %i cores' % (options.arch, "debug" if options.debug else "no debug", options.jobs))
|
|---|
| 314 | make_cmd = "make" if forceJobs else ("make -j%i" % options.jobs)
|
|---|
| 315 |
|
|---|
| 316 | # otherwise run all tests and make sure to return the correct error code
|
|---|
| 317 | sys.exit( run_tests(tests, options.regenerate_expected, options.dry_run, options.jobs, options.debug) )
|
|---|