health_service_end2end_test.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <memory>
  34. #include <thread>
  35. #include <grpc++/channel.h>
  36. #include <grpc++/client_context.h>
  37. #include <grpc++/create_channel.h>
  38. #include <grpc++/health_check_service_interface.h>
  39. #include <grpc++/server.h>
  40. #include <grpc++/server_builder.h>
  41. #include <grpc++/server_context.h>
  42. #include <grpc/grpc.h>
  43. #include <gtest/gtest.h>
  44. #include "src/proto/grpc/health/v1/health.grpc.pb.h"
  45. #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
  46. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  47. #include "test/core/util/port.h"
  48. #include "test/core/util/test_config.h"
  49. #include "test/cpp/end2end/test_service_impl.h"
  50. using grpc::health::v1::Health;
  51. using grpc::health::v1::HealthCheckRequest;
  52. using grpc::health::v1::HealthCheckResponse;
  53. namespace grpc {
  54. namespace testing {
  55. namespace {
  56. class HealthServiceEnd2endTest : public ::testing::Test {
  57. protected:
  58. HealthServiceEnd2endTest() {}
  59. void SetUpServer(grpc::Service* explicit_health_service) {
  60. int port = grpc_pick_unused_port_or_die();
  61. server_address_ << "localhost:" << port;
  62. // Setup server
  63. ServerBuilder builder;
  64. builder.AddListeningPort(server_address_.str(),
  65. grpc::InsecureServerCredentials());
  66. // Register a sync service.
  67. builder.RegisterService(&echo_test_service_);
  68. server_ = builder.BuildAndStart();
  69. }
  70. void TearDown() override {
  71. if (server_) {
  72. server_->Shutdown();
  73. }
  74. }
  75. void ResetStubs() {
  76. std::shared_ptr<Channel> channel =
  77. CreateChannel(server_address_.str(), InsecureChannelCredentials());
  78. hc_stub_ = grpc::health::v1::Health::NewStub(channel);
  79. }
  80. void SendHealthCheckRpc(
  81. const grpc::string& service_name, const Status& expected_status,
  82. HealthCheckResponse::ServingStatus expected_serving_status) {
  83. HealthCheckRequest request;
  84. request.set_service(service_name);
  85. HealthCheckResponse response;
  86. ClientContext context;
  87. Status s = hc_stub_->Check(&context, request, &response);
  88. EXPECT_EQ(expected_status.error_code(), s.error_code());
  89. if (s.ok()) {
  90. EXPECT_EQ(expected_serving_status, response.status());
  91. }
  92. }
  93. TestServiceImpl echo_test_service_;
  94. std::unique_ptr<Health::Stub> hc_stub_;
  95. std::unique_ptr<Server> server_;
  96. std::ostringstream server_address_;
  97. };
  98. TEST_F(HealthServiceEnd2endTest, DefaultHealthServiceDisabled) {
  99. EnableDefaultHealthCheckService(false);
  100. EXPECT_FALSE(DefaultHealthCheckServiceEnabled());
  101. SetUpServer(nullptr);
  102. HealthCheckServiceInterface* default_service =
  103. server_->GetHealthCheckService();
  104. EXPECT_TRUE(default_service == nullptr);
  105. }
  106. TEST_F(HealthServiceEnd2endTest, DefaultHealthService) {
  107. EnableDefaultHealthCheckService(true);
  108. EXPECT_TRUE(DefaultHealthCheckServiceEnabled());
  109. SetUpServer(nullptr);
  110. HealthCheckServiceInterface* default_service =
  111. server_->GetHealthCheckService();
  112. EXPECT_TRUE(default_service != nullptr);
  113. const grpc::string kHealthyService("healthy_service");
  114. const grpc::string kUnhealthyService("unhealthy_service");
  115. const grpc::string kNotRegisteredService("not_registered");
  116. const grpc::string kTooLongServiceName(201, 'x');
  117. default_service->SetServingStatus(kHealthyService, true);
  118. default_service->SetServingStatus(kUnhealthyService, false);
  119. ResetStubs();
  120. SendHealthCheckRpc("", Status::OK, HealthCheckResponse::SERVING);
  121. SendHealthCheckRpc(kHealthyService, Status::OK, HealthCheckResponse::SERVING);
  122. SendHealthCheckRpc(kUnhealthyService, Status::OK,
  123. HealthCheckResponse::NOT_SERVING);
  124. SendHealthCheckRpc(kNotRegisteredService, Status(StatusCode::NOT_FOUND, ""),
  125. HealthCheckResponse::NOT_SERVING);
  126. SendHealthCheckRpc(kTooLongServiceName, Status(StatusCode::INVALID_ARGUMENT, ""),
  127. HealthCheckResponse::NOT_SERVING);
  128. default_service->SetServingStatus(false);
  129. SendHealthCheckRpc("", Status::OK, HealthCheckResponse::NOT_SERVING);
  130. SendHealthCheckRpc(kHealthyService, Status::OK,
  131. HealthCheckResponse::NOT_SERVING);
  132. SendHealthCheckRpc(kUnhealthyService, Status::OK,
  133. HealthCheckResponse::NOT_SERVING);
  134. SendHealthCheckRpc(kNotRegisteredService, Status(StatusCode::NOT_FOUND, ""),
  135. HealthCheckResponse::NOT_SERVING);
  136. SendHealthCheckRpc(kTooLongServiceName, Status(StatusCode::INVALID_ARGUMENT, ""),
  137. HealthCheckResponse::NOT_SERVING);
  138. }
  139. } // namespace
  140. } // namespace testing
  141. } // namespace grpc
  142. int main(int argc, char** argv) {
  143. grpc_test_init(argc, argv);
  144. ::testing::InitGoogleTest(&argc, argv);
  145. return RUN_ALL_TESTS();
  146. }