mirror of https://github.com/grpc/grpc.git
[interop] Implement "hostname" for RPC behavior (#33446)
This enables outlier detection test. See #33135pull/33470/head
parent
2bd20b8c87
commit
d55431995c
8 changed files with 524 additions and 177 deletions
@ -0,0 +1,227 @@ |
||||
//
|
||||
//
|
||||
// Copyright 2023 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
//
|
||||
|
||||
#include "test/cpp/interop/xds_interop_server_lib.h" |
||||
|
||||
#include <sstream> |
||||
|
||||
#include "absl/strings/str_cat.h" |
||||
#include "absl/strings/str_split.h" |
||||
#include "absl/synchronization/mutex.h" |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/time.h> |
||||
#include <grpcpp/ext/admin_services.h> |
||||
#include <grpcpp/ext/proto_server_reflection_plugin.h> |
||||
#include <grpcpp/server.h> |
||||
#include <grpcpp/server_builder.h> |
||||
#include <grpcpp/server_context.h> |
||||
#include <grpcpp/xds_server_builder.h> |
||||
|
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gprpp/crash.h" |
||||
#include "src/core/lib/transport/transport.h" |
||||
#include "src/proto/grpc/testing/empty.pb.h" |
||||
#include "src/proto/grpc/testing/messages.pb.h" |
||||
#include "src/proto/grpc/testing/test.grpc.pb.h" |
||||
#include "test/cpp/end2end/test_health_check_service_impl.h" |
||||
|
||||
namespace grpc { |
||||
namespace testing { |
||||
namespace { |
||||
|
||||
using grpc::Server; |
||||
using grpc::ServerBuilder; |
||||
using grpc::ServerContext; |
||||
using grpc::Status; |
||||
using grpc::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; |
||||
|
||||
constexpr absl::string_view kRpcBehaviorMetadataKey = "rpc-behavior"; |
||||
constexpr absl::string_view kErrorCodeRpcBehavior = "error-code-"; |
||||
constexpr absl::string_view kHostnameRpcBehaviorFilter = "hostname="; |
||||
|
||||
std::set<std::string> GetRpcBehaviorMetadata(ServerContext* context) { |
||||
std::set<std::string> rpc_behaviors; |
||||
auto rpc_behavior_metadata = |
||||
context->client_metadata().equal_range(grpc::string_ref( |
||||
kRpcBehaviorMetadataKey.data(), kRpcBehaviorMetadataKey.length())); |
||||
for (auto metadata = rpc_behavior_metadata.first; |
||||
metadata != rpc_behavior_metadata.second; ++metadata) { |
||||
auto value = metadata->second; |
||||
for (auto behavior : |
||||
absl::StrSplit(absl::string_view(value.data(), value.length()), ',')) { |
||||
rpc_behaviors.emplace(behavior); |
||||
} |
||||
} |
||||
return rpc_behaviors; |
||||
} |
||||
|
||||
class TestServiceImpl : public TestService::Service { |
||||
public: |
||||
explicit TestServiceImpl(absl::string_view hostname, |
||||
absl::string_view server_id) |
||||
: hostname_(hostname), server_id_(server_id) {} |
||||
|
||||
Status UnaryCall(ServerContext* context, const SimpleRequest* /*request*/, |
||||
SimpleResponse* response) override { |
||||
response->set_server_id(server_id_); |
||||
for (const auto& rpc_behavior : GetRpcBehaviorMetadata(context)) { |
||||
auto maybe_status = |
||||
GetStatusForRpcBehaviorMetadata(rpc_behavior, hostname_); |
||||
if (maybe_status.has_value()) { |
||||
return *maybe_status; |
||||
} |
||||
} |
||||
response->set_hostname(hostname_); |
||||
context->AddInitialMetadata("hostname", hostname_); |
||||
return Status::OK; |
||||
} |
||||
|
||||
Status EmptyCall(ServerContext* context, const Empty* /*request*/, |
||||
Empty* /*response*/) override { |
||||
context->AddInitialMetadata("hostname", hostname_); |
||||
return Status::OK; |
||||
} |
||||
|
||||
private: |
||||
std::string hostname_; |
||||
std::string server_id_; |
||||
}; |
||||
|
||||
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_; |
||||
}; |
||||
} // namespace
|
||||
|
||||
absl::optional<grpc::Status> GetStatusForRpcBehaviorMetadata( |
||||
absl::string_view header_value, absl::string_view hostname) { |
||||
for (auto part : absl::StrSplit(header_value, ' ')) { |
||||
if (absl::ConsumePrefix(&part, kHostnameRpcBehaviorFilter)) { |
||||
gpr_log(GPR_INFO, "%s", std::string(part).c_str()); |
||||
if (part.empty()) { |
||||
return Status( |
||||
grpc::StatusCode::INVALID_ARGUMENT, |
||||
absl::StrCat("Empty host name in the RPC behavior header: ", |
||||
header_value)); |
||||
} |
||||
if (part != hostname) { |
||||
gpr_log( |
||||
GPR_DEBUG, |
||||
"RPC behavior for a different host: \"%s\", this one is: \"%s\"", |
||||
std::string(part).c_str(), std::string(hostname).c_str()); |
||||
return absl::nullopt; |
||||
} |
||||
} else if (absl::ConsumePrefix(&part, kErrorCodeRpcBehavior)) { |
||||
grpc::StatusCode code; |
||||
if (absl::SimpleAtoi(part, &code)) { |
||||
return Status( |
||||
code, |
||||
absl::StrCat("Rpc failed as per the rpc-behavior header value: ", |
||||
header_value)); |
||||
} else { |
||||
return Status(grpc::StatusCode::INVALID_ARGUMENT, |
||||
absl::StrCat("Invalid format for rpc-behavior header: ", |
||||
header_value)); |
||||
} |
||||
} else { |
||||
// TODO (eugeneo): Add support for other behaviors as needed
|
||||
return Status( |
||||
grpc::StatusCode::INVALID_ARGUMENT, |
||||
absl::StrCat("Unsupported rpc behavior header: ", header_value)); |
||||
} |
||||
} |
||||
return absl::nullopt; |
||||
} |
||||
|
||||
void RunServer(bool secure_mode, const int port, const int maintenance_port, |
||||
absl::string_view hostname, absl::string_view server_id) { |
||||
std::unique_ptr<Server> xds_enabled_server; |
||||
std::unique_ptr<Server> server; |
||||
TestServiceImpl service(hostname, server_id); |
||||
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); |
||||
|
||||
grpc::reflection::InitProtoReflectionServerBuilderPlugin(); |
||||
ServerBuilder builder; |
||||
if (secure_mode) { |
||||
XdsServerBuilder xds_builder; |
||||
xds_builder.RegisterService(&service); |
||||
xds_builder.AddListeningPort( |
||||
absl::StrCat("0.0.0.0:", port), |
||||
grpc::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); |
||||
grpc::AddAdminServices(&builder); |
||||
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); |
||||
grpc::AddAdminServices(&builder); |
||||
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(); |
||||
} |
||||
|
||||
} // namespace testing
|
||||
} // namespace grpc
|
@ -0,0 +1,39 @@ |
||||
//
|
||||
//
|
||||
// Copyright 2023 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
//
|
||||
|
||||
#ifndef GRPC_TEST_CPP_INTEROP_XDS_INTEROP_SERVER_LIB_H |
||||
#define GRPC_TEST_CPP_INTEROP_XDS_INTEROP_SERVER_LIB_H |
||||
#include "absl/strings/string_view.h" |
||||
#include "absl/types/optional.h" |
||||
|
||||
#include <grpcpp/server.h> |
||||
|
||||
namespace grpc { |
||||
namespace testing { |
||||
|
||||
// Exposed for the tests
|
||||
absl::optional<grpc::Status> GetStatusForRpcBehaviorMetadata( |
||||
absl::string_view header_value, absl::string_view hostname); |
||||
|
||||
void RunServer(bool secure_mode, const int port, const int maintenance_port, |
||||
absl::string_view hostname, absl::string_view server_id); |
||||
|
||||
} // namespace testing
|
||||
} // namespace grpc
|
||||
|
||||
#endif // GRPC_TEST_CPP_INTEROP_XDS_INTEROP_SERVER_LIB_H
|
@ -0,0 +1,83 @@ |
||||
//
|
||||
// Copyright 2022 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#include <memory> |
||||
|
||||
#include <gmock/gmock.h> |
||||
#include <gtest/gtest.h> |
||||
|
||||
#include <grpc/grpc.h> |
||||
|
||||
#include "test/core/util/test_config.h" |
||||
#include "test/cpp/interop/xds_interop_server_lib.h" |
||||
|
||||
namespace grpc { |
||||
namespace testing { |
||||
namespace { |
||||
|
||||
TEST(GetRpcBehaviorMetadataTest, ErrorCodeNoFilter) { |
||||
auto status = GetStatusForRpcBehaviorMetadata("error-code-16", "hostname"); |
||||
ASSERT_TRUE(status.has_value()); |
||||
ASSERT_EQ(status->error_code(), 16) << status->error_message(); |
||||
} |
||||
|
||||
TEST(GetRpcBehaviorMetadataTest, ErrorCodeThisHost) { |
||||
auto status = GetStatusForRpcBehaviorMetadata( |
||||
"hostname=hostname error-code-16", "hostname"); |
||||
ASSERT_TRUE(status.has_value()); |
||||
ASSERT_EQ(status->error_code(), 16) << status->error_message(); |
||||
} |
||||
|
||||
TEST(GetRpcBehaviorMetadataTest, ErrorCodeOtherHost) { |
||||
auto status = GetStatusForRpcBehaviorMetadata( |
||||
"hostname=hostname2 error-code-16", "hostname"); |
||||
ASSERT_FALSE(status.has_value()); |
||||
} |
||||
|
||||
TEST(GetRpcBehaviorMetadataTest, MalformedErrorCode) { |
||||
auto status = GetStatusForRpcBehaviorMetadata("error-code-", "hostname"); |
||||
ASSERT_TRUE(status.has_value()); |
||||
ASSERT_EQ(status->error_code(), grpc::StatusCode::INVALID_ARGUMENT) |
||||
<< status->error_message(); |
||||
} |
||||
|
||||
TEST(GetRpcBehaviorMetadataTest, MalformedHostName) { |
||||
auto status = |
||||
GetStatusForRpcBehaviorMetadata("hostname= error-code-16", "hostname"); |
||||
ASSERT_TRUE(status.has_value()); |
||||
ASSERT_EQ(status->error_code(), grpc::StatusCode::INVALID_ARGUMENT) |
||||
<< status->error_message(); |
||||
} |
||||
|
||||
TEST(GetRpcBehaviorMetadataTest, ErrorWhenUnsupported) { |
||||
auto status = GetStatusForRpcBehaviorMetadata("unsupported", "hostname"); |
||||
ASSERT_TRUE(status.has_value()); |
||||
ASSERT_EQ(status->error_code(), grpc::StatusCode::INVALID_ARGUMENT) |
||||
<< status->error_message(); |
||||
} |
||||
|
||||
} // namespace
|
||||
} // namespace testing
|
||||
} // namespace grpc
|
||||
|
||||
int main(int argc, char** argv) { |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
grpc::testing::TestEnvironment env(&argc, argv); |
||||
grpc_init(); |
||||
auto result = RUN_ALL_TESTS(); |
||||
grpc_shutdown(); |
||||
return result; |
||||
} |
Loading…
Reference in new issue