xref: /libCEED/tests/junit.py (revision c9f8acf23fad434515654d1dfdd3a5c4d5c61abb)
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']),
511da99368SJeremy L Thompson        test.startswith('t507') and contains_any(resource, ['occa']),
52*c9f8acf2SJeremy L Thompson        test.startswith('t318') and contains_any(resource, ['magma']),
53*c9f8acf2SJeremy L Thompson        test.startswith('t506') and contains_any(resource, ['magma']),
54b974e86eSJed Brown        ))
55b974e86eSJed Brown
568ec9d54bSJed Browndef run(test, backends):
578ec9d54bSJed Brown    import subprocess
588ec9d54bSJed Brown    import time
598ec9d54bSJed Brown    import difflib
60288c0443SJeremy L Thompson    allargs = get_testargs(test)
61288c0443SJeremy L Thompson
628ec9d54bSJed Brown    testcases = []
63288c0443SJeremy L Thompson    for args in allargs:
648ec9d54bSJed Brown        for ceed_resource in backends:
658ec9d54bSJed Brown            rargs = [os.path.join('build', test)] + args.copy()
668ec9d54bSJed Brown            rargs[rargs.index('{ceed_resource}')] = ceed_resource
67b974e86eSJed Brown
68b974e86eSJed Brown            if skip_rule(test, ceed_resource):
69b974e86eSJed Brown                case = TestCase('{} {}'.format(test, ceed_resource),
70b974e86eSJed Brown                                elapsed_sec=0,
71b974e86eSJed Brown                                timestamp=time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime(start)),
72b974e86eSJed Brown                                stdout='',
73b974e86eSJed Brown                                stderr='')
74b974e86eSJed Brown                case.add_skipped_info('Pre-run skip rule')
75b974e86eSJed Brown            else:
768ec9d54bSJed Brown                start = time.time()
778ec9d54bSJed Brown                proc = subprocess.run(rargs,
788ec9d54bSJed Brown                                      stdout=subprocess.PIPE,
7973132ccbSJed Brown                                      stderr=subprocess.PIPE)
8073132ccbSJed Brown                proc.stdout = proc.stdout.decode('utf-8')
8173132ccbSJed Brown                proc.stderr = proc.stderr.decode('utf-8')
828ec9d54bSJed Brown
839bcbe8bdSJed Brown                case = TestCase('{} {}'.format(test, ceed_resource),
848ec9d54bSJed Brown                                elapsed_sec=time.time()-start,
858ec9d54bSJed Brown                                timestamp=time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime(start)),
868ec9d54bSJed Brown                                stdout=proc.stdout,
878ec9d54bSJed Brown                                stderr=proc.stderr)
88288c0443SJeremy L Thompson                ref_stdout = os.path.join('tests/output', test + '.out')
89bdb0bdbbSJed Brown
90b974e86eSJed Brown            if not case.is_skipped() and proc.stderr:
918ec9d54bSJed Brown                if 'OCCA backend failed to use' in proc.stderr:
929bcbe8bdSJed Brown                    case.add_skipped_info('occa mode not supported {} {}'.format(test, ceed_resource))
938ec9d54bSJed Brown                elif 'Backend does not implement' in proc.stderr:
949bcbe8bdSJed Brown                    case.add_skipped_info('not implemented {} {}'.format(test, ceed_resource))
95cbac262cSjeremylt                elif 'Can only provide to HOST memory' in proc.stderr:
96cbac262cSjeremylt                    case.add_skipped_info('device memory not supported {} {}'.format(test, ceed_resource))
97bdb0bdbbSJed Brown
988ec9d54bSJed Brown            if not case.is_skipped():
999f4513dfSjeremylt                if test[:4] in 't110 t111 t112 t113 t114'.split():
100bdb0bdbbSJed Brown                    check_required_failure(case, proc.stderr, 'Cannot grant CeedVector array access')
1019f4513dfSjeremylt                if test[:4] in 't115'.split():
1029f4513dfSjeremylt                    check_required_failure(case, proc.stderr, 'Cannot grant CeedVector read-only array access, the access lock is already in use')
1039f4513dfSjeremylt                if test[:4] in 't116'.split():
104187168c7SJeremy L Thompson                    check_required_failure(case, proc.stderr, 'Cannot destroy CeedVector, the writable access lock is in use')
1059f4513dfSjeremylt                if test[:4] in 't117'.split():
106bdb0bdbbSJed Brown                    check_required_failure(case, proc.stderr, 'Cannot restore CeedVector array access, access was not granted')
1079f4513dfSjeremylt                if test[:4] in 't118'.split():
1089f4513dfSjeremylt                    check_required_failure(case, proc.stderr, 'Cannot sync CeedVector, the access lock is already in use')
109430758c8SJeremy L Thompson                if test[:4] in 't215'.split():
110430758c8SJeremy L Thompson                    check_required_failure(case, proc.stderr, 'Cannot destroy CeedElemRestriction, a process has read access to the offset data')
11152bfb9bbSJeremy L Thompson                if test[:4] in 't303'.split():
112bdb0bdbbSJed Brown                    check_required_failure(case, proc.stderr, 'Length of input/output vectors incompatible with basis dimensions')
113bdb0bdbbSJed Brown
114bdb0bdbbSJed Brown            if not case.is_skipped() and not case.status:
115bdb0bdbbSJed Brown                if proc.stderr:
116bdb0bdbbSJed Brown                    case.add_failure_info('stderr', proc.stderr)
117bdb0bdbbSJed Brown                elif proc.returncode != 0:
1189bcbe8bdSJed Brown                    case.add_error_info('returncode = {}'.format(proc.returncode))
1198ec9d54bSJed Brown                elif os.path.isfile(ref_stdout):
1208ec9d54bSJed Brown                    with open(ref_stdout) as ref:
1218ec9d54bSJed Brown                        diff = list(difflib.unified_diff(ref.readlines(),
1228ec9d54bSJed Brown                                                         proc.stdout.splitlines(keepends=True),
1238ec9d54bSJed Brown                                                         fromfile=ref_stdout,
1248ec9d54bSJed Brown                                                         tofile='New'))
1258ec9d54bSJed Brown                    if diff:
1268ec9d54bSJed Brown                        case.add_failure_info('stdout', output=''.join(diff))
1270a0da059Sjeremylt                elif proc.stdout and test[:4] not in 't003':
1288ec9d54bSJed Brown                    case.add_failure_info('stdout', output=proc.stdout)
1298ec9d54bSJed Brown            testcases.append(case)
1308ec9d54bSJed Brown        return TestSuite(test, testcases)
1318ec9d54bSJed Brown
1328ec9d54bSJed Brownif __name__ == '__main__':
1338ec9d54bSJed Brown    import argparse
1348ec9d54bSJed Brown    parser = argparse.ArgumentParser('Test runner with JUnit output')
1358ec9d54bSJed Brown    parser.add_argument('--output', help='Output file to write test', default=None)
1368ec9d54bSJed Brown    parser.add_argument('--gather', help='Gather all *.junit files into XML', action='store_true')
1378ec9d54bSJed Brown    parser.add_argument('test', help='Test executable', nargs='?')
1388ec9d54bSJed Brown    args = parser.parse_args()
1398ec9d54bSJed Brown
1408ec9d54bSJed Brown    if args.gather:
1418ec9d54bSJed Brown        gather()
1428ec9d54bSJed Brown    else:
1438ec9d54bSJed Brown        backends = os.environ['BACKENDS'].split()
1448ec9d54bSJed Brown
1458ec9d54bSJed Brown        result = run(args.test, backends)
1468ec9d54bSJed Brown        output = (os.path.join('build', args.test + '.junit')
1478ec9d54bSJed Brown                  if args.output is None
1488ec9d54bSJed Brown                  else args.output)
1498ec9d54bSJed Brown        with open(output, 'w') as fd:
1508ec9d54bSJed Brown            TestSuite.to_file(fd, [result])
1514d57a9fcSJed Brown        for t in result.test_cases:
1524d57a9fcSJed Brown            failures = len([c for c in result.test_cases if c.is_failure()])
1534d57a9fcSJed Brown            errors = len([c for c in result.test_cases if c.is_error()])
1544d57a9fcSJed Brown            if failures + errors > 0:
1554d57a9fcSJed Brown                sys.exit(1)
156