| 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/strings/str_cat.h" |
| 24 |
|
| 25 |
#include "src/core/ext/filters/client_channel/backup_poller.h" |
| 26 |
#include "test/core/util/scoped_env_var.h" |
| 27 |
#include "test/cpp/end2end/xds/xds_end2end_test_lib.h" |
| 28 |
|
| 29 |
namespace grpc { |
| 30 |
namespace testing { |
| 31 |
namespace { |
| 32 |
|
| 33 |
using ClientStats = LrsServiceImpl::ClientStats; |
| 34 |
|
| 35 |
using ::grpc_core::testing::ScopedExperimentalEnvVar; |
| 36 |
|
| 37 |
// |
| 38 |
// XdsClientTest - basic tests of XdsClient functionality |
| 39 |
// |
| 40 |
|
| 41 |
using XdsClientTest = XdsEnd2endTest; |
| 42 |
|
| 43 |
INSTANTIATE_TEST_SUITE_P(XdsTest, XdsClientTest, |
| 44 |
::testing::Values(XdsTestType()), &XdsTestType::Name); |
| 45 |
|
| 46 |
// Tests that the client can handle resource wrapped in a Resource message. |
| 47 |
TEST_P(XdsClientTest, ResourceWrappedInResourceMessage) { |
| 48 |
CreateAndStartBackends(1); |
| 49 |
balancer_->ads_service()->set_wrap_resources(true); |
| 50 |
const size_t kNumRpcsPerAddress = 100; |
| 51 |
EdsResourceArgs args({ |
| 52 |
{"locality0", CreateEndpointsForBackends()}, |
| 53 |
}); |
| 54 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 55 |
// Make sure that trying to connect works without a call. |
| 56 |
channel_->GetState(true /* try_to_connect */); |
| 57 |
// We need to wait for all backends to come online. |
| 58 |
WaitForAllBackends(DEBUG_LOCATION); |
| 59 |
// Send kNumRpcsPerAddress RPCs per server. |
| 60 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcsPerAddress * backends_.size()); |
| 61 |
// Each backend should have gotten 100 requests. |
| 62 |
for (size_t i = 0; i < backends_.size(); ++i) { |
| 63 |
EXPECT_EQ(kNumRpcsPerAddress, |
| 64 |
backends_[i]->backend_service()->request_count()); |
| 65 |
} |
| 66 |
// Check LB policy name for the channel. |
| 67 |
EXPECT_EQ("xds_cluster_manager_experimental", |
| 68 |
channel_->GetLoadBalancingPolicyName()); |
| 69 |
} |
| 70 |
|
| 71 |
TEST_P(XdsClientTest, ResourceTypeVersionPersistsAcrossStreamRestarts) { |
| 72 |
CreateAndStartBackends(2); |
| 73 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 74 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 75 |
// Wait for backends to come online. |
| 76 |
WaitForAllBackends(DEBUG_LOCATION, 0, 1); |
| 77 |
// Stop balancer. |
| 78 |
balancer_->Shutdown(); |
| 79 |
// Tell balancer to require minimum version 1 for all resource types. |
| 80 |
balancer_->ads_service()->SetResourceMinVersion(kLdsTypeUrl, 1); |
| 81 |
balancer_->ads_service()->SetResourceMinVersion(kRdsTypeUrl, 1); |
| 82 |
balancer_->ads_service()->SetResourceMinVersion(kCdsTypeUrl, 1); |
| 83 |
balancer_->ads_service()->SetResourceMinVersion(kEdsTypeUrl, 1); |
| 84 |
// Update backend, just so we can be sure that the client has |
| 85 |
// reconnected to the balancer. |
| 86 |
args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(1, 2)}}); |
| 87 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 88 |
// Restart balancer. |
| 89 |
balancer_->Start(); |
| 90 |
// Make sure client has reconnected. |
| 91 |
WaitForAllBackends(DEBUG_LOCATION, 1, 2); |
| 92 |
} |
| 93 |
|
| 94 |
// Tests that we restart all xDS requests when we reestablish the ADS call. |
| 95 |
TEST_P(XdsClientTest, RestartsRequestsUponReconnection) { |
| 96 |
CreateAndStartBackends(2); |
| 97 |
// Manually configure use of RDS. |
| 98 |
auto listener = default_listener_; |
| 99 |
HttpConnectionManager http_connection_manager; |
| 100 |
listener.mutable_api_listener()->mutable_api_listener()->UnpackTo( |
| 101 |
&http_connection_manager); |
| 102 |
auto* rds = http_connection_manager.mutable_rds(); |
| 103 |
rds->set_route_config_name(kDefaultRouteConfigurationName); |
| 104 |
rds->mutable_config_source()->mutable_self(); |
| 105 |
listener.mutable_api_listener()->mutable_api_listener()->PackFrom( |
| 106 |
http_connection_manager); |
| 107 |
balancer_->ads_service()->SetLdsResource(listener); |
| 108 |
balancer_->ads_service()->SetRdsResource(default_route_config_); |
| 109 |
const char* kNewClusterName = "new_cluster_name"; |
| 110 |
const char* kNewEdsServiceName = "new_eds_service_name"; |
| 111 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 112 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 113 |
// We need to wait for all backends to come online. |
| 114 |
WaitForAllBackends(DEBUG_LOCATION, 0, 1); |
| 115 |
// Now shut down and restart the balancer. When the client |
| 116 |
// reconnects, it should automatically restart the requests for all |
| 117 |
// resource types. |
| 118 |
balancer_->Shutdown(); |
| 119 |
balancer_->Start(); |
| 120 |
// Make sure things are still working. |
| 121 |
CheckRpcSendOk(DEBUG_LOCATION, 100); |
| 122 |
// Populate new EDS resource. |
| 123 |
args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(1, 2)}}); |
| 124 |
balancer_->ads_service()->SetEdsResource( |
| 125 |
BuildEdsResource(args, kNewEdsServiceName)); |
| 126 |
// Populate new CDS resource. |
| 127 |
Cluster new_cluster = default_cluster_; |
| 128 |
new_cluster.set_name(kNewClusterName); |
| 129 |
new_cluster.mutable_eds_cluster_config()->set_service_name( |
| 130 |
kNewEdsServiceName); |
| 131 |
balancer_->ads_service()->SetCdsResource(new_cluster); |
| 132 |
// Change RDS resource to point to new cluster. |
| 133 |
RouteConfiguration new_route_config = default_route_config_; |
| 134 |
new_route_config.mutable_virtual_hosts(0) |
| 135 |
->mutable_routes(0) |
| 136 |
->mutable_route() |
| 137 |
->set_cluster(kNewClusterName); |
| 138 |
balancer_->ads_service()->SetRdsResource(new_route_config); |
| 139 |
// Wait for all new backends to be used. |
| 140 |
WaitForAllBackends(DEBUG_LOCATION, 1, 2); |
| 141 |
} |
| 142 |
|
| 143 |
// Tests that the NACK for multiple bad resources includes both errors. |
| 144 |
TEST_P(XdsClientTest, MultipleBadCdsResources) { |
| 145 |
constexpr char kClusterName2[] = "cluster_name_2"; |
| 146 |
constexpr char kClusterName3[] = "cluster_name_3"; |
| 147 |
CreateAndStartBackends(1); |
| 148 |
balancer_->ads_service()->set_inject_bad_resources_for_resource_type( |
| 149 |
kCdsTypeUrl); |
| 150 |
// Add cluster with unsupported type. |
| 151 |
auto cluster = default_cluster_; |
| 152 |
cluster.set_name(kClusterName2); |
| 153 |
cluster.set_type(Cluster::STATIC); |
| 154 |
balancer_->ads_service()->SetCdsResource(cluster); |
| 155 |
// Add second cluster with the same error. |
| 156 |
cluster.set_name(kClusterName3); |
| 157 |
balancer_->ads_service()->SetCdsResource(cluster); |
| 158 |
// Change RouteConfig to point to all clusters. |
| 159 |
RouteConfiguration route_config = default_route_config_; |
| 160 |
route_config.mutable_virtual_hosts(0)->clear_routes(); |
| 161 |
// First route: default cluster, selected based on header. |
| 162 |
auto* route = route_config.mutable_virtual_hosts(0)->add_routes(); |
| 163 |
route->mutable_match()->set_prefix(""); |
| 164 |
auto* header_matcher = route->mutable_match()->add_headers(); |
| 165 |
header_matcher->set_name("cluster"); |
| 166 |
header_matcher->set_exact_match(kDefaultClusterName); |
| 167 |
route->mutable_route()->set_cluster(kDefaultClusterName); |
| 168 |
// Second route: cluster 2, selected based on header. |
| 169 |
route = route_config.mutable_virtual_hosts(0)->add_routes(); |
| 170 |
route->mutable_match()->set_prefix(""); |
| 171 |
header_matcher = route->mutable_match()->add_headers(); |
| 172 |
header_matcher->set_name("cluster"); |
| 173 |
header_matcher->set_exact_match(kClusterName2); |
| 174 |
route->mutable_route()->set_cluster(kClusterName2); |
| 175 |
// Third route: cluster 3, used by default. |
| 176 |
route = route_config.mutable_virtual_hosts(0)->add_routes(); |
| 177 |
route->mutable_match()->set_prefix(""); |
| 178 |
route->mutable_route()->set_cluster(kClusterName3); |
| 179 |
SetRouteConfiguration(balancer_.get(), route_config); |
| 180 |
// Add EDS resource. |
| 181 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 182 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 183 |
// Send RPC. |
| 184 |
const auto response_state = WaitForCdsNack(DEBUG_LOCATION); |
| 185 |
ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK"; |
| 186 |
EXPECT_EQ( |
| 187 |
response_state->error_message, |
| 188 |
absl::StrCat("xDS response validation errors: [" |
| 189 |
"resource index 0: Can't decode Resource proto wrapper; ", |
| 190 |
"resource index 1: foo: " |
| 191 |
"INVALID_ARGUMENT: Can't parse Cluster resource.; " |
| 192 |
"resource index 3: ", |
| 193 |
kClusterName2, |
| 194 |
": INVALID_ARGUMENT: errors validating Cluster resource: [" |
| 195 |
"field:type error:unknown discovery type]; " |
| 196 |
"resource index 4: ", |
| 197 |
kClusterName3, |
| 198 |
": INVALID_ARGUMENT: errors validating Cluster resource: [" |
| 199 |
"field:type error:unknown discovery type]]")); |
| 200 |
// RPCs for default cluster should succeed. |
| 201 |
std::vector<std::pair<std::string, std::string>> metadata_default_cluster = { |
| 202 |
{"cluster", kDefaultClusterName}, |
| 203 |
}; |
| 204 |
CheckRpcSendOk( |
| 205 |
DEBUG_LOCATION, 1, |
| 206 |
RpcOptions().set_metadata(std::move(metadata_default_cluster))); |
| 207 |
// RPCs for cluster 2 should fail. |
| 208 |
std::vector<std::pair<std::string, std::string>> metadata_cluster_2 = { |
| 209 |
{"cluster", kClusterName2}, |
| 210 |
}; |
| 211 |
CheckRpcSendFailure( |
| 212 |
DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 213 |
"cluster_name_2: UNAVAILABLE: invalid resource: " |
| 214 |
"INVALID_ARGUMENT: errors validating Cluster resource: \\[" |
| 215 |
"field:type error:unknown discovery type\\] .*", |
| 216 |
RpcOptions().set_metadata(std::move(metadata_cluster_2))); |
| 217 |
} |
| 218 |
|
| 219 |
TEST_P(XdsClientTest, XdsStreamErrorPropagation) { |
| 220 |
const std::string kErrorMessage = "test forced ADS stream failure"; |
| 221 |
balancer_->ads_service()->ForceADSFailure( |
| 222 |
Status(StatusCode::RESOURCE_EXHAUSTED, kErrorMessage)); |
| 223 |
auto status = SendRpc(); |
| 224 |
gpr_log(GPR_INFO, |
| 225 |
"XdsStreamErrorPropagation test: RPC got error: code=%d message=%s", |
| 226 |
status.error_code(), status.error_message().c_str()); |
| 227 |
EXPECT_THAT(status.error_code(), StatusCode::UNAVAILABLE); |
| 228 |
EXPECT_THAT(status.error_message(), ::testing::HasSubstr(kErrorMessage)); |
| 229 |
EXPECT_THAT(status.error_message(), |
| 230 |
::testing::HasSubstr("(node ID:xds_end2end_test)")); |
| 231 |
} |
| 232 |
|
| 233 |
// |
| 234 |
// GlobalXdsClientTest - tests that need to run with a global XdsClient |
| 235 |
// (this is the default in production) |
| 236 |
// |
| 237 |
|
| 238 |
using GlobalXdsClientTest = XdsEnd2endTest; |
| 239 |
|
| 240 |
// Get bootstrap from env var, so that there's a global XdsClient. |
| 241 |
INSTANTIATE_TEST_SUITE_P(XdsTest, GlobalXdsClientTest, |
| 242 |
::testing::Values(XdsTestType().set_bootstrap_source( |
| 243 |
XdsTestType::kBootstrapFromEnvVar)), |
| 244 |
&XdsTestType::Name); |
| 245 |
|
| 246 |
TEST_P(GlobalXdsClientTest, MultipleChannelsShareXdsClient) { |
| 247 |
CreateAndStartBackends(1); |
| 248 |
const char* kNewServerName = "new-server.example.com"; |
| 249 |
Listener listener = default_listener_; |
| 250 |
listener.set_name(kNewServerName); |
| 251 |
SetListenerAndRouteConfiguration(balancer_.get(), listener, |
| 252 |
default_route_config_); |
| 253 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 254 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 255 |
WaitForAllBackends(DEBUG_LOCATION); |
| 256 |
// Create second channel and tell it to connect to kNewServerName. |
| 257 |
auto channel2 = CreateChannel(/*failover_timeout_ms=*/0, kNewServerName); |
| 258 |
channel2->GetState(/*try_to_connect=*/true); |
| 259 |
ASSERT_TRUE(channel2->WaitForConnected(grpc_timeout_seconds_to_deadline(1))); |
| 260 |
// Make sure there's only one client connected. |
| 261 |
EXPECT_EQ(1UL, balancer_->ads_service()->clients().size()); |
| 262 |
} |
| 263 |
|
| 264 |
TEST_P( |
| 265 |
GlobalXdsClientTest, |
| 266 |
MultipleChannelsShareXdsClientWithResourceUpdateAfterOneChannelGoesAway) { |
| 267 |
CreateAndStartBackends(2); |
| 268 |
// Test for https://github.com/grpc/grpc/issues/28468. Makes sure that the |
| 269 |
// XdsClient properly handles the case where there are multiple watchers on |
| 270 |
// the same resource and one of them unsubscribes. |
| 271 |
const char* kNewServerName = "new-server.example.com"; |
| 272 |
Listener listener = default_listener_; |
| 273 |
listener.set_name(kNewServerName); |
| 274 |
SetListenerAndRouteConfiguration(balancer_.get(), listener, |
| 275 |
default_route_config_); |
| 276 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(EdsResourceArgs({ |
| 277 |
{"locality0", CreateEndpointsForBackends(0, 1)}, |
| 278 |
}))); |
| 279 |
WaitForBackend(DEBUG_LOCATION, 0); |
| 280 |
// Create second channel and tell it to connect to kNewServerName. |
| 281 |
auto channel2 = CreateChannel(/*failover_timeout_ms=*/0, kNewServerName); |
| 282 |
channel2->GetState(/*try_to_connect=*/true); |
| 283 |
ASSERT_TRUE(channel2->WaitForConnected(grpc_timeout_seconds_to_deadline(1))); |
| 284 |
// Now, destroy the new channel, send an EDS update to use a different backend |
| 285 |
// and test that the channel switches to that backend. |
| 286 |
channel2.reset(); |
| 287 |
// This sleep is needed to be able to reproduce the bug and to give time for |
| 288 |
// the buggy unsubscription to take place. |
| 289 |
// TODO(yashykt): Figure out a way to do this without the sleep. |
| 290 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(10)); |
| 291 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(EdsResourceArgs({ |
| 292 |
{"locality0", CreateEndpointsForBackends(1, 2)}, |
| 293 |
}))); |
| 294 |
WaitForBackend(DEBUG_LOCATION, 1); |
| 295 |
} |
| 296 |
|
| 297 |
// Tests that the NACK for multiple bad LDS resources includes both errors. |
| 298 |
// This needs to be in GlobalXdsClientTest because the only way to request |
| 299 |
// two LDS resources in the same XdsClient is for two channels to share |
| 300 |
// the same XdsClient. |
| 301 |
TEST_P(GlobalXdsClientTest, MultipleBadLdsResources) { |
| 302 |
CreateAndStartBackends(1); |
| 303 |
constexpr char kServerName2[] = "server.other.com"; |
| 304 |
constexpr char kServerName3[] = "server.another.com"; |
| 305 |
auto listener = default_listener_; |
| 306 |
listener.clear_api_listener(); |
| 307 |
balancer_->ads_service()->SetLdsResource(listener); |
| 308 |
listener.set_name(kServerName2); |
| 309 |
balancer_->ads_service()->SetLdsResource(listener); |
| 310 |
listener = default_listener_; |
| 311 |
listener.set_name(kServerName3); |
| 312 |
SetListenerAndRouteConfiguration(balancer_.get(), listener, |
| 313 |
default_route_config_); |
| 314 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 315 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 316 |
const auto response_state = WaitForLdsNack(DEBUG_LOCATION); |
| 317 |
ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK"; |
| 318 |
EXPECT_EQ(response_state->error_message, |
| 319 |
"xDS response validation errors: [" |
| 320 |
"resource index 0: server.example.com: " |
| 321 |
"INVALID_ARGUMENT: Listener has neither address nor ApiListener]"); |
| 322 |
// Need to create a second channel to subscribe to a second LDS resource. |
| 323 |
auto channel2 = CreateChannel(0, kServerName2); |
| 324 |
auto stub2 = grpc::testing::EchoTestService::NewStub(channel2); |
| 325 |
{ |
| 326 |
ClientContext context; |
| 327 |
EchoRequest request; |
| 328 |
request.set_message(kRequestMessage); |
| 329 |
EchoResponse response; |
| 330 |
grpc::Status status = stub2->Echo(&context, request, &response); |
| 331 |
EXPECT_FALSE(status.ok()); |
| 332 |
// Wait for second NACK to be reported to xDS server. |
| 333 |
const auto response_state = WaitForLdsNack(DEBUG_LOCATION); |
| 334 |
ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK"; |
| 335 |
EXPECT_EQ( |
| 336 |
response_state->error_message, |
| 337 |
"xDS response validation errors: [" |
| 338 |
"resource index 0: server.other.com: " |
| 339 |
"INVALID_ARGUMENT: Listener has neither address nor ApiListener; " |
| 340 |
"resource index 1: server.example.com: " |
| 341 |
"INVALID_ARGUMENT: Listener has neither address nor ApiListener]"); |
| 342 |
} |
| 343 |
// Now start a new channel with a third server name, this one with a |
| 344 |
// valid resource. |
| 345 |
auto channel3 = CreateChannel(0, kServerName3); |
| 346 |
auto stub3 = grpc::testing::EchoTestService::NewStub(channel3); |
| 347 |
{ |
| 348 |
ClientContext context; |
| 349 |
EchoRequest request; |
| 350 |
request.set_message(kRequestMessage); |
| 351 |
EchoResponse response; |
| 352 |
grpc::Status status = stub3->Echo(&context, request, &response); |
| 353 |
EXPECT_TRUE(status.ok()) << "code=" << status.error_code() |
| 354 |
<< " message=" << status.error_message(); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
// Tests that we don't trigger does-not-exist callbacks for a resource |
| 359 |
// that was previously valid but is updated to be invalid. |
| 360 |
TEST_P(GlobalXdsClientTest, InvalidListenerStillExistsIfPreviouslyCached) { |
| 361 |
CreateAndStartBackends(1); |
| 362 |
// Set up valid resources and check that the channel works. |
| 363 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 364 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 365 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 366 |
// Now send an update changing the Listener to be invalid. |
| 367 |
auto listener = default_listener_; |
| 368 |
listener.clear_api_listener(); |
| 369 |
balancer_->ads_service()->SetLdsResource(listener); |
| 370 |
const auto response_state = |
| 371 |
WaitForLdsNack(DEBUG_LOCATION, RpcOptions(), StatusCode::OK); |
| 372 |
ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK"; |
| 373 |
EXPECT_EQ(response_state->error_message, |
| 374 |
"xDS response validation errors: [" |
| 375 |
"resource index 0: server.example.com: " |
| 376 |
"INVALID_ARGUMENT: Listener has neither address nor ApiListener]"); |
| 377 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 378 |
} |
| 379 |
|
| 380 |
// |
| 381 |
// TimeoutTest - tests xDS initial timeout handling |
| 382 |
// |
| 383 |
|
| 384 |
class TimeoutTest : public XdsEnd2endTest { |
| 385 |
protected: |
| 386 |
void SetUp() override { |
| 387 |
InitClient(BootstrapBuilder(), /*lb_expected_authority=*/"", |
| 388 |
/*xds_resource_does_not_exist_timeout_ms=*/2000); |
| 389 |
} |
| 390 |
}; |
| 391 |
|
| 392 |
// Enable RDS, so that we can test all resource types. |
| 393 |
// Run with bootstrap from env var so that multiple channels share the same |
| 394 |
// XdsClient (needed for testing the timeout for the 2nd LDS and RDS resource). |
| 395 |
INSTANTIATE_TEST_SUITE_P( |
| 396 |
XdsTest, TimeoutTest, |
| 397 |
::testing::Values( |
| 398 |
XdsTestType().set_enable_rds_testing().set_bootstrap_source( |
| 399 |
XdsTestType::kBootstrapFromEnvVar)), |
| 400 |
&XdsTestType::Name); |
| 401 |
|
| 402 |
TEST_P(TimeoutTest, LdsServerIgnoresRequest) { |
| 403 |
balancer_->ads_service()->IgnoreResourceType(kLdsTypeUrl); |
| 404 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 405 |
absl::StrCat("empty address list: ", kServerName, |
| 406 |
": xDS listener resource does not exist"), |
| 407 |
RpcOptions().set_timeout_ms(4000)); |
| 408 |
} |
| 409 |
|
| 410 |
TEST_P(TimeoutTest, LdsResourceNotPresentInRequest) { |
| 411 |
balancer_->ads_service()->UnsetResource(kLdsTypeUrl, kServerName); |
| 412 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 413 |
absl::StrCat("empty address list: ", kServerName, |
| 414 |
": xDS listener resource does not exist"), |
| 415 |
RpcOptions().set_timeout_ms(4000)); |
| 416 |
} |
| 417 |
|
| 418 |
TEST_P(TimeoutTest, LdsSecondResourceNotPresentInRequest) { |
| 419 |
ASSERT_NE(GetParam().bootstrap_source(), |
| 420 |
XdsTestType::kBootstrapFromChannelArg) |
| 421 |
<< "This test cannot use bootstrap from channel args, because it " |
| 422 |
"needs two channels to use the same XdsClient instance."; |
| 423 |
CreateAndStartBackends(1); |
| 424 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 425 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 426 |
CheckRpcSendOk(DEBUG_LOCATION, 1, RpcOptions().set_timeout_ms(4000)); |
| 427 |
// Create second channel for a new server name. |
| 428 |
// This should fail because there is no LDS resource for this server name. |
| 429 |
const char* kNewServerName = "new-server.example.com"; |
| 430 |
auto channel2 = CreateChannel(/*failover_timeout_ms=*/0, kNewServerName); |
| 431 |
auto stub2 = grpc::testing::EchoTestService::NewStub(channel2); |
| 432 |
ClientContext context; |
| 433 |
EchoRequest request; |
| 434 |
EchoResponse response; |
| 435 |
RpcOptions rpc_options; |
| 436 |
rpc_options.set_timeout_ms(4000).SetupRpc(&context, &request); |
| 437 |
auto status = |
| 438 |
SendRpcMethod(stub2.get(), rpc_options, &context, request, &response); |
| 439 |
EXPECT_EQ(StatusCode::UNAVAILABLE, status.error_code()); |
| 440 |
EXPECT_THAT(status.error_message(), |
| 441 |
absl::StrCat("empty address list: ", kNewServerName, |
| 442 |
": xDS listener resource does not exist")); |
| 443 |
} |
| 444 |
|
| 445 |
TEST_P(TimeoutTest, RdsServerIgnoresRequest) { |
| 446 |
balancer_->ads_service()->IgnoreResourceType(kRdsTypeUrl); |
| 447 |
CheckRpcSendFailure( |
| 448 |
DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 449 |
absl::StrCat("empty address list: ", kDefaultRouteConfigurationName, |
| 450 |
": xDS route configuration resource does not exist"), |
| 451 |
RpcOptions().set_timeout_ms(4000)); |
| 452 |
} |
| 453 |
|
| 454 |
TEST_P(TimeoutTest, RdsResourceNotPresentInRequest) { |
| 455 |
balancer_->ads_service()->UnsetResource(kRdsTypeUrl, |
| 456 |
kDefaultRouteConfigurationName); |
| 457 |
CheckRpcSendFailure( |
| 458 |
DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 459 |
absl::StrCat("empty address list: ", kDefaultRouteConfigurationName, |
| 460 |
": xDS route configuration resource does not exist"), |
| 461 |
RpcOptions().set_timeout_ms(4000)); |
| 462 |
} |
| 463 |
|
| 464 |
TEST_P(TimeoutTest, RdsSecondResourceNotPresentInRequest) { |
| 465 |
ASSERT_NE(GetParam().bootstrap_source(), |
| 466 |
XdsTestType::kBootstrapFromChannelArg) |
| 467 |
<< "This test cannot use bootstrap from channel args, because it " |
| 468 |
"needs two channels to use the same XdsClient instance."; |
| 469 |
CreateAndStartBackends(1); |
| 470 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 471 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 472 |
CheckRpcSendOk(DEBUG_LOCATION, 1, RpcOptions().set_timeout_ms(4000)); |
| 473 |
// Add listener for 2nd channel, but no RDS resource. |
| 474 |
const char* kNewServerName = "new-server.example.com"; |
| 475 |
const char* kNewRouteConfigName = "rds_resource_does_not_exist"; |
| 476 |
Listener listener = default_listener_; |
| 477 |
listener.set_name(kNewServerName); |
| 478 |
HttpConnectionManager http_connection_manager = |
| 479 |
ClientHcmAccessor().Unpack(listener); |
| 480 |
auto* rds = http_connection_manager.mutable_rds(); |
| 481 |
rds->set_route_config_name(kNewRouteConfigName); |
| 482 |
rds->mutable_config_source()->mutable_self(); |
| 483 |
ClientHcmAccessor().Pack(http_connection_manager, &listener); |
| 484 |
balancer_->ads_service()->SetLdsResource(listener); |
| 485 |
// Create second channel for a new server name. |
| 486 |
// This should fail because the LDS resource points to a non-existent RDS |
| 487 |
// resource. |
| 488 |
auto channel2 = CreateChannel(/*failover_timeout_ms=*/0, kNewServerName); |
| 489 |
auto stub2 = grpc::testing::EchoTestService::NewStub(channel2); |
| 490 |
ClientContext context; |
| 491 |
EchoRequest request; |
| 492 |
EchoResponse response; |
| 493 |
RpcOptions rpc_options; |
| 494 |
rpc_options.set_timeout_ms(4000).SetupRpc(&context, &request); |
| 495 |
auto status = |
| 496 |
SendRpcMethod(stub2.get(), rpc_options, &context, request, &response); |
| 497 |
EXPECT_EQ(StatusCode::UNAVAILABLE, status.error_code()); |
| 498 |
EXPECT_THAT( |
| 499 |
status.error_message(), |
| 500 |
absl::StrCat("empty address list: ", kNewRouteConfigName, |
| 501 |
": xDS route configuration resource does not exist")); |
| 502 |
} |
| 503 |
|
| 504 |
TEST_P(TimeoutTest, CdsServerIgnoresRequest) { |
| 505 |
balancer_->ads_service()->IgnoreResourceType(kCdsTypeUrl); |
| 506 |
CheckRpcSendFailure( |
| 507 |
DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 508 |
absl::StrCat("CDS resource \"", kDefaultClusterName, "\" does not exist"), |
| 509 |
RpcOptions().set_timeout_ms(4000)); |
| 510 |
} |
| 511 |
|
| 512 |
TEST_P(TimeoutTest, CdsResourceNotPresentInRequest) { |
| 513 |
balancer_->ads_service()->UnsetResource(kCdsTypeUrl, kDefaultClusterName); |
| 514 |
CheckRpcSendFailure( |
| 515 |
DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 516 |
absl::StrCat("CDS resource \"", kDefaultClusterName, "\" does not exist"), |
| 517 |
RpcOptions().set_timeout_ms(4000)); |
| 518 |
} |
| 519 |
|
| 520 |
TEST_P(TimeoutTest, CdsSecondResourceNotPresentInRequest) { |
| 521 |
CreateAndStartBackends(1); |
| 522 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 523 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 524 |
CheckRpcSendOk(DEBUG_LOCATION, 1, RpcOptions().set_timeout_ms(4000)); |
| 525 |
// Change route config to point to non-existing cluster. |
| 526 |
const char* kNewClusterName = "new_cluster_name"; |
| 527 |
RouteConfiguration route_config = default_route_config_; |
| 528 |
route_config.mutable_virtual_hosts(0) |
| 529 |
->mutable_routes(0) |
| 530 |
->mutable_route() |
| 531 |
->set_cluster(kNewClusterName); |
| 532 |
balancer_->ads_service()->SetRdsResource(route_config); |
| 533 |
// New cluster times out. |
| 534 |
// May need to wait a bit for the change to propagate to the client. |
| 535 |
SendRpcsUntil( |
| 536 |
DEBUG_LOCATION, |
| 537 |
[&](const RpcResult& result) { |
| 538 |
if (result.status.ok()) return true; // Keep going. |
| 539 |
EXPECT_EQ(StatusCode::UNAVAILABLE, result.status.error_code()); |
| 540 |
EXPECT_EQ(absl::StrCat("CDS resource \"", kNewClusterName, |
| 541 |
"\" does not exist"), |
| 542 |
result.status.error_message()); |
| 543 |
return false; |
| 544 |
}, |
| 545 |
/*timeout_ms=*/30000, RpcOptions().set_timeout_ms(4000)); |
| 546 |
} |
| 547 |
|
| 548 |
TEST_P(TimeoutTest, EdsServerIgnoresRequest) { |
| 549 |
balancer_->ads_service()->IgnoreResourceType(kEdsTypeUrl); |
| 550 |
CheckRpcSendFailure( |
| 551 |
DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 552 |
"no children in weighted_target policy: EDS resource eds_service_name " |
| 553 |
"does not exist", |
| 554 |
RpcOptions().set_timeout_ms(4000)); |
| 555 |
} |
| 556 |
|
| 557 |
TEST_P(TimeoutTest, EdsResourceNotPresentInRequest) { |
| 558 |
// No need to remove EDS resource, since the test suite does not add it |
| 559 |
// by default. |
| 560 |
CheckRpcSendFailure( |
| 561 |
DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 562 |
"no children in weighted_target policy: EDS resource eds_service_name " |
| 563 |
"does not exist", |
| 564 |
RpcOptions().set_timeout_ms(4000)); |
| 565 |
} |
| 566 |
|
| 567 |
TEST_P(TimeoutTest, EdsSecondResourceNotPresentInRequest) { |
| 568 |
CreateAndStartBackends(1); |
| 569 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 570 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 571 |
CheckRpcSendOk(DEBUG_LOCATION, 1, RpcOptions().set_timeout_ms(4000)); |
| 572 |
// New cluster that points to a non-existant EDS resource. |
| 573 |
const char* kNewClusterName = "new_cluster_name"; |
| 574 |
Cluster cluster = default_cluster_; |
| 575 |
cluster.set_name(kNewClusterName); |
| 576 |
cluster.mutable_eds_cluster_config()->set_service_name( |
| 577 |
"eds_service_name_does_not_exist"); |
| 578 |
balancer_->ads_service()->SetCdsResource(cluster); |
| 579 |
// Now add a route pointing to the new cluster. |
| 580 |
RouteConfiguration route_config = default_route_config_; |
| 581 |
auto* route = route_config.mutable_virtual_hosts(0)->mutable_routes(0); |
| 582 |
*route_config.mutable_virtual_hosts(0)->add_routes() = *route; |
| 583 |
route->mutable_match()->set_path("/grpc.testing.EchoTestService/Echo1"); |
| 584 |
route->mutable_route()->set_cluster(kNewClusterName); |
| 585 |
balancer_->ads_service()->SetRdsResource(route_config); |
| 586 |
// New EDS resource times out. |
| 587 |
// May need to wait a bit for the RDS change to propagate to the client. |
| 588 |
SendRpcsUntil( |
| 589 |
DEBUG_LOCATION, |
| 590 |
[](const RpcResult& result) { |
| 591 |
if (result.status.ok()) return true; // Keep going. |
| 592 |
EXPECT_EQ(StatusCode::UNAVAILABLE, result.status.error_code()); |
| 593 |
EXPECT_EQ(result.status.error_message(), |
| 594 |
"no children in weighted_target policy: EDS resource " |
| 595 |
"eds_service_name_does_not_exist does not exist"); |
| 596 |
return false; |
| 597 |
}, |
| 598 |
/*timeout_ms=*/30000, |
| 599 |
RpcOptions().set_rpc_method(METHOD_ECHO1).set_timeout_ms(4000)); |
| 600 |
} |
| 601 |
|
| 602 |
TEST_P(TimeoutTest, ServerDoesNotResendAfterAdsStreamRestart) { |
| 603 |
CreateAndStartBackends(1); |
| 604 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 605 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 606 |
CheckRpcSendOk(DEBUG_LOCATION, 1, RpcOptions().set_timeout_ms(4000)); |
| 607 |
// Stop balancer. |
| 608 |
balancer_->Shutdown(); |
| 609 |
// Tell balancer to require minimum version 1 for all resource types |
| 610 |
// and to not reply to the requests. |
| 611 |
balancer_->ads_service()->SetResourceMinVersion(kLdsTypeUrl, 1); |
| 612 |
balancer_->ads_service()->IgnoreResourceType(kLdsTypeUrl); |
| 613 |
balancer_->ads_service()->SetResourceMinVersion(kRdsTypeUrl, 1); |
| 614 |
balancer_->ads_service()->IgnoreResourceType(kRdsTypeUrl); |
| 615 |
balancer_->ads_service()->SetResourceMinVersion(kCdsTypeUrl, 1); |
| 616 |
balancer_->ads_service()->IgnoreResourceType(kCdsTypeUrl); |
| 617 |
balancer_->ads_service()->SetResourceMinVersion(kEdsTypeUrl, 1); |
| 618 |
balancer_->ads_service()->IgnoreResourceType(kEdsTypeUrl); |
| 619 |
// Restart balancer. |
| 620 |
balancer_->Start(); |
| 621 |
// Send RPCs for long enough to cover the ADS stream restart delay, |
| 622 |
// the stream restart, and then the resulting timeout period, just to |
| 623 |
// be sure that the channel continues to use the resources from before |
| 624 |
// the restart. |
| 625 |
absl::Time deadline = |
| 626 |
absl::Now() + (absl::Seconds(30) * grpc_test_slowdown_factor()); |
| 627 |
do { |
| 628 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 629 |
} while (absl::Now() < deadline); |
| 630 |
} |
| 631 |
|
| 632 |
// |
| 633 |
// BootstrapSourceTest - tests different bootstrap sources |
| 634 |
// |
| 635 |
|
| 636 |
using BootstrapSourceTest = XdsEnd2endTest; |
| 637 |
|
| 638 |
INSTANTIATE_TEST_SUITE_P( |
| 639 |
XdsTest, BootstrapSourceTest, |
| 640 |
::testing::Values( |
| 641 |
XdsTestType().set_bootstrap_source(XdsTestType::kBootstrapFromEnvVar), |
| 642 |
XdsTestType().set_bootstrap_source(XdsTestType::kBootstrapFromFile)), |
| 643 |
&XdsTestType::Name); |
| 644 |
|
| 645 |
TEST_P(BootstrapSourceTest, Vanilla) { |
| 646 |
CreateAndStartBackends(1); |
| 647 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 648 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 649 |
// Increase timeout, since kBootstrapFromFile takes more time on busy |
| 650 |
// test machines. (We've seen at least one occurrence where it's |
| 651 |
// taken over 10 seconds.) |
| 652 |
CheckRpcSendOk(DEBUG_LOCATION, 1, RpcOptions().set_timeout_ms(15000)); |
| 653 |
} |
| 654 |
|
| 655 |
// |
| 656 |
// XdsFederationTest - tests xDS federation |
| 657 |
// |
| 658 |
|
| 659 |
class XdsFederationTest : public XdsEnd2endTest { |
| 660 |
protected: |
| 661 |
XdsFederationTest() : authority_balancer_(CreateAndStartBalancer()) {} |
| 662 |
|
| 663 |
void SetUp() override { |
| 664 |
// Each test will use a slightly different bootstrap config, |
| 665 |
// so SetUp() is intentionally empty here, and the real |
| 666 |
// setup (calling of InitClient()) is moved into each test. |
| 667 |
} |
| 668 |
|
| 669 |
void TearDown() override { |
| 670 |
authority_balancer_->Shutdown(); |
| 671 |
XdsEnd2endTest::TearDown(); |
| 672 |
} |
| 673 |
|
| 674 |
std::unique_ptr<BalancerServerThread> authority_balancer_; |
| 675 |
}; |
| 676 |
|
| 677 |
// Get bootstrap from env var, so that there's a global XdsClient. |
| 678 |
// Runs with RDS so that we know all resource types work properly. |
| 679 |
INSTANTIATE_TEST_SUITE_P( |
| 680 |
XdsTest, XdsFederationTest, |
| 681 |
::testing::Values( |
| 682 |
XdsTestType() |
| 683 |
.set_bootstrap_source(XdsTestType::kBootstrapFromEnvVar) |
| 684 |
.set_enable_rds_testing()), |
| 685 |
&XdsTestType::Name); |
| 686 |
|
| 687 |
// Channel is created with URI "xds:server.example.com". |
| 688 |
// Bootstrap config default client listener template uses new-style name with |
| 689 |
// authority "xds.example.com". |
| 690 |
TEST_P(XdsFederationTest, FederationTargetNoAuthorityWithResourceTemplate) { |
| 691 |
ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_FEDERATION"); |
| 692 |
const char* kAuthority = "xds.example.com"; |
| 693 |
const char* kNewListenerTemplate = |
| 694 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 695 |
"client/%s?psm_project_id=1234"; |
| 696 |
const char* kNewListenerName = |
| 697 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 698 |
"client/server.example.com?psm_project_id=1234"; |
| 699 |
const char* kNewRouteConfigName = |
| 700 |
"xdstp://xds.example.com/envoy.config.route.v3.RouteConfiguration/" |
| 701 |
"new_route_config_name"; |
| 702 |
const char* kNewEdsServiceName = |
| 703 |
"xdstp://xds.example.com/envoy.config.endpoint.v3.ClusterLoadAssignment/" |
| 704 |
"new_edsservice_name"; |
| 705 |
const char* kNewClusterName = |
| 706 |
"xdstp://xds.example.com/envoy.config.cluster.v3.Cluster/" |
| 707 |
"new_cluster_name"; |
| 708 |
BootstrapBuilder builder = BootstrapBuilder(); |
| 709 |
builder.SetClientDefaultListenerResourceNameTemplate(kNewListenerTemplate); |
| 710 |
builder.AddAuthority( |
| 711 |
kAuthority, absl::StrCat("localhost:", authority_balancer_->port()), |
| 712 |
// Note we will not use the client_listener_resource_name_template field |
| 713 |
// in the authority. |
| 714 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener" |
| 715 |
"client/%s?client_listener_resource_name_template_not_in_use"); |
| 716 |
InitClient(builder); |
| 717 |
CreateAndStartBackends(2); |
| 718 |
// Eds for the new authority balancer. |
| 719 |
EdsResourceArgs args = |
| 720 |
EdsResourceArgs({{"locality0", CreateEndpointsForBackends()}}); |
| 721 |
authority_balancer_->ads_service()->SetEdsResource( |
| 722 |
BuildEdsResource(args, kNewEdsServiceName)); |
| 723 |
// New cluster |
| 724 |
Cluster new_cluster = default_cluster_; |
| 725 |
new_cluster.set_name(kNewClusterName); |
| 726 |
new_cluster.mutable_eds_cluster_config()->set_service_name( |
| 727 |
kNewEdsServiceName); |
| 728 |
authority_balancer_->ads_service()->SetCdsResource(new_cluster); |
| 729 |
// New Route |
| 730 |
RouteConfiguration new_route_config = default_route_config_; |
| 731 |
new_route_config.set_name(kNewRouteConfigName); |
| 732 |
new_route_config.mutable_virtual_hosts(0) |
| 733 |
->mutable_routes(0) |
| 734 |
->mutable_route() |
| 735 |
->set_cluster(kNewClusterName); |
| 736 |
// New Listener |
| 737 |
Listener listener = default_listener_; |
| 738 |
listener.set_name(kNewListenerName); |
| 739 |
SetListenerAndRouteConfiguration(authority_balancer_.get(), listener, |
| 740 |
new_route_config); |
| 741 |
WaitForAllBackends(DEBUG_LOCATION); |
| 742 |
} |
| 743 |
|
| 744 |
// Channel is created with URI "xds://xds.example.com/server.example.com". |
| 745 |
// In bootstrap config, authority has no client listener template, so we use the |
| 746 |
// default. |
| 747 |
TEST_P(XdsFederationTest, FederationTargetAuthorityDefaultResourceTemplate) { |
| 748 |
ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_FEDERATION"); |
| 749 |
const char* kAuthority = "xds.example.com"; |
| 750 |
const char* kNewServerName = "whee%/server.example.com"; |
| 751 |
const char* kNewListenerName = |
| 752 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 753 |
"whee%25/server.example.com"; |
| 754 |
const char* kNewRouteConfigName = |
| 755 |
"xdstp://xds.example.com/envoy.config.route.v3.RouteConfiguration/" |
| 756 |
"new_route_config_name"; |
| 757 |
const char* kNewEdsServiceName = |
| 758 |
"xdstp://xds.example.com/envoy.config.endpoint.v3.ClusterLoadAssignment/" |
| 759 |
"edsservice_name"; |
| 760 |
const char* kNewClusterName = |
| 761 |
"xdstp://xds.example.com/envoy.config.cluster.v3.Cluster/" |
| 762 |
"cluster_name"; |
| 763 |
BootstrapBuilder builder = BootstrapBuilder(); |
| 764 |
builder.AddAuthority(kAuthority, |
| 765 |
absl::StrCat("localhost:", authority_balancer_->port())); |
| 766 |
InitClient(builder); |
| 767 |
CreateAndStartBackends(2); |
| 768 |
// Eds for 2 balancers to ensure RPCs sent using current stub go to backend 0 |
| 769 |
// and RPCs sent using the new stub go to backend 1. |
| 770 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 771 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 772 |
args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(1, 2)}}); |
| 773 |
authority_balancer_->ads_service()->SetEdsResource( |
| 774 |
BuildEdsResource(args, kNewEdsServiceName)); |
| 775 |
// New cluster |
| 776 |
Cluster new_cluster = default_cluster_; |
| 777 |
new_cluster.set_name(kNewClusterName); |
| 778 |
new_cluster.mutable_eds_cluster_config()->set_service_name( |
| 779 |
kNewEdsServiceName); |
| 780 |
authority_balancer_->ads_service()->SetCdsResource(new_cluster); |
| 781 |
// New Route |
| 782 |
RouteConfiguration new_route_config = default_route_config_; |
| 783 |
new_route_config.set_name(kNewRouteConfigName); |
| 784 |
new_route_config.mutable_virtual_hosts(0) |
| 785 |
->mutable_routes(0) |
| 786 |
->mutable_route() |
| 787 |
->set_cluster(kNewClusterName); |
| 788 |
// New Listener |
| 789 |
Listener listener = default_listener_; |
| 790 |
listener.set_name(kNewListenerName); |
| 791 |
SetListenerAndRouteConfiguration(authority_balancer_.get(), listener, |
| 792 |
new_route_config); |
| 793 |
// Ensure update has reached and send 10 RPCs to the current stub. |
| 794 |
WaitForAllBackends(DEBUG_LOCATION, 0, 1); |
| 795 |
// Create second channel to new target uri and send 1 RPC. |
| 796 |
auto channel2 = |
| 797 |
CreateChannel(/*failover_timeout_ms=*/0, kNewServerName, kAuthority); |
| 798 |
auto stub2 = grpc::testing::EchoTestService::NewStub(channel2); |
| 799 |
ClientContext context; |
| 800 |
EchoRequest request; |
| 801 |
RpcOptions().SetupRpc(&context, &request); |
| 802 |
EchoResponse response; |
| 803 |
grpc::Status status = stub2->Echo(&context, request, &response); |
| 804 |
EXPECT_TRUE(status.ok()) << "code=" << status.error_code() |
| 805 |
<< " message=" << status.error_message(); |
| 806 |
// We should be reaching backend 1, not 0, as balanced by the authority xds |
| 807 |
// server. |
| 808 |
EXPECT_EQ(0U, backends_[0]->backend_service()->request_count()); |
| 809 |
EXPECT_EQ(1U, backends_[1]->backend_service()->request_count()); |
| 810 |
} |
| 811 |
|
| 812 |
// Channel is created with URI "xds://xds.example.com/server.example.com". |
| 813 |
// Bootstrap entry for that authority specifies a client listener name template. |
| 814 |
TEST_P(XdsFederationTest, FederationTargetAuthorityWithResourceTemplate) { |
| 815 |
ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_FEDERATION"); |
| 816 |
const char* kAuthority = "xds.example.com"; |
| 817 |
const char* kNewServerName = "whee%/server.example.com"; |
| 818 |
const char* kNewListenerTemplate = |
| 819 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 820 |
"client/%s?psm_project_id=1234"; |
| 821 |
const char* kNewListenerName = |
| 822 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 823 |
"client/whee%25/server.example.com?psm_project_id=1234"; |
| 824 |
const char* kNewRouteConfigName = |
| 825 |
"xdstp://xds.example.com/envoy.config.route.v3.RouteConfiguration/" |
| 826 |
"new_route_config_name"; |
| 827 |
const char* kNewEdsServiceName = |
| 828 |
"xdstp://xds.example.com/envoy.config.endpoint.v3.ClusterLoadAssignment/" |
| 829 |
"edsservice_name"; |
| 830 |
const char* kNewClusterName = |
| 831 |
"xdstp://xds.example.com/envoy.config.cluster.v3.Cluster/" |
| 832 |
"cluster_name"; |
| 833 |
BootstrapBuilder builder = BootstrapBuilder(); |
| 834 |
builder.AddAuthority(kAuthority, |
| 835 |
absl::StrCat("localhost:", authority_balancer_->port()), |
| 836 |
kNewListenerTemplate); |
| 837 |
InitClient(builder); |
| 838 |
CreateAndStartBackends(2); |
| 839 |
// Eds for 2 balancers to ensure RPCs sent using current stub go to backend 0 |
| 840 |
// and RPCs sent using the new stub go to backend 1. |
| 841 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 842 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 843 |
args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(1, 2)}}); |
| 844 |
authority_balancer_->ads_service()->SetEdsResource( |
| 845 |
BuildEdsResource(args, kNewEdsServiceName)); |
| 846 |
// New cluster |
| 847 |
Cluster new_cluster = default_cluster_; |
| 848 |
new_cluster.set_name(kNewClusterName); |
| 849 |
new_cluster.mutable_eds_cluster_config()->set_service_name( |
| 850 |
kNewEdsServiceName); |
| 851 |
authority_balancer_->ads_service()->SetCdsResource(new_cluster); |
| 852 |
// New Route |
| 853 |
RouteConfiguration new_route_config = default_route_config_; |
| 854 |
new_route_config.set_name(kNewRouteConfigName); |
| 855 |
new_route_config.mutable_virtual_hosts(0) |
| 856 |
->mutable_routes(0) |
| 857 |
->mutable_route() |
| 858 |
->set_cluster(kNewClusterName); |
| 859 |
// New Listener |
| 860 |
Listener listener = default_listener_; |
| 861 |
listener.set_name(kNewListenerName); |
| 862 |
SetListenerAndRouteConfiguration(authority_balancer_.get(), listener, |
| 863 |
new_route_config); |
| 864 |
// Ensure update has reached and send 10 RPCs to the current stub. |
| 865 |
WaitForAllBackends(DEBUG_LOCATION, 0, 1); |
| 866 |
// Create second channel to new target uri and send 1 RPC. |
| 867 |
auto channel2 = |
| 868 |
CreateChannel(/*failover_timeout_ms=*/0, kNewServerName, kAuthority); |
| 869 |
auto stub2 = grpc::testing::EchoTestService::NewStub(channel2); |
| 870 |
ClientContext context; |
| 871 |
EchoRequest request; |
| 872 |
RpcOptions().SetupRpc(&context, &request); |
| 873 |
EchoResponse response; |
| 874 |
grpc::Status status = stub2->Echo(&context, request, &response); |
| 875 |
EXPECT_TRUE(status.ok()) << "code=" << status.error_code() |
| 876 |
<< " message=" << status.error_message(); |
| 877 |
// We should be reaching backend 1, not 0, as balanced by the authority xds |
| 878 |
// server. |
| 879 |
EXPECT_EQ(0U, backends_[0]->backend_service()->request_count()); |
| 880 |
EXPECT_EQ(1U, backends_[1]->backend_service()->request_count()); |
| 881 |
} |
| 882 |
|
| 883 |
TEST_P(XdsFederationTest, TargetUriAuthorityUnknown) { |
| 884 |
ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_FEDERATION"); |
| 885 |
const char* kAuthority = "xds.example.com"; |
| 886 |
const char* kNewServerName = "whee%/server.example.com"; |
| 887 |
const char* kNewListenerTemplate = |
| 888 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 889 |
"client/%s?psm_project_id=1234"; |
| 890 |
BootstrapBuilder builder = BootstrapBuilder(); |
| 891 |
builder.AddAuthority( |
| 892 |
kAuthority, absl::StrCat("localhost:", grpc_pick_unused_port_or_die()), |
| 893 |
kNewListenerTemplate); |
| 894 |
InitClient(builder); |
| 895 |
auto channel2 = CreateChannel( |
| 896 |
/*failover_timeout_ms=*/0, kNewServerName, "xds.unknown.com"); |
| 897 |
auto stub2 = grpc::testing::EchoTestService::NewStub(channel2); |
| 898 |
ClientContext context; |
| 899 |
EchoRequest request; |
| 900 |
RpcOptions().SetupRpc(&context, &request); |
| 901 |
EchoResponse response; |
| 902 |
grpc::Status status = stub2->Echo(&context, request, &response); |
| 903 |
EXPECT_EQ(status.error_code(), StatusCode::UNAVAILABLE); |
| 904 |
EXPECT_EQ(status.error_message(), |
| 905 |
"Invalid target URI -- authority not found for xds.unknown.com"); |
| 906 |
ASSERT_EQ(GRPC_CHANNEL_TRANSIENT_FAILURE, channel2->GetState(false)); |
| 907 |
} |
| 908 |
|
| 909 |
TEST_P(XdsFederationTest, RdsResourceNameAuthorityUnknown) { |
| 910 |
ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_FEDERATION"); |
| 911 |
const char* kAuthority = "xds.example.com"; |
| 912 |
const char* kNewServerName = "whee%/server.example.com"; |
| 913 |
const char* kNewListenerTemplate = |
| 914 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 915 |
"client/%s?psm_project_id=1234"; |
| 916 |
const char* kNewListenerName = |
| 917 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 918 |
"client/whee%25/server.example.com?psm_project_id=1234"; |
| 919 |
const char* kNewRouteConfigName = |
| 920 |
"xdstp://xds.unknown.com/envoy.config.route.v3.RouteConfiguration/" |
| 921 |
"new_route_config_name"; |
| 922 |
BootstrapBuilder builder = BootstrapBuilder(); |
| 923 |
builder.AddAuthority(kAuthority, |
| 924 |
absl::StrCat("localhost:", authority_balancer_->port()), |
| 925 |
kNewListenerTemplate); |
| 926 |
InitClient(builder); |
| 927 |
// New RouteConfig |
| 928 |
RouteConfiguration new_route_config = default_route_config_; |
| 929 |
new_route_config.set_name(kNewRouteConfigName); |
| 930 |
// New Listener |
| 931 |
Listener listener = default_listener_; |
| 932 |
listener.set_name(kNewListenerName); |
| 933 |
SetListenerAndRouteConfiguration(authority_balancer_.get(), listener, |
| 934 |
new_route_config); |
| 935 |
// Channel should report TRANSIENT_FAILURE. |
| 936 |
auto channel2 = |
| 937 |
CreateChannel(/*failover_timeout_ms=*/0, kNewServerName, kAuthority); |
| 938 |
auto stub2 = grpc::testing::EchoTestService::NewStub(channel2); |
| 939 |
ClientContext context; |
| 940 |
EchoRequest request; |
| 941 |
RpcOptions().SetupRpc(&context, &request); |
| 942 |
EchoResponse response; |
| 943 |
grpc::Status status = stub2->Echo(&context, request, &response); |
| 944 |
EXPECT_EQ(status.error_code(), StatusCode::UNAVAILABLE); |
| 945 |
EXPECT_EQ(status.error_message(), |
| 946 |
absl::StrCat( |
| 947 |
kNewRouteConfigName, |
| 948 |
": UNAVAILABLE: authority \"xds.unknown.com\" not present in " |
| 949 |
"bootstrap config")); |
| 950 |
ASSERT_EQ(GRPC_CHANNEL_TRANSIENT_FAILURE, channel2->GetState(false)); |
| 951 |
} |
| 952 |
|
| 953 |
TEST_P(XdsFederationTest, CdsResourceNameAuthorityUnknown) { |
| 954 |
ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_FEDERATION"); |
| 955 |
const char* kAuthority = "xds.example.com"; |
| 956 |
const char* kNewServerName = "whee%/server.example.com"; |
| 957 |
const char* kNewListenerTemplate = |
| 958 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 959 |
"client/%s?psm_project_id=1234"; |
| 960 |
const char* kNewListenerName = |
| 961 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 962 |
"client/whee%25/server.example.com?psm_project_id=1234"; |
| 963 |
const char* kNewRouteConfigName = |
| 964 |
"xdstp://xds.example.com/envoy.config.route.v3.RouteConfiguration/" |
| 965 |
"new_route_config_name"; |
| 966 |
const char* kNewClusterName = |
| 967 |
"xdstp://xds.unknown.com/envoy.config.cluster.v3.Cluster/" |
| 968 |
"cluster_name"; |
| 969 |
BootstrapBuilder builder = BootstrapBuilder(); |
| 970 |
builder.AddAuthority(kAuthority, |
| 971 |
absl::StrCat("localhost:", authority_balancer_->port()), |
| 972 |
kNewListenerTemplate); |
| 973 |
InitClient(builder); |
| 974 |
// New Route |
| 975 |
RouteConfiguration new_route_config = default_route_config_; |
| 976 |
new_route_config.set_name(kNewRouteConfigName); |
| 977 |
new_route_config.mutable_virtual_hosts(0) |
| 978 |
->mutable_routes(0) |
| 979 |
->mutable_route() |
| 980 |
->set_cluster(kNewClusterName); |
| 981 |
// New Listener |
| 982 |
Listener listener = default_listener_; |
| 983 |
listener.set_name(kNewListenerName); |
| 984 |
SetListenerAndRouteConfiguration(authority_balancer_.get(), listener, |
| 985 |
new_route_config); |
| 986 |
// Channel should report TRANSIENT_FAILURE. |
| 987 |
auto channel2 = |
| 988 |
CreateChannel(/*failover_timeout_ms=*/0, kNewServerName, kAuthority); |
| 989 |
auto stub2 = grpc::testing::EchoTestService::NewStub(channel2); |
| 990 |
ClientContext context; |
| 991 |
EchoRequest request; |
| 992 |
RpcOptions().SetupRpc(&context, &request); |
| 993 |
EchoResponse response; |
| 994 |
grpc::Status status = stub2->Echo(&context, request, &response); |
| 995 |
EXPECT_EQ(status.error_code(), StatusCode::UNAVAILABLE); |
| 996 |
EXPECT_EQ(status.error_message(), |
| 997 |
absl::StrCat( |
| 998 |
kNewClusterName, |
| 999 |
": UNAVAILABLE: authority \"xds.unknown.com\" not present in " |
| 1000 |
"bootstrap config")); |
| 1001 |
ASSERT_EQ(GRPC_CHANNEL_TRANSIENT_FAILURE, channel2->GetState(false)); |
| 1002 |
} |
| 1003 |
|
| 1004 |
TEST_P(XdsFederationTest, EdsResourceNameAuthorityUnknown) { |
| 1005 |
ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_FEDERATION"); |
| 1006 |
const char* kAuthority = "xds.example.com"; |
| 1007 |
const char* kNewServerName = "whee%/server.example.com"; |
| 1008 |
const char* kNewListenerTemplate = |
| 1009 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 1010 |
"client/%s?psm_project_id=1234"; |
| 1011 |
const char* kNewListenerName = |
| 1012 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 1013 |
"client/whee%25/server.example.com?psm_project_id=1234"; |
| 1014 |
const char* kNewRouteConfigName = |
| 1015 |
"xdstp://xds.example.com/envoy.config.route.v3.RouteConfiguration/" |
| 1016 |
"new_route_config_name"; |
| 1017 |
const char* kNewEdsServiceName = |
| 1018 |
"xdstp://xds.unknown.com/envoy.config.endpoint.v3.ClusterLoadAssignment/" |
| 1019 |
"edsservice_name"; |
| 1020 |
const char* kNewClusterName = |
| 1021 |
"xdstp://xds.example.com/envoy.config.cluster.v3.Cluster/" |
| 1022 |
"cluster_name"; |
| 1023 |
BootstrapBuilder builder = BootstrapBuilder(); |
| 1024 |
builder.AddAuthority(kAuthority, |
| 1025 |
absl::StrCat("localhost:", authority_balancer_->port()), |
| 1026 |
kNewListenerTemplate); |
| 1027 |
InitClient(builder); |
| 1028 |
// New cluster |
| 1029 |
Cluster new_cluster = default_cluster_; |
| 1030 |
new_cluster.set_name(kNewClusterName); |
| 1031 |
new_cluster.mutable_eds_cluster_config()->set_service_name( |
| 1032 |
kNewEdsServiceName); |
| 1033 |
authority_balancer_->ads_service()->SetCdsResource(new_cluster); |
| 1034 |
// New Route |
| 1035 |
RouteConfiguration new_route_config = default_route_config_; |
| 1036 |
new_route_config.set_name(kNewRouteConfigName); |
| 1037 |
new_route_config.mutable_virtual_hosts(0) |
| 1038 |
->mutable_routes(0) |
| 1039 |
->mutable_route() |
| 1040 |
->set_cluster(kNewClusterName); |
| 1041 |
// New Listener |
| 1042 |
Listener listener = default_listener_; |
| 1043 |
listener.set_name(kNewListenerName); |
| 1044 |
SetListenerAndRouteConfiguration(authority_balancer_.get(), listener, |
| 1045 |
new_route_config); |
| 1046 |
// Channel should report TRANSIENT_FAILURE. |
| 1047 |
auto channel2 = |
| 1048 |
CreateChannel(/*failover_timeout_ms=*/0, kNewServerName, kAuthority); |
| 1049 |
auto stub2 = grpc::testing::EchoTestService::NewStub(channel2); |
| 1050 |
ClientContext context; |
| 1051 |
EchoRequest request; |
| 1052 |
RpcOptions().SetupRpc(&context, &request); |
| 1053 |
EchoResponse response; |
| 1054 |
grpc::Status status = stub2->Echo(&context, request, &response); |
| 1055 |
EXPECT_EQ(status.error_code(), StatusCode::UNAVAILABLE); |
| 1056 |
EXPECT_EQ( |
| 1057 |
status.error_message(), |
| 1058 |
"no children in weighted_target policy: EDS watcher error for resource " |
| 1059 |
"xdstp://xds.unknown.com/envoy.config.endpoint.v3.ClusterLoadAssignment/" |
| 1060 |
"edsservice_name (UNAVAILABLE: authority \"xds.unknown.com\" not " |
| 1061 |
"present in bootstrap config)"); |
| 1062 |
ASSERT_EQ(GRPC_CHANNEL_TRANSIENT_FAILURE, channel2->GetState(false)); |
| 1063 |
} |
| 1064 |
|
| 1065 |
// Setting server_listener_resource_name_template to start with "xdstp:" and |
| 1066 |
// look up xds server under an authority map. |
| 1067 |
TEST_P(XdsFederationTest, FederationServer) { |
| 1068 |
ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_FEDERATION"); |
| 1069 |
const char* kAuthority = "xds.example.com"; |
| 1070 |
const char* kNewListenerTemplate = |
| 1071 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 1072 |
"client/%s?psm_project_id=1234"; |
| 1073 |
const char* kNewServerListenerTemplate = |
| 1074 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 1075 |
"server/%s?psm_project_id=1234"; |
| 1076 |
const char* kNewListenerName = |
| 1077 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 1078 |
"client/server.example.com?psm_project_id=1234"; |
| 1079 |
const char* kNewRouteConfigName = |
| 1080 |
"xdstp://xds.example.com/envoy.config.route.v3.RouteConfiguration/" |
| 1081 |
"new_route_config_name"; |
| 1082 |
const char* kNewServerRouteConfigName = |
| 1083 |
"xdstp://xds.example.com/envoy.config.route.v3.RouteConfiguration/" |
| 1084 |
"new_server_route_config_name"; |
| 1085 |
const char* kNewEdsServiceName = |
| 1086 |
"xdstp://xds.example.com/envoy.config.endpoint.v3.ClusterLoadAssignment/" |
| 1087 |
"new_edsservice_name"; |
| 1088 |
const char* kNewClusterName = |
| 1089 |
"xdstp://xds.example.com/envoy.config.cluster.v3.Cluster/" |
| 1090 |
"new_cluster_name"; |
| 1091 |
BootstrapBuilder builder = BootstrapBuilder(); |
| 1092 |
builder.SetClientDefaultListenerResourceNameTemplate(kNewListenerTemplate); |
| 1093 |
builder.SetServerListenerResourceNameTemplate(kNewServerListenerTemplate); |
| 1094 |
builder.AddAuthority( |
| 1095 |
kAuthority, absl::StrCat("localhost:", authority_balancer_->port()), |
| 1096 |
// Note we will not use the client_listener_resource_name_template field |
| 1097 |
// in the authority. |
| 1098 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener" |
| 1099 |
"client/%s?client_listener_resource_name_template_not_in_use"); |
| 1100 |
InitClient(builder); |
| 1101 |
CreateAndStartBackends(2, /*xds_enabled=*/true); |
| 1102 |
// Eds for new authority balancer. |
| 1103 |
EdsResourceArgs args = |
| 1104 |
EdsResourceArgs({{"locality0", CreateEndpointsForBackends()}}); |
| 1105 |
authority_balancer_->ads_service()->SetEdsResource( |
| 1106 |
BuildEdsResource(args, kNewEdsServiceName)); |
| 1107 |
// New cluster |
| 1108 |
Cluster new_cluster = default_cluster_; |
| 1109 |
new_cluster.set_name(kNewClusterName); |
| 1110 |
new_cluster.mutable_eds_cluster_config()->set_service_name( |
| 1111 |
kNewEdsServiceName); |
| 1112 |
authority_balancer_->ads_service()->SetCdsResource(new_cluster); |
| 1113 |
// New RouteConfig |
| 1114 |
RouteConfiguration new_route_config = default_route_config_; |
| 1115 |
new_route_config.set_name(kNewRouteConfigName); |
| 1116 |
new_route_config.mutable_virtual_hosts(0) |
| 1117 |
->mutable_routes(0) |
| 1118 |
->mutable_route() |
| 1119 |
->set_cluster(kNewClusterName); |
| 1120 |
// New Listener |
| 1121 |
Listener listener = default_listener_; |
| 1122 |
listener.set_name(kNewListenerName); |
| 1123 |
SetListenerAndRouteConfiguration(authority_balancer_.get(), listener, |
| 1124 |
new_route_config); |
| 1125 |
// New Server RouteConfig |
| 1126 |
RouteConfiguration new_server_route_config = default_server_route_config_; |
| 1127 |
new_server_route_config.set_name(kNewServerRouteConfigName); |
| 1128 |
// New Server Listeners |
| 1129 |
for (int port : GetBackendPorts()) { |
| 1130 |
Listener server_listener = default_server_listener_; |
| 1131 |
server_listener.set_name(absl::StrCat( |
| 1132 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/server/", |
| 1133 |
ipv6_only_ ? "%5B::1%5D:" : "127.0.0.1:", port, |
| 1134 |
"?psm_project_id=1234")); |
| 1135 |
server_listener.mutable_address()->mutable_socket_address()->set_port_value( |
| 1136 |
port); |
| 1137 |
SetListenerAndRouteConfiguration(authority_balancer_.get(), server_listener, |
| 1138 |
new_server_route_config, |
| 1139 |
ServerHcmAccessor()); |
| 1140 |
} |
| 1141 |
WaitForAllBackends(DEBUG_LOCATION); |
| 1142 |
} |
| 1143 |
|
| 1144 |
// |
| 1145 |
// XdsFederationDisabledTest |
| 1146 |
// |
| 1147 |
|
| 1148 |
using XdsFederationDisabledTest = XdsEnd2endTest; |
| 1149 |
|
| 1150 |
// Runs with RDS so that we know all resource types work properly. |
| 1151 |
INSTANTIATE_TEST_SUITE_P( |
| 1152 |
XdsTest, XdsFederationDisabledTest, |
| 1153 |
::testing::Values(XdsTestType().set_enable_rds_testing()), |
| 1154 |
&XdsTestType::Name); |
| 1155 |
|
| 1156 |
TEST_P(XdsFederationDisabledTest, FederationDisabledWithNewStyleNames) { |
| 1157 |
const char* kNewRouteConfigName = |
| 1158 |
"xdstp://xds.example.com/envoy.config.route.v3.RouteConfiguration/" |
| 1159 |
"new_route_config_name"; |
| 1160 |
const char* kNewClusterName = |
| 1161 |
"xdstp://xds.example.com/envoy.config.cluster.v3.Cluster/" |
| 1162 |
"cluster_name"; |
| 1163 |
const char* kNewEdsResourceName = |
| 1164 |
"xdstp://xds.example.com/envoy.config.endpoint.v3.ClusterLoadAssignment/" |
| 1165 |
"edsservice_name"; |
| 1166 |
InitClient(); |
| 1167 |
CreateAndStartBackends(1); |
| 1168 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 1169 |
balancer_->ads_service()->SetEdsResource( |
| 1170 |
BuildEdsResource(args, kNewEdsResourceName)); |
| 1171 |
// New cluster |
| 1172 |
Cluster new_cluster = default_cluster_; |
| 1173 |
new_cluster.set_name(kNewClusterName); |
| 1174 |
new_cluster.mutable_eds_cluster_config()->set_service_name( |
| 1175 |
kNewEdsResourceName); |
| 1176 |
balancer_->ads_service()->SetCdsResource(new_cluster); |
| 1177 |
// New RouteConfig |
| 1178 |
RouteConfiguration new_route_config = default_route_config_; |
| 1179 |
new_route_config.set_name(kNewRouteConfigName); |
| 1180 |
new_route_config.mutable_virtual_hosts(0) |
| 1181 |
->mutable_routes(0) |
| 1182 |
->mutable_route() |
| 1183 |
->set_cluster(kNewClusterName); |
| 1184 |
SetListenerAndRouteConfiguration(balancer_.get(), default_listener_, |
| 1185 |
new_route_config); |
| 1186 |
// Channel should work. |
| 1187 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 1188 |
} |
| 1189 |
|
| 1190 |
// |
| 1191 |
// XdsFederationLoadReportingTest - xDS federation and load reporting |
| 1192 |
// |
| 1193 |
|
| 1194 |
using XdsFederationLoadReportingTest = XdsFederationTest; |
| 1195 |
|
| 1196 |
// Get bootstrap from env var, so that there's a global XdsClient. |
| 1197 |
// Runs with and without RDS. |
| 1198 |
INSTANTIATE_TEST_SUITE_P( |
| 1199 |
XdsTest, XdsFederationLoadReportingTest, |
| 1200 |
::testing::Values( |
| 1201 |
XdsTestType() |
| 1202 |
.set_bootstrap_source(XdsTestType::kBootstrapFromEnvVar) |
| 1203 |
.set_enable_load_reporting(), |
| 1204 |
XdsTestType() |
| 1205 |
.set_bootstrap_source(XdsTestType::kBootstrapFromEnvVar) |
| 1206 |
.set_enable_load_reporting() |
| 1207 |
.set_enable_rds_testing()), |
| 1208 |
&XdsTestType::Name); |
| 1209 |
|
| 1210 |
// Channel is created with URI "xds://xds.example.com/server.example.com". |
| 1211 |
// Bootstrap entry for that authority specifies a client listener name template. |
| 1212 |
// Sending traffic to both default balancer and authority balancer and checking |
| 1213 |
// load reporting with each one. |
| 1214 |
TEST_P(XdsFederationLoadReportingTest, FederationMultipleLoadReportingTest) { |
| 1215 |
ScopedExperimentalEnvVar env_var("GRPC_EXPERIMENTAL_XDS_FEDERATION"); |
| 1216 |
const char* kAuthority = "xds.example.com"; |
| 1217 |
const char* kNewServerName = "whee%/server.example.com"; |
| 1218 |
const char* kNewListenerTemplate = |
| 1219 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 1220 |
"client/%s?psm_project_id=1234"; |
| 1221 |
const char* kNewListenerName = |
| 1222 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/" |
| 1223 |
"client/whee%25/server.example.com?psm_project_id=1234"; |
| 1224 |
const char* kNewRouteConfigName = |
| 1225 |
"xdstp://xds.example.com/envoy.config.route.v3.RouteConfiguration/" |
| 1226 |
"new_route_config_name"; |
| 1227 |
const char* kNewEdsServiceName = |
| 1228 |
"xdstp://xds.example.com/envoy.config.endpoint.v3.ClusterLoadAssignment/" |
| 1229 |
"edsservice_name"; |
| 1230 |
const char* kNewClusterName = |
| 1231 |
"xdstp://xds.example.com/envoy.config.cluster.v3.Cluster/" |
| 1232 |
"cluster_name"; |
| 1233 |
const size_t kNumRpcsToDefaultBalancer = 5; |
| 1234 |
const size_t kNumRpcsToAuthorityBalancer = 10; |
| 1235 |
BootstrapBuilder builder = BootstrapBuilder(); |
| 1236 |
builder.AddAuthority(kAuthority, |
| 1237 |
absl::StrCat("localhost:", authority_balancer_->port()), |
| 1238 |
kNewListenerTemplate); |
| 1239 |
InitClient(builder); |
| 1240 |
CreateAndStartBackends(2, /*xds_enabled=*/true); |
| 1241 |
// Eds for 2 balancers to ensure RPCs sent using current stub go to backend 0 |
| 1242 |
// and RPCs sent using the new stub go to backend 1. |
| 1243 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 1244 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1245 |
args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(1, 2)}}); |
| 1246 |
authority_balancer_->ads_service()->SetEdsResource( |
| 1247 |
BuildEdsResource(args, kNewEdsServiceName)); |
| 1248 |
authority_balancer_->lrs_service()->set_cluster_names({kNewClusterName}); |
| 1249 |
// New cluster |
| 1250 |
Cluster new_cluster = default_cluster_; |
| 1251 |
new_cluster.set_name(kNewClusterName); |
| 1252 |
new_cluster.mutable_lrs_server()->mutable_self(); |
| 1253 |
new_cluster.mutable_eds_cluster_config()->set_service_name( |
| 1254 |
kNewEdsServiceName); |
| 1255 |
authority_balancer_->ads_service()->SetCdsResource(new_cluster); |
| 1256 |
// New Route |
| 1257 |
RouteConfiguration new_route_config = default_route_config_; |
| 1258 |
new_route_config.set_name(kNewRouteConfigName); |
| 1259 |
new_route_config.mutable_virtual_hosts(0) |
| 1260 |
->mutable_routes(0) |
| 1261 |
->mutable_route() |
| 1262 |
->set_cluster(kNewClusterName); |
| 1263 |
// New Listener |
| 1264 |
Listener listener = default_listener_; |
| 1265 |
listener.set_name(kNewListenerName); |
| 1266 |
SetListenerAndRouteConfiguration(authority_balancer_.get(), listener, |
| 1267 |
new_route_config); |
| 1268 |
// Send kNumRpcsToDefaultBalancer RPCs to the current stub. |
| 1269 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcsToDefaultBalancer); |
| 1270 |
// Create second channel to new target uri. |
| 1271 |
auto channel2 = |
| 1272 |
CreateChannel(/*failover_timeout_ms=*/0, kNewServerName, kAuthority); |
| 1273 |
auto stub2 = grpc::testing::EchoTestService::NewStub(channel2); |
| 1274 |
// Send kNumRpcsToAuthorityBalancer on the second channel. |
| 1275 |
for (size_t i = 0; i < kNumRpcsToAuthorityBalancer; ++i) { |
| 1276 |
ClientContext context; |
| 1277 |
EchoRequest request; |
| 1278 |
RpcOptions().SetupRpc(&context, &request); |
| 1279 |
EchoResponse response; |
| 1280 |
grpc::Status status = stub2->Echo(&context, request, &response); |
| 1281 |
EXPECT_TRUE(status.ok()) << "code=" << status.error_code() |
| 1282 |
<< " message=" << status.error_message(); |
| 1283 |
} |
| 1284 |
// Each backend should have received the expected number of RPCs, |
| 1285 |
// and the load report also reflect the correct numbers. |
| 1286 |
EXPECT_EQ(kNumRpcsToAuthorityBalancer, |
| 1287 |
backends_[1]->backend_service()->request_count()); |
| 1288 |
EXPECT_EQ(kNumRpcsToDefaultBalancer, |
| 1289 |
backends_[0]->backend_service()->request_count()); |
| 1290 |
// Load report for authority LRS. |
| 1291 |
std::vector<ClientStats> authority_load_report = |
| 1292 |
authority_balancer_->lrs_service()->WaitForLoadReport(); |
| 1293 |
ASSERT_EQ(authority_load_report.size(), 1UL); |
| 1294 |
ClientStats& authority_client_stats = authority_load_report.front(); |
| 1295 |
EXPECT_EQ(kNumRpcsToAuthorityBalancer, |
| 1296 |
authority_client_stats.total_successful_requests()); |
| 1297 |
EXPECT_EQ(0U, authority_client_stats.total_requests_in_progress()); |
| 1298 |
EXPECT_EQ(kNumRpcsToAuthorityBalancer, |
| 1299 |
authority_client_stats.total_issued_requests()); |
| 1300 |
EXPECT_EQ(0U, authority_client_stats.total_error_requests()); |
| 1301 |
EXPECT_EQ(0U, authority_client_stats.total_dropped_requests()); |
| 1302 |
EXPECT_EQ(1U, authority_balancer_->lrs_service()->request_count()); |
| 1303 |
EXPECT_EQ(1U, authority_balancer_->lrs_service()->response_count()); |
| 1304 |
// Load report for default LRS. |
| 1305 |
std::vector<ClientStats> default_load_report = |
| 1306 |
balancer_->lrs_service()->WaitForLoadReport(); |
| 1307 |
ASSERT_EQ(default_load_report.size(), 1UL); |
| 1308 |
ClientStats& default_client_stats = default_load_report.front(); |
| 1309 |
EXPECT_EQ(kNumRpcsToDefaultBalancer, |
| 1310 |
default_client_stats.total_successful_requests()); |
| 1311 |
EXPECT_EQ(0U, default_client_stats.total_requests_in_progress()); |
| 1312 |
EXPECT_EQ(kNumRpcsToDefaultBalancer, |
| 1313 |
default_client_stats.total_issued_requests()); |
| 1314 |
EXPECT_EQ(0U, default_client_stats.total_error_requests()); |
| 1315 |
EXPECT_EQ(0U, default_client_stats.total_dropped_requests()); |
| 1316 |
EXPECT_EQ(1U, balancer_->lrs_service()->request_count()); |
| 1317 |
EXPECT_EQ(1U, balancer_->lrs_service()->response_count()); |
| 1318 |
} |
| 1319 |
|
| 1320 |
// |
| 1321 |
// SecureNamingTest - test that the right authority is used for the xDS server |
| 1322 |
// |
| 1323 |
|
| 1324 |
class SecureNamingTest : public XdsEnd2endTest { |
| 1325 |
public: |
| 1326 |
void SetUp() override { |
| 1327 |
// Each test calls InitClient() on its own. |
| 1328 |
} |
| 1329 |
}; |
| 1330 |
|
| 1331 |
INSTANTIATE_TEST_SUITE_P(XdsTest, SecureNamingTest, |
| 1332 |
::testing::Values(XdsTestType()), &XdsTestType::Name); |
| 1333 |
|
| 1334 |
// Tests that secure naming check passes if target name is expected. |
| 1335 |
TEST_P(SecureNamingTest, TargetNameIsExpected) { |
| 1336 |
InitClient(BootstrapBuilder(), /*lb_expected_authority=*/"localhost:%d"); |
| 1337 |
CreateAndStartBackends(4); |
| 1338 |
EdsResourceArgs args({ |
| 1339 |
{"locality0", CreateEndpointsForBackends()}, |
| 1340 |
}); |
| 1341 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1342 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 1343 |
} |
| 1344 |
|
| 1345 |
// Tests that secure naming check fails if target name is unexpected. |
| 1346 |
TEST_P(SecureNamingTest, TargetNameIsUnexpected) { |
| 1347 |
GTEST_FLAG_SET(death_test_style, "threadsafe"); |
| 1348 |
InitClient(BootstrapBuilder(), |
| 1349 |
/*lb_expected_authority=*/"incorrect_server_name"); |
| 1350 |
CreateAndStartBackends(4); |
| 1351 |
EdsResourceArgs args({ |
| 1352 |
{"locality0", CreateEndpointsForBackends()}, |
| 1353 |
}); |
| 1354 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1355 |
// Make sure that we blow up (via abort() from the security connector) when |
| 1356 |
// the name from the balancer doesn't match expectations. |
| 1357 |
ASSERT_DEATH_IF_SUPPORTED({ CheckRpcSendOk(DEBUG_LOCATION); }, ""); |
| 1358 |
} |
| 1359 |
|
| 1360 |
} // namespace |
| 1361 |
} // namespace testing |
| 1362 |
} // namespace grpc |
| 1363 |
|
| 1364 |
int main(int argc, char** argv) { |
| 1365 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 1366 |
::testing::InitGoogleTest(&argc, argv); |
| 1367 |
// Make the backup poller poll very frequently in order to pick up |
| 1368 |
// updates from all the subchannels's FDs. |
| 1369 |
GPR_GLOBAL_CONFIG_SET(grpc_client_channel_backup_poll_interval_ms, 1); |
| 1370 |
#if TARGET_OS_IPHONE |
| 1371 |
// Workaround Apple CFStream bug |
| 1372 |
grpc_core::SetEnv("grpc_cfstream", "0"); |
| 1373 |
#endif |
| 1374 |
grpc_init(); |
| 1375 |
const auto result = RUN_ALL_TESTS(); |
| 1376 |
grpc_shutdown(); |
| 1377 |
return result; |
| 1378 |
} |