resolver_component_tests_defs.include 5.6 KB

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