resolver_component_tests_runner_invoker.cc 5.6 KB

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