1<%def name="resolver_component_tests(tests)">#!/usr/bin/env python3 2# Copyright 2015 gRPC authors. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16# This file is auto-generated 17 18import argparse 19import os 20import platform 21import signal 22import subprocess 23import sys 24import tempfile 25import time 26 27argp = argparse.ArgumentParser(description='Run c-ares resolver tests') 28argp.add_argument('--test_bin_path', default=None, type=str, 29 help='Path to gtest test binary to invoke.') 30argp.add_argument('--dns_server_bin_path', default=None, type=str, 31 help='Path to local DNS server python script.') 32argp.add_argument('--records_config_path', default=None, type=str, 33 help=('Path to DNS records yaml file that ' 34 'specifies records for the DNS sever. ')) 35argp.add_argument('--dns_server_port', default=None, type=int, 36 help=('Port that local DNS server is listening on.')) 37argp.add_argument('--dns_resolver_bin_path', default=None, type=str, 38 help=('Path to the DNS health check utility.')) 39argp.add_argument('--tcp_connect_bin_path', default=None, type=str, 40 help=('Path to the TCP health check utility.')) 41argp.add_argument('--extra_args', default='', type=str, 42 help=('Comma-separate list of command args to ' 43 'plumb through to --test_bin_path')) 44args = argp.parse_args() 45 46def test_runner_log(msg): 47 sys.stderr.write('\n%s: %s\n' % (__file__, msg)) 48 49def python_args(arg_list): 50 if platform.system() == 'Windows' and arg_list[0].endswith('.py'): 51 return [sys.executable] + arg_list 52 return arg_list 53 54cur_resolver = os.environ.get('GRPC_DNS_RESOLVER') 55if cur_resolver and cur_resolver != 'ares': 56 test_runner_log(('WARNING: cur resolver set to %s. This set of tests ' 57 'needs to use GRPC_DNS_RESOLVER=ares.')) 58 test_runner_log('Exit 1 without running tests.') 59 sys.exit(1) 60os.environ.update({'GRPC_TRACE': 'cares_resolver,cares_address_sorting'}) 61experiments = os.environ.get('GRPC_EXPERIMENTS') 62if experiments is not None and 'event_engine_dns' in experiments: 63 os.environ.update({'GRPC_TRACE': 'event_engine_client_channel_resolver,cares_resolver'}) 64 65def wait_until_dns_server_is_up(args, 66 dns_server_subprocess, 67 dns_server_subprocess_output): 68 for i in range(0, 30): 69 test_runner_log('Health check: attempt to connect to DNS server over TCP.') 70 tcp_connect_subprocess = subprocess.Popen(python_args([ 71 args.tcp_connect_bin_path, 72 '--server_host', '127.0.0.1', 73 '--server_port', str(args.dns_server_port), 74 '--timeout', str(1)])) 75 tcp_connect_subprocess.communicate() 76 if tcp_connect_subprocess.returncode == 0: 77 test_runner_log(('Health check: attempt to make an A-record ' 78 'query to DNS server.')) 79 dns_resolver_subprocess = subprocess.Popen(python_args([ 80 args.dns_resolver_bin_path, 81 '--qname', 'health-check-local-dns-server-is-alive.resolver-tests.grpctestingexp', 82 '--server_host', '127.0.0.1', 83 '--server_port', str(args.dns_server_port)]), 84 stdout=subprocess.PIPE) 85 dns_resolver_stdout, _ = dns_resolver_subprocess.communicate(str.encode('ascii')) 86 if dns_resolver_subprocess.returncode == 0: 87 if '123.123.123.123'.encode('ascii') in dns_resolver_stdout: 88 test_runner_log(('DNS server is up! ' 89 'Successfully reached it over UDP and TCP.')) 90 return 91 time.sleep(0.1) 92 dns_server_subprocess.kill() 93 dns_server_subprocess.wait() 94 test_runner_log(('Failed to reach DNS server over TCP and/or UDP. ' 95 'Exitting without running tests.')) 96 test_runner_log('======= DNS server stdout ' 97 '(merged stdout and stderr) =============') 98 with open(dns_server_subprocess_output, 'r') as l: 99 test_runner_log(l.read()) 100 test_runner_log('======= end DNS server output=========') 101 sys.exit(1) 102 103dns_server_subprocess_output = tempfile.mktemp() 104with open(dns_server_subprocess_output, 'w') as l: 105 dns_server_subprocess = subprocess.Popen(python_args([ 106 args.dns_server_bin_path, 107 '--port', str(args.dns_server_port), 108 '--records_config_path', args.records_config_path]), 109 stdin=subprocess.PIPE, 110 stdout=l, 111 stderr=l) 112 113def _quit_on_signal(signum, _frame): 114 test_runner_log('Received signal: %d' % signum) 115 dns_server_subprocess.kill() 116 dns_server_subprocess.wait() 117 sys.exit(1) 118 119signal.signal(signal.SIGINT, _quit_on_signal) 120signal.signal(signal.SIGTERM, _quit_on_signal) 121wait_until_dns_server_is_up(args, 122 dns_server_subprocess, 123 dns_server_subprocess_output) 124num_test_failures = 0 125 126% for test in tests: 127test_runner_log('Run test with target: %s' % '${test['test_title']}')\ 128 129current_test_subprocess = subprocess.Popen([\ 130 131 args.test_bin_path,\ 132 133 % for arg_name_and_value in test['arg_names_and_values']: 134\ 135 '--${arg_name_and_value[0]}', '${arg_name_and_value[1]}',\ 136 137\ 138 % endfor 139 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port 140 ] + args.extra_args.split(','))\ 141 142current_test_subprocess.communicate()\ 143 144if current_test_subprocess.returncode != 0:\ 145 146 num_test_failures += 1 147 148% endfor 149test_runner_log('now kill DNS server') 150dns_server_subprocess.kill() 151dns_server_subprocess.wait() 152test_runner_log('%d tests failed.' % num_test_failures) 153sys.exit(num_test_failures)</%def> 154