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