[945047e] | 1 | #!/usr/bin/python
|
---|
[efc15918] | 2 | from __future__ import print_function
|
---|
| 3 |
|
---|
[ced2e989] | 4 | from functools import partial
|
---|
| 5 | from multiprocessing import Pool
|
---|
[122cac7] | 6 | from os import listdir, environ
|
---|
[0534c3c] | 7 | from os.path import isfile, join, splitext
|
---|
[c07d724] | 8 | from pybin.tools import *
|
---|
[efc15918] | 9 |
|
---|
| 10 | import argparse
|
---|
[9fcb5e4] | 11 | import multiprocessing
|
---|
[122cac7] | 12 | import os
|
---|
| 13 | import re
|
---|
[9fcb5e4] | 14 | import signal
|
---|
[efc15918] | 15 | import sys
|
---|
| 16 |
|
---|
| 17 | ################################################################################
|
---|
| 18 | # help functions
|
---|
| 19 | ################################################################################
|
---|
[f1231f2] | 20 |
|
---|
[911348cd] | 21 | # Test class that defines what a test is
|
---|
[f1231f2] | 22 | class Test:
|
---|
| 23 | def __init__(self, name, path):
|
---|
| 24 | self.name, self.path = name, path
|
---|
| 25 |
|
---|
[911348cd] | 26 | # parses the Makefile to find the machine type (32-bit / 64-bit)
|
---|
[f1231f2] | 27 | def getMachineType():
|
---|
[7895d46] | 28 | sh('echo "void ?{}(int*a,int b){}int main(){return 0;}" > .dummy.c')
|
---|
[c07d724] | 29 | ret, out = sh("make .dummy -s", print2stdout=True)
|
---|
[86c8fd6] | 30 |
|
---|
[00303d50] | 31 | if ret != 0:
|
---|
[47f9422] | 32 | print("Failed to identify architecture:")
|
---|
[00303d50] | 33 | print(out)
|
---|
[47f9422] | 34 | print("Stopping")
|
---|
[c07d724] | 35 | rm( (".dummy.c",".dummy") )
|
---|
[47f9422] | 36 | sys.exit(1)
|
---|
[86c8fd6] | 37 |
|
---|
[20340c2] | 38 | _, out = sh("file .dummy", print2stdout=False)
|
---|
[c07d724] | 39 | rm( (".dummy.c",".dummy") )
|
---|
| 40 |
|
---|
[20340c2] | 41 | return re.search("ELF\s([0-9]+)-bit", out).group(1)
|
---|
[f1231f2] | 42 |
|
---|
[be65cca] | 43 | def listTestsFolder(folder) :
|
---|
[871b664] | 44 | path = ('./.expect/%s/' % folder) if folder else './.expect/'
|
---|
| 45 | subpath = "%s/" % folder if folder else ""
|
---|
[f1231f2] | 46 |
|
---|
[911348cd] | 47 | # tests directly in the .expect folder will always be processed
|
---|
[871b664] | 48 | return map(lambda fname: Test(fname, subpath + fname),
|
---|
[be65cca] | 49 | [splitext(f)[0] for f in listdir( path )
|
---|
[0534c3c] | 50 | if not f.startswith('.') and f.endswith('.txt')
|
---|
[f1231f2] | 51 | ])
|
---|
[efc15918] | 52 |
|
---|
[be65cca] | 53 | # reads the directory ./.expect and indentifies the tests
|
---|
| 54 | def listTests( concurrent ):
|
---|
| 55 | machineType = getMachineType()
|
---|
| 56 |
|
---|
| 57 | # tests directly in the .expect folder will always be processed
|
---|
[871b664] | 58 | generic_list = listTestsFolder( "" )
|
---|
[be65cca] | 59 |
|
---|
[911348cd] | 60 | # tests in the machineType folder will be ran only for the corresponding compiler
|
---|
[be65cca] | 61 | typed_list = listTestsFolder( machineType )
|
---|
| 62 |
|
---|
| 63 | # tests in the concurrent folder will be ran only if concurrency is enabled
|
---|
| 64 | concurrent_list = listTestsFolder( "concurrent" ) if concurrent else []
|
---|
[f1231f2] | 65 |
|
---|
[911348cd] | 66 | # append both lists to get
|
---|
[be65cca] | 67 | return generic_list + typed_list + concurrent_list;
|
---|
[efc15918] | 68 |
|
---|
[c07d724] | 69 | # from the found tests, filter all the valid tests/desired tests
|
---|
| 70 | def validTests( 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 | if testname.endswith( (".c", ".cc", ".cpp") ):
|
---|
| 78 | print('ERROR: "%s", tests are not allowed to end with a C/C++/CFA extension, ignoring it' % testname, file=sys.stderr)
|
---|
| 79 | else :
|
---|
| 80 | found = [test for test in allTests if test.name == testname]
|
---|
| 81 | tests.append( found[0] if len(found) == 1 else Test(testname, testname) )
|
---|
| 82 |
|
---|
| 83 | else :
|
---|
| 84 | # otherwise we only need to validate that all tests are present in the complete list
|
---|
| 85 | for testname in options.tests:
|
---|
| 86 | test = [t for t in allTests if t.name == testname]
|
---|
| 87 |
|
---|
| 88 | if len(test) != 0 :
|
---|
| 89 | tests.append( test[0] )
|
---|
| 90 | else :
|
---|
| 91 | print('ERROR: No expected file for test %s, ignoring it' % testname, file=sys.stderr)
|
---|
| 92 |
|
---|
| 93 | # make sure we have at least some test to run
|
---|
| 94 | if len(tests) == 0 :
|
---|
| 95 | print('ERROR: No valid test to run', file=sys.stderr)
|
---|
| 96 | sys.exit(1)
|
---|
| 97 |
|
---|
| 98 | return tests
|
---|
| 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('--concurrent', help='Run concurrent tests', type=yes_no, default='yes')
|
---|
| 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('tests', metavar='test', type=str, nargs='*', help='a list of tests to run')
|
---|
| 113 |
|
---|
| 114 | options = parser.parse_args()
|
---|
| 115 |
|
---|
| 116 | # script must have at least some tests to run or be listing
|
---|
| 117 | listing = options.list or options.list_comp
|
---|
| 118 | all_tests = options.all
|
---|
| 119 | some_tests = len(options.tests) > 0
|
---|
| 120 |
|
---|
| 121 | # check that exactly one of the booleans is set to true
|
---|
| 122 | if not sum( (listing, all_tests, some_tests) ) == 1 :
|
---|
| 123 | print('ERROR: must have option \'--all\', \'--list\' or non-empty test list', file=sys.stderr)
|
---|
| 124 | parser.print_help()
|
---|
| 125 | sys.exit(1)
|
---|
| 126 |
|
---|
| 127 | return options
|
---|
| 128 |
|
---|
| 129 | def jobCount( options ):
|
---|
| 130 | # check if the user already passed in a number of jobs for multi-threading
|
---|
| 131 | make_flags = environ.get('MAKEFLAGS')
|
---|
| 132 | make_jobs_fds = re.search("--jobserver-(auth|fds)=\s*([0-9]+),([0-9]+)", make_flags) if make_flags else None
|
---|
| 133 | if make_jobs_fds :
|
---|
| 134 | tokens = os.read(int(make_jobs_fds.group(2)), 1024)
|
---|
| 135 | options.jobs = len(tokens)
|
---|
| 136 | os.write(int(make_jobs_fds.group(3)), tokens)
|
---|
| 137 | else :
|
---|
| 138 | options.jobs = multiprocessing.cpu_count()
|
---|
| 139 |
|
---|
| 140 | # make sure we have a valid number of jobs that corresponds to user input
|
---|
| 141 | if options.jobs <= 0 :
|
---|
| 142 | print('ERROR: Invalid number of jobs', file=sys.stderr)
|
---|
| 143 | sys.exit(1)
|
---|
| 144 |
|
---|
| 145 | return min( options.jobs, len(tests) ), True if make_flags else False
|
---|
[122cac7] | 146 |
|
---|
[efc15918] | 147 | ################################################################################
|
---|
| 148 | # running test functions
|
---|
| 149 | ################################################################################
|
---|
[c07d724] | 150 | # logic to run a single test and return the result (No handling of printing or other test framework logic)
|
---|
[6a1bdfd] | 151 | def run_single_test(test, generate, dry_run, debug):
|
---|
[3c1d702] | 152 |
|
---|
[c07d724] | 153 | # find the output file based on the test name and options flag
|
---|
| 154 | out_file = (".out/%s.log" % test.name) if not generate else (".expect/%s.txt" % test.path)
|
---|
| 155 | err_file = ".err/%s.log" % test.name
|
---|
[efc15918] | 156 |
|
---|
[c07d724] | 157 | # remove any outputs from the previous tests to prevent side effects
|
---|
| 158 | rm( (out_file, test.name), dry_run )
|
---|
[6a1bdfd] | 159 |
|
---|
[c07d724] | 160 | options = "-debug" if debug else "-nodebug"
|
---|
[efc15918] | 161 |
|
---|
[c07d724] | 162 | # build, skipping to next test on error
|
---|
| 163 | make_ret, _ = sh("""%s test=yes EXTRA_FLAGS="-quiet %s" %s 2> %s 1> /dev/null""" % (make_cmd, options, test.name, out_file), dry_run)
|
---|
[efc15918] | 164 |
|
---|
[c07d724] | 165 | # if the make command succeds continue otherwise skip to diff
|
---|
| 166 | if make_ret == 0 :
|
---|
| 167 | # fetch optional input
|
---|
| 168 | stdinput = "< .in/%s.txt" % test.name if isfile(".in/%s.txt" % test.path) else ""
|
---|
[efc15918] | 169 |
|
---|
[c07d724] | 170 | if fileIsExecutable(test.name) :
|
---|
| 171 | # run test
|
---|
| 172 | sh("./%s %s > %s 2>&1" % (test.name, stdinput, out_file), dry_run)
|
---|
[9fcb5e4] | 173 | else :
|
---|
[c07d724] | 174 | # simply cat the result into the output
|
---|
| 175 | sh("cat %s > %s" % (test.name, out_file), dry_run)
|
---|
[4e9151f] | 176 |
|
---|
[c07d724] | 177 | else :
|
---|
| 178 | # command failed save the log to less temporary file
|
---|
| 179 | sh("mv %s %s" % (err_file, out_file), dry_run)
|
---|
[122cac7] | 180 |
|
---|
[c07d724] | 181 | retcode = 0
|
---|
| 182 | error = None
|
---|
[122cac7] | 183 |
|
---|
[c07d724] | 184 | if generate :
|
---|
| 185 | # if we are ounly generating the output we still need to check that the test actually exists
|
---|
| 186 | if not dry_run and fileContainsOnly(out_file, "make: *** No rule to make target `%s'. Stop." % test.name) :
|
---|
| 187 | retcode = 1;
|
---|
| 188 | error = "\t\tNo make target for test %s!" % test.name
|
---|
| 189 | sh("rm %s" % out_file, False)
|
---|
[84d4d6f] | 190 |
|
---|
[c07d724] | 191 | else :
|
---|
| 192 | # fetch return code and error from the diff command
|
---|
| 193 | retcode, error = diff(".expect/%s.txt" % test.path, ".out/%s.log" % test.name, dry_run)
|
---|
| 194 |
|
---|
| 195 | # clean the executable
|
---|
| 196 | sh("rm -f %s > /dev/null 2>&1" % test.name, dry_run)
|
---|
[efc15918] | 197 |
|
---|
[472ca32] | 198 | return retcode, error
|
---|
[efc15918] | 199 |
|
---|
[c07d724] | 200 | # run a single test and handle the errors, outputs, printing, exception handling, etc.
|
---|
| 201 | def run_test_worker(t, generate, dry_run, debug) :
|
---|
[ced2e989] | 202 |
|
---|
[9fcb5e4] | 203 | signal.signal(signal.SIGINT, signal.SIG_DFL)
|
---|
| 204 | # print formated name
|
---|
| 205 | name_txt = "%20s " % t.name
|
---|
[ced2e989] | 206 |
|
---|
[9fcb5e4] | 207 | #run the test instance and collect the result
|
---|
| 208 | test_failed, error = run_single_test(t, generate, dry_run, debug)
|
---|
[0a1a680] | 209 |
|
---|
[9fcb5e4] | 210 | # update output based on current action
|
---|
| 211 | if generate :
|
---|
| 212 | failed_txt = "ERROR"
|
---|
| 213 | success_txt = "Done"
|
---|
| 214 | else :
|
---|
| 215 | failed_txt = "FAILED"
|
---|
| 216 | success_txt = "PASSED"
|
---|
[0a1a680] | 217 |
|
---|
[9fcb5e4] | 218 | #print result with error if needed
|
---|
| 219 | text = name_txt + (failed_txt if test_failed else success_txt)
|
---|
| 220 | out = sys.stdout
|
---|
| 221 | if error :
|
---|
| 222 | text = text + "\n" + error
|
---|
| 223 | out = sys.stderr
|
---|
[be65cca] | 224 |
|
---|
[9fcb5e4] | 225 | print(text, file = out);
|
---|
| 226 | sys.stdout.flush()
|
---|
| 227 | sys.stderr.flush()
|
---|
| 228 | signal.signal(signal.SIGINT, signal.SIG_IGN)
|
---|
| 229 |
|
---|
| 230 | return test_failed
|
---|
[ced2e989] | 231 |
|
---|
[911348cd] | 232 | # run the given list of tests with the given parameters
|
---|
[6a1bdfd] | 233 | def run_tests(tests, generate, dry_run, jobs, debug) :
|
---|
[911348cd] | 234 | # clean the sandbox from previous commands
|
---|
[74358c3] | 235 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run)
|
---|
[911348cd] | 236 |
|
---|
[c07d724] | 237 | # make sure the required folder are present
|
---|
[4e9151f] | 238 | sh('mkdir -p .out .expect .err', dry_run)
|
---|
[3c1d702] | 239 |
|
---|
| 240 | if generate :
|
---|
[ebcd82b] | 241 | print( "Regenerate tests for: " )
|
---|
[efc15918] | 242 |
|
---|
[c07d724] | 243 | # create the executor for our jobs and handle the signal properly
|
---|
[9fcb5e4] | 244 | original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
|
---|
[ced2e989] | 245 | pool = Pool(jobs)
|
---|
[9fcb5e4] | 246 | signal.signal(signal.SIGINT, original_sigint_handler)
|
---|
[c07d724] | 247 |
|
---|
| 248 | # for each test to run
|
---|
[ced2e989] | 249 | try :
|
---|
[c07d724] | 250 | results = pool.map_async(partial(run_test_worker, generate=generate, dry_run=dry_run, debug=debug), tests ).get(3600)
|
---|
[ced2e989] | 251 | except KeyboardInterrupt:
|
---|
| 252 | pool.terminate()
|
---|
| 253 | print("Tests interrupted by user")
|
---|
| 254 | sys.exit(1)
|
---|
[efc15918] | 255 |
|
---|
[c07d724] | 256 | # clean the workspace
|
---|
[74358c3] | 257 | sh("%s clean > /dev/null 2>&1" % make_cmd, dry_run)
|
---|
[efc15918] | 258 |
|
---|
[ced2e989] | 259 | for failed in results:
|
---|
| 260 | if failed :
|
---|
| 261 | return 1
|
---|
| 262 |
|
---|
| 263 | return 0
|
---|
[efc15918] | 264 |
|
---|
[6a1bdfd] | 265 |
|
---|
[efc15918] | 266 | ################################################################################
|
---|
| 267 | # main loop
|
---|
| 268 | ################################################################################
|
---|
[c07d724] | 269 | if __name__ == "__main__":
|
---|
| 270 | #always run from same folder
|
---|
| 271 | chdir()
|
---|
| 272 |
|
---|
| 273 | # parse the command line arguments
|
---|
| 274 | options = getOptions()
|
---|
[f1231f2] | 275 |
|
---|
[c07d724] | 276 | # fetch the liest of all valid tests
|
---|
| 277 | allTests = listTests( options.concurrent )
|
---|
[f1231f2] | 278 |
|
---|
[c07d724] | 279 | # if user wants all tests than no other treatement of the test list is required
|
---|
| 280 | if options.all or options.list or options.list_comp :
|
---|
| 281 | tests = allTests
|
---|
[0534c3c] | 282 |
|
---|
[c07d724] | 283 | else :
|
---|
| 284 | #otherwise we need to validate that the test list that was entered is valid
|
---|
| 285 | tests = validTests( options )
|
---|
[0534c3c] | 286 |
|
---|
[c07d724] | 287 | # sort the test alphabetically for convenience
|
---|
| 288 | tests.sort(key=lambda t: t.name)
|
---|
[f1231f2] | 289 |
|
---|
[c07d724] | 290 | # users may want to simply list the tests
|
---|
| 291 | if options.list_comp :
|
---|
| 292 | print("-h --help --debug --concurrent --dry-run --list --all --regenerate-expected -j --jobs ", end='')
|
---|
| 293 | print(" ".join(map(lambda t: "%s" % (t.name), tests)))
|
---|
[911348cd] | 294 |
|
---|
[c07d724] | 295 | elif options.list :
|
---|
| 296 | print("\n".join(map(lambda t: "%s (%s)" % (t.name, t.path), tests)))
|
---|
[911348cd] | 297 |
|
---|
[b98c913] | 298 | else :
|
---|
[c07d724] | 299 | options.jobs, forceJobs = jobCount( options )
|
---|
[b98c913] | 300 |
|
---|
[c07d724] | 301 | print('Running (%s) on %i cores' % ("debug" if options.debug else "no debug", options.jobs))
|
---|
| 302 | make_cmd = "make" if forceJobs else ("make -j%i" % options.jobs)
|
---|
[efc15918] | 303 |
|
---|
[c07d724] | 304 | # otherwise run all tests and make sure to return the correct error code
|
---|
| 305 | sys.exit( run_tests(tests, options.regenerate_expected, options.dry_run, options.jobs, options.debug) )
|
---|