xref: /libCEED/tests/junit.py (revision d6ed5abffa3823f6603d4dc65272439584c38270)
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('-b', '--junit-batch', type=str, default='', help='Name of JUnit batch for output file')
2819868e18SZach Atkins    parser.add_argument('-np', '--pool-size', type=int, default=1, help='Number of test cases to run in parallel')
29f36e7531SZach Atkins    parser.add_argument('-s', '--search', type=str, default='.*',
30f36e7531SZach Atkins                        help='Search string to filter tests, using `re` package format')
31f36e7531SZach Atkins    parser.add_argument('-v', '--verbose', action='store_true', default=False,
32f36e7531SZach Atkins                        help='print details for all runs, not just failures')
331b16049aSZach Atkins    parser.add_argument('test', help='Test executable', nargs='?')
341b16049aSZach Atkins
351b16049aSZach Atkins    return parser
368ec9d54bSJed Brown
373d94f746Srezgarshakeri
381b16049aSZach Atkins# Necessary functions for running tests
391b16049aSZach Atkinsclass CeedSuiteSpec(SuiteSpec):
403e0c2f3fSJeremy L Thompson    def __init__(self):
413e0c2f3fSJeremy L Thompson        pass
42637c7b11SJames Wright
431b16049aSZach Atkins    def get_source_path(self, test: str) -> Path:
441b16049aSZach Atkins        """Compute path to test source file
458ec9d54bSJed Brown
461b16049aSZach Atkins        Args:
471b16049aSZach Atkins            test (str): Name of test
483d94f746Srezgarshakeri
491b16049aSZach Atkins        Returns:
501b16049aSZach Atkins            Path: Path to source file
511b16049aSZach Atkins        """
52372821a4SZach Atkins        prefix, rest = test.split('-', 1)
532027fb9dSSirAlienTheGreat        if prefix == 'rustqfunctions':
542027fb9dSSirAlienTheGreat            return (Path('examples') / 'rust-qfunctions' / rest).with_suffix('.c')
55372821a4SZach Atkins        if prefix == 'petsc':
56372821a4SZach Atkins            return (Path('examples') / 'petsc' / rest).with_suffix('.c')
57372821a4SZach Atkins        elif prefix == 'mfem':
58372821a4SZach Atkins            return (Path('examples') / 'mfem' / rest).with_suffix('.cpp')
59372821a4SZach Atkins        elif prefix == 'nek':
60372821a4SZach Atkins            return (Path('examples') / 'nek' / 'bps' / rest).with_suffix('.usr')
618c81f8b0SPeter Munch        elif prefix == 'dealii':
628c81f8b0SPeter Munch            return (Path('examples') / 'deal.II' / rest).with_suffix('.cc')
63372821a4SZach Atkins        elif prefix == 'fluids':
64372821a4SZach Atkins            return (Path('examples') / 'fluids' / rest).with_suffix('.c')
65372821a4SZach Atkins        elif prefix == 'solids':
66372821a4SZach Atkins            return (Path('examples') / 'solids' / rest).with_suffix('.c')
67372821a4SZach Atkins        elif test.startswith('ex'):
68*860dc821SJeremy L Thompson            if test.endswith('-f'):
69*860dc821SJeremy L Thompson                return (Path('examples') / 'ceed' / test).with_suffix('.f90')
70*860dc821SJeremy L Thompson            else:
71372821a4SZach Atkins                return (Path('examples') / 'ceed' / test).with_suffix('.c')
72372821a4SZach Atkins        elif test.endswith('-f'):
73372821a4SZach Atkins            return (Path('tests') / test).with_suffix('.f90')
74372821a4SZach Atkins        else:
75372821a4SZach Atkins            return (Path('tests') / test).with_suffix('.c')
76372821a4SZach Atkins
771b16049aSZach Atkins    # get path to executable
781b16049aSZach Atkins    def get_run_path(self, test: str) -> Path:
791b16049aSZach Atkins        """Compute path to built test executable file
80372821a4SZach Atkins
811b16049aSZach Atkins        Args:
821b16049aSZach Atkins            test (str): Name of test
83bdb0bdbbSJed Brown
841b16049aSZach Atkins        Returns:
851b16049aSZach Atkins            Path: Path to test executable
861b16049aSZach Atkins        """
871b16049aSZach Atkins        return Path('build') / test
883d94f746Srezgarshakeri
891b16049aSZach Atkins    def get_output_path(self, test: str, output_file: str) -> Path:
901b16049aSZach Atkins        """Compute path to expected output file
91b974e86eSJed Brown
921b16049aSZach Atkins        Args:
931b16049aSZach Atkins            test (str): Name of test
941b16049aSZach Atkins            output_file (str): File name of output file
953d94f746Srezgarshakeri
961b16049aSZach Atkins        Returns:
971b16049aSZach Atkins            Path: Path to expected output file
981b16049aSZach Atkins        """
991b16049aSZach Atkins        return Path('tests') / 'output' / output_file
100b974e86eSJed Brown
1011b16049aSZach Atkins    def check_pre_skip(self, test: str, spec: TestSpec, resource: str, nproc: int) -> Optional[str]:
1021b16049aSZach Atkins        """Check if a test case should be skipped prior to running, returning the reason for skipping
1033d94f746Srezgarshakeri
1041b16049aSZach Atkins        Args:
1051b16049aSZach Atkins            test (str): Name of test
1061b16049aSZach Atkins            spec (TestSpec): Test case specification
1071b16049aSZach Atkins            resource (str): libCEED backend
1081b16049aSZach Atkins            nproc (int): Number of MPI processes to use when running test case
109288c0443SJeremy L Thompson
1101b16049aSZach Atkins        Returns:
1111b16049aSZach Atkins            Optional[str]: Skip reason, or `None` if test case should not be skipped
1121b16049aSZach Atkins        """
1131b16049aSZach Atkins        if test.startswith('t318') and contains_any(resource, ['/gpu/cuda/ref']):
1141b16049aSZach Atkins            return 'CUDA ref backend not supported'
1151b16049aSZach Atkins        if test.startswith('t506') and contains_any(resource, ['/gpu/cuda/shared']):
1161b16049aSZach Atkins            return 'CUDA shared backend not supported'
117f85e4a7bSJeremy L Thompson        for condition in spec.only:
118f85e4a7bSJeremy L Thompson            if (condition == 'cpu') and ('gpu' in resource):
119f85e4a7bSJeremy L Thompson                return 'CPU only test with GPU backend'
1204a2fcf2fSJeremy L Thompson
1211b16049aSZach Atkins    def check_post_skip(self, test: str, spec: TestSpec, resource: str, stderr: str) -> Optional[str]:
1221b16049aSZach Atkins        """Check if a test case should be allowed to fail, based on its stderr output
123b974e86eSJed Brown
1241b16049aSZach Atkins        Args:
1251b16049aSZach Atkins            test (str): Name of test
1261b16049aSZach Atkins            spec (TestSpec): Test case specification
1271b16049aSZach Atkins            resource (str): libCEED backend
1281b16049aSZach Atkins            stderr (str): Standard error output from test case execution
1298ec9d54bSJed Brown
1301b16049aSZach Atkins        Returns:
1311b16049aSZach Atkins            Optional[str]: Skip reason, or `None` if unexpeced error
1321b16049aSZach Atkins        """
133346c77e6SJeremy L Thompson        if 'Backend does not implement' in stderr:
1341b16049aSZach Atkins            return f'Backend does not implement'
1351b16049aSZach Atkins        elif 'Can only provide HOST memory for this backend' in stderr:
1361b16049aSZach Atkins            return f'Device memory not supported'
137b0976d5aSZach Atkins        elif 'Can only set HOST memory for this backend' in stderr:
138b0976d5aSZach Atkins            return f'Device memory not supported'
1391b16049aSZach Atkins        elif 'Test not implemented in single precision' in stderr:
1401b16049aSZach Atkins            return f'Test not implemented in single precision'
1411b16049aSZach Atkins        elif 'No SYCL devices of the requested type are available' in stderr:
1421b16049aSZach Atkins            return f'SYCL device type not available'
143d26b3214SJeremy L Thompson        elif 'You may need to add --download-ctetgen or --download-tetgen' in stderr:
144d26b3214SJeremy L Thompson            return f'Tet mesh generator not installed for {test}, {spec.name}'
1451b16049aSZach Atkins        return None
146bdb0bdbbSJed Brown
14778cb100bSJames Wright    def check_required_failure(self, test: str, spec: TestSpec, resource: str, stderr: str) -> Tuple[str, bool]:
1481b16049aSZach Atkins        """Check whether a test case is expected to fail and if it failed expectedly
149bdb0bdbbSJed Brown
1501b16049aSZach Atkins        Args:
1511b16049aSZach Atkins            test (str): Name of test
1521b16049aSZach Atkins            spec (TestSpec): Test case specification
1531b16049aSZach Atkins            resource (str): libCEED backend
1541b16049aSZach Atkins            stderr (str): Standard error output from test case execution
155bdb0bdbbSJed Brown
1561b16049aSZach Atkins        Returns:
1571b16049aSZach Atkins            tuple[str, bool]: Tuple of the expected failure string and whether it was present in `stderr`
1581b16049aSZach Atkins        """
1591b16049aSZach Atkins        test_id: str = test[:4]
1601b16049aSZach Atkins        fail_str: str = ''
1611b16049aSZach Atkins        if test_id in ['t006', 't007']:
1621b16049aSZach Atkins            fail_str = 'No suitable backend:'
1631b16049aSZach Atkins        elif test_id in ['t008']:
1641b16049aSZach Atkins            fail_str = 'Available backend resources:'
1651b16049aSZach Atkins        elif test_id in ['t110', 't111', 't112', 't113', 't114']:
1661b16049aSZach Atkins            fail_str = 'Cannot grant CeedVector array access'
1671b16049aSZach Atkins        elif test_id in ['t115']:
1681b16049aSZach Atkins            fail_str = 'Cannot grant CeedVector read-only array access, the access lock is already in use'
1691b16049aSZach Atkins        elif test_id in ['t116']:
1701b16049aSZach Atkins            fail_str = 'Cannot destroy CeedVector, the writable access lock is in use'
1711b16049aSZach Atkins        elif test_id in ['t117']:
1721b16049aSZach Atkins            fail_str = 'Cannot restore CeedVector array access, access was not granted'
1731b16049aSZach Atkins        elif test_id in ['t118']:
1741b16049aSZach Atkins            fail_str = 'Cannot sync CeedVector, the access lock is already in use'
1751b16049aSZach Atkins        elif test_id in ['t215']:
1761b16049aSZach Atkins            fail_str = 'Cannot destroy CeedElemRestriction, a process has read access to the offset data'
1771b16049aSZach Atkins        elif test_id in ['t303']:
1788bbba8cdSJeremy L Thompson            fail_str = 'Input/output vectors too short for basis and evaluation mode'
1791b16049aSZach Atkins        elif test_id in ['t408']:
1801b16049aSZach Atkins            fail_str = 'CeedQFunctionContextGetData(): Cannot grant CeedQFunctionContext data access, a process has read access'
1811b16049aSZach Atkins        elif test_id in ['t409'] and contains_any(resource, ['memcheck']):
1821b16049aSZach Atkins            fail_str = 'Context data changed while accessed in read-only mode'
1834a2fcf2fSJeremy L Thompson
1841b16049aSZach Atkins        return fail_str, fail_str in stderr
1854a2fcf2fSJeremy L Thompson
1861b16049aSZach Atkins    def check_allowed_stdout(self, test: str) -> bool:
1871b16049aSZach Atkins        """Check whether a test is allowed to print console output
1884a2fcf2fSJeremy L Thompson
1891b16049aSZach Atkins        Args:
1901b16049aSZach Atkins            test (str): Name of test
1911b16049aSZach Atkins
1921b16049aSZach Atkins        Returns:
1931b16049aSZach Atkins            bool: True if the test is allowed to print console output
1941b16049aSZach Atkins        """
1951b16049aSZach Atkins        return test[:4] in ['t003']
1961b16049aSZach Atkins
1978ec9d54bSJed Brown
1988ec9d54bSJed Brownif __name__ == '__main__':
1991b16049aSZach Atkins    args = create_argparser().parse_args()
2008ec9d54bSJed Brown
201e17e35bbSJames Wright    result: TestSuite = run_tests(
202e17e35bbSJames Wright        args.test,
203e17e35bbSJames Wright        args.ceed_backends,
204e17e35bbSJames Wright        args.mode,
205e17e35bbSJames Wright        args.nproc,
2063e0c2f3fSJeremy L Thompson        CeedSuiteSpec(),
207f36e7531SZach Atkins        args.pool_size,
208f36e7531SZach Atkins        search=args.search,
209f36e7531SZach Atkins        verbose=args.verbose)
210add335c8SJeremy L Thompson
2111b16049aSZach Atkins    # write output and check for failures
2121b16049aSZach Atkins    if args.mode is RunMode.JUNIT:
213681c4c48SJeremy L Thompson        write_junit_xml(result, args.junit_batch)
2141b16049aSZach Atkins        if has_failures(result):
2154d57a9fcSJed Brown            sys.exit(1)
216