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