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