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