source: tests/test.py @ 21923bd

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 21923bd was 0f3d844, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Updates to cfa.nanorc. Added information on how to do some auto-completion in bash.

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