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