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()[2:]] 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('petsc-multigrid') and contains_any(resource, ['occa']), 51 test.startswith('t506') and contains_any(resource, ['occa']), 52 test.startswith('t507') and contains_any(resource, ['occa']), 53 )) 54 55def run(test, backends): 56 import subprocess 57 import time 58 import difflib 59 allargs = get_testargs(test) 60 61 testcases = [] 62 for args in allargs: 63 for ceed_resource in backends: 64 rargs = [os.path.join('build', test)] + args.copy() 65 rargs[rargs.index('{ceed_resource}')] = ceed_resource 66 67 if skip_rule(test, ceed_resource): 68 case = TestCase('{} {}'.format(test, ceed_resource), 69 elapsed_sec=0, 70 timestamp=time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime(start)), 71 stdout='', 72 stderr='') 73 case.add_skipped_info('Pre-run skip rule') 74 else: 75 start = time.time() 76 proc = subprocess.run(rargs, 77 stdout=subprocess.PIPE, 78 stderr=subprocess.PIPE) 79 proc.stdout = proc.stdout.decode('utf-8') 80 proc.stderr = proc.stderr.decode('utf-8') 81 82 case = TestCase('{} {}'.format(test, ceed_resource), 83 elapsed_sec=time.time()-start, 84 timestamp=time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime(start)), 85 stdout=proc.stdout, 86 stderr=proc.stderr) 87 ref_stdout = os.path.join('tests/output', test + '.out') 88 89 if not case.is_skipped() and proc.stderr: 90 if 'OCCA backend failed to use' in proc.stderr: 91 case.add_skipped_info('occa mode not supported {} {}'.format(test, ceed_resource)) 92 elif 'Backend does not implement' in proc.stderr: 93 case.add_skipped_info('not implemented {} {}'.format(test, ceed_resource)) 94 elif 'Can only provide to HOST memory' in proc.stderr: 95 case.add_skipped_info('device memory not supported {} {}'.format(test, ceed_resource)) 96 97 if not case.is_skipped(): 98 if test[:4] in 't110 t111 t112 t113 t114'.split(): 99 check_required_failure(case, proc.stderr, 'Cannot grant CeedVector array access') 100 if test[:4] in 't115'.split(): 101 check_required_failure(case, proc.stderr, 'Cannot grant CeedVector read-only array access, the access lock is already in use') 102 if test[:4] in 't116'.split(): 103 check_required_failure(case, proc.stderr, 'Cannot destroy CeedVector, the access lock is in use') 104 if test[:4] in 't117'.split(): 105 check_required_failure(case, proc.stderr, 'Cannot restore CeedVector array access, access was not granted') 106 if test[:4] in 't118'.split(): 107 check_required_failure(case, proc.stderr, 'Cannot sync CeedVector, the access lock is already in use') 108 if test[:4] in 't303'.split(): 109 check_required_failure(case, proc.stderr, 'Length of input/output vectors incompatible with basis dimensions') 110 111 if not case.is_skipped() and not case.status: 112 if proc.stderr: 113 case.add_failure_info('stderr', proc.stderr) 114 elif proc.returncode != 0: 115 case.add_error_info('returncode = {}'.format(proc.returncode)) 116 elif os.path.isfile(ref_stdout): 117 with open(ref_stdout) as ref: 118 diff = list(difflib.unified_diff(ref.readlines(), 119 proc.stdout.splitlines(keepends=True), 120 fromfile=ref_stdout, 121 tofile='New')) 122 if diff: 123 case.add_failure_info('stdout', output=''.join(diff)) 124 elif proc.stdout and test[:4] not in 't003': 125 case.add_failure_info('stdout', output=proc.stdout) 126 testcases.append(case) 127 return TestSuite(test, testcases) 128 129if __name__ == '__main__': 130 import argparse 131 parser = argparse.ArgumentParser('Test runner with JUnit output') 132 parser.add_argument('--output', help='Output file to write test', default=None) 133 parser.add_argument('--gather', help='Gather all *.junit files into XML', action='store_true') 134 parser.add_argument('test', help='Test executable', nargs='?') 135 args = parser.parse_args() 136 137 if args.gather: 138 gather() 139 else: 140 backends = os.environ['BACKENDS'].split() 141 142 result = run(args.test, backends) 143 output = (os.path.join('build', args.test + '.junit') 144 if args.output is None 145 else args.output) 146 with open(output, 'w') as fd: 147 TestSuite.to_file(fd, [result]) 148 149