3 // Copyright 2021 gRPC authors.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
22 #include "absl/strings/str_cat.h"
24 #include <grpcpp/ext/proto_server_reflection_plugin.h>
25 #include <grpcpp/grpcpp.h>
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"
31 #include <grpcpp/ext/admin_services.h>
36 class AdminServicesTest : public ::testing::Test {
38 void SetUp() override {
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();
48 auto reflection_stub = reflection::v1alpha::ServerReflection::NewStub(
49 CreateChannel(address, InsecureChannelCredentials()));
50 stream_ = reflection_stub->ServerReflectionInfo(&reflection_ctx_);
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());
67 std::unique_ptr<Server> server_;
68 ClientContext reflection_ctx_;
70 ClientReaderWriter<reflection::v1alpha::ServerReflectionRequest,
71 reflection::v1alpha::ServerReflectionResponse>>
75 TEST_F(AdminServicesTest, ValidateRegisteredServices) {
76 // Using Contains here, because the server builder might register other
77 // services in certain environments.
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")));
88 EXPECT_THAT(GetServiceList(),
90 "envoy.service.status.v3.ClientStatusDiscoveryService"));
91 #endif // GRPC_NO_XDS or DISABLED_XDS_PROTO_IN_CC
94 } // namespace testing
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();