| 1 |
<%def name="resolver_component_tests(tests)">#!/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 |
|
| 18 |
import argparse |
| 19 |
import os |
| 20 |
import platform |
| 21 |
import signal |
| 22 |
import subprocess |
| 23 |
import sys |
| 24 |
import tempfile |
| 25 |
import time |
| 26 |
|
| 27 |
argp = argparse.ArgumentParser(description='Run c-ares resolver tests') |
| 28 |
argp.add_argument('--test_bin_path', default=None, type=str, |
| 29 |
help='Path to gtest test binary to invoke.') |
| 30 |
argp.add_argument('--dns_server_bin_path', default=None, type=str, |
| 31 |
help='Path to local DNS server python script.') |
| 32 |
argp.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. ')) |
| 35 |
argp.add_argument('--dns_server_port', default=None, type=int, |
| 36 |
help=('Port that local DNS server is listening on.')) |
| 37 |
argp.add_argument('--dns_resolver_bin_path', default=None, type=str, |
| 38 |
help=('Path to the DNS health check utility.')) |
| 39 |
argp.add_argument('--tcp_connect_bin_path', default=None, type=str, |
| 40 |
help=('Path to the TCP health check utility.')) |
| 41 |
argp.add_argument('--extra_args', default='', type=str, |
| 42 |
help=('Comma-separate list of command args to ' |
| 43 |
'plumb through to --test_bin_path')) |
| 44 |
args = argp.parse_args() |
| 45 |
|
| 46 |
def test_runner_log(msg): |
| 47 |
sys.stderr.write('\n%s: %s\n' % (__file__, msg)) |
| 48 |
|
| 49 |
def python_args(arg_list): |
| 50 |
if platform.system() == 'Windows': |
| 51 |
return [sys.executable] + arg_list |
| 52 |
return arg_list |
| 53 |
|
| 54 |
cur_resolver = os.environ.get('GRPC_DNS_RESOLVER') |
| 55 |
if 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) |
| 60 |
os.environ.update({'GRPC_TRACE': 'cares_resolver,cares_address_sorting'}) |
| 61 |
|
| 62 |
def wait_until_dns_server_is_up(args, |
| 63 |
dns_server_subprocess, |
| 64 |
dns_server_subprocess_output): |
| 65 |
for i in range(0, 30): |
| 66 |
test_runner_log('Health check: attempt to connect to DNS server over TCP.') |
| 67 |
tcp_connect_subprocess = subprocess.Popen(python_args([ |
| 68 |
args.tcp_connect_bin_path, |
| 69 |
'--server_host', '127.0.0.1', |
| 70 |
'--server_port', str(args.dns_server_port), |
| 71 |
'--timeout', str(1)])) |
| 72 |
tcp_connect_subprocess.communicate() |
| 73 |
if tcp_connect_subprocess.returncode == 0: |
| 74 |
test_runner_log(('Health check: attempt to make an A-record ' |
| 75 |
'query to DNS server.')) |
| 76 |
dns_resolver_subprocess = subprocess.Popen(python_args([ |
| 77 |
args.dns_resolver_bin_path, |
| 78 |
'--qname', 'health-check-local-dns-server-is-alive.resolver-tests.grpctestingexp', |
| 79 |
'--server_host', '127.0.0.1', |
| 80 |
'--server_port', str(args.dns_server_port)]), |
| 81 |
stdout=subprocess.PIPE) |
| 82 |
dns_resolver_stdout, _ = dns_resolver_subprocess.communicate(str.encode('ascii')) |
| 83 |
if dns_resolver_subprocess.returncode == 0: |
| 84 |
if '123.123.123.123'.encode('ascii') in dns_resolver_stdout: |
| 85 |
test_runner_log(('DNS server is up! ' |
| 86 |
'Successfully reached it over UDP and TCP.')) |
| 87 |
return |
| 88 |
time.sleep(0.1) |
| 89 |
dns_server_subprocess.kill() |
| 90 |
dns_server_subprocess.wait() |
| 91 |
test_runner_log(('Failed to reach DNS server over TCP and/or UDP. ' |
| 92 |
'Exitting without running tests.')) |
| 93 |
test_runner_log('======= DNS server stdout ' |
| 94 |
'(merged stdout and stderr) =============') |
| 95 |
with open(dns_server_subprocess_output, 'r') as l: |
| 96 |
test_runner_log(l.read()) |
| 97 |
test_runner_log('======= end DNS server output=========') |
| 98 |
sys.exit(1) |
| 99 |
|
| 100 |
dns_server_subprocess_output = tempfile.mktemp() |
| 101 |
with open(dns_server_subprocess_output, 'w') as l: |
| 102 |
dns_server_subprocess = subprocess.Popen(python_args([ |
| 103 |
args.dns_server_bin_path, |
| 104 |
'--port', str(args.dns_server_port), |
| 105 |
'--records_config_path', args.records_config_path]), |
| 106 |
stdin=subprocess.PIPE, |
| 107 |
stdout=l, |
| 108 |
stderr=l) |
| 109 |
|
| 110 |
def _quit_on_signal(signum, _frame): |
| 111 |
test_runner_log('Received signal: %d' % signum) |
| 112 |
dns_server_subprocess.kill() |
| 113 |
dns_server_subprocess.wait() |
| 114 |
sys.exit(1) |
| 115 |
|
| 116 |
signal.signal(signal.SIGINT, _quit_on_signal) |
| 117 |
signal.signal(signal.SIGTERM, _quit_on_signal) |
| 118 |
wait_until_dns_server_is_up(args, |
| 119 |
dns_server_subprocess, |
| 120 |
dns_server_subprocess_output) |
| 121 |
num_test_failures = 0 |
| 122 |
|
| 123 |
% for test in tests: |
| 124 |
test_runner_log('Run test with target: %s' % '${test['test_title']}')\ |
| 125 |
|
| 126 |
current_test_subprocess = subprocess.Popen([\ |
| 127 |
|
| 128 |
args.test_bin_path,\ |
| 129 |
|
| 130 |
% for arg_name_and_value in test['arg_names_and_values']: |
| 131 |
\ |
| 132 |
'--${arg_name_and_value[0]}', '${arg_name_and_value[1]}',\ |
| 133 |
|
| 134 |
\ |
| 135 |
% endfor |
| 136 |
'--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port |
| 137 |
] + args.extra_args.split(','))\ |
| 138 |
|
| 139 |
current_test_subprocess.communicate()\ |
| 140 |
|
| 141 |
if current_test_subprocess.returncode != 0:\ |
| 142 |
|
| 143 |
num_test_failures += 1 |
| 144 |
|
| 145 |
% endfor |
| 146 |
test_runner_log('now kill DNS server') |
| 147 |
dns_server_subprocess.kill() |
| 148 |
dns_server_subprocess.wait() |
| 149 |
test_runner_log('%d tests failed.' % num_test_failures) |
| 150 |
sys.exit(num_test_failures)</%def> |