• 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_TRACE': 'cares_resolver,cares_address_sorting'})
59
60def wait_until_dns_server_is_up(args,
61                                dns_server_subprocess,
62                                dns_server_subprocess_output):
63  for i in range(0, 30):
64    test_runner_log('Health check: attempt to connect to DNS server over TCP.')
65    tcp_connect_subprocess = subprocess.Popen(python_args([
66        args.tcp_connect_bin_path,
67        '--server_host', '127.0.0.1',
68        '--server_port', str(args.dns_server_port),
69        '--timeout', str(1)]))
70    tcp_connect_subprocess.communicate()
71    if tcp_connect_subprocess.returncode == 0:
72      test_runner_log(('Health check: attempt to make an A-record '
73                       'query to DNS server.'))
74      dns_resolver_subprocess = subprocess.Popen(python_args([
75          args.dns_resolver_bin_path,
76          '--qname', 'health-check-local-dns-server-is-alive.resolver-tests.grpctestingexp',
77          '--server_host', '127.0.0.1',
78          '--server_port', str(args.dns_server_port)]),
79          stdout=subprocess.PIPE)
80      dns_resolver_stdout, _ = dns_resolver_subprocess.communicate()
81      if dns_resolver_subprocess.returncode == 0:
82        if '123.123.123.123' in dns_resolver_stdout:
83          test_runner_log(('DNS server is up! '
84                           'Successfully reached it over UDP and TCP.'))
85        return
86    time.sleep(0.1)
87  dns_server_subprocess.kill()
88  dns_server_subprocess.wait()
89  test_runner_log(('Failed to reach DNS server over TCP and/or UDP. '
90                   'Exitting without running tests.'))
91  test_runner_log('======= DNS server stdout '
92                  '(merged stdout and stderr) =============')
93  with open(dns_server_subprocess_output, 'r') as l:
94    test_runner_log(l.read())
95  test_runner_log('======= end DNS server output=========')
96  sys.exit(1)
97
98dns_server_subprocess_output = tempfile.mktemp()
99with open(dns_server_subprocess_output, 'w') as l:
100  dns_server_subprocess = subprocess.Popen(python_args([
101      args.dns_server_bin_path,
102      '--port', str(args.dns_server_port),
103      '--records_config_path', args.records_config_path]),
104      stdin=subprocess.PIPE,
105      stdout=l,
106      stderr=l)
107
108def _quit_on_signal(signum, _frame):
109  test_runner_log('Received signal: %d' % signum)
110  dns_server_subprocess.kill()
111  dns_server_subprocess.wait()
112  sys.exit(1)
113
114signal.signal(signal.SIGINT, _quit_on_signal)
115signal.signal(signal.SIGTERM, _quit_on_signal)
116wait_until_dns_server_is_up(args,
117                            dns_server_subprocess,
118                            dns_server_subprocess_output)
119num_test_failures = 0
120
121% for test in tests:
122test_runner_log('Run test with target: %s' % '${test['test_title']}')\
123
124current_test_subprocess = subprocess.Popen([\
125
126  args.test_bin_path,\
127
128  % for arg_name_and_value in test['arg_names_and_values']:
129\
130  '--${arg_name_and_value[0]}', '${arg_name_and_value[1]}',\
131
132\
133  % endfor
134  '--local_dns_server_address', '127.0.0.1:%d' % args.dns_server_port])\
135
136current_test_subprocess.communicate()\
137
138if current_test_subprocess.returncode != 0:\
139
140  num_test_failures += 1
141
142% endfor
143test_runner_log('now kill DNS server')
144dns_server_subprocess.kill()
145dns_server_subprocess.wait()
146test_runner_log('%d tests failed.' % num_test_failures)
147sys.exit(num_test_failures)</%def>
148