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 't006 t007'.split(): 102 check_required_failure(case, proc.stderr, 'No suitable backend:') 103 if test[:4] in 't008'.split(): 104 check_required_failure(case, proc.stderr, 'Avaliable backend resources:') 105 if test[:4] in 't110 t111 t112 t113 t114'.split(): 106 check_required_failure(case, proc.stderr, 'Cannot grant CeedVector array access') 107 if test[:4] in 't115'.split(): 108 check_required_failure(case, proc.stderr, 'Cannot grant CeedVector read-only array access, the access lock is already in use') 109 if test[:4] in 't116'.split(): 110 check_required_failure(case, proc.stderr, 'Cannot destroy CeedVector, the writable access lock is in use') 111 if test[:4] in 't117'.split(): 112 check_required_failure(case, proc.stderr, 'Cannot restore CeedVector array access, access was not granted') 113 if test[:4] in 't118'.split(): 114 check_required_failure(case, proc.stderr, 'Cannot sync CeedVector, the access lock is already in use') 115 if test[:4] in 't215'.split(): 116 check_required_failure(case, proc.stderr, 'Cannot destroy CeedElemRestriction, a process has read access to the offset data') 117 if test[:4] in 't303'.split(): 118 check_required_failure(case, proc.stderr, 'Length of input/output vectors incompatible with basis dimensions') 119 120 if not case.is_skipped() and not case.status: 121 if proc.stderr: 122 case.add_failure_info('stderr', proc.stderr) 123 elif proc.returncode != 0: 124 case.add_error_info('returncode = {}'.format(proc.returncode)) 125 elif os.path.isfile(ref_stdout): 126 with open(ref_stdout) as ref: 127 diff = list(difflib.unified_diff(ref.readlines(), 128 proc.stdout.splitlines(keepends=True), 129 fromfile=ref_stdout, 130 tofile='New')) 131 if diff: 132 case.add_failure_info('stdout', output=''.join(diff)) 133 elif proc.stdout and test[:4] not in 't003': 134 case.add_failure_info('stdout', output=proc.stdout) 135 testcases.append(case) 136 return TestSuite(test, testcases) 137 138if __name__ == '__main__': 139 import argparse 140 parser = argparse.ArgumentParser('Test runner with JUnit output') 141 parser.add_argument('--output', help='Output file to write test', default=None) 142 parser.add_argument('--gather', help='Gather all *.junit files into XML', action='store_true') 143 parser.add_argument('test', help='Test executable', nargs='?') 144 args = parser.parse_args() 145 146 if args.gather: 147 gather() 148 else: 149 backends = os.environ['BACKENDS'].split() 150 151 result = run(args.test, backends) 152 output = (os.path.join('build', args.test + '.junit') 153 if args.output is None 154 else args.output) 155 with open(output, 'w') as fd: 156 TestSuite.to_file(fd, [result]) 157 for t in result.test_cases: 158 failures = len([c for c in result.test_cases if c.is_failure()]) 159 errors = len([c for c in result.test_cases if c.is_error()]) 160 if failures + errors > 0: 161 sys.exit(1) 162