| 1 |
// Copyright 2017 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
// |
| 15 |
|
| 16 |
#include <memory> |
| 17 |
#include <string> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
#include <gmock/gmock.h> |
| 21 |
#include <gtest/gtest.h> |
| 22 |
|
| 23 |
#include "absl/memory/memory.h" |
| 24 |
#include "absl/strings/str_cat.h" |
| 25 |
|
| 26 |
#include <grpcpp/create_channel.h> |
| 27 |
#include <grpcpp/security/credentials.h> |
| 28 |
|
| 29 |
#include "src/core/ext/filters/client_channel/backup_poller.h" |
| 30 |
#include "src/cpp/client/secure_credentials.h" |
| 31 |
#include "src/proto/grpc/testing/xds/v3/cluster.grpc.pb.h" |
| 32 |
#include "src/proto/grpc/testing/xds/v3/endpoint.grpc.pb.h" |
| 33 |
#include "src/proto/grpc/testing/xds/v3/http_connection_manager.grpc.pb.h" |
| 34 |
#include "src/proto/grpc/testing/xds/v3/listener.grpc.pb.h" |
| 35 |
#include "src/proto/grpc/testing/xds/v3/route.grpc.pb.h" |
| 36 |
#include "test/core/util/test_config.h" |
| 37 |
#include "test/cpp/end2end/xds/xds_end2end_test_lib.h" |
| 38 |
|
| 39 |
#ifndef DISABLED_XDS_PROTO_IN_CC |
| 40 |
|
| 41 |
#include "src/cpp/server/csds/csds.h" |
| 42 |
#include "src/proto/grpc/testing/xds/v3/csds.grpc.pb.h" |
| 43 |
|
| 44 |
namespace grpc { |
| 45 |
namespace testing { |
| 46 |
namespace { |
| 47 |
|
| 48 |
using ::envoy::admin::v3::ClientResourceStatus; |
| 49 |
using ::envoy::config::cluster::v3::Cluster; |
| 50 |
using ::envoy::config::endpoint::v3::ClusterLoadAssignment; |
| 51 |
using ::envoy::config::listener::v3::Listener; |
| 52 |
using ::envoy::config::route::v3::RouteConfiguration; |
| 53 |
using ::envoy::extensions::filters::network::http_connection_manager::v3:: |
| 54 |
HttpConnectionManager; |
| 55 |
|
| 56 |
MATCHER_P4(EqNode, id, user_agent_name, user_agent_version, client_features, |
| 57 |
"equals Node") { |
| 58 |
bool ok = true; |
| 59 |
ok &= ::testing::ExplainMatchResult(id, arg.id(), result_listener); |
| 60 |
ok &= ::testing::ExplainMatchResult(user_agent_name, arg.user_agent_name(), |
| 61 |
result_listener); |
| 62 |
ok &= ::testing::ExplainMatchResult( |
| 63 |
user_agent_version, arg.user_agent_version(), result_listener); |
| 64 |
ok &= ::testing::ExplainMatchResult(client_features, arg.client_features(), |
| 65 |
result_listener); |
| 66 |
return ok; |
| 67 |
} |
| 68 |
|
| 69 |
MATCHER_P6(EqGenericXdsConfig, type_url, name, version_info, xds_config, |
| 70 |
client_status, error_state, "equals GenericXdsConfig") { |
| 71 |
bool ok = true; |
| 72 |
ok &= |
| 73 |
::testing::ExplainMatchResult(type_url, arg.type_url(), result_listener); |
| 74 |
ok &= ::testing::ExplainMatchResult(name, arg.name(), result_listener); |
| 75 |
ok &= ::testing::ExplainMatchResult(version_info, arg.version_info(), |
| 76 |
result_listener); |
| 77 |
ok &= ::testing::ExplainMatchResult(xds_config, arg.xds_config(), |
| 78 |
result_listener); |
| 79 |
ok &= ::testing::ExplainMatchResult(client_status, arg.client_status(), |
| 80 |
result_listener); |
| 81 |
ok &= ::testing::ExplainMatchResult(error_state, arg.error_state(), |
| 82 |
result_listener); |
| 83 |
return ok; |
| 84 |
} |
| 85 |
|
| 86 |
MATCHER_P2(EqListener, name, api_listener, "equals Listener") { |
| 87 |
bool ok = true; |
| 88 |
ok &= ::testing::ExplainMatchResult(name, arg.name(), result_listener); |
| 89 |
ok &= ::testing::ExplainMatchResult( |
| 90 |
api_listener, arg.api_listener().api_listener(), result_listener); |
| 91 |
return ok; |
| 92 |
} |
| 93 |
|
| 94 |
MATCHER_P(EqHttpConnectionManagerNotRds, route_config, |
| 95 |
"equals HttpConnectionManager") { |
| 96 |
bool ok = true; |
| 97 |
ok &= ::testing::ExplainMatchResult(route_config, arg.route_config(), |
| 98 |
result_listener); |
| 99 |
return ok; |
| 100 |
} |
| 101 |
|
| 102 |
MATCHER_P(EqRouteConfigurationName, name, "equals RouteConfiguration") { |
| 103 |
bool ok = true; |
| 104 |
ok &= ::testing::ExplainMatchResult(name, arg.name(), result_listener); |
| 105 |
return ok; |
| 106 |
} |
| 107 |
|
| 108 |
MATCHER_P2(EqRouteConfiguration, name, cluster_name, |
| 109 |
"equals RouteConfiguration") { |
| 110 |
bool ok = true; |
| 111 |
ok &= ::testing::ExplainMatchResult(name, arg.name(), result_listener); |
| 112 |
ok &= ::testing::ExplainMatchResult( |
| 113 |
::testing::ElementsAre(::testing::Property( |
| 114 |
&envoy::config::route::v3::VirtualHost::routes, |
| 115 |
::testing::ElementsAre(::testing::Property( |
| 116 |
&envoy::config::route::v3::Route::route, |
| 117 |
::testing::Property( |
| 118 |
&envoy::config::route::v3::RouteAction::cluster, |
| 119 |
cluster_name))))), |
| 120 |
arg.virtual_hosts(), result_listener); |
| 121 |
return ok; |
| 122 |
} |
| 123 |
|
| 124 |
MATCHER_P(EqCluster, name, "equals Cluster") { |
| 125 |
bool ok = true; |
| 126 |
ok &= ::testing::ExplainMatchResult(name, arg.name(), result_listener); |
| 127 |
return ok; |
| 128 |
} |
| 129 |
|
| 130 |
MATCHER_P(EqEndpoint, port, "equals Endpoint") { |
| 131 |
bool ok = true; |
| 132 |
ok &= ::testing::ExplainMatchResult( |
| 133 |
port, arg.address().socket_address().port_value(), result_listener); |
| 134 |
return ok; |
| 135 |
} |
| 136 |
|
| 137 |
MATCHER_P2(EqLocalityLbEndpoints, port, weight, "equals LocalityLbEndpoints") { |
| 138 |
bool ok = true; |
| 139 |
ok &= ::testing::ExplainMatchResult( |
| 140 |
::testing::ElementsAre(::testing::Property( |
| 141 |
&envoy::config::endpoint::v3::LbEndpoint::endpoint, |
| 142 |
EqEndpoint(port))), |
| 143 |
arg.lb_endpoints(), result_listener); |
| 144 |
ok &= ::testing::ExplainMatchResult( |
| 145 |
weight, arg.load_balancing_weight().value(), result_listener); |
| 146 |
return ok; |
| 147 |
} |
| 148 |
|
| 149 |
MATCHER_P(EqClusterLoadAssignmentName, cluster_name, |
| 150 |
"equals ClusterLoadAssignment") { |
| 151 |
bool ok = true; |
| 152 |
ok &= ::testing::ExplainMatchResult(cluster_name, arg.cluster_name(), |
| 153 |
result_listener); |
| 154 |
return ok; |
| 155 |
} |
| 156 |
|
| 157 |
MATCHER_P3(EqClusterLoadAssignment, cluster_name, port, weight, |
| 158 |
"equals ClusterLoadAssignment") { |
| 159 |
bool ok = true; |
| 160 |
ok &= ::testing::ExplainMatchResult(cluster_name, arg.cluster_name(), |
| 161 |
result_listener); |
| 162 |
ok &= ::testing::ExplainMatchResult( |
| 163 |
::testing::ElementsAre(EqLocalityLbEndpoints(port, weight)), |
| 164 |
arg.endpoints(), result_listener); |
| 165 |
return ok; |
| 166 |
} |
| 167 |
|
| 168 |
MATCHER_P2(EqUpdateFailureState, details, version_info, |
| 169 |
"equals UpdateFailureState") { |
| 170 |
bool ok = true; |
| 171 |
ok &= ::testing::ExplainMatchResult(details, arg.details(), result_listener); |
| 172 |
ok &= ::testing::ExplainMatchResult(version_info, arg.version_info(), |
| 173 |
result_listener); |
| 174 |
return ok; |
| 175 |
} |
| 176 |
|
| 177 |
MATCHER_P(UnpackListener, matcher, "is a Listener") { |
| 178 |
Listener config; |
| 179 |
if (!::testing::ExplainMatchResult(true, arg.UnpackTo(&config), |
| 180 |
result_listener)) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
return ::testing::ExplainMatchResult(matcher, config, result_listener); |
| 184 |
} |
| 185 |
|
| 186 |
MATCHER_P(UnpackRouteConfiguration, matcher, "is a RouteConfiguration") { |
| 187 |
RouteConfiguration config; |
| 188 |
if (!::testing::ExplainMatchResult(true, arg.UnpackTo(&config), |
| 189 |
result_listener)) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
return ::testing::ExplainMatchResult(matcher, config, result_listener); |
| 193 |
} |
| 194 |
|
| 195 |
MATCHER_P(UnpackHttpConnectionManager, matcher, "is a HttpConnectionManager") { |
| 196 |
HttpConnectionManager config; |
| 197 |
if (!::testing::ExplainMatchResult(true, arg.UnpackTo(&config), |
| 198 |
result_listener)) { |
| 199 |
return false; |
| 200 |
} |
| 201 |
return ::testing::ExplainMatchResult(matcher, config, result_listener); |
| 202 |
} |
| 203 |
|
| 204 |
MATCHER_P(UnpackCluster, matcher, "is a Cluster") { |
| 205 |
Cluster config; |
| 206 |
if (!::testing::ExplainMatchResult(true, arg.UnpackTo(&config), |
| 207 |
result_listener)) { |
| 208 |
return false; |
| 209 |
} |
| 210 |
return ::testing::ExplainMatchResult(matcher, config, result_listener); |
| 211 |
} |
| 212 |
|
| 213 |
MATCHER_P(UnpackClusterLoadAssignment, matcher, "is a ClusterLoadAssignment") { |
| 214 |
ClusterLoadAssignment config; |
| 215 |
if (!::testing::ExplainMatchResult(true, arg.UnpackTo(&config), |
| 216 |
result_listener)) { |
| 217 |
return false; |
| 218 |
} |
| 219 |
return ::testing::ExplainMatchResult(matcher, config, result_listener); |
| 220 |
} |
| 221 |
|
| 222 |
MATCHER(IsRdsEnabledHCM, "is a RDS enabled HttpConnectionManager") { |
| 223 |
return ::testing::ExplainMatchResult( |
| 224 |
UnpackHttpConnectionManager( |
| 225 |
::testing::Property(&HttpConnectionManager::has_rds, true)), |
| 226 |
arg, result_listener); |
| 227 |
} |
| 228 |
|
| 229 |
MATCHER_P2(EqNoRdsHCM, route_configuration_name, cluster_name, |
| 230 |
"equals RDS disabled HttpConnectionManager") { |
| 231 |
return ::testing::ExplainMatchResult( |
| 232 |
UnpackHttpConnectionManager(EqHttpConnectionManagerNotRds( |
| 233 |
EqRouteConfiguration(route_configuration_name, cluster_name))), |
| 234 |
arg, result_listener); |
| 235 |
} |
| 236 |
|
| 237 |
class ClientStatusDiscoveryServiceTest : public XdsEnd2endTest { |
| 238 |
public: |
| 239 |
ClientStatusDiscoveryServiceTest() { |
| 240 |
admin_server_thread_ = std::make_unique<AdminServerThread>(this); |
| 241 |
admin_server_thread_->Start(); |
| 242 |
std::string admin_server_address = absl::StrCat( |
| 243 |
ipv6_only_ ? "[::1]:" : "127.0.0.1:", admin_server_thread_->port()); |
| 244 |
admin_channel_ = grpc::CreateChannel( |
| 245 |
admin_server_address, |
| 246 |
std::make_shared<SecureChannelCredentials>( |
| 247 |
grpc_fake_transport_security_credentials_create())); |
| 248 |
csds_stub_ = |
| 249 |
envoy::service::status::v3::ClientStatusDiscoveryService::NewStub( |
| 250 |
admin_channel_); |
| 251 |
if (GetParam().use_csds_streaming()) { |
| 252 |
stream_ = csds_stub_->StreamClientStatus(&stream_context_); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
~ClientStatusDiscoveryServiceTest() override { |
| 257 |
if (stream_ != nullptr) { |
| 258 |
EXPECT_TRUE(stream_->WritesDone()); |
| 259 |
Status status = stream_->Finish(); |
| 260 |
EXPECT_TRUE(status.ok()) << "code=" << status.error_code() |
| 261 |
<< " message=" << status.error_message(); |
| 262 |
} |
| 263 |
admin_server_thread_->Shutdown(); |
| 264 |
} |
| 265 |
|
| 266 |
envoy::service::status::v3::ClientStatusResponse FetchCsdsResponse() { |
| 267 |
envoy::service::status::v3::ClientStatusResponse response; |
| 268 |
if (!GetParam().use_csds_streaming()) { |
| 269 |
// Fetch through unary pulls |
| 270 |
ClientContext context; |
| 271 |
Status status = csds_stub_->FetchClientStatus( |
| 272 |
&context, envoy::service::status::v3::ClientStatusRequest(), |
| 273 |
&response); |
| 274 |
EXPECT_TRUE(status.ok()) << "code=" << status.error_code() |
| 275 |
<< " message=" << status.error_message(); |
| 276 |
} else { |
| 277 |
// Fetch through streaming pulls |
| 278 |
EXPECT_TRUE( |
| 279 |
stream_->Write(envoy::service::status::v3::ClientStatusRequest())); |
| 280 |
EXPECT_TRUE(stream_->Read(&response)); |
| 281 |
} |
| 282 |
return response; |
| 283 |
} |
| 284 |
|
| 285 |
private: |
| 286 |
// Server thread for CSDS server. |
| 287 |
class AdminServerThread : public ServerThread { |
| 288 |
public: |
| 289 |
explicit AdminServerThread(XdsEnd2endTest* test_obj) |
| 290 |
: ServerThread(test_obj) {} |
| 291 |
|
| 292 |
private: |
| 293 |
const char* Type() override { return "Admin"; } |
| 294 |
|
| 295 |
void RegisterAllServices(ServerBuilder* builder) override { |
| 296 |
builder->RegisterService(&csds_service_); |
| 297 |
} |
| 298 |
void StartAllServices() override {} |
| 299 |
void ShutdownAllServices() override {} |
| 300 |
|
| 301 |
grpc::xds::experimental::ClientStatusDiscoveryService csds_service_; |
| 302 |
}; |
| 303 |
|
| 304 |
std::unique_ptr<AdminServerThread> admin_server_thread_; |
| 305 |
std::shared_ptr<Channel> admin_channel_; |
| 306 |
std::unique_ptr< |
| 307 |
envoy::service::status::v3::ClientStatusDiscoveryService::Stub> |
| 308 |
csds_stub_; |
| 309 |
ClientContext stream_context_; |
| 310 |
std::unique_ptr< |
| 311 |
ClientReaderWriter<envoy::service::status::v3::ClientStatusRequest, |
| 312 |
envoy::service::status::v3::ClientStatusResponse>> |
| 313 |
stream_; |
| 314 |
}; |
| 315 |
|
| 316 |
// Run CSDS tests with RDS enabled and disabled. |
| 317 |
// These need to run with the bootstrap from an env var instead of from |
| 318 |
// a channel arg, since there needs to be a global XdsClient instance. |
| 319 |
INSTANTIATE_TEST_SUITE_P( |
| 320 |
XdsTest, ClientStatusDiscoveryServiceTest, |
| 321 |
::testing::Values( |
| 322 |
XdsTestType().set_bootstrap_source(XdsTestType::kBootstrapFromEnvVar), |
| 323 |
XdsTestType() |
| 324 |
.set_bootstrap_source(XdsTestType::kBootstrapFromEnvVar) |
| 325 |
.set_enable_rds_testing(), |
| 326 |
XdsTestType() |
| 327 |
.set_bootstrap_source(XdsTestType::kBootstrapFromEnvVar) |
| 328 |
.set_use_csds_streaming(), |
| 329 |
XdsTestType() |
| 330 |
.set_bootstrap_source(XdsTestType::kBootstrapFromEnvVar) |
| 331 |
.set_enable_rds_testing() |
| 332 |
.set_use_csds_streaming()), |
| 333 |
&XdsTestType::Name); |
| 334 |
|
| 335 |
TEST_P(ClientStatusDiscoveryServiceTest, XdsConfigDumpVanilla) { |
| 336 |
CreateAndStartBackends(1); |
| 337 |
const size_t kNumRpcs = 5; |
| 338 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 339 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 340 |
// Send several RPCs to ensure the xDS setup works |
| 341 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcs); |
| 342 |
// Fetches the client config |
| 343 |
auto csds_response = FetchCsdsResponse(); |
| 344 |
gpr_log(GPR_INFO, "xDS config dump: %s", csds_response.DebugString().c_str()); |
| 345 |
EXPECT_EQ(1, csds_response.config_size()); |
| 346 |
const auto& client_config = csds_response.config(0); |
| 347 |
// Validate the Node information |
| 348 |
EXPECT_THAT(client_config.node(), |
| 349 |
EqNode("xds_end2end_test", ::testing::HasSubstr("C-core"), |
| 350 |
::testing::HasSubstr(grpc_version_string()), |
| 351 |
::testing::ElementsAre( |
| 352 |
"envoy.lb.does_not_support_overprovisioning"))); |
| 353 |
// Listener matcher depends on whether RDS is enabled. |
| 354 |
::testing::Matcher<google::protobuf::Any> api_listener_matcher; |
| 355 |
if (GetParam().enable_rds_testing()) { |
| 356 |
api_listener_matcher = IsRdsEnabledHCM(); |
| 357 |
} else { |
| 358 |
api_listener_matcher = |
| 359 |
EqNoRdsHCM(kDefaultRouteConfigurationName, kDefaultClusterName); |
| 360 |
} |
| 361 |
// Construct list of all matchers. |
| 362 |
std::vector<::testing::Matcher< |
| 363 |
envoy::service::status::v3::ClientConfig_GenericXdsConfig>> |
| 364 |
matchers = { |
| 365 |
// Listener |
| 366 |
EqGenericXdsConfig( |
| 367 |
kLdsTypeUrl, kServerName, "1", |
| 368 |
UnpackListener(EqListener(kServerName, api_listener_matcher)), |
| 369 |
ClientResourceStatus::ACKED, ::testing::_), |
| 370 |
// Cluster |
| 371 |
EqGenericXdsConfig(kCdsTypeUrl, kDefaultClusterName, "1", |
| 372 |
UnpackCluster(EqCluster(kDefaultClusterName)), |
| 373 |
ClientResourceStatus::ACKED, ::testing::_), |
| 374 |
// ClusterLoadAssignment |
| 375 |
EqGenericXdsConfig( |
| 376 |
kEdsTypeUrl, kDefaultEdsServiceName, "1", |
| 377 |
UnpackClusterLoadAssignment(EqClusterLoadAssignment( |
| 378 |
kDefaultEdsServiceName, backends_[0]->port(), |
| 379 |
kDefaultLocalityWeight)), |
| 380 |
ClientResourceStatus::ACKED, ::testing::_), |
| 381 |
}; |
| 382 |
// If RDS is enabled, add matcher for RDS resource. |
| 383 |
if (GetParam().enable_rds_testing()) { |
| 384 |
matchers.push_back(EqGenericXdsConfig( |
| 385 |
kRdsTypeUrl, kDefaultRouteConfigurationName, "1", |
| 386 |
UnpackRouteConfiguration(EqRouteConfiguration( |
| 387 |
kDefaultRouteConfigurationName, kDefaultClusterName)), |
| 388 |
ClientResourceStatus::ACKED, ::testing::_)); |
| 389 |
} |
| 390 |
// Validate the dumped xDS configs |
| 391 |
EXPECT_THAT(client_config.generic_xds_configs(), |
| 392 |
::testing::UnorderedElementsAreArray(matchers)) |
| 393 |
<< "Actual: " << client_config.DebugString(); |
| 394 |
} |
| 395 |
|
| 396 |
TEST_P(ClientStatusDiscoveryServiceTest, XdsConfigDumpEmpty) { |
| 397 |
// The CSDS service should not fail if XdsClient is not initialized or there |
| 398 |
// is no working xDS configs. |
| 399 |
FetchCsdsResponse(); |
| 400 |
} |
| 401 |
|
| 402 |
TEST_P(ClientStatusDiscoveryServiceTest, XdsConfigDumpListenerError) { |
| 403 |
CreateAndStartBackends(1); |
| 404 |
int kFetchConfigRetries = 3; |
| 405 |
int kFetchIntervalMilliseconds = 200; |
| 406 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 407 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 408 |
// Ensure the xDS resolver has working configs. |
| 409 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 410 |
// Bad Listener should be rejected. |
| 411 |
Listener listener; |
| 412 |
listener.set_name(kServerName); |
| 413 |
balancer_->ads_service()->SetLdsResource(listener); |
| 414 |
// The old xDS configs should still be effective. |
| 415 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 416 |
::testing::Matcher<google::protobuf::Any> api_listener_matcher; |
| 417 |
if (GetParam().enable_rds_testing()) { |
| 418 |
api_listener_matcher = IsRdsEnabledHCM(); |
| 419 |
} else { |
| 420 |
api_listener_matcher = |
| 421 |
EqNoRdsHCM(kDefaultRouteConfigurationName, kDefaultClusterName); |
| 422 |
} |
| 423 |
for (int i = 0; i < kFetchConfigRetries; ++i) { |
| 424 |
auto csds_response = FetchCsdsResponse(); |
| 425 |
// Check if error state is propagated |
| 426 |
bool ok = ::testing::Value( |
| 427 |
csds_response.config(0).generic_xds_configs(), |
| 428 |
::testing::Contains(EqGenericXdsConfig( |
| 429 |
kLdsTypeUrl, kServerName, "1", |
| 430 |
UnpackListener(EqListener(kServerName, api_listener_matcher)), |
| 431 |
ClientResourceStatus::NACKED, |
| 432 |
EqUpdateFailureState( |
| 433 |
::testing::HasSubstr( |
| 434 |
"Listener has neither address nor ApiListener"), |
| 435 |
"2")))); |
| 436 |
if (ok) return; // TEST PASSED! |
| 437 |
gpr_sleep_until( |
| 438 |
grpc_timeout_milliseconds_to_deadline(kFetchIntervalMilliseconds)); |
| 439 |
} |
| 440 |
FAIL() << "error_state not seen in CSDS responses"; |
| 441 |
} |
| 442 |
|
| 443 |
TEST_P(ClientStatusDiscoveryServiceTest, XdsConfigDumpRouteError) { |
| 444 |
CreateAndStartBackends(1); |
| 445 |
int kFetchConfigRetries = 3; |
| 446 |
int kFetchIntervalMilliseconds = 200; |
| 447 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 448 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 449 |
// Ensure the xDS resolver has working configs. |
| 450 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 451 |
// Bad route config will be rejected. |
| 452 |
RouteConfiguration route_config; |
| 453 |
route_config.set_name(kDefaultRouteConfigurationName); |
| 454 |
route_config.add_virtual_hosts(); |
| 455 |
SetRouteConfiguration(balancer_.get(), route_config); |
| 456 |
// The old xDS configs should still be effective. |
| 457 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 458 |
for (int i = 0; i < kFetchConfigRetries; ++i) { |
| 459 |
auto csds_response = FetchCsdsResponse(); |
| 460 |
bool ok = false; |
| 461 |
if (GetParam().enable_rds_testing()) { |
| 462 |
ok = ::testing::Value( |
| 463 |
csds_response.config(0).generic_xds_configs(), |
| 464 |
::testing::Contains(EqGenericXdsConfig( |
| 465 |
kRdsTypeUrl, kDefaultRouteConfigurationName, "1", |
| 466 |
UnpackRouteConfiguration(EqRouteConfiguration( |
| 467 |
kDefaultRouteConfigurationName, kDefaultClusterName)), |
| 468 |
ClientResourceStatus::NACKED, |
| 469 |
EqUpdateFailureState( |
| 470 |
::testing::HasSubstr("VirtualHost has no domains"), "2")))); |
| 471 |
} else { |
| 472 |
ok = ::testing::Value( |
| 473 |
csds_response.config(0).generic_xds_configs(), |
| 474 |
::testing::Contains(EqGenericXdsConfig( |
| 475 |
kLdsTypeUrl, kServerName, "1", |
| 476 |
UnpackListener(EqListener( |
| 477 |
kServerName, EqNoRdsHCM(kDefaultRouteConfigurationName, |
| 478 |
kDefaultClusterName))), |
| 479 |
ClientResourceStatus::NACKED, |
| 480 |
EqUpdateFailureState( |
| 481 |
::testing::HasSubstr("VirtualHost has no domains"), "2")))); |
| 482 |
} |
| 483 |
if (ok) return; // TEST PASSED! |
| 484 |
gpr_sleep_until( |
| 485 |
grpc_timeout_milliseconds_to_deadline(kFetchIntervalMilliseconds)); |
| 486 |
} |
| 487 |
FAIL() << "error_state not seen in CSDS responses"; |
| 488 |
} |
| 489 |
|
| 490 |
TEST_P(ClientStatusDiscoveryServiceTest, XdsConfigDumpClusterError) { |
| 491 |
CreateAndStartBackends(1); |
| 492 |
int kFetchConfigRetries = 3; |
| 493 |
int kFetchIntervalMilliseconds = 200; |
| 494 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 495 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 496 |
// Ensure the xDS resolver has working configs. |
| 497 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 498 |
// Listener without any route, will be rejected. |
| 499 |
Cluster cluster; |
| 500 |
cluster.set_name(kDefaultClusterName); |
| 501 |
balancer_->ads_service()->SetCdsResource(cluster); |
| 502 |
// The old xDS configs should still be effective. |
| 503 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 504 |
for (int i = 0; i < kFetchConfigRetries; ++i) { |
| 505 |
auto csds_response = FetchCsdsResponse(); |
| 506 |
// Check if error state is propagated |
| 507 |
bool ok = ::testing::Value( |
| 508 |
csds_response.config(0).generic_xds_configs(), |
| 509 |
::testing::Contains(EqGenericXdsConfig( |
| 510 |
kCdsTypeUrl, kDefaultClusterName, "1", |
| 511 |
UnpackCluster(EqCluster(kDefaultClusterName)), |
| 512 |
ClientResourceStatus::NACKED, |
| 513 |
EqUpdateFailureState(::testing::HasSubstr("unknown discovery type"), |
| 514 |
"2")))); |
| 515 |
if (ok) return; // TEST PASSED! |
| 516 |
gpr_sleep_until( |
| 517 |
grpc_timeout_milliseconds_to_deadline(kFetchIntervalMilliseconds)); |
| 518 |
} |
| 519 |
FAIL() << "error_state not seen in CSDS responses"; |
| 520 |
} |
| 521 |
|
| 522 |
TEST_P(ClientStatusDiscoveryServiceTest, XdsConfigDumpEndpointError) { |
| 523 |
CreateAndStartBackends(1); |
| 524 |
int kFetchConfigRetries = 3; |
| 525 |
int kFetchIntervalMilliseconds = 200; |
| 526 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 527 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 528 |
// Ensure the xDS resolver has working configs. |
| 529 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 530 |
// Bad endpoint config will be rejected. |
| 531 |
ClusterLoadAssignment cluster_load_assignment; |
| 532 |
cluster_load_assignment.set_cluster_name(kDefaultEdsServiceName); |
| 533 |
auto* endpoints = cluster_load_assignment.add_endpoints(); |
| 534 |
endpoints->mutable_load_balancing_weight()->set_value(1); |
| 535 |
auto* endpoint = endpoints->add_lb_endpoints()->mutable_endpoint(); |
| 536 |
endpoint->mutable_address()->mutable_socket_address()->set_port_value(1 << 1); |
| 537 |
balancer_->ads_service()->SetEdsResource(cluster_load_assignment); |
| 538 |
// The old xDS configs should still be effective. |
| 539 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 540 |
for (int i = 0; i < kFetchConfigRetries; ++i) { |
| 541 |
auto csds_response = FetchCsdsResponse(); |
| 542 |
// Check if error state is propagated |
| 543 |
bool ok = ::testing::Value( |
| 544 |
csds_response.config(0).generic_xds_configs(), |
| 545 |
::testing::Contains(EqGenericXdsConfig( |
| 546 |
kEdsTypeUrl, kDefaultEdsServiceName, "1", |
| 547 |
UnpackClusterLoadAssignment(EqClusterLoadAssignment( |
| 548 |
kDefaultEdsServiceName, backends_[0]->port(), |
| 549 |
kDefaultLocalityWeight)), |
| 550 |
ClientResourceStatus::NACKED, |
| 551 |
EqUpdateFailureState( |
| 552 |
::testing::HasSubstr( |
| 553 |
"errors parsing EDS resource: [" |
| 554 |
"field:endpoints[0].locality error:field not present]"), |
| 555 |
"2")))); |
| 556 |
if (ok) return; // TEST PASSED! |
| 557 |
gpr_sleep_until( |
| 558 |
grpc_timeout_milliseconds_to_deadline(kFetchIntervalMilliseconds)); |
| 559 |
} |
| 560 |
FAIL() << "error_state not seen in CSDS responses"; |
| 561 |
} |
| 562 |
|
| 563 |
TEST_P(ClientStatusDiscoveryServiceTest, XdsConfigDumpListenerRequested) { |
| 564 |
int kTimeoutMillisecond = 1000; |
| 565 |
balancer_->ads_service()->UnsetResource(kLdsTypeUrl, kServerName); |
| 566 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED, |
| 567 |
"Deadline Exceeded", |
| 568 |
RpcOptions().set_timeout_ms(kTimeoutMillisecond)); |
| 569 |
auto csds_response = FetchCsdsResponse(); |
| 570 |
EXPECT_THAT(csds_response.config(0).generic_xds_configs(), |
| 571 |
::testing::Contains(EqGenericXdsConfig( |
| 572 |
kLdsTypeUrl, kServerName, ::testing::_, ::testing::_, |
| 573 |
ClientResourceStatus::REQUESTED, ::testing::_))); |
| 574 |
} |
| 575 |
|
| 576 |
TEST_P(ClientStatusDiscoveryServiceTest, XdsConfigDumpClusterRequested) { |
| 577 |
int kTimeoutMillisecond = 1000; |
| 578 |
std::string kClusterName1 = "cluster-1"; |
| 579 |
std::string kClusterName2 = "cluster-2"; |
| 580 |
// Create a route config requesting two non-existing clusters |
| 581 |
RouteConfiguration route_config; |
| 582 |
route_config.set_name(kDefaultRouteConfigurationName); |
| 583 |
auto* vh = route_config.add_virtual_hosts(); |
| 584 |
// The VirtualHost must match the domain name, otherwise will cause resolver |
| 585 |
// transient failure. |
| 586 |
vh->add_domains("*"); |
| 587 |
auto* routes1 = vh->add_routes(); |
| 588 |
routes1->mutable_match()->set_prefix(""); |
| 589 |
routes1->mutable_route()->set_cluster(kClusterName1); |
| 590 |
auto* routes2 = vh->add_routes(); |
| 591 |
routes2->mutable_match()->set_prefix(""); |
| 592 |
routes2->mutable_route()->set_cluster(kClusterName2); |
| 593 |
SetRouteConfiguration(balancer_.get(), route_config); |
| 594 |
// Try to get the configs plumb through |
| 595 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED, |
| 596 |
"Deadline Exceeded", |
| 597 |
RpcOptions().set_timeout_ms(kTimeoutMillisecond)); |
| 598 |
auto csds_response = FetchCsdsResponse(); |
| 599 |
EXPECT_THAT(csds_response.config(0).generic_xds_configs(), |
| 600 |
::testing::AllOf( |
| 601 |
::testing::Contains(EqGenericXdsConfig( |
| 602 |
kCdsTypeUrl, kClusterName1, ::testing::_, ::testing::_, |
| 603 |
ClientResourceStatus::REQUESTED, ::testing::_)), |
| 604 |
::testing::Contains(EqGenericXdsConfig( |
| 605 |
kCdsTypeUrl, kClusterName2, ::testing::_, ::testing::_, |
| 606 |
ClientResourceStatus::REQUESTED, ::testing::_)))); |
| 607 |
} |
| 608 |
|
| 609 |
class CsdsShortAdsTimeoutTest : public ClientStatusDiscoveryServiceTest { |
| 610 |
protected: |
| 611 |
void SetUp() override { |
| 612 |
// Shorten the ADS subscription timeout to speed up the test run. |
| 613 |
InitClient(BootstrapBuilder(), /*lb_expected_authority=*/"", |
| 614 |
/*xds_resource_does_not_exist_timeout_ms=*/2000); |
| 615 |
} |
| 616 |
}; |
| 617 |
|
| 618 |
// Run CSDS tests with RDS enabled and disabled. |
| 619 |
// These need to run with the bootstrap from an env var instead of from |
| 620 |
// a channel arg, since there needs to be a global XdsClient instance. |
| 621 |
INSTANTIATE_TEST_SUITE_P( |
| 622 |
XdsTest, CsdsShortAdsTimeoutTest, |
| 623 |
::testing::Values( |
| 624 |
XdsTestType().set_bootstrap_source(XdsTestType::kBootstrapFromEnvVar), |
| 625 |
XdsTestType() |
| 626 |
.set_bootstrap_source(XdsTestType::kBootstrapFromEnvVar) |
| 627 |
.set_enable_rds_testing(), |
| 628 |
XdsTestType() |
| 629 |
.set_bootstrap_source(XdsTestType::kBootstrapFromEnvVar) |
| 630 |
.set_use_csds_streaming(), |
| 631 |
XdsTestType() |
| 632 |
.set_bootstrap_source(XdsTestType::kBootstrapFromEnvVar) |
| 633 |
.set_enable_rds_testing() |
| 634 |
.set_use_csds_streaming()), |
| 635 |
&XdsTestType::Name); |
| 636 |
|
| 637 |
TEST_P(CsdsShortAdsTimeoutTest, XdsConfigDumpListenerDoesNotExist) { |
| 638 |
int kTimeoutMillisecond = 1000000; // 1000s wait for the transient failure. |
| 639 |
balancer_->ads_service()->UnsetResource(kLdsTypeUrl, kServerName); |
| 640 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 641 |
absl::StrCat("empty address list: ", kServerName, |
| 642 |
": xDS listener resource does not exist"), |
| 643 |
RpcOptions().set_timeout_ms(kTimeoutMillisecond)); |
| 644 |
auto csds_response = FetchCsdsResponse(); |
| 645 |
EXPECT_THAT(csds_response.config(0).generic_xds_configs(), |
| 646 |
::testing::Contains(EqGenericXdsConfig( |
| 647 |
kLdsTypeUrl, kServerName, ::testing::_, ::testing::_, |
| 648 |
ClientResourceStatus::DOES_NOT_EXIST, ::testing::_))); |
| 649 |
} |
| 650 |
|
| 651 |
TEST_P(CsdsShortAdsTimeoutTest, XdsConfigDumpRouteConfigDoesNotExist) { |
| 652 |
if (!GetParam().enable_rds_testing()) return; |
| 653 |
int kTimeoutMillisecond = 1000000; // 1000s wait for the transient failure. |
| 654 |
balancer_->ads_service()->UnsetResource(kRdsTypeUrl, |
| 655 |
kDefaultRouteConfigurationName); |
| 656 |
CheckRpcSendFailure( |
| 657 |
DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 658 |
absl::StrCat("empty address list: ", kDefaultRouteConfigurationName, |
| 659 |
": xDS route configuration resource does not exist"), |
| 660 |
RpcOptions().set_timeout_ms(kTimeoutMillisecond)); |
| 661 |
auto csds_response = FetchCsdsResponse(); |
| 662 |
EXPECT_THAT( |
| 663 |
csds_response.config(0).generic_xds_configs(), |
| 664 |
::testing::Contains(EqGenericXdsConfig( |
| 665 |
kRdsTypeUrl, kDefaultRouteConfigurationName, ::testing::_, |
| 666 |
::testing::_, ClientResourceStatus::DOES_NOT_EXIST, ::testing::_))); |
| 667 |
} |
| 668 |
|
| 669 |
TEST_P(CsdsShortAdsTimeoutTest, XdsConfigDumpClusterDoesNotExist) { |
| 670 |
int kTimeoutMillisecond = 1000000; // 1000s wait for the transient failure. |
| 671 |
balancer_->ads_service()->UnsetResource(kCdsTypeUrl, kDefaultClusterName); |
| 672 |
CheckRpcSendFailure( |
| 673 |
DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 674 |
absl::StrCat("CDS resource \"", kDefaultClusterName, "\" does not exist"), |
| 675 |
RpcOptions().set_timeout_ms(kTimeoutMillisecond)); |
| 676 |
auto csds_response = FetchCsdsResponse(); |
| 677 |
EXPECT_THAT(csds_response.config(0).generic_xds_configs(), |
| 678 |
::testing::Contains(EqGenericXdsConfig( |
| 679 |
kCdsTypeUrl, kDefaultClusterName, ::testing::_, ::testing::_, |
| 680 |
ClientResourceStatus::DOES_NOT_EXIST, ::testing::_))); |
| 681 |
} |
| 682 |
|
| 683 |
TEST_P(CsdsShortAdsTimeoutTest, XdsConfigDumpEndpointDoesNotExist) { |
| 684 |
int kTimeoutMillisecond = 1000000; // 1000s wait for the transient failure. |
| 685 |
balancer_->ads_service()->UnsetResource(kEdsTypeUrl, kDefaultEdsServiceName); |
| 686 |
CheckRpcSendFailure( |
| 687 |
DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 688 |
"no children in weighted_target policy: EDS resource eds_service_name " |
| 689 |
"does not exist", |
| 690 |
RpcOptions().set_timeout_ms(kTimeoutMillisecond)); |
| 691 |
auto csds_response = FetchCsdsResponse(); |
| 692 |
EXPECT_THAT( |
| 693 |
csds_response.config(0).generic_xds_configs(), |
| 694 |
::testing::Contains(EqGenericXdsConfig( |
| 695 |
kEdsTypeUrl, kDefaultEdsServiceName, ::testing::_, ::testing::_, |
| 696 |
ClientResourceStatus::DOES_NOT_EXIST, ::testing::_))); |
| 697 |
} |
| 698 |
|
| 699 |
} // namespace |
| 700 |
} // namespace testing |
| 701 |
} // namespace grpc |
| 702 |
|
| 703 |
#endif // DISABLED_XDS_PROTO_IN_CC |
| 704 |
|
| 705 |
int main(int argc, char** argv) { |
| 706 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 707 |
::testing::InitGoogleTest(&argc, argv); |
| 708 |
// Make the backup poller poll very frequently in order to pick up |
| 709 |
// updates from all the subchannels's FDs. |
| 710 |
GPR_GLOBAL_CONFIG_SET(grpc_client_channel_backup_poll_interval_ms, 1); |
| 711 |
#if TARGET_OS_IPHONE |
| 712 |
// Workaround Apple CFStream bug |
| 713 |
grpc_core::SetEnv("grpc_cfstream", "0"); |
| 714 |
#endif |
| 715 |
grpc_init(); |
| 716 |
const auto result = RUN_ALL_TESTS(); |
| 717 |
grpc_shutdown(); |
| 718 |
return result; |
| 719 |
} |