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