xref: /libCEED/tests/junit.py (revision 4d57a9fc2ce3337fb4f245e2070ef9aaced7ec24)
18ec9d54bSJed Brown#!/usr/bin/env python3
28ec9d54bSJed Brown
38ec9d54bSJed Brownimport os
48ec9d54bSJed Brownimport sys
58ec9d54bSJed Brownsys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'junit-xml')))
68ec9d54bSJed Brownfrom junit_xml import TestCase, TestSuite
78ec9d54bSJed Brown
88ec9d54bSJed Browndef parse_testargs(file):
98ec9d54bSJed Brown    if os.path.splitext(file)[1] in ['.c', '.cpp']:
10288c0443SJeremy L Thompson        return sum([[line.split()[1:]] for line in open(file).readlines()
118ec9d54bSJed Brown                    if line.startswith('//TESTARGS')], [])
128ec9d54bSJed Brown    elif os.path.splitext(file)[1] == '.usr':
13288c0443SJeremy L Thompson        return sum([[line.split()[2:]] for line in open(file).readlines()
14288c0443SJeremy L Thompson                    if line.startswith('C TESTARGS')])
159bcbe8bdSJed Brown    raise RuntimeError('Unrecognized extension for file: {}'.format(file))
168ec9d54bSJed Brown
178ec9d54bSJed Browndef get_source(test):
188ec9d54bSJed Brown    if test.startswith('petsc-'):
198ec9d54bSJed Brown        return os.path.join('examples', 'petsc', test[6:] + '.c')
208ec9d54bSJed Brown    elif test.startswith('mfem-'):
218ec9d54bSJed Brown        return os.path.join('examples', 'mfem', test[5:] + '.cpp')
228ec9d54bSJed Brown    elif test.startswith('nek-'):
2386a4271fSThilina Rathnayake        return os.path.join('examples', 'nek', 'bps', test[4:] + '.usr')
24ccaff030SJeremy L Thompson    elif test.startswith('fluids-'):
25ccaff030SJeremy L Thompson        return os.path.join('examples', 'fluids', test[7:] + '.c')
26ccaff030SJeremy L Thompson    elif test.startswith('solids-'):
27ccaff030SJeremy L Thompson        return os.path.join('examples', 'solids', test[7:] + '.c')
288ec9d54bSJed Brown    elif test.startswith('ex'):
298ec9d54bSJed Brown        return os.path.join('examples', 'ceed', test + '.c')
308ec9d54bSJed Brown
318ec9d54bSJed Browndef get_testargs(test):
328ec9d54bSJed Brown    source = get_source(test)
338ec9d54bSJed Brown    if source is None:
34288c0443SJeremy L Thompson        return [['{ceed_resource}']]
358ec9d54bSJed Brown    return parse_testargs(source)
368ec9d54bSJed Brown
37bdb0bdbbSJed Browndef check_required_failure(case, stderr, required):
38bdb0bdbbSJed Brown    if required in stderr:
39bdb0bdbbSJed Brown        case.status = 'fails with required: {}'.format(required)
40bdb0bdbbSJed Brown    else:
41bdb0bdbbSJed Brown        case.add_failure_info('required: {}'.format(required))
42bdb0bdbbSJed Brown
43b974e86eSJed Browndef contains_any(resource, substrings):
44b974e86eSJed Brown    return any((sub in resource for sub in substrings))
45b974e86eSJed Brown
46b974e86eSJed Browndef skip_rule(test, resource):
47b974e86eSJed Brown    return any((
48ccaff030SJeremy L Thompson        test.startswith('fluids-') and contains_any(resource, ['occa', 'gpu']) and not contains_any(resource, ['/gpu/cuda/gen']),
49ccaff030SJeremy L Thompson        test.startswith('solids-') and contains_any(resource, ['occa']),
50b974e86eSJed Brown        test.startswith('petsc-multigrid') and contains_any(resource, ['occa']),
51f2f4e060Sjeremylt        test.startswith('t506') and contains_any(resource, ['occa']),
521da99368SJeremy L Thompson        test.startswith('t507') and contains_any(resource, ['occa']),
53b974e86eSJed Brown        ))
54b974e86eSJed Brown
558ec9d54bSJed Browndef run(test, backends):
568ec9d54bSJed Brown    import subprocess
578ec9d54bSJed Brown    import time
588ec9d54bSJed Brown    import difflib
59288c0443SJeremy L Thompson    allargs = get_testargs(test)
60288c0443SJeremy L Thompson
618ec9d54bSJed Brown    testcases = []
62288c0443SJeremy L Thompson    for args in allargs:
638ec9d54bSJed Brown        for ceed_resource in backends:
648ec9d54bSJed Brown            rargs = [os.path.join('build', test)] + args.copy()
658ec9d54bSJed Brown            rargs[rargs.index('{ceed_resource}')] = ceed_resource
66b974e86eSJed Brown
67b974e86eSJed Brown            if skip_rule(test, ceed_resource):
68b974e86eSJed Brown                case = TestCase('{} {}'.format(test, ceed_resource),
69b974e86eSJed Brown                                elapsed_sec=0,
70b974e86eSJed Brown                                timestamp=time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime(start)),
71b974e86eSJed Brown                                stdout='',
72b974e86eSJed Brown                                stderr='')
73b974e86eSJed Brown                case.add_skipped_info('Pre-run skip rule')
74b974e86eSJed Brown            else:
758ec9d54bSJed Brown                start = time.time()
768ec9d54bSJed Brown                proc = subprocess.run(rargs,
778ec9d54bSJed Brown                                      stdout=subprocess.PIPE,
7873132ccbSJed Brown                                      stderr=subprocess.PIPE)
7973132ccbSJed Brown                proc.stdout = proc.stdout.decode('utf-8')
8073132ccbSJed Brown                proc.stderr = proc.stderr.decode('utf-8')
818ec9d54bSJed Brown
829bcbe8bdSJed Brown                case = TestCase('{} {}'.format(test, ceed_resource),
838ec9d54bSJed Brown                                elapsed_sec=time.time()-start,
848ec9d54bSJed Brown                                timestamp=time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime(start)),
858ec9d54bSJed Brown                                stdout=proc.stdout,
868ec9d54bSJed Brown                                stderr=proc.stderr)
87288c0443SJeremy L Thompson                ref_stdout = os.path.join('tests/output', test + '.out')
88bdb0bdbbSJed Brown
89b974e86eSJed Brown            if not case.is_skipped() and proc.stderr:
908ec9d54bSJed Brown                if 'OCCA backend failed to use' in proc.stderr:
919bcbe8bdSJed Brown                    case.add_skipped_info('occa mode not supported {} {}'.format(test, ceed_resource))
928ec9d54bSJed Brown                elif 'Backend does not implement' in proc.stderr:
939bcbe8bdSJed Brown                    case.add_skipped_info('not implemented {} {}'.format(test, ceed_resource))
94cbac262cSjeremylt                elif 'Can only provide to HOST memory' in proc.stderr:
95cbac262cSjeremylt                    case.add_skipped_info('device memory not supported {} {}'.format(test, ceed_resource))
96bdb0bdbbSJed Brown
978ec9d54bSJed Brown            if not case.is_skipped():
989f4513dfSjeremylt                if test[:4] in 't110 t111 t112 t113 t114'.split():
99bdb0bdbbSJed Brown                    check_required_failure(case, proc.stderr, 'Cannot grant CeedVector array access')
1009f4513dfSjeremylt                if test[:4] in 't115'.split():
1019f4513dfSjeremylt                    check_required_failure(case, proc.stderr, 'Cannot grant CeedVector read-only array access, the access lock is already in use')
1029f4513dfSjeremylt                if test[:4] in 't116'.split():
103bdb0bdbbSJed Brown                    check_required_failure(case, proc.stderr, 'Cannot destroy CeedVector, the access lock is in use')
1049f4513dfSjeremylt                if test[:4] in 't117'.split():
105bdb0bdbbSJed Brown                    check_required_failure(case, proc.stderr, 'Cannot restore CeedVector array access, access was not granted')
1069f4513dfSjeremylt                if test[:4] in 't118'.split():
1079f4513dfSjeremylt                    check_required_failure(case, proc.stderr, 'Cannot sync CeedVector, the access lock is already in use')
108430758c8SJeremy L Thompson                if test[:4] in 't215'.split():
109430758c8SJeremy L Thompson                    check_required_failure(case, proc.stderr, 'Cannot destroy CeedElemRestriction, a process has read access to the offset data')
11052bfb9bbSJeremy L Thompson                if test[:4] in 't303'.split():
111bdb0bdbbSJed Brown                    check_required_failure(case, proc.stderr, 'Length of input/output vectors incompatible with basis dimensions')
112bdb0bdbbSJed Brown
113bdb0bdbbSJed Brown            if not case.is_skipped() and not case.status:
114bdb0bdbbSJed Brown                if proc.stderr:
115bdb0bdbbSJed Brown                    case.add_failure_info('stderr', proc.stderr)
116bdb0bdbbSJed Brown                elif proc.returncode != 0:
1179bcbe8bdSJed Brown                    case.add_error_info('returncode = {}'.format(proc.returncode))
1188ec9d54bSJed Brown                elif os.path.isfile(ref_stdout):
1198ec9d54bSJed Brown                    with open(ref_stdout) as ref:
1208ec9d54bSJed Brown                        diff = list(difflib.unified_diff(ref.readlines(),
1218ec9d54bSJed Brown                                                         proc.stdout.splitlines(keepends=True),
1228ec9d54bSJed Brown                                                         fromfile=ref_stdout,
1238ec9d54bSJed Brown                                                         tofile='New'))
1248ec9d54bSJed Brown                    if diff:
1258ec9d54bSJed Brown                        case.add_failure_info('stdout', output=''.join(diff))
1260a0da059Sjeremylt                elif proc.stdout and test[:4] not in 't003':
1278ec9d54bSJed Brown                    case.add_failure_info('stdout', output=proc.stdout)
1288ec9d54bSJed Brown            testcases.append(case)
1298ec9d54bSJed Brown        return TestSuite(test, testcases)
1308ec9d54bSJed Brown
1318ec9d54bSJed Brownif __name__ == '__main__':
1328ec9d54bSJed Brown    import argparse
1338ec9d54bSJed Brown    parser = argparse.ArgumentParser('Test runner with JUnit output')
1348ec9d54bSJed Brown    parser.add_argument('--output', help='Output file to write test', default=None)
1358ec9d54bSJed Brown    parser.add_argument('--gather', help='Gather all *.junit files into XML', action='store_true')
1368ec9d54bSJed Brown    parser.add_argument('test', help='Test executable', nargs='?')
1378ec9d54bSJed Brown    args = parser.parse_args()
1388ec9d54bSJed Brown
1398ec9d54bSJed Brown    if args.gather:
1408ec9d54bSJed Brown        gather()
1418ec9d54bSJed Brown    else:
1428ec9d54bSJed Brown        backends = os.environ['BACKENDS'].split()
1438ec9d54bSJed Brown
1448ec9d54bSJed Brown        result = run(args.test, backends)
1458ec9d54bSJed Brown        output = (os.path.join('build', args.test + '.junit')
1468ec9d54bSJed Brown                  if args.output is None
1478ec9d54bSJed Brown                  else args.output)
1488ec9d54bSJed Brown        with open(output, 'w') as fd:
1498ec9d54bSJed Brown            TestSuite.to_file(fd, [result])
150*4d57a9fcSJed Brown        for t in result.test_cases:
151*4d57a9fcSJed Brown            failures = len([c for c in result.test_cases if c.is_failure()])
152*4d57a9fcSJed Brown            errors = len([c for c in result.test_cases if c.is_error()])
153*4d57a9fcSJed Brown            if failures + errors > 0:
154*4d57a9fcSJed Brown                sys.exit(1)
155