admin_services_end2end_test.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. //
  3. // Copyright 2021 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 <gmock/gmock.h>
  19. #include <gtest/gtest.h>
  20. #include "absl/strings/str_cat.h"
  21. #include <grpcpp/ext/admin_services.h>
  22. #include <grpcpp/ext/proto_server_reflection_plugin.h>
  23. #include <grpcpp/grpcpp.h>
  24. #include "src/proto/grpc/reflection/v1alpha/reflection.grpc.pb.h"
  25. #include "test/core/util/port.h"
  26. #include "test/core/util/test_config.h"
  27. namespace grpc {
  28. namespace testing {
  29. class AdminServicesTest : public ::testing::Test {
  30. public:
  31. void SetUp() override {
  32. std::string address =
  33. absl::StrCat("localhost:", grpc_pick_unused_port_or_die());
  34. // Create admin server
  35. grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  36. ServerBuilder builder;
  37. builder.AddListeningPort(address, InsecureServerCredentials());
  38. grpc::AddAdminServices(&builder);
  39. server_ = builder.BuildAndStart();
  40. // Create channel
  41. auto reflection_stub = reflection::v1alpha::ServerReflection::NewStub(
  42. CreateChannel(address, InsecureChannelCredentials()));
  43. stream_ = reflection_stub->ServerReflectionInfo(&reflection_ctx_);
  44. }
  45. std::vector<std::string> GetServiceList() {
  46. std::vector<std::string> services;
  47. reflection::v1alpha::ServerReflectionRequest request;
  48. reflection::v1alpha::ServerReflectionResponse response;
  49. request.set_list_services("");
  50. stream_->Write(request);
  51. stream_->Read(&response);
  52. for (auto& service : response.list_services_response().service()) {
  53. services.push_back(service.name());
  54. }
  55. return services;
  56. }
  57. private:
  58. std::unique_ptr<Server> server_;
  59. ClientContext reflection_ctx_;
  60. std::shared_ptr<
  61. ClientReaderWriter<reflection::v1alpha::ServerReflectionRequest,
  62. reflection::v1alpha::ServerReflectionResponse>>
  63. stream_;
  64. };
  65. TEST_F(AdminServicesTest, ValidateRegisteredServices) {
  66. // Using Contains here, because the server builder might register other
  67. // services in certain environments.
  68. EXPECT_THAT(
  69. GetServiceList(),
  70. ::testing::AllOf(
  71. ::testing::Contains("grpc.channelz.v1.Channelz"),
  72. ::testing::Contains("grpc.reflection.v1alpha.ServerReflection")));
  73. #if defined(GRPC_NO_XDS) || defined(DISABLED_XDS_PROTO_IN_CC)
  74. EXPECT_THAT(GetServiceList(),
  75. ::testing::Not(::testing::Contains(
  76. "envoy.service.status.v3.ClientStatusDiscoveryService")));
  77. #else
  78. EXPECT_THAT(GetServiceList(),
  79. ::testing::Contains(
  80. "envoy.service.status.v3.ClientStatusDiscoveryService"));
  81. #endif // GRPC_NO_XDS or DISABLED_XDS_PROTO_IN_CC
  82. }
  83. } // namespace testing
  84. } // namespace grpc
  85. int main(int argc, char** argv) {
  86. grpc::testing::TestEnvironment env(argc, argv);
  87. ::testing::InitGoogleTest(&argc, argv);
  88. int ret = RUN_ALL_TESTS();
  89. return ret;
  90. }