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