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 <grpc/grpc.h>
20 #include <grpc/support/alloc.h>
21 #include <grpc/support/log.h>
22 #include <grpc/support/string_util.h>
23 #include <signal.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <gflags/gflags.h>
28 #include <string>
29 #include <thread>
30 #include <vector>
31
32 #include "test/cpp/util/subprocess.h"
33 #include "test/cpp/util/test_config.h"
34
35 #include "src/core/lib/gpr/env.h"
36 #include "test/core/util/port.h"
37
38 DEFINE_bool(
39 running_under_bazel, false,
40 "True if this test is running under bazel. "
41 "False indicates that this test is running under run_tests.py. "
42 "Child process test binaries are located differently based on this flag. ");
43
44 DEFINE_string(test_bin_name, "",
45 "Name, without the preceding path, of the test binary");
46
47 DEFINE_string(grpc_test_directory_relative_to_test_srcdir,
48 "/com_github_grpc_grpc",
49 "This flag only applies if runner_under_bazel is true. This "
50 "flag is ignored if runner_under_bazel is false. "
51 "Directory of the <repo-root>/test directory relative to bazel's "
52 "TEST_SRCDIR environment variable");
53
54 using grpc::SubProcess;
55
56 static volatile sig_atomic_t abort_wait_for_child = 0;
57
sighandler(int sig)58 static void sighandler(int sig) { abort_wait_for_child = 1; }
59
register_sighandler()60 static void register_sighandler() {
61 struct sigaction act;
62 memset(&act, 0, sizeof(act));
63 act.sa_handler = sighandler;
64 sigaction(SIGINT, &act, nullptr);
65 sigaction(SIGTERM, &act, nullptr);
66 }
67
68 namespace {
69
70 const int kTestTimeoutSeconds = 60 * 2;
71
RunSigHandlingThread(SubProcess * test_driver,gpr_mu * test_driver_mu,gpr_cv * test_driver_cv,int * test_driver_done)72 void RunSigHandlingThread(SubProcess* test_driver, gpr_mu* test_driver_mu,
73 gpr_cv* test_driver_cv, int* test_driver_done) {
74 gpr_timespec overall_deadline =
75 gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
76 gpr_time_from_seconds(kTestTimeoutSeconds, GPR_TIMESPAN));
77 while (true) {
78 gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC);
79 if (gpr_time_cmp(now, overall_deadline) > 0 || abort_wait_for_child) break;
80 gpr_mu_lock(test_driver_mu);
81 if (*test_driver_done) {
82 gpr_mu_unlock(test_driver_mu);
83 return;
84 }
85 gpr_timespec wait_deadline = gpr_time_add(
86 gpr_now(GPR_CLOCK_MONOTONIC), gpr_time_from_seconds(1, GPR_TIMESPAN));
87 gpr_cv_wait(test_driver_cv, test_driver_mu, wait_deadline);
88 gpr_mu_unlock(test_driver_mu);
89 }
90 gpr_log(GPR_DEBUG,
91 "Test timeout reached or received signal. Interrupting test driver "
92 "child process.");
93 test_driver->Interrupt();
94 return;
95 }
96 } // namespace
97
98 namespace grpc {
99
100 namespace testing {
101
InvokeResolverComponentTestsRunner(std::string test_runner_bin_path,const std::string & test_bin_path,const std::string & dns_server_bin_path,const std::string & records_config_path,const std::string & dns_resolver_bin_path,const std::string & tcp_connect_bin_path)102 void InvokeResolverComponentTestsRunner(
103 std::string test_runner_bin_path, const std::string& test_bin_path,
104 const std::string& dns_server_bin_path,
105 const std::string& records_config_path,
106 const std::string& dns_resolver_bin_path,
107 const std::string& tcp_connect_bin_path) {
108 int dns_server_port = grpc_pick_unused_port_or_die();
109
110 SubProcess* test_driver = new SubProcess(
111 {std::move(test_runner_bin_path), "--test_bin_path=" + test_bin_path,
112 "--dns_server_bin_path=" + dns_server_bin_path,
113 "--records_config_path=" + records_config_path,
114 "--dns_server_port=" + std::to_string(dns_server_port),
115 "--dns_resolver_bin_path=" + dns_resolver_bin_path,
116 "--tcp_connect_bin_path=" + tcp_connect_bin_path});
117 gpr_mu test_driver_mu;
118 gpr_mu_init(&test_driver_mu);
119 gpr_cv test_driver_cv;
120 gpr_cv_init(&test_driver_cv);
121 int test_driver_done = 0;
122 register_sighandler();
123 std::thread sig_handling_thread(RunSigHandlingThread, test_driver,
124 &test_driver_mu, &test_driver_cv,
125 &test_driver_done);
126 int status = test_driver->Join();
127 if (WIFEXITED(status)) {
128 if (WEXITSTATUS(status)) {
129 gpr_log(GPR_INFO,
130 "Resolver component test test-runner exited with code %d",
131 WEXITSTATUS(status));
132 abort();
133 }
134 } else if (WIFSIGNALED(status)) {
135 gpr_log(GPR_INFO,
136 "Resolver component test test-runner ended from signal %d",
137 WTERMSIG(status));
138 abort();
139 } else {
140 gpr_log(GPR_INFO,
141 "Resolver component test test-runner ended with unknown status %d",
142 status);
143 abort();
144 }
145 gpr_mu_lock(&test_driver_mu);
146 test_driver_done = 1;
147 gpr_cv_signal(&test_driver_cv);
148 gpr_mu_unlock(&test_driver_mu);
149 sig_handling_thread.join();
150 delete test_driver;
151 gpr_mu_destroy(&test_driver_mu);
152 gpr_cv_destroy(&test_driver_cv);
153 }
154
155 } // namespace testing
156
157 } // namespace grpc
158
main(int argc,char ** argv)159 int main(int argc, char** argv) {
160 grpc::testing::InitTest(&argc, &argv, true);
161 grpc_init();
162 GPR_ASSERT(FLAGS_test_bin_name != "");
163 std::string my_bin = argv[0];
164 if (FLAGS_running_under_bazel) {
165 GPR_ASSERT(FLAGS_grpc_test_directory_relative_to_test_srcdir != "");
166 // Use bazel's TEST_SRCDIR environment variable to locate the "test data"
167 // binaries.
168 char* test_srcdir = gpr_getenv("TEST_SRCDIR");
169 std::string const bin_dir =
170 test_srcdir + FLAGS_grpc_test_directory_relative_to_test_srcdir +
171 std::string("/test/cpp/naming");
172 // Invoke bazel's executeable links to the .sh and .py scripts (don't use
173 // the .sh and .py suffixes) to make
174 // sure that we're using bazel's test environment.
175 grpc::testing::InvokeResolverComponentTestsRunner(
176 bin_dir + "/resolver_component_tests_runner",
177 bin_dir + "/" + FLAGS_test_bin_name, bin_dir + "/utils/dns_server",
178 bin_dir + "/resolver_test_record_groups.yaml",
179 bin_dir + "/utils/dns_resolver", bin_dir + "/utils/tcp_connect");
180 gpr_free(test_srcdir);
181 } else {
182 // Get the current binary's directory relative to repo root to invoke the
183 // correct build config (asan/tsan/dbg, etc.).
184 std::string const bin_dir = my_bin.substr(0, my_bin.rfind('/'));
185 // Invoke the .sh and .py scripts directly where they are in source code.
186 grpc::testing::InvokeResolverComponentTestsRunner(
187 "test/cpp/naming/resolver_component_tests_runner.py",
188 bin_dir + "/" + FLAGS_test_bin_name,
189 "test/cpp/naming/utils/dns_server.py",
190 "test/cpp/naming/resolver_test_record_groups.yaml",
191 "test/cpp/naming/utils/dns_resolver.py",
192 "test/cpp/naming/utils/tcp_connect.py");
193 }
194 grpc_shutdown();
195 return 0;
196 }
197