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