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