source: tests/test.py @ a2f2fda

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a2f2fda was a2f2fda, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Finished support for running tests for new and old ast

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