1#!/usr/bin/env python 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 sys 20import subprocess 21import tempfile 22import os 23import time 24import signal 25import platform 26 27 28argp = argparse.ArgumentParser(description='Run c-ares resolver tests') 29argp.add_argument('--test_bin_path', default=None, type=str, 30 help='Path to gtest test binary to invoke.') 31argp.add_argument('--dns_server_bin_path', default=None, type=str, 32 help='Path to local DNS server python script.') 33argp.add_argument('--records_config_path', default=None, type=str, 34 help=('Path to DNS records yaml file that ' 35 'specifies records for the DNS sever. ')) 36argp.add_argument('--dns_server_port', default=None, type=int, 37 help=('Port that local DNS server is listening on.')) 38argp.add_argument('--dns_resolver_bin_path', default=None, type=str, 39 help=('Path to the DNS health check utility.')) 40argp.add_argument('--tcp_connect_bin_path', default=None, type=str, 41 help=('Path to the TCP health check utility.')) 42args = argp.parse_args() 43 44def test_runner_log(msg): 45 sys.stderr.write('\n%s: %s\n' % (__file__, msg)) 46 47def python_args(arg_list): 48 if platform.system() == 'Windows': 49 return [sys.executable] + arg_list 50 return arg_list 51 52cur_resolver = os.environ.get('GRPC_DNS_RESOLVER') 53if cur_resolver and cur_resolver != 'ares': 54 test_runner_log(('WARNING: cur resolver set to %s. This set of tests ' 55 'needs to use GRPC_DNS_RESOLVER=ares.')) 56 test_runner_log('Exit 1 without running tests.') 57 sys.exit(1) 58os.environ.update({'GRPC_DNS_RESOLVER': 'ares'}) 59os.environ.update({'GRPC_TRACE': 'cares_resolver'}) 60 61def wait_until_dns_server_is_up(args, 62 dns_server_subprocess, 63 dns_server_subprocess_output): 64 for i in range(0, 30): 65 test_runner_log('Health check: attempt to connect to DNS server over TCP.') 66 tcp_connect_subprocess = subprocess.Popen(python_args([ 67 args.tcp_connect_bin_path, 68 '--server_host', '127.0.0.1', 69 '--server_port', str(args.dns_server_port), 70 '--timeout', str(1)])) 71 tcp_connect_subprocess.communicate() 72 if tcp_connect_subprocess.returncode == 0: 73 test_runner_log(('Health check: attempt to make an A-record ' 74 'query to DNS server.')) 75 dns_resolver_subprocess = subprocess.Popen(python_args([ 76 args.dns_resolver_bin_path, 77 '--qname', 'health-check-local-dns-server-is-alive.resolver-tests.grpctestingexp', 78 '--server_host', '127.0.0.1', 79 '--server_port', str(args.dns_server_port)]), 80 stdout=subprocess.PIPE) 81 dns_resolver_stdout, _ = dns_resolver_subprocess.communicate() 82 if dns_resolver_subprocess.returncode == 0: 83 if '123.123.123.123' in dns_resolver_stdout: 84 test_runner_log(('DNS server is up! ' 85 'Successfully reached it over UDP and TCP.')) 86 return 87 time.sleep(0.1) 88 dns_server_subprocess.kill() 89 dns_server_subprocess.wait() 90 test_runner_log(('Failed to reach DNS server over TCP and/or UDP. ' 91 'Exitting without running tests.')) 92 test_runner_log('======= DNS server stdout ' 93 '(merged stdout and stderr) =============') 94 with open(dns_server_subprocess_output, 'r') as l: 95 test_runner_log(l.read()) 96 test_runner_log('======= end DNS server output=========') 97 sys.exit(1) 98 99dns_server_subprocess_output = tempfile.mktemp() 100with open(dns_server_subprocess_output, 'w') as l: 101 dns_server_subprocess = subprocess.Popen(python_args([ 102 args.dns_server_bin_path, 103 '--port', str(args.dns_server_port), 104 '--records_config_path', args.records_config_path]), 105 stdin=subprocess.PIPE, 106 stdout=l, 107 stderr=l) 108 109def _quit_on_signal(signum, _frame): 110 test_runner_log('Received signal: %d' % signum) 111 dns_server_subprocess.kill() 112 dns_server_subprocess.wait() 113 sys.exit(1) 114 115signal.signal(signal.SIGINT, _quit_on_signal) 116signal.signal(signal.SIGTERM, _quit_on_signal) 117wait_until_dns_server_is_up(args, 118 dns_server_subprocess, 119 dns_server_subprocess_output) 120num_test_failures = 0 121 122test_runner_log('Run test with target: %s' % 'no-srv-ipv4-single-target.resolver-tests-version-4.grpctestingexp.') 123current_test_subprocess = subprocess.Popen([ 124 args.test_bin_path, 125 '--target_name', 'no-srv-ipv4-single-target.resolver-tests-version-4.grpctestingexp.', 126 '--expected_addrs', '5.5.5.5:443,False', 127 '--expected_chosen_service_config', '', 128 '--expected_lb_policy', '', 129 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 130current_test_subprocess.communicate() 131if current_test_subprocess.returncode != 0: 132 num_test_failures += 1 133 134test_runner_log('Run test with target: %s' % 'srv-ipv4-single-target.resolver-tests-version-4.grpctestingexp.') 135current_test_subprocess = subprocess.Popen([ 136 args.test_bin_path, 137 '--target_name', 'srv-ipv4-single-target.resolver-tests-version-4.grpctestingexp.', 138 '--expected_addrs', '1.2.3.4:1234,True', 139 '--expected_chosen_service_config', '', 140 '--expected_lb_policy', '', 141 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 142current_test_subprocess.communicate() 143if current_test_subprocess.returncode != 0: 144 num_test_failures += 1 145 146test_runner_log('Run test with target: %s' % 'srv-ipv4-multi-target.resolver-tests-version-4.grpctestingexp.') 147current_test_subprocess = subprocess.Popen([ 148 args.test_bin_path, 149 '--target_name', 'srv-ipv4-multi-target.resolver-tests-version-4.grpctestingexp.', 150 '--expected_addrs', '1.2.3.5:1234,True;1.2.3.6:1234,True;1.2.3.7:1234,True', 151 '--expected_chosen_service_config', '', 152 '--expected_lb_policy', '', 153 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 154current_test_subprocess.communicate() 155if current_test_subprocess.returncode != 0: 156 num_test_failures += 1 157 158test_runner_log('Run test with target: %s' % 'srv-ipv6-single-target.resolver-tests-version-4.grpctestingexp.') 159current_test_subprocess = subprocess.Popen([ 160 args.test_bin_path, 161 '--target_name', 'srv-ipv6-single-target.resolver-tests-version-4.grpctestingexp.', 162 '--expected_addrs', '[2607:f8b0:400a:801::1001]:1234,True', 163 '--expected_chosen_service_config', '', 164 '--expected_lb_policy', '', 165 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 166current_test_subprocess.communicate() 167if current_test_subprocess.returncode != 0: 168 num_test_failures += 1 169 170test_runner_log('Run test with target: %s' % 'srv-ipv6-multi-target.resolver-tests-version-4.grpctestingexp.') 171current_test_subprocess = subprocess.Popen([ 172 args.test_bin_path, 173 '--target_name', 'srv-ipv6-multi-target.resolver-tests-version-4.grpctestingexp.', 174 '--expected_addrs', '[2607:f8b0:400a:801::1002]:1234,True;[2607:f8b0:400a:801::1003]:1234,True;[2607:f8b0:400a:801::1004]:1234,True', 175 '--expected_chosen_service_config', '', 176 '--expected_lb_policy', '', 177 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 178current_test_subprocess.communicate() 179if current_test_subprocess.returncode != 0: 180 num_test_failures += 1 181 182test_runner_log('Run test with target: %s' % 'srv-ipv4-simple-service-config.resolver-tests-version-4.grpctestingexp.') 183current_test_subprocess = subprocess.Popen([ 184 args.test_bin_path, 185 '--target_name', 'srv-ipv4-simple-service-config.resolver-tests-version-4.grpctestingexp.', 186 '--expected_addrs', '1.2.3.4:1234,True', 187 '--expected_chosen_service_config', '{"loadBalancingPolicy":"round_robin","methodConfig":[{"name":[{"method":"Foo","service":"SimpleService","waitForReady":true}]}]}', 188 '--expected_lb_policy', 'round_robin', 189 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 190current_test_subprocess.communicate() 191if current_test_subprocess.returncode != 0: 192 num_test_failures += 1 193 194test_runner_log('Run test with target: %s' % 'ipv4-no-srv-simple-service-config.resolver-tests-version-4.grpctestingexp.') 195current_test_subprocess = subprocess.Popen([ 196 args.test_bin_path, 197 '--target_name', 'ipv4-no-srv-simple-service-config.resolver-tests-version-4.grpctestingexp.', 198 '--expected_addrs', '1.2.3.4:443,False', 199 '--expected_chosen_service_config', '{"loadBalancingPolicy":"round_robin","methodConfig":[{"name":[{"method":"Foo","service":"NoSrvSimpleService","waitForReady":true}]}]}', 200 '--expected_lb_policy', 'round_robin', 201 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 202current_test_subprocess.communicate() 203if current_test_subprocess.returncode != 0: 204 num_test_failures += 1 205 206test_runner_log('Run test with target: %s' % 'ipv4-no-config-for-cpp.resolver-tests-version-4.grpctestingexp.') 207current_test_subprocess = subprocess.Popen([ 208 args.test_bin_path, 209 '--target_name', 'ipv4-no-config-for-cpp.resolver-tests-version-4.grpctestingexp.', 210 '--expected_addrs', '1.2.3.4:443,False', 211 '--expected_chosen_service_config', '', 212 '--expected_lb_policy', '', 213 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 214current_test_subprocess.communicate() 215if current_test_subprocess.returncode != 0: 216 num_test_failures += 1 217 218test_runner_log('Run test with target: %s' % 'ipv4-cpp-config-has-zero-percentage.resolver-tests-version-4.grpctestingexp.') 219current_test_subprocess = subprocess.Popen([ 220 args.test_bin_path, 221 '--target_name', 'ipv4-cpp-config-has-zero-percentage.resolver-tests-version-4.grpctestingexp.', 222 '--expected_addrs', '1.2.3.4:443,False', 223 '--expected_chosen_service_config', '', 224 '--expected_lb_policy', '', 225 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 226current_test_subprocess.communicate() 227if current_test_subprocess.returncode != 0: 228 num_test_failures += 1 229 230test_runner_log('Run test with target: %s' % 'ipv4-second-language-is-cpp.resolver-tests-version-4.grpctestingexp.') 231current_test_subprocess = subprocess.Popen([ 232 args.test_bin_path, 233 '--target_name', 'ipv4-second-language-is-cpp.resolver-tests-version-4.grpctestingexp.', 234 '--expected_addrs', '1.2.3.4:443,False', 235 '--expected_chosen_service_config', '{"loadBalancingPolicy":"round_robin","methodConfig":[{"name":[{"method":"Foo","service":"CppService","waitForReady":true}]}]}', 236 '--expected_lb_policy', 'round_robin', 237 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 238current_test_subprocess.communicate() 239if current_test_subprocess.returncode != 0: 240 num_test_failures += 1 241 242test_runner_log('Run test with target: %s' % 'ipv4-config-with-percentages.resolver-tests-version-4.grpctestingexp.') 243current_test_subprocess = subprocess.Popen([ 244 args.test_bin_path, 245 '--target_name', 'ipv4-config-with-percentages.resolver-tests-version-4.grpctestingexp.', 246 '--expected_addrs', '1.2.3.4:443,False', 247 '--expected_chosen_service_config', '{"loadBalancingPolicy":"round_robin","methodConfig":[{"name":[{"method":"Foo","service":"AlwaysPickedService","waitForReady":true}]}]}', 248 '--expected_lb_policy', 'round_robin', 249 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 250current_test_subprocess.communicate() 251if current_test_subprocess.returncode != 0: 252 num_test_failures += 1 253 254test_runner_log('Run test with target: %s' % 'srv-ipv4-target-has-backend-and-balancer.resolver-tests-version-4.grpctestingexp.') 255current_test_subprocess = subprocess.Popen([ 256 args.test_bin_path, 257 '--target_name', 'srv-ipv4-target-has-backend-and-balancer.resolver-tests-version-4.grpctestingexp.', 258 '--expected_addrs', '1.2.3.4:1234,True;1.2.3.4:443,False', 259 '--expected_chosen_service_config', '', 260 '--expected_lb_policy', '', 261 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 262current_test_subprocess.communicate() 263if current_test_subprocess.returncode != 0: 264 num_test_failures += 1 265 266test_runner_log('Run test with target: %s' % 'srv-ipv6-target-has-backend-and-balancer.resolver-tests-version-4.grpctestingexp.') 267current_test_subprocess = subprocess.Popen([ 268 args.test_bin_path, 269 '--target_name', 'srv-ipv6-target-has-backend-and-balancer.resolver-tests-version-4.grpctestingexp.', 270 '--expected_addrs', '[2607:f8b0:400a:801::1002]:1234,True;[2607:f8b0:400a:801::1002]:443,False', 271 '--expected_chosen_service_config', '', 272 '--expected_lb_policy', '', 273 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 274current_test_subprocess.communicate() 275if current_test_subprocess.returncode != 0: 276 num_test_failures += 1 277 278test_runner_log('Run test with target: %s' % 'ipv4-config-causing-fallback-to-tcp.resolver-tests-version-4.grpctestingexp.') 279current_test_subprocess = subprocess.Popen([ 280 args.test_bin_path, 281 '--target_name', 'ipv4-config-causing-fallback-to-tcp.resolver-tests-version-4.grpctestingexp.', 282 '--expected_addrs', '1.2.3.4:443,False', 283 '--expected_chosen_service_config', '{"loadBalancingPolicy":"round_robin","methodConfig":[{"name":[{"method":"Foo","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooTwo","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooThree","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooFour","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooFive","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooSix","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooSeven","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooEight","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooNine","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooTen","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooEleven","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooTwelve","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooTwelve","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooTwelve","service":"SimpleService","waitForReady":true}]},{"name":[{"method":"FooTwelve","service":"SimpleService","waitForReady":true}]}]}', 284 '--expected_lb_policy', '', 285 '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port]) 286current_test_subprocess.communicate() 287if current_test_subprocess.returncode != 0: 288 num_test_failures += 1 289 290test_runner_log('now kill DNS server') 291dns_server_subprocess.kill() 292dns_server_subprocess.wait() 293test_runner_log('%d tests failed.' % num_test_failures) 294sys.exit(num_test_failures) 295