Debian Codegraph @

test/cpp/interop/xds_interop_server.cc

raw
1 /*
2 *
3 * Copyright 2020 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 <sstream>
20
21 #include "absl/flags/flag.h"
22 #include "absl/strings/str_cat.h"
23 #include "absl/synchronization/mutex.h"
24
25 #include <grpc/grpc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/time.h>
28 #include <grpcpp/ext/admin_services.h>
29 #include <grpcpp/ext/proto_server_reflection_plugin.h>
30 #include <grpcpp/health_check_service_interface.h>
31 #include <grpcpp/server.h>
32 #include <grpcpp/server_builder.h>
33 #include <grpcpp/server_context.h>
34 #include <grpcpp/xds_server_builder.h>
35
36 #include "src/core/lib/gpr/string.h"
37 #include "src/core/lib/iomgr/gethostname.h"
38 #include "src/core/lib/transport/transport.h"
39 #include "src/proto/grpc/testing/empty.pb.h"
40 #include "src/proto/grpc/testing/messages.pb.h"
41 #include "src/proto/grpc/testing/test.grpc.pb.h"
42 #include "test/core/util/test_config.h"
43 #include "test/cpp/end2end/test_health_check_service_impl.h"
44 #include "test/cpp/util/test_config.h"
45
46 ABSL_FLAG(int32_t, port, 8080, "Server port for service.");
47 ABSL_FLAG(int32_t, maintenance_port, 8081,
48 "Server port for maintenance if --security is \"secure\".");
49 ABSL_FLAG(std::string, server_id, "cpp_server",
50 "Server ID to include in responses.");
51 ABSL_FLAG(bool, secure_mode, false,
52 "If true, XdsServerCredentials are used, InsecureServerCredentials "
53 "otherwise");
54
55 using grpc::Server;
56 using grpc::ServerBuilder;
57 using grpc::ServerContext;
58 using grpc::Status;
59 using grpc::XdsServerBuilder;
60 using grpc::testing::Empty;
61 using grpc::testing::HealthCheckServiceImpl;
62 using grpc::testing::SimpleRequest;
63 using grpc::testing::SimpleResponse;
64 using grpc::testing::TestService;
65 using grpc::testing::XdsUpdateHealthService;
66
67 class TestServiceImpl : public TestService::Service {
68 public:
69 explicit TestServiceImpl(const std::string& hostname) : hostname_(hostname) {}
70
71 Status UnaryCall(ServerContext* context, const SimpleRequest* /*request*/,
72 SimpleResponse* response) override {
73 response->set_server_id(absl::GetFlag(FLAGS_server_id));
74 response->set_hostname(hostname_);
75 context->AddInitialMetadata("hostname", hostname_);
76 return Status::OK;
77 }
78
79 Status EmptyCall(ServerContext* context, const Empty* /*request*/,
80 Empty* /*response*/) override {
81 context->AddInitialMetadata("hostname", hostname_);
82 return Status::OK;
83 }
84
85 private:
86 std::string hostname_;
87 };
88
89 class XdsUpdateHealthServiceImpl : public XdsUpdateHealthService::Service {
90 public:
91 explicit XdsUpdateHealthServiceImpl(
92 HealthCheckServiceImpl* health_check_service)
93 : health_check_service_(health_check_service) {}
94
95 Status SetServing(ServerContext* /* context */, const Empty* /* request */,
96 Empty* /* response */) override {
97 health_check_service_->SetAll(
98 grpc::health::v1::HealthCheckResponse::SERVING);
99 return Status::OK;
100 }
101
102 Status SetNotServing(ServerContext* /* context */, const Empty* /* request */,
103 Empty* /* response */) override {
104 health_check_service_->SetAll(
105 grpc::health::v1::HealthCheckResponse::NOT_SERVING);
106 return Status::OK;
107 }
108
109 private:
110 HealthCheckServiceImpl* const health_check_service_;
111 };
112
113 void RunServer(bool secure_mode, const int port, const int maintenance_port,
114 const std::string& hostname) {
115 std::unique_ptr<Server> xds_enabled_server;
116 std::unique_ptr<Server> server;
117 TestServiceImpl service(hostname);
118 HealthCheckServiceImpl health_check_service;
119 health_check_service.SetStatus(
120 "", grpc::health::v1::HealthCheckResponse::SERVING);
121 health_check_service.SetStatus(
122 "grpc.testing.TestService",
123 grpc::health::v1::HealthCheckResponse::SERVING);
124 health_check_service.SetStatus(
125 "grpc.testing.XdsUpdateHealthService",
126 grpc::health::v1::HealthCheckResponse::SERVING);
127 XdsUpdateHealthServiceImpl update_health_service(&health_check_service);
128
129 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
130 ServerBuilder builder;
131 if (secure_mode) {
132 XdsServerBuilder xds_builder;
133 xds_builder.RegisterService(&service);
134 xds_builder.AddListeningPort(
135 absl::StrCat("0.0.0.0:", port),
136 grpc::XdsServerCredentials(grpc::InsecureServerCredentials()));
137 xds_enabled_server = xds_builder.BuildAndStart();
138 gpr_log(GPR_INFO, "Server starting on 0.0.0.0:%d", port);
139 builder.RegisterService(&health_check_service);
140 builder.RegisterService(&update_health_service);
141 grpc::AddAdminServices(&builder);
142 builder.AddListeningPort(absl::StrCat("0.0.0.0:", maintenance_port),
143 grpc::InsecureServerCredentials());
144 server = builder.BuildAndStart();
145 gpr_log(GPR_INFO, "Maintenance server listening on 0.0.0.0:%d",
146 maintenance_port);
147 } else {
148 builder.RegisterService(&service);
149 builder.RegisterService(&health_check_service);
150 builder.RegisterService(&update_health_service);
151 grpc::AddAdminServices(&builder);
152 builder.AddListeningPort(absl::StrCat("0.0.0.0:", port),
153 grpc::InsecureServerCredentials());
154 server = builder.BuildAndStart();
155 gpr_log(GPR_INFO, "Server listening on 0.0.0.0:%d", port);
156 }
157
158 server->Wait();
159 }
160
161 int main(int argc, char** argv) {
162 grpc::testing::TestEnvironment env(&argc, argv);
163 grpc::testing::InitTest(&argc, &argv, true);
164 char* hostname = grpc_gethostname();
165 if (hostname == nullptr) {
166 std::cout << "Failed to get hostname, terminating" << std::endl;
167 return 1;
168 }
169 int port = absl::GetFlag(FLAGS_port);
170 if (port == 0) {
171 std::cout << "Invalid port, terminating" << std::endl;
172 return 1;
173 }
174 int maintenance_port = absl::GetFlag(FLAGS_maintenance_port);
175 if (maintenance_port == 0) {
176 std::cout << "Invalid maintenance port, terminating" << std::endl;
177 return 1;
178 }
179 grpc::EnableDefaultHealthCheckService(false);
180 RunServer(absl::GetFlag(FLAGS_secure_mode), port, maintenance_port, hostname);
181
182 return 0;
183 }