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 'Test not implemented in single precision' in stderr: 113 return f'Test not implemented in single precision' 114 elif 'No SYCL devices of the requested type are available' in stderr: 115 return f'SYCL device type not available' 116 return None 117 118 def check_required_failure(self, test: str, spec: TestSpec, resource: str, stderr: str) -> tuple[str, bool]: 119 """Check whether a test case is expected to fail and if it failed expectedly 120 121 Args: 122 test (str): Name of test 123 spec (TestSpec): Test case specification 124 resource (str): libCEED backend 125 stderr (str): Standard error output from test case execution 126 127 Returns: 128 tuple[str, bool]: Tuple of the expected failure string and whether it was present in `stderr` 129 """ 130 test_id: str = test[:4] 131 fail_str: str = '' 132 if test_id in ['t006', 't007']: 133 fail_str = 'No suitable backend:' 134 elif test_id in ['t008']: 135 fail_str = 'Available backend resources:' 136 elif test_id in ['t110', 't111', 't112', 't113', 't114']: 137 fail_str = 'Cannot grant CeedVector array access' 138 elif test_id in ['t115']: 139 fail_str = 'Cannot grant CeedVector read-only array access, the access lock is already in use' 140 elif test_id in ['t116']: 141 fail_str = 'Cannot destroy CeedVector, the writable access lock is in use' 142 elif test_id in ['t117']: 143 fail_str = 'Cannot restore CeedVector array access, access was not granted' 144 elif test_id in ['t118']: 145 fail_str = 'Cannot sync CeedVector, the access lock is already in use' 146 elif test_id in ['t215']: 147 fail_str = 'Cannot destroy CeedElemRestriction, a process has read access to the offset data' 148 elif test_id in ['t303']: 149 fail_str = 'Length of input/output vectors incompatible with basis dimensions' 150 elif test_id in ['t408']: 151 fail_str = 'CeedQFunctionContextGetData(): Cannot grant CeedQFunctionContext data access, a process has read access' 152 elif test_id in ['t409'] and contains_any(resource, ['memcheck']): 153 fail_str = 'Context data changed while accessed in read-only mode' 154 155 return fail_str, fail_str in stderr 156 157 def check_allowed_stdout(self, test: str) -> bool: 158 """Check whether a test is allowed to print console output 159 160 Args: 161 test (str): Name of test 162 163 Returns: 164 bool: True if the test is allowed to print console output 165 """ 166 return test[:4] in ['t003'] 167 168 169if __name__ == '__main__': 170 args = create_argparser().parse_args() 171 172 # run tests 173 result: TestSuite = run_tests(args.test, args.ceed_backends, args.mode, args.nproc, CeedSuiteSpec()) 174 175 # write output and check for failures 176 if args.mode is RunMode.JUNIT: 177 write_junit_xml(result, args.output, args.junit_batch) 178 if has_failures(result): 179 sys.exit(1) 180