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