Debian Codegraph @

test/cpp/naming/resolver_component_tests_runner_invoker.cc

raw
1 /*
2 *
3 * Copyright 2017 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <signal.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include <string>
24 #include <thread>
25 #include <vector>
26
27 #include "absl/flags/flag.h"
28
29 #include <grpc/grpc.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/string_util.h>
33
34 #ifdef __FreeBSD__
35 #include <sys/wait.h>
36 #endif
37
38 #include "src/core/lib/gprpp/env.h"
39 #include "test/core/util/port.h"
40 #include "test/core/util/test_config.h"
41 #include "test/cpp/util/subprocess.h"
42 #include "test/cpp/util/test_config.h"
43
44 ABSL_FLAG(
45 bool, running_under_bazel, false,
46 "True if this test is running under bazel. "
47 "False indicates that this test is running under run_tests.py. "
48 "Child process test binaries are located differently based on this flag. ");
49
50 ABSL_FLAG(std::string, test_bin_name, "",
51 "Name, without the preceding path, of the test binary");
52
53 ABSL_FLAG(std::string, grpc_test_directory_relative_to_test_srcdir,
54 "/com_github_grpc_grpc",
55 "This flag only applies if runner_under_bazel is true. This "
56 "flag is ignored if runner_under_bazel is false. "
57 "Directory of the <repo-root>/test directory relative to bazel's "
58 "TEST_SRCDIR environment variable");
59
60 ABSL_FLAG(std::string, extra_args, "",
61 "Comma-separated list of opaque command args to plumb through to "
62 "the binary pointed at by --test_bin_name");
63
64 namespace grpc {
65
66 namespace testing {
67
68 void InvokeResolverComponentTestsRunner(
69 std::string test_runner_bin_path, const std::string& test_bin_path,
70 const std::string& dns_server_bin_path,
71 const std::string& records_config_path,
72 const std::string& dns_resolver_bin_path,
73 const std::string& tcp_connect_bin_path) {
74 int dns_server_port = grpc_pick_unused_port_or_die();
75
76 SubProcess* test_driver = new SubProcess(
77 {std::move(test_runner_bin_path), "--test_bin_path=" + test_bin_path,
78 "--dns_server_bin_path=" + dns_server_bin_path,
79 "--records_config_path=" + records_config_path,
80 "--dns_server_port=" + std::to_string(dns_server_port),
81 "--dns_resolver_bin_path=" + dns_resolver_bin_path,
82 "--tcp_connect_bin_path=" + tcp_connect_bin_path,
83 "--extra_args=" + absl::GetFlag(FLAGS_extra_args)});
84 gpr_mu test_driver_mu;
85 gpr_mu_init(&test_driver_mu);
86 gpr_cv test_driver_cv;
87 gpr_cv_init(&test_driver_cv);
88 int status = test_driver->Join();
89 if (WIFEXITED(status)) {
90 if (WEXITSTATUS(status)) {
91 gpr_log(GPR_INFO,
92 "Resolver component test test-runner exited with code %d",
93 WEXITSTATUS(status));
94 abort();
95 }
96 } else if (WIFSIGNALED(status)) {
97 gpr_log(GPR_INFO,
98 "Resolver component test test-runner ended from signal %d",
99 WTERMSIG(status));
100 abort();
101 } else {
102 gpr_log(GPR_INFO,
103 "Resolver component test test-runner ended with unknown status %d",
104 status);
105 abort();
106 }
107 gpr_mu_lock(&test_driver_mu);
108 gpr_cv_signal(&test_driver_cv);
109 gpr_mu_unlock(&test_driver_mu);
110 delete test_driver;
111 gpr_mu_destroy(&test_driver_mu);
112 gpr_cv_destroy(&test_driver_cv);
113 }
114
115 } // namespace testing
116
117 } // namespace grpc
118
119 int main(int argc, char** argv) {
120 grpc::testing::TestEnvironment env(&argc, argv);
121 grpc::testing::InitTest(&argc, &argv, true);
122 grpc_init();
123 GPR_ASSERT(!absl::GetFlag(FLAGS_test_bin_name).empty());
124 std::string my_bin = argv[0];
125 if (absl::GetFlag(FLAGS_running_under_bazel)) {
126 GPR_ASSERT(!absl::GetFlag(FLAGS_grpc_test_directory_relative_to_test_srcdir)
127 .empty());
128 // Use bazel's TEST_SRCDIR environment variable to locate the "test data"
129 // binaries.
130 auto test_srcdir = grpc_core::GetEnv("TEST_SRCDIR");
131 std::string const bin_dir =
132 test_srcdir.value() +
133 absl::GetFlag(FLAGS_grpc_test_directory_relative_to_test_srcdir) +
134 std::string("/test/cpp/naming");
135 // Invoke bazel's executeable links to the .sh and .py scripts (don't use
136 // the .sh and .py suffixes) to make
137 // sure that we're using bazel's test environment.
138 grpc::testing::InvokeResolverComponentTestsRunner(
139 bin_dir + "/resolver_component_tests_runner",
140 bin_dir + "/" + absl::GetFlag(FLAGS_test_bin_name),
141 bin_dir + "/utils/dns_server",
142 bin_dir + "/resolver_test_record_groups.yaml",
143 bin_dir + "/utils/dns_resolver", bin_dir + "/utils/tcp_connect");
144 } else {
145 // Get the current binary's directory relative to repo root to invoke the
146 // correct build config (asan/tsan/dbg, etc.).
147 std::string const bin_dir = my_bin.substr(0, my_bin.rfind('/'));
148 // Invoke the .sh and .py scripts directly where they are in source code.
149 grpc::testing::InvokeResolverComponentTestsRunner(
150 "test/cpp/naming/resolver_component_tests_runner.py",
151 bin_dir + "/" + absl::GetFlag(FLAGS_test_bin_name),
152 "test/cpp/naming/utils/dns_server.py",
153 "test/cpp/naming/resolver_test_record_groups.yaml",
154 "test/cpp/naming/utils/dns_resolver.py",
155 "test/cpp/naming/utils/tcp_connect.py");
156 }
157 grpc_shutdown();
158 return 0;
159 }