resolver_component_tests_runner_invoker.cc
Go to the documentation of this file.
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 <signal.h>
20 #include <string.h>
21 #include <unistd.h>
22 
23 #include <string>
24 #include <thread>
25 #include <vector>
26 
27 #include "absl/flags/flag.h"
28 
29 #include <grpc/grpc.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
33 
34 #ifdef __FreeBSD__
35 #include <sys/wait.h>
36 #endif
37 
38 #include "src/core/lib/gpr/env.h"
39 #include "test/core/util/port.h"
43 
44 ABSL_FLAG(
45  bool, running_under_bazel, false,
46  "True if this test is running under bazel. "
47  "False indicates that this test is running under run_tests.py. "
48  "Child process test binaries are located differently based on this flag. ");
49 
50 ABSL_FLAG(std::string, test_bin_name, "",
51  "Name, without the preceding path, of the test binary");
52 
53 ABSL_FLAG(std::string, grpc_test_directory_relative_to_test_srcdir,
54  "/com_github_grpc_grpc",
55  "This flag only applies if runner_under_bazel is true. This "
56  "flag is ignored if runner_under_bazel is false. "
57  "Directory of the <repo-root>/test directory relative to bazel's "
58  "TEST_SRCDIR environment variable");
59 
61  "Comma-separated list of opaque command args to plumb through to "
62  "the binary pointed at by --test_bin_name");
63 
64 using grpc::SubProcess;
65 
66 namespace grpc {
67 
68 namespace testing {
69 
71  std::string test_runner_bin_path, const std::string& test_bin_path,
72  const std::string& dns_server_bin_path,
74  const std::string& dns_resolver_bin_path,
75  const std::string& tcp_connect_bin_path) {
76  int dns_server_port = grpc_pick_unused_port_or_die();
77 
78  SubProcess* test_driver = new SubProcess(
79  {std::move(test_runner_bin_path), "--test_bin_path=" + test_bin_path,
80  "--dns_server_bin_path=" + dns_server_bin_path,
81  "--records_config_path=" + records_config_path,
82  "--dns_server_port=" + std::to_string(dns_server_port),
83  "--dns_resolver_bin_path=" + dns_resolver_bin_path,
84  "--tcp_connect_bin_path=" + tcp_connect_bin_path,
85  "--extra_args=" + absl::GetFlag(FLAGS_extra_args)});
86  gpr_mu test_driver_mu;
87  gpr_mu_init(&test_driver_mu);
88  gpr_cv test_driver_cv;
89  gpr_cv_init(&test_driver_cv);
90  int status = test_driver->Join();
91  if (WIFEXITED(status)) {
92  if (WEXITSTATUS(status)) {
94  "Resolver component test test-runner exited with code %d",
95  WEXITSTATUS(status));
96  abort();
97  }
98  } else if (WIFSIGNALED(status)) {
100  "Resolver component test test-runner ended from signal %d",
101  WTERMSIG(status));
102  abort();
103  } else {
105  "Resolver component test test-runner ended with unknown status %d",
106  status);
107  abort();
108  }
109  gpr_mu_lock(&test_driver_mu);
110  gpr_cv_signal(&test_driver_cv);
111  gpr_mu_unlock(&test_driver_mu);
112  delete test_driver;
113  gpr_mu_destroy(&test_driver_mu);
114  gpr_cv_destroy(&test_driver_cv);
115 }
116 
117 } // namespace testing
118 
119 } // namespace grpc
120 
121 int main(int argc, char** argv) {
122  grpc::testing::TestEnvironment env(&argc, argv);
123  grpc::testing::InitTest(&argc, &argv, true);
124  grpc_init();
125  GPR_ASSERT(!absl::GetFlag(FLAGS_test_bin_name).empty());
126  std::string my_bin = argv[0];
127  if (absl::GetFlag(FLAGS_running_under_bazel)) {
128  GPR_ASSERT(!absl::GetFlag(FLAGS_grpc_test_directory_relative_to_test_srcdir)
129  .empty());
130  // Use bazel's TEST_SRCDIR environment variable to locate the "test data"
131  // binaries.
132  char* test_srcdir = gpr_getenv("TEST_SRCDIR");
133  std::string const bin_dir =
134  test_srcdir +
135  absl::GetFlag(FLAGS_grpc_test_directory_relative_to_test_srcdir) +
136  std::string("/test/cpp/naming");
137  // Invoke bazel's executeable links to the .sh and .py scripts (don't use
138  // the .sh and .py suffixes) to make
139  // sure that we're using bazel's test environment.
141  bin_dir + "/resolver_component_tests_runner",
142  bin_dir + "/" + absl::GetFlag(FLAGS_test_bin_name),
143  bin_dir + "/utils/dns_server",
144  bin_dir + "/resolver_test_record_groups.yaml",
145  bin_dir + "/utils/dns_resolver", bin_dir + "/utils/tcp_connect");
146  gpr_free(test_srcdir);
147  } else {
148  // Get the current binary's directory relative to repo root to invoke the
149  // correct build config (asan/tsan/dbg, etc.).
150  std::string const bin_dir = my_bin.substr(0, my_bin.rfind('/'));
151  // Invoke the .sh and .py scripts directly where they are in source code.
153  "test/cpp/naming/resolver_component_tests_runner.py",
154  bin_dir + "/" + absl::GetFlag(FLAGS_test_bin_name),
155  "test/cpp/naming/utils/dns_server.py",
156  "test/cpp/naming/resolver_test_record_groups.yaml",
157  "test/cpp/naming/utils/dns_resolver.py",
158  "test/cpp/naming/utils/tcp_connect.py");
159  }
160  grpc_shutdown();
161  return 0;
162 }
gpr_cv_signal
GPRAPI void gpr_cv_signal(gpr_cv *cv)
grpc::testing::InitTest
void InitTest(int *argc, char ***argv, bool remove_flags)
Definition: test_config_cc.cc:28
grpc::SubProcess::Join
int Join()
Definition: test/cpp/util/subprocess.cc:40
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
testing
Definition: aws_request_signer_test.cc:25
grpc::status
auto status
Definition: cpp/client/credentials_test.cc:200
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
main
int main(int argc, char **argv)
Definition: resolver_component_tests_runner_invoker.cc:121
log.h
port.h
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
gpr_cv
pthread_cond_t gpr_cv
Definition: impl/codegen/sync_posix.h:48
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
env.h
run_dns_server_for_lb_interop_tests.records_config_path
records_config_path
Definition: run_dns_server_for_lb_interop_tests.py:98
ABSL_FLAG
ABSL_FLAG(bool, running_under_bazel, false, "True if this test is running under bazel. " "False indicates that this test is running under run_tests.py. " "Child process test binaries are located differently based on this flag. ")
gpr_mu_destroy
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
string_util.h
subprocess.h
gpr_getenv
char * gpr_getenv(const char *name)
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gpr_cv_destroy
GPRAPI void gpr_cv_destroy(gpr_cv *cv)
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
gpr_mu_init
GPRAPI void gpr_mu_init(gpr_mu *mu)
grpc.h
grpc::SubProcess
Definition: test/cpp/util/subprocess.h:30
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
absl::GetFlag
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag< T > &flag)
Definition: abseil-cpp/absl/flags/flag.h:98
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
google_benchmark.example.empty
def empty(state)
Definition: example.py:31
test_config.h
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
test_config.h
run_tests_matrix.extra_args
list extra_args
Definition: run_tests_matrix.py:486
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc::testing::InvokeResolverComponentTestsRunner
void 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)
Definition: resolver_component_tests_runner_invoker.cc:70
to_string
static bool to_string(zval *from)
Definition: protobuf/php/ext/google/protobuf/convert.c:333
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
gpr_cv_init
GPRAPI void gpr_cv_init(gpr_cv *cv)


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:04