xref: /libCEED/tests/junit.py (revision 37e4ed59572999a330db22f79fa9d8dd87f87d55)
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':
131b685ca3Sjeth8984        return sum([[line.split()[1:]] for line in open(file).readlines()
141b685ca3Sjeth8984                    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']),
515dc4928cSjeremylt        test.startswith('nek') and contains_any(resource, ['occa']),
521da99368SJeremy L Thompson        test.startswith('t507') and contains_any(resource, ['occa']),
53c9f8acf2SJeremy L Thompson        test.startswith('t318') and contains_any(resource, ['magma']),
54c9f8acf2SJeremy L Thompson        test.startswith('t506') and contains_any(resource, ['magma']),
55b974e86eSJed Brown        ))
56b974e86eSJed Brown
578ec9d54bSJed Browndef run(test, backends):
588ec9d54bSJed Brown    import subprocess
598ec9d54bSJed Brown    import time
608ec9d54bSJed Brown    import difflib
61288c0443SJeremy L Thompson    allargs = get_testargs(test)
62288c0443SJeremy L Thompson
638ec9d54bSJed Brown    testcases = []
64288c0443SJeremy L Thompson    for args in allargs:
658ec9d54bSJed Brown        for ceed_resource in backends:
668ec9d54bSJed Brown            rargs = [os.path.join('build', test)] + args.copy()
678ec9d54bSJed Brown            rargs[rargs.index('{ceed_resource}')] = ceed_resource
68b974e86eSJed Brown
69b974e86eSJed Brown            if skip_rule(test, ceed_resource):
70b974e86eSJed Brown                case = TestCase('{} {}'.format(test, ceed_resource),
71b974e86eSJed Brown                                elapsed_sec=0,
72*37e4ed59SJed Brown                                timestamp=time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime()),
73b974e86eSJed Brown                                stdout='',
74b974e86eSJed Brown                                stderr='')
75b974e86eSJed Brown                case.add_skipped_info('Pre-run skip rule')
76b974e86eSJed Brown            else:
778ec9d54bSJed Brown                start = time.time()
788ec9d54bSJed Brown                proc = subprocess.run(rargs,
798ec9d54bSJed Brown                                      stdout=subprocess.PIPE,
8073132ccbSJed Brown                                      stderr=subprocess.PIPE)
8173132ccbSJed Brown                proc.stdout = proc.stdout.decode('utf-8')
8273132ccbSJed Brown                proc.stderr = proc.stderr.decode('utf-8')
838ec9d54bSJed Brown
849bcbe8bdSJed Brown                case = TestCase('{} {}'.format(test, ceed_resource),
858ec9d54bSJed Brown                                elapsed_sec=time.time()-start,
868ec9d54bSJed Brown                                timestamp=time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime(start)),
878ec9d54bSJed Brown                                stdout=proc.stdout,
888ec9d54bSJed Brown                                stderr=proc.stderr)
89288c0443SJeremy L Thompson                ref_stdout = os.path.join('tests/output', test + '.out')
90bdb0bdbbSJed Brown
91b974e86eSJed Brown            if not case.is_skipped() and proc.stderr:
928ec9d54bSJed Brown                if 'OCCA backend failed to use' in proc.stderr:
939bcbe8bdSJed Brown                    case.add_skipped_info('occa mode not supported {} {}'.format(test, ceed_resource))
948ec9d54bSJed Brown                elif 'Backend does not implement' in proc.stderr:
959bcbe8bdSJed Brown                    case.add_skipped_info('not implemented {} {}'.format(test, ceed_resource))
96cbac262cSjeremylt                elif 'Can only provide to HOST memory' in proc.stderr:
97cbac262cSjeremylt                    case.add_skipped_info('device memory not supported {} {}'.format(test, ceed_resource))
98bdb0bdbbSJed Brown
998ec9d54bSJed Brown            if not case.is_skipped():
1009f4513dfSjeremylt                if test[:4] in 't110 t111 t112 t113 t114'.split():
101bdb0bdbbSJed Brown                    check_required_failure(case, proc.stderr, 'Cannot grant CeedVector array access')
1029f4513dfSjeremylt                if test[:4] in 't115'.split():
1039f4513dfSjeremylt                    check_required_failure(case, proc.stderr, 'Cannot grant CeedVector read-only array access, the access lock is already in use')
1049f4513dfSjeremylt                if test[:4] in 't116'.split():
105187168c7SJeremy L Thompson                    check_required_failure(case, proc.stderr, 'Cannot destroy CeedVector, the writable access lock is in use')
1069f4513dfSjeremylt                if test[:4] in 't117'.split():
107bdb0bdbbSJed Brown                    check_required_failure(case, proc.stderr, 'Cannot restore CeedVector array access, access was not granted')
1089f4513dfSjeremylt                if test[:4] in 't118'.split():
1099f4513dfSjeremylt                    check_required_failure(case, proc.stderr, 'Cannot sync CeedVector, the access lock is already in use')
110430758c8SJeremy L Thompson                if test[:4] in 't215'.split():
111430758c8SJeremy L Thompson                    check_required_failure(case, proc.stderr, 'Cannot destroy CeedElemRestriction, a process has read access to the offset data')
11252bfb9bbSJeremy L Thompson                if test[:4] in 't303'.split():
113bdb0bdbbSJed Brown                    check_required_failure(case, proc.stderr, 'Length of input/output vectors incompatible with basis dimensions')
114bdb0bdbbSJed Brown
115bdb0bdbbSJed Brown            if not case.is_skipped() and not case.status:
116bdb0bdbbSJed Brown                if proc.stderr:
117bdb0bdbbSJed Brown                    case.add_failure_info('stderr', proc.stderr)
118bdb0bdbbSJed Brown                elif proc.returncode != 0:
1199bcbe8bdSJed Brown                    case.add_error_info('returncode = {}'.format(proc.returncode))
1208ec9d54bSJed Brown                elif os.path.isfile(ref_stdout):
1218ec9d54bSJed Brown                    with open(ref_stdout) as ref:
1228ec9d54bSJed Brown                        diff = list(difflib.unified_diff(ref.readlines(),
1238ec9d54bSJed Brown                                                         proc.stdout.splitlines(keepends=True),
1248ec9d54bSJed Brown                                                         fromfile=ref_stdout,
1258ec9d54bSJed Brown                                                         tofile='New'))
1268ec9d54bSJed Brown                    if diff:
1278ec9d54bSJed Brown                        case.add_failure_info('stdout', output=''.join(diff))
1280a0da059Sjeremylt                elif proc.stdout and test[:4] not in 't003':
1298ec9d54bSJed Brown                    case.add_failure_info('stdout', output=proc.stdout)
1308ec9d54bSJed Brown            testcases.append(case)
1318ec9d54bSJed Brown        return TestSuite(test, testcases)
1328ec9d54bSJed Brown
1338ec9d54bSJed Brownif __name__ == '__main__':
1348ec9d54bSJed Brown    import argparse
1358ec9d54bSJed Brown    parser = argparse.ArgumentParser('Test runner with JUnit output')
1368ec9d54bSJed Brown    parser.add_argument('--output', help='Output file to write test', default=None)
1378ec9d54bSJed Brown    parser.add_argument('--gather', help='Gather all *.junit files into XML', action='store_true')
1388ec9d54bSJed Brown    parser.add_argument('test', help='Test executable', nargs='?')
1398ec9d54bSJed Brown    args = parser.parse_args()
1408ec9d54bSJed Brown
1418ec9d54bSJed Brown    if args.gather:
1428ec9d54bSJed Brown        gather()
1438ec9d54bSJed Brown    else:
1448ec9d54bSJed Brown        backends = os.environ['BACKENDS'].split()
1458ec9d54bSJed Brown
1468ec9d54bSJed Brown        result = run(args.test, backends)
1478ec9d54bSJed Brown        output = (os.path.join('build', args.test + '.junit')
1488ec9d54bSJed Brown                  if args.output is None
1498ec9d54bSJed Brown                  else args.output)
1508ec9d54bSJed Brown        with open(output, 'w') as fd:
1518ec9d54bSJed Brown            TestSuite.to_file(fd, [result])
1524d57a9fcSJed Brown        for t in result.test_cases:
1534d57a9fcSJed Brown            failures = len([c for c in result.test_cases if c.is_failure()])
1544d57a9fcSJed Brown            errors = len([c for c in result.test_cases if c.is_error()])
1554d57a9fcSJed Brown            if failures + errors > 0:
1564d57a9fcSJed Brown                sys.exit(1)
157