xref: /libCEED/tests/junit.py (revision b7453713e95c1c6eb59ce174cbcb87227e92884e)
1#!/usr/bin/env python3
2from junit_common import *
3
4
5def create_argparser() -> argparse.ArgumentParser:
6    """Creates argument parser to read command line arguments
7
8    Returns:
9        argparse.ArgumentParser: Created `ArgumentParser`
10    """
11    parser = argparse.ArgumentParser('Test runner with JUnit and TAP output')
12    parser.add_argument('-c', '--ceed-backends', type=str, nargs='*', default=['/cpu/self'], help='libCEED backend to use with convergence tests')
13    parser.add_argument('-m', '--mode', type=RunMode, action=CaseInsensitiveEnumAction, help='Output mode, junit or tap', default=RunMode.JUNIT)
14    parser.add_argument('-n', '--nproc', type=int, default=1, help='number of MPI processes')
15    parser.add_argument('-o', '--output', type=Optional[Path], default=None, help='Output file to write test')
16    parser.add_argument('-b', '--junit-batch', type=str, default='', help='Name of JUnit batch for output file')
17    parser.add_argument('test', help='Test executable', nargs='?')
18
19    return parser
20
21
22# Necessary functions for running tests
23class CeedSuiteSpec(SuiteSpec):
24    def get_source_path(self, test: str) -> Path:
25        """Compute path to test source file
26
27        Args:
28            test (str): Name of test
29
30        Returns:
31            Path: Path to source file
32        """
33        prefix, rest = test.split('-', 1)
34        if prefix == 'petsc':
35            return (Path('examples') / 'petsc' / rest).with_suffix('.c')
36        elif prefix == 'mfem':
37            return (Path('examples') / 'mfem' / rest).with_suffix('.cpp')
38        elif prefix == 'nek':
39            return (Path('examples') / 'nek' / 'bps' / rest).with_suffix('.usr')
40        elif prefix == 'fluids':
41            return (Path('examples') / 'fluids' / rest).with_suffix('.c')
42        elif prefix == 'solids':
43            return (Path('examples') / 'solids' / rest).with_suffix('.c')
44        elif test.startswith('ex'):
45            return (Path('examples') / 'ceed' / test).with_suffix('.c')
46        elif test.endswith('-f'):
47            return (Path('tests') / test).with_suffix('.f90')
48        else:
49            return (Path('tests') / test).with_suffix('.c')
50
51    # get path to executable
52    def get_run_path(self, test: str) -> Path:
53        """Compute path to built test executable file
54
55        Args:
56            test (str): Name of test
57
58        Returns:
59            Path: Path to test executable
60        """
61        return Path('build') / test
62
63    def get_output_path(self, test: str, output_file: str) -> Path:
64        """Compute path to expected output file
65
66        Args:
67            test (str): Name of test
68            output_file (str): File name of output file
69
70        Returns:
71            Path: Path to expected output file
72        """
73        return Path('tests') / 'output' / output_file
74
75    def check_pre_skip(self, test: str, spec: TestSpec, resource: str, nproc: int) -> Optional[str]:
76        """Check if a test case should be skipped prior to running, returning the reason for skipping
77
78        Args:
79            test (str): Name of test
80            spec (TestSpec): Test case specification
81            resource (str): libCEED backend
82            nproc (int): Number of MPI processes to use when running test case
83
84        Returns:
85            Optional[str]: Skip reason, or `None` if test case should not be skipped
86        """
87        if contains_any(resource, ['occa']) and startswith_any(test, ['t4', 't5', 'ex', 'mfem', 'nek', 'petsc', 'fluids', 'solids']):
88            return 'OCCA mode not supported'
89        if test.startswith('t318') and contains_any(resource, ['/gpu/cuda/ref']):
90            return 'CUDA ref backend not supported'
91        if test.startswith('t506') and contains_any(resource, ['/gpu/cuda/shared']):
92            return 'CUDA shared backend not supported'
93
94    def check_post_skip(self, test: str, spec: TestSpec, resource: str, stderr: str) -> Optional[str]:
95        """Check if a test case should be allowed to fail, based on its stderr output
96
97        Args:
98            test (str): Name of test
99            spec (TestSpec): Test case specification
100            resource (str): libCEED backend
101            stderr (str): Standard error output from test case execution
102
103        Returns:
104            Optional[str]: Skip reason, or `None` if unexpeced error
105        """
106        if 'OCCA backend failed to use' in stderr:
107            return f'OCCA mode not supported'
108        elif 'Backend does not implement' in stderr:
109            return f'Backend does not implement'
110        elif 'Can only provide HOST memory for this backend' in stderr:
111            return f'Device memory not supported'
112        elif 'Can only set HOST memory for this backend' in stderr:
113            return f'Device memory not supported'
114        elif 'Test not implemented in single precision' in stderr:
115            return f'Test not implemented in single precision'
116        elif 'No SYCL devices of the requested type are available' in stderr:
117            return f'SYCL device type not available'
118        return None
119
120    def check_required_failure(self, test: str, spec: TestSpec, resource: str, stderr: str) -> tuple[str, bool]:
121        """Check whether a test case is expected to fail and if it failed expectedly
122
123        Args:
124            test (str): Name of test
125            spec (TestSpec): Test case specification
126            resource (str): libCEED backend
127            stderr (str): Standard error output from test case execution
128
129        Returns:
130            tuple[str, bool]: Tuple of the expected failure string and whether it was present in `stderr`
131        """
132        test_id: str = test[:4]
133        fail_str: str = ''
134        if test_id in ['t006', 't007']:
135            fail_str = 'No suitable backend:'
136        elif test_id in ['t008']:
137            fail_str = 'Available backend resources:'
138        elif test_id in ['t110', 't111', 't112', 't113', 't114']:
139            fail_str = 'Cannot grant CeedVector array access'
140        elif test_id in ['t115']:
141            fail_str = 'Cannot grant CeedVector read-only array access, the access lock is already in use'
142        elif test_id in ['t116']:
143            fail_str = 'Cannot destroy CeedVector, the writable access lock is in use'
144        elif test_id in ['t117']:
145            fail_str = 'Cannot restore CeedVector array access, access was not granted'
146        elif test_id in ['t118']:
147            fail_str = 'Cannot sync CeedVector, the access lock is already in use'
148        elif test_id in ['t215']:
149            fail_str = 'Cannot destroy CeedElemRestriction, a process has read access to the offset data'
150        elif test_id in ['t303']:
151            fail_str = 'Length of input/output vectors incompatible with basis dimensions'
152        elif test_id in ['t408']:
153            fail_str = 'CeedQFunctionContextGetData(): Cannot grant CeedQFunctionContext data access, a process has read access'
154        elif test_id in ['t409'] and contains_any(resource, ['memcheck']):
155            fail_str = 'Context data changed while accessed in read-only mode'
156
157        return fail_str, fail_str in stderr
158
159    def check_allowed_stdout(self, test: str) -> bool:
160        """Check whether a test is allowed to print console output
161
162        Args:
163            test (str): Name of test
164
165        Returns:
166            bool: True if the test is allowed to print console output
167        """
168        return test[:4] in ['t003']
169
170
171if __name__ == '__main__':
172    args = create_argparser().parse_args()
173
174    # run tests
175    result: TestSuite = run_tests(args.test, args.ceed_backends, args.mode, args.nproc, CeedSuiteSpec())
176
177    # write output and check for failures
178    if args.mode is RunMode.JUNIT:
179        write_junit_xml(result, args.output, args.junit_batch)
180        if has_failures(result):
181            sys.exit(1)
182