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