1#!/usr/bin/env python3 2 3import os 4import sys 5sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'junit-xml'))) 6from junit_xml import TestCase, TestSuite 7 8def parse_testargs(file): 9 if os.path.splitext(file)[1] in ['.c', '.cpp']: 10 return sum([[line.split()[1:]] for line in open(file).readlines() 11 if line.startswith('//TESTARGS')], []) 12 elif os.path.splitext(file)[1] == '.usr': 13 return sum([[line.split()[1:]] for line in open(file).readlines() 14 if line.startswith('C_TESTARGS')], []) 15 raise RuntimeError('Unrecognized extension for file: {}'.format(file)) 16 17def get_source(test): 18 if test.startswith('petsc-'): 19 return os.path.join('examples', 'petsc', test[6:] + '.c') 20 elif test.startswith('mfem-'): 21 return os.path.join('examples', 'mfem', test[5:] + '.cpp') 22 elif test.startswith('nek-'): 23 return os.path.join('examples', 'nek', 'bps', test[4:] + '.usr') 24 elif test.startswith('fluids-'): 25 return os.path.join('examples', 'fluids', test[7:] + '.c') 26 elif test.startswith('solids-'): 27 return os.path.join('examples', 'solids', test[7:] + '.c') 28 elif test.startswith('ex'): 29 return os.path.join('examples', 'ceed', test + '.c') 30 31def get_testargs(test): 32 source = get_source(test) 33 if source is None: 34 return [['{ceed_resource}']] 35 return parse_testargs(source) 36 37def check_required_failure(case, stderr, required): 38 if required in stderr: 39 case.status = 'fails with required: {}'.format(required) 40 else: 41 case.add_failure_info('required: {}'.format(required)) 42 43def contains_any(resource, substrings): 44 return any((sub in resource for sub in substrings)) 45 46def skip_rule(test, resource): 47 return any(( 48 test.startswith('fluids-') and contains_any(resource, ['occa', 'gpu']) and not contains_any(resource, ['/gpu/cuda/gen']), 49 test.startswith('solids-') and contains_any(resource, ['occa']), 50 test.startswith('nek') and contains_any(resource, ['occa']), 51 test.startswith('t507') and contains_any(resource, ['occa']), 52 test.startswith('t318') and contains_any(resource, ['magma']), 53 test.startswith('t506') and contains_any(resource, ['magma']), 54 )) 55 56def run(test, backends): 57 import subprocess 58 import time 59 import difflib 60 allargs = get_testargs(test) 61 62 testcases = [] 63 for args in allargs: 64 for ceed_resource in backends: 65 rargs = [os.path.join('build', test)] + args.copy() 66 rargs[rargs.index('{ceed_resource}')] = ceed_resource 67 68 if skip_rule(test, ceed_resource): 69 case = TestCase('{} {}'.format(test, ceed_resource), 70 elapsed_sec=0, 71 timestamp=time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime()), 72 stdout='', 73 stderr='') 74 case.add_skipped_info('Pre-run skip rule') 75 else: 76 start = time.time() 77 proc = subprocess.run(rargs, 78 stdout=subprocess.PIPE, 79 stderr=subprocess.PIPE) 80 proc.stdout = proc.stdout.decode('utf-8') 81 proc.stderr = proc.stderr.decode('utf-8') 82 83 case = TestCase('{} {}'.format(test, ceed_resource), 84 elapsed_sec=time.time()-start, 85 timestamp=time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime(start)), 86 stdout=proc.stdout, 87 stderr=proc.stderr) 88 ref_stdout = os.path.join('tests/output', test + '.out') 89 90 if not case.is_skipped() and proc.stderr: 91 if 'OCCA backend failed to use' in proc.stderr: 92 case.add_skipped_info('occa mode not supported {} {}'.format(test, ceed_resource)) 93 elif 'Backend does not implement' in proc.stderr: 94 case.add_skipped_info('not implemented {} {}'.format(test, ceed_resource)) 95 elif 'Can only provide to HOST memory' in proc.stderr: 96 case.add_skipped_info('device memory not supported {} {}'.format(test, ceed_resource)) 97 98 if not case.is_skipped(): 99 if test[:4] in 't110 t111 t112 t113 t114'.split(): 100 check_required_failure(case, proc.stderr, 'Cannot grant CeedVector array access') 101 if test[:4] in 't115'.split(): 102 check_required_failure(case, proc.stderr, 'Cannot grant CeedVector read-only array access, the access lock is already in use') 103 if test[:4] in 't116'.split(): 104 check_required_failure(case, proc.stderr, 'Cannot destroy CeedVector, the writable access lock is in use') 105 if test[:4] in 't117'.split(): 106 check_required_failure(case, proc.stderr, 'Cannot restore CeedVector array access, access was not granted') 107 if test[:4] in 't118'.split(): 108 check_required_failure(case, proc.stderr, 'Cannot sync CeedVector, the access lock is already in use') 109 if test[:4] in 't215'.split(): 110 check_required_failure(case, proc.stderr, 'Cannot destroy CeedElemRestriction, a process has read access to the offset data') 111 if test[:4] in 't303'.split(): 112 check_required_failure(case, proc.stderr, 'Length of input/output vectors incompatible with basis dimensions') 113 114 if not case.is_skipped() and not case.status: 115 if proc.stderr: 116 case.add_failure_info('stderr', proc.stderr) 117 elif proc.returncode != 0: 118 case.add_error_info('returncode = {}'.format(proc.returncode)) 119 elif os.path.isfile(ref_stdout): 120 with open(ref_stdout) as ref: 121 diff = list(difflib.unified_diff(ref.readlines(), 122 proc.stdout.splitlines(keepends=True), 123 fromfile=ref_stdout, 124 tofile='New')) 125 if diff: 126 case.add_failure_info('stdout', output=''.join(diff)) 127 elif proc.stdout and test[:4] not in 't003': 128 case.add_failure_info('stdout', output=proc.stdout) 129 testcases.append(case) 130 return TestSuite(test, testcases) 131 132if __name__ == '__main__': 133 import argparse 134 parser = argparse.ArgumentParser('Test runner with JUnit output') 135 parser.add_argument('--output', help='Output file to write test', default=None) 136 parser.add_argument('--gather', help='Gather all *.junit files into XML', action='store_true') 137 parser.add_argument('test', help='Test executable', nargs='?') 138 args = parser.parse_args() 139 140 if args.gather: 141 gather() 142 else: 143 backends = os.environ['BACKENDS'].split() 144 145 result = run(args.test, backends) 146 output = (os.path.join('build', args.test + '.junit') 147 if args.output is None 148 else args.output) 149 with open(output, 'w') as fd: 150 TestSuite.to_file(fd, [result]) 151 for t in result.test_cases: 152 failures = len([c for c in result.test_cases if c.is_failure()]) 153 errors = len([c for c in result.test_cases if c.is_error()]) 154 if failures + errors > 0: 155 sys.exit(1) 156