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 't110 t111 t112 t113 t114'.split(): 74 check_required_failure(case, proc.stderr, 'Cannot grant CeedVector array access') 75 if test[:4] in 't115'.split(): 76 check_required_failure(case, proc.stderr, 'Cannot grant CeedVector read-only array access, the access lock is already in use') 77 if test[:4] in 't116'.split(): 78 check_required_failure(case, proc.stderr, 'Cannot destroy CeedVector, the access lock is in use') 79 if test[:4] in 't117'.split(): 80 check_required_failure(case, proc.stderr, 'Cannot restore CeedVector array access, access was not granted') 81 if test[:4] in 't118'.split(): 82 check_required_failure(case, proc.stderr, 'Cannot sync CeedVector, the access lock is already in use') 83 if test[:4] in 't303'.split(): 84 check_required_failure(case, proc.stderr, 'Length of input/output vectors incompatible with basis dimensions') 85 86 if not case.is_skipped() and not case.status: 87 if proc.stderr: 88 case.add_failure_info('stderr', proc.stderr) 89 elif proc.returncode != 0: 90 case.add_error_info('returncode = {}'.format(proc.returncode)) 91 elif os.path.isfile(ref_stdout): 92 with open(ref_stdout) as ref: 93 diff = list(difflib.unified_diff(ref.readlines(), 94 proc.stdout.splitlines(keepends=True), 95 fromfile=ref_stdout, 96 tofile='New')) 97 if diff: 98 case.add_failure_info('stdout', output=''.join(diff)) 99 elif proc.stdout: 100 case.add_failure_info('stdout', output=proc.stdout) 101 testcases.append(case) 102 return TestSuite(test, testcases) 103 104if __name__ == '__main__': 105 import argparse 106 parser = argparse.ArgumentParser('Test runner with JUnit output') 107 parser.add_argument('--output', help='Output file to write test', default=None) 108 parser.add_argument('--gather', help='Gather all *.junit files into XML', action='store_true') 109 parser.add_argument('test', help='Test executable', nargs='?') 110 args = parser.parse_args() 111 112 if args.gather: 113 gather() 114 else: 115 backends = os.environ['BACKENDS'].split() 116 117 result = run(args.test, backends) 118 output = (os.path.join('build', args.test + '.junit') 119 if args.output is None 120 else args.output) 121 with open(output, 'w') as fd: 122 TestSuite.to_file(fd, [result]) 123 124