From 8eb2e13a6e60d99e36a07e6cdf218f0cd62bd474 Mon Sep 17 00:00:00 2001 From: Yash Tibrewal Date: Fri, 5 Mar 2021 16:25:50 -0800 Subject: [PATCH] C++ xDS interop test server changes (#25633) * C++ xDS interop test server changes --- CMakeLists.txt | 5 ++ build_autogenerated.yaml | 5 +- test/cpp/interop/BUILD | 2 + test/cpp/interop/xds_interop_client.cc | 7 +- test/cpp/interop/xds_interop_server.cc | 99 ++++++++++++++++++++++---- 5 files changed, 101 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 04b2848f4f2..dcff5ca4e62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16155,6 +16155,10 @@ endif() if(gRPC_BUILD_TESTS) add_executable(xds_interop_server + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/health/v1/health.pb.cc + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/health/v1/health.grpc.pb.cc + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/health/v1/health.pb.h + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/health/v1/health.grpc.pb.h ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/empty.pb.cc ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/empty.grpc.pb.cc ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/empty.pb.h @@ -16167,6 +16171,7 @@ add_executable(xds_interop_server ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/test.grpc.pb.cc ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/test.pb.h ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/test.grpc.pb.h + test/cpp/end2end/test_health_check_service_impl.cc test/cpp/interop/xds_interop_server.cc third_party/googletest/googletest/src/gtest-all.cc third_party/googletest/googlemock/src/gmock-all.cc diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index df3c5f57f12..179a5306be9 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -8374,11 +8374,14 @@ targets: build: test run: false language: c++ - headers: [] + headers: + - test/cpp/end2end/test_health_check_service_impl.h src: + - src/proto/grpc/health/v1/health.proto - src/proto/grpc/testing/empty.proto - src/proto/grpc/testing/messages.proto - src/proto/grpc/testing/test.proto + - test/cpp/end2end/test_health_check_service_impl.cc - test/cpp/interop/xds_interop_server.cc deps: - grpc_test_util diff --git a/test/cpp/interop/BUILD b/test/cpp/interop/BUILD index 1f53c6a3dd9..e92c3270a74 100644 --- a/test/cpp/interop/BUILD +++ b/test/cpp/interop/BUILD @@ -239,6 +239,7 @@ grpc_cc_binary( grpc_cc_binary( name = "xds_interop_server", + testonly = True, srcs = [ "xds_interop_server.cc", ], @@ -251,6 +252,7 @@ grpc_cc_binary( "//src/proto/grpc/testing:messages_proto", "//src/proto/grpc/testing:test_proto", "//test/core/util:grpc_test_util", + "//test/cpp/end2end:test_health_check_service_impl", "//test/cpp/util:test_config", ], ) diff --git a/test/cpp/interop/xds_interop_client.cc b/test/cpp/interop/xds_interop_client.cc index 5582b4f800a..7344905373a 100644 --- a/test/cpp/interop/xds_interop_client.cc +++ b/test/cpp/interop/xds_interop_client.cc @@ -59,8 +59,9 @@ ABSL_FLAG(std::string, rpc, "UnaryCall", ABSL_FLAG(std::string, metadata, "", "metadata to send with the RPC."); ABSL_FLAG(std::string, expect_status, "OK", "RPC status for the test RPC to be considered successful"); -ABSL_FLAG(std::string, security, "none", - "If set to \"secure\", XdsCredentials are used"); +ABSL_FLAG( + bool, secure_mode, false, + "If true, XdsCredentials are used, InsecureChannelCredentials otherwise"); using grpc::Channel; using grpc::ClientAsyncResponseReader; @@ -464,7 +465,7 @@ void RunTestLoop(std::chrono::duration duration_per_query, RpcConfigurationsQueue* rpc_configs_queue) { TestClient client( grpc::CreateChannel(absl::GetFlag(FLAGS_server), - absl::GetFlag(FLAGS_security) == "secure" + absl::GetFlag(FLAGS_secure_mode) ? grpc::experimental::XdsCredentials( grpc::InsecureChannelCredentials()) : grpc::InsecureChannelCredentials()), diff --git a/test/cpp/interop/xds_interop_server.cc b/test/cpp/interop/xds_interop_server.cc index 1913c53964b..0dfef79aae9 100644 --- a/test/cpp/interop/xds_interop_server.cc +++ b/test/cpp/interop/xds_interop_server.cc @@ -19,13 +19,17 @@ #include #include #include +#include #include #include #include +#include #include #include "absl/flags/flag.h" +#include "absl/strings/str_cat.h" +#include "absl/synchronization/mutex.h" #include "src/core/lib/gpr/string.h" #include "src/core/lib/iomgr/gethostname.h" #include "src/core/lib/transport/byte_stream.h" @@ -33,24 +37,33 @@ #include "src/proto/grpc/testing/messages.pb.h" #include "src/proto/grpc/testing/test.grpc.pb.h" #include "test/core/util/test_config.h" +#include "test/cpp/end2end/test_health_check_service_impl.h" #include "test/cpp/util/test_config.h" -ABSL_FLAG(int32_t, port, 50051, "Server port."); +ABSL_FLAG(int32_t, port, 8080, "Server port for service."); +ABSL_FLAG(int32_t, maintenance_port, 8081, + "Server port for maintenance if --security is \"secure\"."); ABSL_FLAG(std::string, server_id, "cpp_server", "Server ID to include in responses."); +ABSL_FLAG(bool, secure_mode, false, + "If true, XdsServerCredentials are used, InsecureServerCredentials " + "otherwise"); using grpc::Server; using grpc::ServerBuilder; using grpc::ServerContext; using grpc::Status; +using grpc::experimental::XdsServerBuilder; using grpc::testing::Empty; +using grpc::testing::HealthCheckServiceImpl; using grpc::testing::SimpleRequest; using grpc::testing::SimpleResponse; using grpc::testing::TestService; +using grpc::testing::XdsUpdateHealthService; class TestServiceImpl : public TestService::Service { public: - explicit TestServiceImpl(const std::string& i) : hostname_(i) {} + explicit TestServiceImpl(const std::string& hostname) : hostname_(hostname) {} Status UnaryCall(ServerContext* context, const SimpleRequest* /*request*/, SimpleResponse* response) override { @@ -70,17 +83,71 @@ class TestServiceImpl : public TestService::Service { std::string hostname_; }; -void RunServer(const int port, const std::string& hostname) { - std::ostringstream server_address; - server_address << "0.0.0.0:" << port; +class XdsUpdateHealthServiceImpl : public XdsUpdateHealthService::Service { + public: + explicit XdsUpdateHealthServiceImpl( + HealthCheckServiceImpl* health_check_service) + : health_check_service_(health_check_service) {} + + Status SetServing(ServerContext* /* context */, const Empty* /* request */, + Empty* /* response */) override { + health_check_service_->SetAll( + grpc::health::v1::HealthCheckResponse::SERVING); + return Status::OK; + } + + Status SetNotServing(ServerContext* /* context */, const Empty* /* request */, + Empty* /* response */) override { + health_check_service_->SetAll( + grpc::health::v1::HealthCheckResponse::NOT_SERVING); + return Status::OK; + } + + private: + HealthCheckServiceImpl* const health_check_service_; +}; +void RunServer(bool secure_mode, const int port, const int maintenance_port, + const std::string& hostname) { + std::unique_ptr xds_enabled_server; + std::unique_ptr server; TestServiceImpl service(hostname); + HealthCheckServiceImpl health_check_service; + health_check_service.SetStatus( + "", grpc::health::v1::HealthCheckResponse::SERVING); + health_check_service.SetStatus( + "grpc.testing.TestService", + grpc::health::v1::HealthCheckResponse::SERVING); + health_check_service.SetStatus( + "grpc.testing.XdsUpdateHealthService", + grpc::health::v1::HealthCheckResponse::SERVING); + XdsUpdateHealthServiceImpl update_health_service(&health_check_service); + ServerBuilder builder; - builder.RegisterService(&service); - builder.AddListeningPort(server_address.str(), - grpc::InsecureServerCredentials()); - std::unique_ptr server(builder.BuildAndStart()); - gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str()); + if (secure_mode) { + XdsServerBuilder xds_builder; + xds_builder.RegisterService(&service); + xds_builder.AddListeningPort(absl::StrCat("0.0.0.0:", port), + grpc::experimental::XdsServerCredentials( + grpc::InsecureServerCredentials())); + xds_enabled_server = xds_builder.BuildAndStart(); + gpr_log(GPR_INFO, "Server starting on 0.0.0.0:%d", port); + builder.RegisterService(&health_check_service); + builder.RegisterService(&update_health_service); + builder.AddListeningPort(absl::StrCat("0.0.0.0:", maintenance_port), + grpc::InsecureServerCredentials()); + server = builder.BuildAndStart(); + gpr_log(GPR_INFO, "Maintenance server listening on 0.0.0.0:%d", + maintenance_port); + } else { + builder.RegisterService(&service); + builder.RegisterService(&health_check_service); + builder.RegisterService(&update_health_service); + builder.AddListeningPort(absl::StrCat("0.0.0.0:", port), + grpc::InsecureServerCredentials()); + server = builder.BuildAndStart(); + gpr_log(GPR_INFO, "Server listening on 0.0.0.0:%d", port); + } server->Wait(); } @@ -94,12 +161,18 @@ int main(int argc, char** argv) { std::cout << "Failed to get hostname, terminating" << std::endl; return 1; } - if (absl::GetFlag(FLAGS_port) == 0) { + int port = absl::GetFlag(FLAGS_port); + if (port == 0) { std::cout << "Invalid port, terminating" << std::endl; return 1; } - - RunServer(absl::GetFlag(FLAGS_port), hostname); + int maintenance_port = absl::GetFlag(FLAGS_maintenance_port); + if (maintenance_port == 0) { + std::cout << "Invalid maintenance port, terminating" << std::endl; + return 1; + } + grpc::EnableDefaultHealthCheckService(false); + RunServer(absl::GetFlag(FLAGS_secure_mode), port, maintenance_port, hostname); return 0; }