diff options
author | Alex Polcyn <apolcyn@google.com> | 2018-03-05 09:33:47 +0000 |
---|---|---|
committer | Alexander Polcyn <apolcyn@google.com> | 2018-03-06 11:52:58 -0800 |
commit | 3c5e12de08a255edf78890d7408e2f98ebca7e2d (patch) | |
tree | fb2b08b3aafb3c908a2ceeb6beacb15c5d0060a7 | |
parent | 092f19947426d74a8b992072e3a9805c3885eada (diff) |
Remove the C++ test dependency on dig and dnsutils
-rw-r--r-- | templates/test/cpp/naming/resolver_component_tests_defs.include | 10 | ||||
-rw-r--r-- | test/cpp/naming/BUILD | 15 | ||||
-rwxr-xr-x | test/cpp/naming/generate_resolver_component_tests.bzl | 2 | ||||
-rwxr-xr-x | test/cpp/naming/resolver_component_tests_runner.sh | 10 | ||||
-rw-r--r-- | test/cpp/naming/resolver_component_tests_runner_invoker.cc | 15 | ||||
-rwxr-xr-x | test/cpp/naming/test_dns_resolver.py | 48 | ||||
-rwxr-xr-x | test/cpp/naming/test_tcp_connect.py | 35 |
7 files changed, 123 insertions, 12 deletions
diff --git a/templates/test/cpp/naming/resolver_component_tests_defs.include b/templates/test/cpp/naming/resolver_component_tests_defs.include index efa54a456b..8faabafc79 100644 --- a/templates/test/cpp/naming/resolver_component_tests_defs.include +++ b/templates/test/cpp/naming/resolver_component_tests_defs.include @@ -22,8 +22,10 @@ FLAGS_test_bin_path=$(echo "$1" | grep '\--test_bin_path=' | cut -d "=" -f 2) FLAGS_dns_server_bin_path=$(echo "$2" | grep '\--dns_server_bin_path=' | cut -d "=" -f 2) FLAGS_records_config_path=$(echo "$3" | grep '\--records_config_path=' | cut -d "=" -f 2) FLAGS_test_dns_server_port=$(echo "$4" | grep '\--test_dns_server_port=' | cut -d "=" -f 2) +FLAGS_test_dns_resolver_bin_path=$(echo "$5" | grep '\--test_dns_resolver_bin_path=' | cut -d "=" -f 2) +FLAGS_test_tcp_connect_bin_path=$(echo "$6" | grep '\--test_tcp_connect_bin_path=' | cut -d "=" -f 2) -for cmd_arg in "$FLAGS_test_bin_path" "$FLAGS_dns_server_bin_path" "$FLAGS_records_config_path" "$FLAGS_test_dns_server_port"; do +for cmd_arg in "$FLAGS_test_bin_path" "$FLAGS_dns_server_bin_path" "$FLAGS_records_config_path" "$FLAGS_test_dns_server_port" "$FLAGS_test_dns_resolver_bin_path" "$FLAGS_test_tcp_connect_bin_path"; do if [[ "$cmd_arg" == "" ]]; then echo "Missing a CMD arg" && exit 1 fi @@ -41,10 +43,10 @@ echo "Local DNS server started. PID: $DNS_SERVER_PID" # Health check local DNS server TCP and UDP ports for ((i=0;i<30;i++)); do - echo "Retry health-check DNS query to local DNS server over tcp and udp" + echo "Retry health-check local DNS server by attempting a DNS query and TCP handshake" RETRY=0 - dig A health-check-local-dns-server-is-alive.resolver-tests.grpctestingexp. @localhost -p "$FLAGS_test_dns_server_port" +tries=1 +timeout=1 | grep '123.123.123.123' || RETRY=1 - dig A health-check-local-dns-server-is-alive.resolver-tests.grpctestingexp. @localhost -p "$FLAGS_test_dns_server_port" +tries=1 +timeout=1 +tcp | grep '123.123.123.123' || RETRY=1 + $FLAGS_test_dns_resolver_bin_path -s 127.0.0.1 -p "$FLAGS_test_dns_server_port" -n health-check-local-dns-server-is-alive.resolver-tests.grpctestingexp. -t 1 | grep '123.123.123.123' || RETRY=1 + $FLAGS_test_tcp_connect_bin_path -s 127.0.0.1 -p "$FLAGS_test_dns_server_port" -t 1 || RETRY=1 if [[ "$RETRY" == 0 ]]; then break fi; diff --git a/test/cpp/naming/BUILD b/test/cpp/naming/BUILD index a8fa0a078a..9e102286aa 100644 --- a/test/cpp/naming/BUILD +++ b/test/cpp/naming/BUILD @@ -47,4 +47,19 @@ grpc_py_binary( ] ) +grpc_py_binary( + name = "test_dns_resolver", + srcs = ["test_dns_resolver.py"], + testonly = True, + external_deps = [ + "twisted", + ] +) + +grpc_py_binary( + name = "test_tcp_connect", + srcs = ["test_tcp_connect.py"], + testonly = True, +) + generate_resolver_component_tests() diff --git a/test/cpp/naming/generate_resolver_component_tests.bzl b/test/cpp/naming/generate_resolver_component_tests.bzl index 118d9452d9..dc1a1304c7 100755 --- a/test/cpp/naming/generate_resolver_component_tests.bzl +++ b/test/cpp/naming/generate_resolver_component_tests.bzl @@ -55,6 +55,8 @@ def generate_resolver_component_tests(): ":resolver_component_tests_runner", ":resolver_component_test%s" % unsecure_build_config_suffix, ":test_dns_server", + ":test_dns_resolver", + ":test_tcp_connect", "resolver_test_record_groups.yaml", # include the transitive dependency so that the dns sever py binary can locate this ], args = [ diff --git a/test/cpp/naming/resolver_component_tests_runner.sh b/test/cpp/naming/resolver_component_tests_runner.sh index 618a173dc0..2510afce48 100755 --- a/test/cpp/naming/resolver_component_tests_runner.sh +++ b/test/cpp/naming/resolver_component_tests_runner.sh @@ -22,8 +22,10 @@ FLAGS_test_bin_path=$(echo "$1" | grep '\--test_bin_path=' | cut -d "=" -f 2) FLAGS_dns_server_bin_path=$(echo "$2" | grep '\--dns_server_bin_path=' | cut -d "=" -f 2) FLAGS_records_config_path=$(echo "$3" | grep '\--records_config_path=' | cut -d "=" -f 2) FLAGS_test_dns_server_port=$(echo "$4" | grep '\--test_dns_server_port=' | cut -d "=" -f 2) +FLAGS_test_dns_resolver_bin_path=$(echo "$5" | grep '\--test_dns_resolver_bin_path=' | cut -d "=" -f 2) +FLAGS_test_tcp_connect_bin_path=$(echo "$6" | grep '\--test_tcp_connect_bin_path=' | cut -d "=" -f 2) -for cmd_arg in "$FLAGS_test_bin_path" "$FLAGS_dns_server_bin_path" "$FLAGS_records_config_path" "$FLAGS_test_dns_server_port"; do +for cmd_arg in "$FLAGS_test_bin_path" "$FLAGS_dns_server_bin_path" "$FLAGS_records_config_path" "$FLAGS_test_dns_server_port" "$FLAGS_test_dns_resolver_bin_path" "$FLAGS_test_tcp_connect_bin_path"; do if [[ "$cmd_arg" == "" ]]; then echo "Missing a CMD arg" && exit 1 fi @@ -41,10 +43,10 @@ echo "Local DNS server started. PID: $DNS_SERVER_PID" # Health check local DNS server TCP and UDP ports for ((i=0;i<30;i++)); do - echo "Retry health-check DNS query to local DNS server over tcp and udp" + echo "Retry health-check local DNS server by attempting a DNS query and TCP handshake" RETRY=0 - dig A health-check-local-dns-server-is-alive.resolver-tests.grpctestingexp. @localhost -p "$FLAGS_test_dns_server_port" +tries=1 +timeout=1 | grep '123.123.123.123' || RETRY=1 - dig A health-check-local-dns-server-is-alive.resolver-tests.grpctestingexp. @localhost -p "$FLAGS_test_dns_server_port" +tries=1 +timeout=1 +tcp | grep '123.123.123.123' || RETRY=1 + $FLAGS_test_dns_resolver_bin_path -s 127.0.0.1 -p "$FLAGS_test_dns_server_port" -n health-check-local-dns-server-is-alive.resolver-tests.grpctestingexp. -t 1 | grep '123.123.123.123' || RETRY=1 + $FLAGS_test_tcp_connect_bin_path -s 127.0.0.1 -p "$FLAGS_test_dns_server_port" -t 1 || RETRY=1 if [[ "$RETRY" == 0 ]]; then break fi; diff --git a/test/cpp/naming/resolver_component_tests_runner_invoker.cc b/test/cpp/naming/resolver_component_tests_runner_invoker.cc index 306c322780..fadfffe0f4 100644 --- a/test/cpp/naming/resolver_component_tests_runner_invoker.cc +++ b/test/cpp/naming/resolver_component_tests_runner_invoker.cc @@ -102,14 +102,18 @@ namespace testing { void InvokeResolverComponentTestsRunner(std::string test_runner_bin_path, std::string test_bin_path, std::string dns_server_bin_path, - std::string records_config_path) { + std::string records_config_path, + std::string test_dns_resolver_bin_path, + std::string test_tcp_connect_bin_path) { int test_dns_server_port = grpc_pick_unused_port_or_die(); SubProcess* test_driver = new SubProcess( {test_runner_bin_path, "--test_bin_path=" + test_bin_path, "--dns_server_bin_path=" + dns_server_bin_path, "--records_config_path=" + records_config_path, - "--test_dns_server_port=" + std::to_string(test_dns_server_port)}); + "--test_dns_server_port=" + std::to_string(test_dns_server_port), + "--test_dns_resolver_bin_path=" + test_dns_resolver_bin_path, + "--test_tcp_connect_bin_path=" + test_tcp_connect_bin_path}); gpr_mu test_driver_mu; gpr_mu_init(&test_driver_mu); gpr_cv test_driver_cv; @@ -171,7 +175,8 @@ int main(int argc, char** argv) { grpc::testing::InvokeResolverComponentTestsRunner( bin_dir + "/resolver_component_tests_runner", bin_dir + "/" + FLAGS_test_bin_name, bin_dir + "/test_dns_server", - bin_dir + "/resolver_test_record_groups.yaml"); + bin_dir + "/resolver_test_record_groups.yaml", + bin_dir + "/test_dns_resolver", bin_dir + "/test_tcp_connect"); } else { // Get the current binary's directory relative to repo root to invoke the // correct build config (asan/tsan/dbg, etc.). @@ -181,7 +186,9 @@ int main(int argc, char** argv) { "test/cpp/naming/resolver_component_tests_runner.sh", bin_dir + "/" + FLAGS_test_bin_name, "test/cpp/naming/test_dns_server.py", - "test/cpp/naming/resolver_test_record_groups.yaml"); + "test/cpp/naming/resolver_test_record_groups.yaml", + "test/cpp/naming/test_dns_resolver.py", + "test/cpp/naming/test_tcp_connect.py"); } grpc_shutdown(); return 0; diff --git a/test/cpp/naming/test_dns_resolver.py b/test/cpp/naming/test_dns_resolver.py new file mode 100755 index 0000000000..6b272444e7 --- /dev/null +++ b/test/cpp/naming/test_dns_resolver.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python2.7 +# Copyright 2015 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Makes DNS queries for A records to specified servers""" + +import argparse +import signal +import twisted.internet.task as task +import twisted.names.client as client + +def main(): + argp = argparse.ArgumentParser(description='Make DNS queries for A records') + argp.add_argument('-s', '--server_host', default='127.0.0.1', type=str, + help='Host for DNS server to listen on for TCP and UDP.') + argp.add_argument('-p', '--server_port', default=53, type=int, + help='Port that the DNS server is listening on.') + argp.add_argument('-n', '--qname', default=None, type=str, + help=('Name of the record to query for. ')) + argp.add_argument('-t', '--timeout', default=1, type=int, + help=('Force process exit after this number of seconds.')) + args = argp.parse_args() + signal.alarm(args.timeout) + def OnResolverResultAvailable(result): + answers, authority, additional = result + for a in answers: + print(a.payload) + def BeginQuery(reactor, qname): + servers = [(args.server_host, args.server_port)] + resolver = client.Resolver(servers=servers) + deferred_result = resolver.lookupAddress(args.qname) + deferred_result.addCallback(OnResolverResultAvailable) + return deferred_result + task.react(BeginQuery, [args.qname]) + +if __name__ == '__main__': + main() diff --git a/test/cpp/naming/test_tcp_connect.py b/test/cpp/naming/test_tcp_connect.py new file mode 100755 index 0000000000..bf7455e3c2 --- /dev/null +++ b/test/cpp/naming/test_tcp_connect.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python2.7 +# Copyright 2015 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Opens a TCP connection to a specified server and then exits.""" + +import argparse +import signal +import socket + +def main(): + argp = argparse.ArgumentParser(description='Open a TCP handshake to a server') + argp.add_argument('-s', '--server_host', default=None, type=str, + help='Server host name or IP.') + argp.add_argument('-p', '--server_port', default=0, type=int, + help='Port that the server is listening on.') + argp.add_argument('-t', '--timeout', default=1, type=int, + help='Force process exit after this number of seconds.') + args = argp.parse_args() + signal.alarm(args.timeout) + socket.create_connection([args.server_host, args.server_port]) + +if __name__ == '__main__': + main() |