xref: /libCEED/tests/junit.py (revision 4d00b080eb3f95d2e04e55c0ff369c5c847bb288)
18ec9d54bSJed Brown#!/usr/bin/env python3
21b16049aSZach Atkinsfrom junit_common import *
38ec9d54bSJed Brown
43d94f746Srezgarshakeri
51b16049aSZach Atkinsdef create_argparser() -> argparse.ArgumentParser:
61b16049aSZach Atkins    """Creates argument parser to read command line arguments
71b16049aSZach Atkins
81b16049aSZach Atkins    Returns:
91b16049aSZach Atkins        argparse.ArgumentParser: Created `ArgumentParser`
101b16049aSZach Atkins    """
111b16049aSZach Atkins    parser = argparse.ArgumentParser('Test runner with JUnit and TAP output')
122fee3251SSebastian Grimberg    parser.add_argument(
132fee3251SSebastian Grimberg        '-c',
142fee3251SSebastian Grimberg        '--ceed-backends',
152fee3251SSebastian Grimberg        type=str,
162fee3251SSebastian Grimberg        nargs='*',
172fee3251SSebastian Grimberg        default=['/cpu/self'],
182fee3251SSebastian Grimberg        help='libCEED backend to use with convergence tests')
192fee3251SSebastian Grimberg    parser.add_argument(
202fee3251SSebastian Grimberg        '-m',
212fee3251SSebastian Grimberg        '--mode',
222fee3251SSebastian Grimberg        type=RunMode,
232fee3251SSebastian Grimberg        action=CaseInsensitiveEnumAction,
242fee3251SSebastian Grimberg        help='Output mode, junit or tap',
252fee3251SSebastian Grimberg        default=RunMode.JUNIT)
261b16049aSZach Atkins    parser.add_argument('-n', '--nproc', type=int, default=1, help='number of MPI processes')
271b16049aSZach Atkins    parser.add_argument('-o', '--output', type=Optional[Path], default=None, help='Output file to write test')
281b16049aSZach Atkins    parser.add_argument('-b', '--junit-batch', type=str, default='', help='Name of JUnit batch for output file')
2919868e18SZach Atkins    parser.add_argument('-np', '--pool-size', type=int, default=1, help='Number of test cases to run in parallel')
30*4d00b080SJeremy L Thompson    parser.add_argument('-s', '--smartredis_dir', type=str, default='', help='path to SmartSim library, if present')
311b16049aSZach Atkins    parser.add_argument('test', help='Test executable', nargs='?')
321b16049aSZach Atkins
331b16049aSZach Atkins    return parser
348ec9d54bSJed Brown
353d94f746Srezgarshakeri
361b16049aSZach Atkins# Necessary functions for running tests
371b16049aSZach Atkinsclass CeedSuiteSpec(SuiteSpec):
381b16049aSZach Atkins    def get_source_path(self, test: str) -> Path:
391b16049aSZach Atkins        """Compute path to test source file
408ec9d54bSJed Brown
411b16049aSZach Atkins        Args:
421b16049aSZach Atkins            test (str): Name of test
433d94f746Srezgarshakeri
441b16049aSZach Atkins        Returns:
451b16049aSZach Atkins            Path: Path to source file
461b16049aSZach Atkins        """
47372821a4SZach Atkins        prefix, rest = test.split('-', 1)
48372821a4SZach Atkins        if prefix == 'petsc':
49372821a4SZach Atkins            return (Path('examples') / 'petsc' / rest).with_suffix('.c')
50372821a4SZach Atkins        elif prefix == 'mfem':
51372821a4SZach Atkins            return (Path('examples') / 'mfem' / rest).with_suffix('.cpp')
52372821a4SZach Atkins        elif prefix == 'nek':
53372821a4SZach Atkins            return (Path('examples') / 'nek' / 'bps' / rest).with_suffix('.usr')
548c81f8b0SPeter Munch        elif prefix == 'dealii':
558c81f8b0SPeter Munch            return (Path('examples') / 'deal.II' / rest).with_suffix('.cc')
56372821a4SZach Atkins        elif prefix == 'fluids':
57372821a4SZach Atkins            return (Path('examples') / 'fluids' / rest).with_suffix('.c')
58372821a4SZach Atkins        elif prefix == 'solids':
59372821a4SZach Atkins            return (Path('examples') / 'solids' / rest).with_suffix('.c')
60372821a4SZach Atkins        elif test.startswith('ex'):
61372821a4SZach Atkins            return (Path('examples') / 'ceed' / test).with_suffix('.c')
62372821a4SZach Atkins        elif test.endswith('-f'):
63372821a4SZach Atkins            return (Path('tests') / test).with_suffix('.f90')
64372821a4SZach Atkins        else:
65372821a4SZach Atkins            return (Path('tests') / test).with_suffix('.c')
66372821a4SZach Atkins
671b16049aSZach Atkins    # get path to executable
681b16049aSZach Atkins    def get_run_path(self, test: str) -> Path:
691b16049aSZach Atkins        """Compute path to built test executable file
70372821a4SZach Atkins
711b16049aSZach Atkins        Args:
721b16049aSZach Atkins            test (str): Name of test
73bdb0bdbbSJed Brown
741b16049aSZach Atkins        Returns:
751b16049aSZach Atkins            Path: Path to test executable
761b16049aSZach Atkins        """
771b16049aSZach Atkins        return Path('build') / test
783d94f746Srezgarshakeri
791b16049aSZach Atkins    def get_output_path(self, test: str, output_file: str) -> Path:
801b16049aSZach Atkins        """Compute path to expected output file
81b974e86eSJed Brown
821b16049aSZach Atkins        Args:
831b16049aSZach Atkins            test (str): Name of test
841b16049aSZach Atkins            output_file (str): File name of output file
853d94f746Srezgarshakeri
861b16049aSZach Atkins        Returns:
871b16049aSZach Atkins            Path: Path to expected output file
881b16049aSZach Atkins        """
891b16049aSZach Atkins        return Path('tests') / 'output' / output_file
90b974e86eSJed Brown
911b16049aSZach Atkins    def check_pre_skip(self, test: str, spec: TestSpec, resource: str, nproc: int) -> Optional[str]:
921b16049aSZach Atkins        """Check if a test case should be skipped prior to running, returning the reason for skipping
933d94f746Srezgarshakeri
941b16049aSZach Atkins        Args:
951b16049aSZach Atkins            test (str): Name of test
961b16049aSZach Atkins            spec (TestSpec): Test case specification
971b16049aSZach Atkins            resource (str): libCEED backend
981b16049aSZach Atkins            nproc (int): Number of MPI processes to use when running test case
99288c0443SJeremy L Thompson
1001b16049aSZach Atkins        Returns:
1011b16049aSZach Atkins            Optional[str]: Skip reason, or `None` if test case should not be skipped
1021b16049aSZach Atkins        """
1032fee3251SSebastian Grimberg        if contains_any(resource, ['occa']) and startswith_any(
1042fee3251SSebastian Grimberg                test, ['t4', 't5', 'ex', 'mfem', 'nek', 'petsc', 'fluids', 'solids']):
1051b16049aSZach Atkins            return 'OCCA mode not supported'
1061b16049aSZach Atkins        if test.startswith('t318') and contains_any(resource, ['/gpu/cuda/ref']):
1071b16049aSZach Atkins            return 'CUDA ref backend not supported'
1081b16049aSZach Atkins        if test.startswith('t506') and contains_any(resource, ['/gpu/cuda/shared']):
1091b16049aSZach Atkins            return 'CUDA shared backend not supported'
110f85e4a7bSJeremy L Thompson        for condition in spec.only:
111f85e4a7bSJeremy L Thompson            if (condition == 'cpu') and ('gpu' in resource):
112f85e4a7bSJeremy L Thompson                return 'CPU only test with GPU backend'
1134a2fcf2fSJeremy L Thompson
1141b16049aSZach Atkins    def check_post_skip(self, test: str, spec: TestSpec, resource: str, stderr: str) -> Optional[str]:
1151b16049aSZach Atkins        """Check if a test case should be allowed to fail, based on its stderr output
116b974e86eSJed Brown
1171b16049aSZach Atkins        Args:
1181b16049aSZach Atkins            test (str): Name of test
1191b16049aSZach Atkins            spec (TestSpec): Test case specification
1201b16049aSZach Atkins            resource (str): libCEED backend
1211b16049aSZach Atkins            stderr (str): Standard error output from test case execution
1228ec9d54bSJed Brown
1231b16049aSZach Atkins        Returns:
1241b16049aSZach Atkins            Optional[str]: Skip reason, or `None` if unexpeced error
1251b16049aSZach Atkins        """
1261b16049aSZach Atkins        if 'OCCA backend failed to use' in stderr:
1271b16049aSZach Atkins            return f'OCCA mode not supported'
1281b16049aSZach Atkins        elif 'Backend does not implement' in stderr:
1291b16049aSZach Atkins            return f'Backend does not implement'
1301b16049aSZach Atkins        elif 'Can only provide HOST memory for this backend' in stderr:
1311b16049aSZach Atkins            return f'Device memory not supported'
132b0976d5aSZach Atkins        elif 'Can only set HOST memory for this backend' in stderr:
133b0976d5aSZach Atkins            return f'Device memory not supported'
1341b16049aSZach Atkins        elif 'Test not implemented in single precision' in stderr:
1351b16049aSZach Atkins            return f'Test not implemented in single precision'
1361b16049aSZach Atkins        elif 'No SYCL devices of the requested type are available' in stderr:
1371b16049aSZach Atkins            return f'SYCL device type not available'
138d26b3214SJeremy L Thompson        elif 'You may need to add --download-ctetgen or --download-tetgen' in stderr:
139d26b3214SJeremy L Thompson            return f'Tet mesh generator not installed for {test}, {spec.name}'
1401b16049aSZach Atkins        return None
141bdb0bdbbSJed Brown
14278cb100bSJames Wright    def check_required_failure(self, test: str, spec: TestSpec, resource: str, stderr: str) -> Tuple[str, bool]:
1431b16049aSZach Atkins        """Check whether a test case is expected to fail and if it failed expectedly
144bdb0bdbbSJed Brown
1451b16049aSZach Atkins        Args:
1461b16049aSZach Atkins            test (str): Name of test
1471b16049aSZach Atkins            spec (TestSpec): Test case specification
1481b16049aSZach Atkins            resource (str): libCEED backend
1491b16049aSZach Atkins            stderr (str): Standard error output from test case execution
150bdb0bdbbSJed Brown
1511b16049aSZach Atkins        Returns:
1521b16049aSZach Atkins            tuple[str, bool]: Tuple of the expected failure string and whether it was present in `stderr`
1531b16049aSZach Atkins        """
1541b16049aSZach Atkins        test_id: str = test[:4]
1551b16049aSZach Atkins        fail_str: str = ''
1561b16049aSZach Atkins        if test_id in ['t006', 't007']:
1571b16049aSZach Atkins            fail_str = 'No suitable backend:'
1581b16049aSZach Atkins        elif test_id in ['t008']:
1591b16049aSZach Atkins            fail_str = 'Available backend resources:'
1601b16049aSZach Atkins        elif test_id in ['t110', 't111', 't112', 't113', 't114']:
1611b16049aSZach Atkins            fail_str = 'Cannot grant CeedVector array access'
1621b16049aSZach Atkins        elif test_id in ['t115']:
1631b16049aSZach Atkins            fail_str = 'Cannot grant CeedVector read-only array access, the access lock is already in use'
1641b16049aSZach Atkins        elif test_id in ['t116']:
1651b16049aSZach Atkins            fail_str = 'Cannot destroy CeedVector, the writable access lock is in use'
1661b16049aSZach Atkins        elif test_id in ['t117']:
1671b16049aSZach Atkins            fail_str = 'Cannot restore CeedVector array access, access was not granted'
1681b16049aSZach Atkins        elif test_id in ['t118']:
1691b16049aSZach Atkins            fail_str = 'Cannot sync CeedVector, the access lock is already in use'
1701b16049aSZach Atkins        elif test_id in ['t215']:
1711b16049aSZach Atkins            fail_str = 'Cannot destroy CeedElemRestriction, a process has read access to the offset data'
1721b16049aSZach Atkins        elif test_id in ['t303']:
1731b16049aSZach Atkins            fail_str = 'Length of input/output vectors incompatible with basis dimensions'
1741b16049aSZach Atkins        elif test_id in ['t408']:
1751b16049aSZach Atkins            fail_str = 'CeedQFunctionContextGetData(): Cannot grant CeedQFunctionContext data access, a process has read access'
1761b16049aSZach Atkins        elif test_id in ['t409'] and contains_any(resource, ['memcheck']):
1771b16049aSZach Atkins            fail_str = 'Context data changed while accessed in read-only mode'
1784a2fcf2fSJeremy L Thompson
1791b16049aSZach Atkins        return fail_str, fail_str in stderr
1804a2fcf2fSJeremy L Thompson
1811b16049aSZach Atkins    def check_allowed_stdout(self, test: str) -> bool:
1821b16049aSZach Atkins        """Check whether a test is allowed to print console output
1834a2fcf2fSJeremy L Thompson
1841b16049aSZach Atkins        Args:
1851b16049aSZach Atkins            test (str): Name of test
1861b16049aSZach Atkins
1871b16049aSZach Atkins        Returns:
1881b16049aSZach Atkins            bool: True if the test is allowed to print console output
1891b16049aSZach Atkins        """
1901b16049aSZach Atkins        return test[:4] in ['t003']
1911b16049aSZach Atkins
1928ec9d54bSJed Brown
1938ec9d54bSJed Brownif __name__ == '__main__':
1941b16049aSZach Atkins    args = create_argparser().parse_args()
1958ec9d54bSJed Brown
1964a2fcf2fSJeremy L Thompson    # run tests
197e17e35bbSJames Wright    if 'smartsim' in args.test:
198*4d00b080SJeremy L Thompson        has_smartsim: bool = args.smartredis_dir and Path(args.smartredis_dir).is_file()
199*4d00b080SJeremy L Thompson        test_cases = []
200*4d00b080SJeremy L Thompson
201*4d00b080SJeremy L Thompson        if args.mode is RunMode.TAP:
202*4d00b080SJeremy L Thompson            print(f'1..1')
203*4d00b080SJeremy L Thompson        if has_smartsim:
204e17e35bbSJames Wright            sys.path.insert(0, str(Path(__file__).parents[1] / "examples" / "fluids"))
205e17e35bbSJames Wright            from smartsim_regression_framework import SmartSimTest
206e17e35bbSJames Wright
207e17e35bbSJames Wright            test_framework = SmartSimTest(Path(__file__).parent / 'test_dir')
208e17e35bbSJames Wright            test_framework.setup()
209*4d00b080SJeremy L Thompson
210e17e35bbSJames Wright            is_new_subtest = True
211e17e35bbSJames Wright            subtest_ok = True
212e17e35bbSJames Wright            for i, backend in enumerate(args.ceed_backends):
213e17e35bbSJames Wright                test_cases.append(test_framework.test_junit(backend))
214e17e35bbSJames Wright                if is_new_subtest and args.mode == RunMode.TAP:
215e17e35bbSJames Wright                    is_new_subtest = False
216e17e35bbSJames Wright                    print(f'# Subtest: {test_cases[0].category}')
217e17e35bbSJames Wright                    print(f'    1..{len(args.ceed_backends)}')
218e17e35bbSJames Wright                print(test_case_output_string(test_cases[i], TestSpec("SmartSim Tests"), args.mode, backend, '', i))
219e17e35bbSJames Wright            if args.mode == RunMode.TAP:
220e17e35bbSJames Wright                print(f'{"" if subtest_ok else "not "}ok 1 - {test_cases[0].category}')
221e17e35bbSJames Wright            test_framework.teardown()
222*4d00b080SJeremy L Thompson        elif args.mode is RunMode.TAP:
223*4d00b080SJeremy L Thompson            print(f'ok 1 - # SKIP SmartSim not installed')
224e17e35bbSJames Wright        result: TestSuite = TestSuite('SmartSim Tests', test_cases)
225e17e35bbSJames Wright    else:
226e17e35bbSJames Wright        result: TestSuite = run_tests(
227e17e35bbSJames Wright            args.test,
228e17e35bbSJames Wright            args.ceed_backends,
229e17e35bbSJames Wright            args.mode,
230e17e35bbSJames Wright            args.nproc,
231e17e35bbSJames Wright            CeedSuiteSpec(),
232e17e35bbSJames Wright            args.pool_size)
233add335c8SJeremy L Thompson
2341b16049aSZach Atkins    # write output and check for failures
2351b16049aSZach Atkins    if args.mode is RunMode.JUNIT:
2361b16049aSZach Atkins        write_junit_xml(result, args.output, args.junit_batch)
2371b16049aSZach Atkins        if has_failures(result):
2384d57a9fcSJed Brown            sys.exit(1)
239