xref: /libCEED/tests/junit.py (revision ed9ed3dea34fef5853dd3f23fdf30a7807a29b2c)
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 == 'dealii':
54            return (Path('examples') / 'deal.II' / rest).with_suffix('.cc')
55        elif prefix == 'fluids':
56            return (Path('examples') / 'fluids' / rest).with_suffix('.c')
57        elif prefix == 'solids':
58            return (Path('examples') / 'solids' / rest).with_suffix('.c')
59        elif test.startswith('ex'):
60            return (Path('examples') / 'ceed' / test).with_suffix('.c')
61        elif test.endswith('-f'):
62            return (Path('tests') / test).with_suffix('.f90')
63        else:
64            return (Path('tests') / test).with_suffix('.c')
65
66    # get path to executable
67    def get_run_path(self, test: str) -> Path:
68        """Compute path to built test executable file
69
70        Args:
71            test (str): Name of test
72
73        Returns:
74            Path: Path to test executable
75        """
76        return Path('build') / test
77
78    def get_output_path(self, test: str, output_file: str) -> Path:
79        """Compute path to expected output file
80
81        Args:
82            test (str): Name of test
83            output_file (str): File name of output file
84
85        Returns:
86            Path: Path to expected output file
87        """
88        return Path('tests') / 'output' / output_file
89
90    def check_pre_skip(self, test: str, spec: TestSpec, resource: str, nproc: int) -> Optional[str]:
91        """Check if a test case should be skipped prior to running, returning the reason for skipping
92
93        Args:
94            test (str): Name of test
95            spec (TestSpec): Test case specification
96            resource (str): libCEED backend
97            nproc (int): Number of MPI processes to use when running test case
98
99        Returns:
100            Optional[str]: Skip reason, or `None` if test case should not be skipped
101        """
102        if contains_any(resource, ['occa']) and startswith_any(
103                test, ['t4', 't5', 'ex', 'mfem', 'nek', 'petsc', 'fluids', 'solids']):
104            return 'OCCA mode not supported'
105        if test.startswith('t318') and contains_any(resource, ['/gpu/cuda/ref']):
106            return 'CUDA ref backend not supported'
107        if test.startswith('t506') and contains_any(resource, ['/gpu/cuda/shared']):
108            return 'CUDA shared backend not supported'
109        for condition in spec.only:
110            if (condition == 'cpu') and ('gpu' in resource):
111                return 'CPU only test with GPU backend'
112
113    def check_post_skip(self, test: str, spec: TestSpec, resource: str, stderr: str) -> Optional[str]:
114        """Check if a test case should be allowed to fail, based on its stderr output
115
116        Args:
117            test (str): Name of test
118            spec (TestSpec): Test case specification
119            resource (str): libCEED backend
120            stderr (str): Standard error output from test case execution
121
122        Returns:
123            Optional[str]: Skip reason, or `None` if unexpeced error
124        """
125        if 'OCCA backend failed to use' in stderr:
126            return f'OCCA mode not supported'
127        elif 'Backend does not implement' in stderr:
128            return f'Backend does not implement'
129        elif 'Can only provide HOST memory for this backend' in stderr:
130            return f'Device memory not supported'
131        elif 'Can only set HOST memory for this backend' in stderr:
132            return f'Device memory not supported'
133        elif 'Test not implemented in single precision' in stderr:
134            return f'Test not implemented in single precision'
135        elif 'No SYCL devices of the requested type are available' in stderr:
136            return f'SYCL device type not available'
137        elif 'You may need to add --download-ctetgen or --download-tetgen' in stderr:
138            return f'Tet mesh generator not installed for {test}, {spec.name}'
139        return None
140
141    def check_required_failure(self, test: str, spec: TestSpec, resource: str, stderr: str) -> Tuple[str, bool]:
142        """Check whether a test case is expected to fail and if it failed expectedly
143
144        Args:
145            test (str): Name of test
146            spec (TestSpec): Test case specification
147            resource (str): libCEED backend
148            stderr (str): Standard error output from test case execution
149
150        Returns:
151            tuple[str, bool]: Tuple of the expected failure string and whether it was present in `stderr`
152        """
153        test_id: str = test[:4]
154        fail_str: str = ''
155        if test_id in ['t006', 't007']:
156            fail_str = 'No suitable backend:'
157        elif test_id in ['t008']:
158            fail_str = 'Available backend resources:'
159        elif test_id in ['t110', 't111', 't112', 't113', 't114']:
160            fail_str = 'Cannot grant CeedVector array access'
161        elif test_id in ['t115']:
162            fail_str = 'Cannot grant CeedVector read-only array access, the access lock is already in use'
163        elif test_id in ['t116']:
164            fail_str = 'Cannot destroy CeedVector, the writable access lock is in use'
165        elif test_id in ['t117']:
166            fail_str = 'Cannot restore CeedVector array access, access was not granted'
167        elif test_id in ['t118']:
168            fail_str = 'Cannot sync CeedVector, the access lock is already in use'
169        elif test_id in ['t215']:
170            fail_str = 'Cannot destroy CeedElemRestriction, a process has read access to the offset data'
171        elif test_id in ['t303']:
172            fail_str = 'Length of input/output vectors incompatible with basis dimensions'
173        elif test_id in ['t408']:
174            fail_str = 'CeedQFunctionContextGetData(): Cannot grant CeedQFunctionContext data access, a process has read access'
175        elif test_id in ['t409'] and contains_any(resource, ['memcheck']):
176            fail_str = 'Context data changed while accessed in read-only mode'
177
178        return fail_str, fail_str in stderr
179
180    def check_allowed_stdout(self, test: str) -> bool:
181        """Check whether a test is allowed to print console output
182
183        Args:
184            test (str): Name of test
185
186        Returns:
187            bool: True if the test is allowed to print console output
188        """
189        return test[:4] in ['t003']
190
191
192if __name__ == '__main__':
193    args = create_argparser().parse_args()
194
195    # run tests
196    if 'smartsim' in args.test:
197        sys.path.insert(0, str(Path(__file__).parents[1] / "examples" / "fluids"))
198        from smartsim_regression_framework import SmartSimTest
199
200        test_framework = SmartSimTest(Path(__file__).parent / 'test_dir')
201        test_framework.setup()
202        test_cases = []
203        print(f'1..1')
204        is_new_subtest = True
205        subtest_ok = True
206        for i, backend in enumerate(args.ceed_backends):
207            test_cases.append(test_framework.test_junit(backend))
208            if is_new_subtest and args.mode == RunMode.TAP:
209                is_new_subtest = False
210                print(f'# Subtest: {test_cases[0].category}')
211                print(f'    1..{len(args.ceed_backends)}')
212            print(test_case_output_string(test_cases[i], TestSpec("SmartSim Tests"), args.mode, backend, '', i))
213        if args.mode == RunMode.TAP:
214            print(f'{"" if subtest_ok else "not "}ok 1 - {test_cases[0].category}')
215        test_framework.teardown()
216        result: TestSuite = TestSuite('SmartSim Tests', test_cases)
217    else:
218        result: TestSuite = run_tests(
219            args.test,
220            args.ceed_backends,
221            args.mode,
222            args.nproc,
223            CeedSuiteSpec(),
224            args.pool_size)
225
226    # write output and check for failures
227    if args.mode is RunMode.JUNIT:
228        write_junit_xml(result, args.output, args.junit_batch)
229        if has_failures(result):
230            sys.exit(1)
231