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