1# Copyright 2024 gRPC authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import os 16import re 17import subprocess 18import sys 19import tempfile 20 21_OK_TEST_REGEX = r"^-+.*Ran ([\d]+) tests* in ([\d.]+)s.*OK(?: \(skipped=(\d+)\))?\n$" 22 23# Tests with known exception logs. 24# TODO(sourabhsinghs): Investigate and enable _rpc_part_1_test and _rpc_part_2_test tests. 25_SKIP_TESTS = [ 26 "_rpc_part_1_test", 27 "_server_shutdown_test", 28 "_xds_credentials_test", 29 "_server_test", 30 "_invalid_metadata_test", 31 "_reconnect_test", 32 "_channel_close_test", 33 "_rpc_part_2_test", 34 "_invocation_defects_test", 35 "_dynamic_stubs_test", 36 "_channel_connectivity_test", 37] 38 39if __name__ == "__main__": 40 if len(sys.argv) != 3: 41 print(f"USAGE: {sys.argv[0]} TARGET_MODULE", file=sys.stderr) 42 sys.exit(1) 43 44 test_script = sys.argv[1] 45 target_module = sys.argv[2] 46 47 if target_module in _SKIP_TESTS: 48 print(f"Skipping {target_module}") 49 sys.exit(0) 50 51 command = [ 52 sys.executable, 53 os.path.realpath(test_script), 54 target_module, 55 os.path.dirname(os.path.relpath(__file__)), 56 ] 57 58 with tempfile.TemporaryFile(mode="w+") as stdout_file: 59 with tempfile.TemporaryFile(mode="w+") as stderr_file: 60 result = subprocess.run( 61 command, 62 stdout=stdout_file, 63 stderr=stderr_file, 64 text=True, 65 check=True, 66 ) 67 68 stdout_file.seek(0) 69 stderr_file.seek(0) 70 71 stdout_count = len(stdout_file.readlines()) 72 stderr_count = len(stderr_file.readlines()) 73 74 if result.returncode != 0: 75 sys.exit("Test failure") 76 77 stderr_file.seek(0) 78 if not re.fullmatch(_OK_TEST_REGEX, stderr_file.read(), re.DOTALL): 79 print( 80 f"Warning: Excessive error output detected ({stderr_count} lines):" 81 ) 82 stderr_file.seek(0) 83 for line in stderr_file: 84 print(line) 85 86 if stdout_count > 0: 87 print( 88 f"Warning: Unexpected output detected ({stdout_count} lines):" 89 ) 90 stdout_file.seek(0) 91 for line in stdout_file: 92 print(line) 93 94