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