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('-s', '--search', type=str, default='.*', 31 help='Search string to filter tests, using `re` package format') 32 parser.add_argument('-v', '--verbose', action='store_true', default=False, 33 help='print details for all runs, not just failures') 34 parser.add_argument('test', help='Test executable', nargs='?') 35 36 return parser 37 38 39# Necessary functions for running tests 40class CeedSuiteSpec(SuiteSpec): 41 def __init__(self): 42 pass 43 44 def get_source_path(self, test: str) -> Path: 45 """Compute path to test source file 46 47 Args: 48 test (str): Name of test 49 50 Returns: 51 Path: Path to source file 52 """ 53 prefix, rest = test.split('-', 1) 54 if prefix == 'rustqfunctions': 55 return (Path('examples') / 'rust-qfunctions' / rest).with_suffix('.c') 56 if prefix == 'petsc': 57 return (Path('examples') / 'petsc' / rest).with_suffix('.c') 58 elif prefix == 'mfem': 59 return (Path('examples') / 'mfem' / rest).with_suffix('.cpp') 60 elif prefix == 'nek': 61 return (Path('examples') / 'nek' / 'bps' / rest).with_suffix('.usr') 62 elif prefix == 'dealii': 63 return (Path('examples') / 'deal.II' / rest).with_suffix('.cc') 64 elif prefix == 'fluids': 65 return (Path('examples') / 'fluids' / rest).with_suffix('.c') 66 elif prefix == 'solids': 67 return (Path('examples') / 'solids' / rest).with_suffix('.c') 68 elif test.startswith('ex'): 69 return (Path('examples') / 'ceed' / test).with_suffix('.c') 70 elif test.endswith('-f'): 71 return (Path('tests') / test).with_suffix('.f90') 72 else: 73 return (Path('tests') / test).with_suffix('.c') 74 75 # get path to executable 76 def get_run_path(self, test: str) -> Path: 77 """Compute path to built test executable file 78 79 Args: 80 test (str): Name of test 81 82 Returns: 83 Path: Path to test executable 84 """ 85 return Path('build') / test 86 87 def get_output_path(self, test: str, output_file: str) -> Path: 88 """Compute path to expected output file 89 90 Args: 91 test (str): Name of test 92 output_file (str): File name of output file 93 94 Returns: 95 Path: Path to expected output file 96 """ 97 return Path('tests') / 'output' / output_file 98 99 def check_pre_skip(self, test: str, spec: TestSpec, resource: str, nproc: int) -> Optional[str]: 100 """Check if a test case should be skipped prior to running, returning the reason for skipping 101 102 Args: 103 test (str): Name of test 104 spec (TestSpec): Test case specification 105 resource (str): libCEED backend 106 nproc (int): Number of MPI processes to use when running test case 107 108 Returns: 109 Optional[str]: Skip reason, or `None` if test case should not be skipped 110 """ 111 if contains_any(resource, ['occa']) and startswith_any( 112 test, ['t4', 't5', 'ex', 'mfem', 'nek', 'petsc', 'fluids', 'solids']): 113 return 'OCCA mode not supported' 114 if test.startswith('t318') and contains_any(resource, ['/gpu/cuda/ref']): 115 return 'CUDA ref backend not supported' 116 if test.startswith('t506') and contains_any(resource, ['/gpu/cuda/shared']): 117 return 'CUDA shared backend not supported' 118 for condition in spec.only: 119 if (condition == 'cpu') and ('gpu' in resource): 120 return 'CPU only test with GPU backend' 121 122 def check_post_skip(self, test: str, spec: TestSpec, resource: str, stderr: str) -> Optional[str]: 123 """Check if a test case should be allowed to fail, based on its stderr output 124 125 Args: 126 test (str): Name of test 127 spec (TestSpec): Test case specification 128 resource (str): libCEED backend 129 stderr (str): Standard error output from test case execution 130 131 Returns: 132 Optional[str]: Skip reason, or `None` if unexpeced error 133 """ 134 if 'OCCA backend failed to use' in stderr: 135 return f'OCCA mode not supported' 136 elif 'Backend does not implement' in stderr: 137 return f'Backend does not implement' 138 elif 'Can only provide HOST memory for this backend' in stderr: 139 return f'Device memory not supported' 140 elif 'Can only set HOST memory for this backend' in stderr: 141 return f'Device memory not supported' 142 elif 'Test not implemented in single precision' in stderr: 143 return f'Test not implemented in single precision' 144 elif 'No SYCL devices of the requested type are available' in stderr: 145 return f'SYCL device type not available' 146 elif 'You may need to add --download-ctetgen or --download-tetgen' in stderr: 147 return f'Tet mesh generator not installed for {test}, {spec.name}' 148 return None 149 150 def check_required_failure(self, test: str, spec: TestSpec, resource: str, stderr: str) -> Tuple[str, bool]: 151 """Check whether a test case is expected to fail and if it failed expectedly 152 153 Args: 154 test (str): Name of test 155 spec (TestSpec): Test case specification 156 resource (str): libCEED backend 157 stderr (str): Standard error output from test case execution 158 159 Returns: 160 tuple[str, bool]: Tuple of the expected failure string and whether it was present in `stderr` 161 """ 162 test_id: str = test[:4] 163 fail_str: str = '' 164 if test_id in ['t006', 't007']: 165 fail_str = 'No suitable backend:' 166 elif test_id in ['t008']: 167 fail_str = 'Available backend resources:' 168 elif test_id in ['t110', 't111', 't112', 't113', 't114']: 169 fail_str = 'Cannot grant CeedVector array access' 170 elif test_id in ['t115']: 171 fail_str = 'Cannot grant CeedVector read-only array access, the access lock is already in use' 172 elif test_id in ['t116']: 173 fail_str = 'Cannot destroy CeedVector, the writable access lock is in use' 174 elif test_id in ['t117']: 175 fail_str = 'Cannot restore CeedVector array access, access was not granted' 176 elif test_id in ['t118']: 177 fail_str = 'Cannot sync CeedVector, the access lock is already in use' 178 elif test_id in ['t215']: 179 fail_str = 'Cannot destroy CeedElemRestriction, a process has read access to the offset data' 180 elif test_id in ['t303']: 181 fail_str = 'Input/output vectors too short for basis and evaluation mode' 182 elif test_id in ['t408']: 183 fail_str = 'CeedQFunctionContextGetData(): Cannot grant CeedQFunctionContext data access, a process has read access' 184 elif test_id in ['t409'] and contains_any(resource, ['memcheck']): 185 fail_str = 'Context data changed while accessed in read-only mode' 186 187 return fail_str, fail_str in stderr 188 189 def check_allowed_stdout(self, test: str) -> bool: 190 """Check whether a test is allowed to print console output 191 192 Args: 193 test (str): Name of test 194 195 Returns: 196 bool: True if the test is allowed to print console output 197 """ 198 return test[:4] in ['t003'] 199 200 201if __name__ == '__main__': 202 args = create_argparser().parse_args() 203 204 result: TestSuite = run_tests( 205 args.test, 206 args.ceed_backends, 207 args.mode, 208 args.nproc, 209 CeedSuiteSpec(), 210 args.pool_size, 211 search=args.search, 212 verbose=args.verbose) 213 214 # write output and check for failures 215 if args.mode is RunMode.JUNIT: 216 write_junit_xml(result, args.output, args.junit_batch) 217 if has_failures(result): 218 sys.exit(1) 219