2ae8da9fc75880bfa2427f2b5361c6e1431b7b88
[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 <string>
28 #include <thread>
29 #include <vector>
30
31 #include "absl/flags/flag.h"
32
33 #ifdef __FreeBSD__
34 #include <sys/wait.h>
35 #endif
36
37 #include "src/core/lib/gpr/env.h"
38 #include "test/core/util/port.h"
39 #include "test/core/util/test_config.h"
40 #include "test/cpp/util/subprocess.h"
41 #include "test/cpp/util/test_config.h"
42
43 ABSL_FLAG(
44     bool, running_under_bazel, false,
45     "True if this test is running under bazel. "
46     "False indicates that this test is running under run_tests.py. "
47     "Child process test binaries are located differently based on this flag. ");
48
49 ABSL_FLAG(std::string, test_bin_name, "",
50           "Name, without the preceding path, of the test binary");
51
52 ABSL_FLAG(std::string, grpc_test_directory_relative_to_test_srcdir,
53           "/com_github_grpc_grpc",
54           "This flag only applies if runner_under_bazel is true. This "
55           "flag is ignored if runner_under_bazel is false. "
56           "Directory of the <repo-root>/test directory relative to bazel's "
57           "TEST_SRCDIR environment variable");
58
59 ABSL_FLAG(std::string, extra_args, "",
60           "Comma-separated list of opaque command args to plumb through to "
61           "the binary pointed at by --test_bin_name");
62
63 using grpc::SubProcess;
64
65 namespace grpc {
66
67 namespace testing {
68
69 void InvokeResolverComponentTestsRunner(
70     std::string test_runner_bin_path, const std::string& test_bin_path,
71     const std::string& dns_server_bin_path,
72     const std::string& records_config_path,
73     const std::string& dns_resolver_bin_path,
74     const std::string& tcp_connect_bin_path) {
75   int dns_server_port = grpc_pick_unused_port_or_die();
76
77   SubProcess* test_driver = new SubProcess(
78       {std::move(test_runner_bin_path), "--test_bin_path=" + test_bin_path,
79        "--dns_server_bin_path=" + dns_server_bin_path,
80        "--records_config_path=" + records_config_path,
81        "--dns_server_port=" + std::to_string(dns_server_port),
82        "--dns_resolver_bin_path=" + dns_resolver_bin_path,
83        "--tcp_connect_bin_path=" + tcp_connect_bin_path,
84        "--extra_args=" + absl::GetFlag(FLAGS_extra_args)});
85   gpr_mu test_driver_mu;
86   gpr_mu_init(&test_driver_mu);
87   gpr_cv test_driver_cv;
88   gpr_cv_init(&test_driver_cv);
89   int test_driver_done = 0;
90   int status = test_driver->Join();
91   if (WIFEXITED(status)) {
92     if (WEXITSTATUS(status)) {
93       gpr_log(GPR_INFO,
94               "Resolver component test test-runner exited with code %d",
95               WEXITSTATUS(status));
96       abort();
97     }
98   } else if (WIFSIGNALED(status)) {
99     gpr_log(GPR_INFO,
100             "Resolver component test test-runner ended from signal %d",
101             WTERMSIG(status));
102     abort();
103   } else {
104     gpr_log(GPR_INFO,
105             "Resolver component test test-runner ended with unknown status %d",
106             status);
107     abort();
108   }
109   gpr_mu_lock(&test_driver_mu);
110   test_driver_done = 1;
111   gpr_cv_signal(&test_driver_cv);
112   gpr_mu_unlock(&test_driver_mu);
113   delete test_driver;
114   gpr_mu_destroy(&test_driver_mu);
115   gpr_cv_destroy(&test_driver_cv);
116 }
117
118 }  // namespace testing
119
120 }  // namespace grpc
121
122 int main(int argc, char** argv) {
123   grpc::testing::TestEnvironment env(argc, argv);
124   grpc::testing::InitTest(&argc, &argv, true);
125   grpc_init();
126   GPR_ASSERT(!absl::GetFlag(FLAGS_test_bin_name).empty());
127   std::string my_bin = argv[0];
128   if (absl::GetFlag(FLAGS_running_under_bazel)) {
129     GPR_ASSERT(!absl::GetFlag(FLAGS_grpc_test_directory_relative_to_test_srcdir)
130                     .empty());
131     // Use bazel's TEST_SRCDIR environment variable to locate the "test data"
132     // binaries.
133     char* test_srcdir = gpr_getenv("TEST_SRCDIR");
134     std::string const bin_dir =
135         test_srcdir +
136         absl::GetFlag(FLAGS_grpc_test_directory_relative_to_test_srcdir) +
137         std::string("/test/cpp/naming");
138     // Invoke bazel's executeable links to the .sh and .py scripts (don't use
139     // the .sh and .py suffixes) to make
140     // sure that we're using bazel's test environment.
141     grpc::testing::InvokeResolverComponentTestsRunner(
142         bin_dir + "/resolver_component_tests_runner",
143         bin_dir + "/" + absl::GetFlag(FLAGS_test_bin_name),
144         bin_dir + "/utils/dns_server",
145         bin_dir + "/resolver_test_record_groups.yaml",
146         bin_dir + "/utils/dns_resolver", bin_dir + "/utils/tcp_connect");
147     gpr_free(test_srcdir);
148   } else {
149     // Get the current binary's directory relative to repo root to invoke the
150     // correct build config (asan/tsan/dbg, etc.).
151     std::string const bin_dir = my_bin.substr(0, my_bin.rfind('/'));
152     // Invoke the .sh and .py scripts directly where they are in source code.
153     grpc::testing::InvokeResolverComponentTestsRunner(
154         "test/cpp/naming/resolver_component_tests_runner.py",
155         bin_dir + "/" + absl::GetFlag(FLAGS_test_bin_name),
156         "test/cpp/naming/utils/dns_server.py",
157         "test/cpp/naming/resolver_test_record_groups.yaml",
158         "test/cpp/naming/utils/dns_resolver.py",
159         "test/cpp/naming/utils/tcp_connect.py");
160   }
161   grpc_shutdown();
162   return 0;
163 }