• 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.'))
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
122% for test in tests:
123test_runner_log('Run test with target: %s' % '${test['test_title']}')\
124
125current_test_subprocess = subprocess.Popen([\
126
127  args.test_bin_path,\
128
129  % for arg_name_and_value in test['arg_names_and_values']:
130\
131  '--${arg_name_and_value[0]}', '${arg_name_and_value[1]}',\
132
133\
134  % endfor
135  '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port])\
136
137current_test_subprocess.communicate()\
138
139if current_test_subprocess.returncode != 0:\
140
141  num_test_failures += 1
142
143% endfor
144test_runner_log('now kill DNS server')
145dns_server_subprocess.kill()
146dns_server_subprocess.wait()
147test_runner_log('%d tests failed.' % num_test_failures)
148sys.exit(num_test_failures)</%def>
149