Debian Codegraph @

test/cpp/end2end/xds/xds_routing_end2end_test.cc

raw
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 #include <chrono>
16 #include <string>
17 #include <thread>
18 #include <vector>
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include "src/core/ext/filters/client_channel/backup_poller.h"
24 #include "src/proto/grpc/testing/xds/v3/fault.grpc.pb.h"
25 #include "src/proto/grpc/testing/xds/v3/router.grpc.pb.h"
26 #include "test/cpp/end2end/xds/xds_end2end_test_lib.h"
27
28 namespace grpc {
29 namespace testing {
30 namespace {
31
32 using std::chrono::system_clock;
33
34 using LdsTest = XdsEnd2endTest;
35
36 INSTANTIATE_TEST_SUITE_P(XdsTest, LdsTest, ::testing::Values(XdsTestType()),
37 &XdsTestType::Name);
38
39 // Testing just one example of an invalid resource here.
40 // Unit tests for XdsListenerResourceType have exhaustive tests for all
41 // of the invalid cases.
42 TEST_P(LdsTest, NacksInvalidListener) {
43 auto listener = default_listener_;
44 listener.clear_api_listener();
45 balancer_->ads_service()->SetLdsResource(listener);
46 const auto response_state = WaitForLdsNack(DEBUG_LOCATION);
47 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
48 EXPECT_THAT(
49 response_state->error_message,
50 ::testing::HasSubstr("Listener has neither address nor ApiListener"));
51 }
52
53 // Tests that we go into TRANSIENT_FAILURE if the Listener is not an API
54 // listener.
55 TEST_P(LdsTest, NotAnApiListener) {
56 Listener listener = default_server_listener_;
57 listener.set_name(kServerName);
58 auto hcm = ServerHcmAccessor().Unpack(listener);
59 auto* rds = hcm.mutable_rds();
60 rds->set_route_config_name(kDefaultRouteConfigurationName);
61 rds->mutable_config_source()->mutable_self();
62 ServerHcmAccessor().Pack(hcm, &listener);
63 balancer_->ads_service()->SetLdsResource(listener);
64 // RPCs should fail.
65 CheckRpcSendFailure(
66 DEBUG_LOCATION, StatusCode::UNAVAILABLE,
67 absl::StrCat(kServerName, ": UNAVAILABLE: not an API listener"));
68 // We should have ACKed the LDS resource.
69 const auto deadline =
70 absl::Now() + (absl::Seconds(30) * grpc_test_slowdown_factor());
71 while (true) {
72 ASSERT_LT(absl::Now(), deadline) << "timed out waiting for LDS ACK";
73 auto response_state = balancer_->ads_service()->lds_response_state();
74 if (response_state.has_value()) {
75 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
76 break;
77 }
78 absl::SleepFor(absl::Seconds(1) * grpc_test_slowdown_factor());
79 }
80 }
81
82 class LdsDeletionTest : public XdsEnd2endTest {
83 protected:
84 void SetUp() override {} // Individual tests call InitClient().
85 };
86
87 INSTANTIATE_TEST_SUITE_P(XdsTest, LdsDeletionTest,
88 ::testing::Values(XdsTestType()), &XdsTestType::Name);
89
90 // Tests that we go into TRANSIENT_FAILURE if the Listener is deleted.
91 TEST_P(LdsDeletionTest, ListenerDeleted) {
92 InitClient();
93 CreateAndStartBackends(1);
94 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
95 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
96 // We need to wait for all backends to come online.
97 WaitForAllBackends(DEBUG_LOCATION);
98 // Unset LDS resource.
99 balancer_->ads_service()->UnsetResource(kLdsTypeUrl, kServerName);
100 // Wait for RPCs to start failing.
101 SendRpcsUntil(DEBUG_LOCATION, [](const RpcResult& result) {
102 if (result.status.ok()) return true; // Keep going.
103 EXPECT_EQ(result.status.error_code(), StatusCode::UNAVAILABLE);
104 EXPECT_EQ(result.status.error_message(),
105 absl::StrCat("empty address list: ", kServerName,
106 ": xDS listener resource does not exist"));
107 return false;
108 });
109 // Make sure we ACK'ed the update.
110 auto response_state = balancer_->ads_service()->lds_response_state();
111 ASSERT_TRUE(response_state.has_value());
112 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
113 }
114
115 // Tests that we ignore Listener deletions if configured to do so.
116 TEST_P(LdsDeletionTest, ListenerDeletionIgnored) {
117 InitClient(BootstrapBuilder().SetIgnoreResourceDeletion());
118 CreateAndStartBackends(2);
119 // Bring up client pointing to backend 0 and wait for it to connect.
120 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}});
121 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
122 WaitForAllBackends(DEBUG_LOCATION, 0, 1);
123 // Make sure we ACKed the LDS update.
124 auto response_state = balancer_->ads_service()->lds_response_state();
125 ASSERT_TRUE(response_state.has_value());
126 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
127 // Unset LDS resource and wait for client to ACK the update.
128 balancer_->ads_service()->UnsetResource(kLdsTypeUrl, kServerName);
129 const auto deadline =
130 absl::Now() + (absl::Seconds(30) * grpc_test_slowdown_factor());
131 while (true) {
132 ASSERT_LT(absl::Now(), deadline) << "timed out waiting for LDS ACK";
133 response_state = balancer_->ads_service()->lds_response_state();
134 if (response_state.has_value()) break;
135 absl::SleepFor(absl::Seconds(1) * grpc_test_slowdown_factor());
136 }
137 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
138 // Make sure we can still send RPCs.
139 CheckRpcSendOk(DEBUG_LOCATION);
140 // Now recreate the LDS resource pointing to a different CDS and EDS
141 // resource, pointing to backend 1, and make sure the client uses it.
142 const char* kNewClusterName = "new_cluster_name";
143 const char* kNewEdsResourceName = "new_eds_resource_name";
144 auto cluster = default_cluster_;
145 cluster.set_name(kNewClusterName);
146 cluster.mutable_eds_cluster_config()->set_service_name(kNewEdsResourceName);
147 balancer_->ads_service()->SetCdsResource(cluster);
148 args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(1, 2)}});
149 balancer_->ads_service()->SetEdsResource(
150 BuildEdsResource(args, kNewEdsResourceName));
151 RouteConfiguration new_route_config = default_route_config_;
152 new_route_config.mutable_virtual_hosts(0)
153 ->mutable_routes(0)
154 ->mutable_route()
155 ->set_cluster(kNewClusterName);
156 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
157 new_route_config);
158 // Wait for client to start using backend 1.
159 WaitForAllBackends(DEBUG_LOCATION, 1, 2);
160 }
161
162 using LdsRdsInteractionTest = XdsEnd2endTest;
163
164 INSTANTIATE_TEST_SUITE_P(
165 XdsTest, LdsRdsInteractionTest,
166 ::testing::Values(XdsTestType().set_enable_rds_testing()),
167 &XdsTestType::Name);
168
169 TEST_P(LdsRdsInteractionTest, SwitchFromRdsToInlineRouteConfig) {
170 CreateAndStartBackends(2);
171 // Bring up client pointing to backend 0 and wait for it to connect.
172 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}});
173 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
174 WaitForBackend(DEBUG_LOCATION, 0);
175 // RDS should have been ACKed.
176 auto response_state = balancer_->ads_service()->rds_response_state();
177 ASSERT_TRUE(response_state.has_value());
178 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
179 // Now recreate the LDS resource with an inline route config pointing to a
180 // different CDS and EDS resource, pointing to backend 1, and make sure
181 // the client uses it.
182 const char* kNewClusterName = "new_cluster_name";
183 const char* kNewEdsResourceName = "new_eds_resource_name";
184 auto cluster = default_cluster_;
185 cluster.set_name(kNewClusterName);
186 cluster.mutable_eds_cluster_config()->set_service_name(kNewEdsResourceName);
187 balancer_->ads_service()->SetCdsResource(cluster);
188 args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(1, 2)}});
189 balancer_->ads_service()->SetEdsResource(
190 BuildEdsResource(args, kNewEdsResourceName));
191 RouteConfiguration new_route_config = default_route_config_;
192 new_route_config.mutable_virtual_hosts(0)
193 ->mutable_routes(0)
194 ->mutable_route()
195 ->set_cluster(kNewClusterName);
196 Listener listener = default_listener_;
197 HttpConnectionManager http_connection_manager =
198 ClientHcmAccessor().Unpack(listener);
199 *http_connection_manager.mutable_route_config() = new_route_config;
200 ClientHcmAccessor().Pack(http_connection_manager, &listener);
201 balancer_->ads_service()->SetLdsResource(listener);
202 // Wait for client to start using backend 1.
203 WaitForBackend(DEBUG_LOCATION, 1);
204 // Send an update to the original RDS resource, which the client
205 // should no longer be subscribed to. We need this RouteConfig to be
206 // different than the original one so that the update does not get
207 // squelched by XdsClient, so we add a second domain to the vhost that
208 // will not actually be used.
209 new_route_config = default_route_config_;
210 new_route_config.mutable_virtual_hosts(0)->add_domains("foo.example.com");
211 balancer_->ads_service()->SetRdsResource(new_route_config);
212 // Wait for RDS ACK to know that the client saw the change.
213 // TODO(roth): The client does not actually ACK here, it just sends an
214 // unsubscription request, but our fake xDS server is incorrectly treating
215 // that as an ACK. When we have time, fix the behavior of the fake
216 // xDS server, and then change this test to ensure that there is no RDS
217 // ACK within the 30-second timeout period.
218 const auto deadline =
219 absl::Now() + (absl::Seconds(30) * grpc_test_slowdown_factor());
220 while (true) {
221 ASSERT_LT(absl::Now(), deadline) << "timed out waiting for RDS ACK";
222 response_state = balancer_->ads_service()->rds_response_state();
223 if (response_state.has_value()) break;
224 absl::SleepFor(absl::Seconds(1) * grpc_test_slowdown_factor());
225 }
226 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
227 // Make sure RPCs are still going to backend 1. This shows that the
228 // client did not replace its route config with the one from the RDS
229 // resource that it should no longer be using.
230 ResetBackendCounters();
231 CheckRpcSendOk(DEBUG_LOCATION);
232 EXPECT_EQ(0, backends_[0]->backend_service()->request_count());
233 EXPECT_EQ(1, backends_[1]->backend_service()->request_count());
234 }
235
236 TEST_P(LdsRdsInteractionTest, SwitchFromInlineRouteConfigToRds) {
237 CreateAndStartBackends(2);
238 // Create an LDS resource with an inline RouteConfig pointing to a
239 // different CDS and EDS resource, sending traffic to backend 0.
240 const char* kNewClusterName = "new_cluster_name";
241 const char* kNewEdsResourceName = "new_eds_resource_name";
242 auto cluster = default_cluster_;
243 cluster.set_name(kNewClusterName);
244 cluster.mutable_eds_cluster_config()->set_service_name(kNewEdsResourceName);
245 balancer_->ads_service()->SetCdsResource(cluster);
246 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}});
247 balancer_->ads_service()->SetEdsResource(
248 BuildEdsResource(args, kNewEdsResourceName));
249 RouteConfiguration route_config = default_route_config_;
250 route_config.mutable_virtual_hosts(0)
251 ->mutable_routes(0)
252 ->mutable_route()
253 ->set_cluster(kNewClusterName);
254 Listener listener = default_listener_;
255 HttpConnectionManager http_connection_manager =
256 ClientHcmAccessor().Unpack(listener);
257 *http_connection_manager.mutable_route_config() = route_config;
258 ClientHcmAccessor().Pack(http_connection_manager, &listener);
259 balancer_->ads_service()->SetLdsResource(listener);
260 // Start the client and make sure traffic goes to backend 0.
261 WaitForBackend(DEBUG_LOCATION, 0);
262 // RDS should not have been ACKed, because the RouteConfig was inlined.
263 ASSERT_FALSE(balancer_->ads_service()->rds_response_state().has_value());
264 // Change the LDS resource to point to an RDS resource. The LDS resource
265 // configures the fault injection filter with a config that fails all RPCs.
266 // However, the RDS resource has a typed_per_filter_config override that
267 // disables the fault injection filter. The RDS resource points to a
268 // new cluster that sends traffic to backend 1.
269 args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(1, 2)}});
270 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
271 route_config = default_route_config_;
272 auto* config_map = route_config.mutable_virtual_hosts(0)
273 ->mutable_routes(0)
274 ->mutable_typed_per_filter_config();
275 (*config_map)["envoy.fault"].PackFrom(
276 envoy::extensions::filters::http::fault::v3::HTTPFault());
277 envoy::extensions::filters::http::fault::v3::HTTPFault http_fault;
278 auto* abort_percentage = http_fault.mutable_abort()->mutable_percentage();
279 abort_percentage->set_numerator(100);
280 abort_percentage->set_denominator(abort_percentage->HUNDRED);
281 http_fault.mutable_abort()->set_grpc_status(
282 static_cast<uint32_t>(StatusCode::ABORTED));
283 listener = default_listener_;
284 http_connection_manager = ClientHcmAccessor().Unpack(listener);
285 *http_connection_manager.add_http_filters() =
286 http_connection_manager.http_filters(0);
287 auto* filter = http_connection_manager.mutable_http_filters(0);
288 filter->set_name("envoy.fault");
289 filter->mutable_typed_config()->PackFrom(http_fault);
290 ClientHcmAccessor().Pack(http_connection_manager, &listener);
291 SetListenerAndRouteConfiguration(balancer_.get(), std::move(listener),
292 route_config);
293 // Wait for traffic to switch to backend 1. There should be no RPC
294 // failures here; if there are, that indicates that the client started
295 // using the new LDS resource before it saw the new RDS resource.
296 WaitForBackend(DEBUG_LOCATION, 1);
297 }
298
299 TEST_P(LdsRdsInteractionTest, HcmConfigUpdatedWithoutRdsChange) {
300 CreateAndStartBackends(1);
301 // Bring up client pointing to backend 0 and wait for it to connect.
302 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}});
303 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
304 WaitForBackend(DEBUG_LOCATION, 0);
305 // LDS should have been ACKed.
306 auto response_state = balancer_->ads_service()->lds_response_state();
307 ASSERT_TRUE(response_state.has_value());
308 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
309 // Now update the LDS resource to add the fault injection filter with
310 // a config that fails all RPCs.
311 envoy::extensions::filters::http::fault::v3::HTTPFault http_fault;
312 auto* abort_percentage = http_fault.mutable_abort()->mutable_percentage();
313 abort_percentage->set_numerator(100);
314 abort_percentage->set_denominator(abort_percentage->HUNDRED);
315 http_fault.mutable_abort()->set_grpc_status(
316 static_cast<uint32_t>(StatusCode::ABORTED));
317 Listener listener = default_listener_;
318 HttpConnectionManager http_connection_manager =
319 ClientHcmAccessor().Unpack(listener);
320 *http_connection_manager.add_http_filters() =
321 http_connection_manager.http_filters(0);
322 auto* filter = http_connection_manager.mutable_http_filters(0);
323 filter->set_name("envoy.fault");
324 filter->mutable_typed_config()->PackFrom(http_fault);
325 ClientHcmAccessor().Pack(http_connection_manager, &listener);
326 SetListenerAndRouteConfiguration(balancer_.get(), std::move(listener),
327 default_route_config_);
328 // Wait for the LDS update to be ACKed.
329 const auto deadline =
330 absl::Now() + (absl::Seconds(30) * grpc_test_slowdown_factor());
331 while (true) {
332 ASSERT_LT(absl::Now(), deadline) << "timed out waiting for LDS ACK";
333 response_state = balancer_->ads_service()->lds_response_state();
334 if (response_state.has_value()) break;
335 absl::SleepFor(absl::Seconds(1) * grpc_test_slowdown_factor());
336 }
337 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
338 // Now RPCs should fail with ABORTED status.
339 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::ABORTED, "Fault injected");
340 }
341
342 TEST_P(LdsRdsInteractionTest, LdsUpdateChangesHcmConfigAndRdsResourceName) {
343 CreateAndStartBackends(2);
344 // Bring up client pointing to backend 0 and wait for it to connect.
345 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}});
346 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
347 WaitForBackend(DEBUG_LOCATION, 0);
348 // Change the LDS resource to point to an RDS resource. The LDS resource
349 // configures the fault injection filter with a config that fails all RPCs.
350 // However, the RDS resource has a typed_per_filter_config override that
351 // disables the fault injection filter. The RDS resource points to a
352 // new cluster that sends traffic to backend 1.
353 const char* kNewClusterName = "new_cluster_name";
354 const char* kNewEdsResourceName = "new_eds_resource_name";
355 auto cluster = default_cluster_;
356 cluster.set_name(kNewClusterName);
357 cluster.mutable_eds_cluster_config()->set_service_name(kNewEdsResourceName);
358 balancer_->ads_service()->SetCdsResource(cluster);
359 args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(1, 2)}});
360 balancer_->ads_service()->SetEdsResource(
361 BuildEdsResource(args, kNewEdsResourceName));
362 RouteConfiguration route_config = default_route_config_;
363 route_config.set_name("new_route_config");
364 route_config.mutable_virtual_hosts(0)
365 ->mutable_routes(0)
366 ->mutable_route()
367 ->set_cluster(kNewClusterName);
368 auto* config_map = route_config.mutable_virtual_hosts(0)
369 ->mutable_routes(0)
370 ->mutable_typed_per_filter_config();
371 (*config_map)["envoy.fault"].PackFrom(
372 envoy::extensions::filters::http::fault::v3::HTTPFault());
373 envoy::extensions::filters::http::fault::v3::HTTPFault http_fault;
374 auto* abort_percentage = http_fault.mutable_abort()->mutable_percentage();
375 abort_percentage->set_numerator(100);
376 abort_percentage->set_denominator(abort_percentage->HUNDRED);
377 http_fault.mutable_abort()->set_grpc_status(
378 static_cast<uint32_t>(StatusCode::ABORTED));
379 Listener listener = default_listener_;
380 HttpConnectionManager http_connection_manager =
381 ClientHcmAccessor().Unpack(listener);
382 *http_connection_manager.add_http_filters() =
383 http_connection_manager.http_filters(0);
384 auto* filter = http_connection_manager.mutable_http_filters(0);
385 filter->set_name("envoy.fault");
386 filter->mutable_typed_config()->PackFrom(http_fault);
387 ClientHcmAccessor().Pack(http_connection_manager, &listener);
388 SetListenerAndRouteConfiguration(balancer_.get(), std::move(listener),
389 route_config);
390 // Wait for traffic to switch to backend 1. There should be no RPC
391 // failures here; if there are, that indicates that the client started
392 // using the new LDS resource before it saw the new RDS resource.
393 WaitForBackend(DEBUG_LOCATION, 1);
394 }
395
396 using LdsRdsTest = XdsEnd2endTest;
397
398 // Test with and without RDS.
399 INSTANTIATE_TEST_SUITE_P(
400 XdsTest, LdsRdsTest,
401 ::testing::Values(XdsTestType(), XdsTestType().set_enable_rds_testing()),
402 &XdsTestType::Name);
403
404 MATCHER_P2(AdjustedClockInRange, t1, t2, "equals time") {
405 gpr_cycle_counter cycle_now = gpr_get_cycle_counter();
406 grpc_core::Timestamp cycle_time =
407 grpc_core::Timestamp::FromCycleCounterRoundDown(cycle_now);
408 grpc_core::Timestamp time_spec =
409 grpc_core::Timestamp::FromTimespecRoundDown(gpr_now(GPR_CLOCK_MONOTONIC));
410 grpc_core::Timestamp now = arg + (time_spec - cycle_time);
411 bool ok = true;
412 ok &= ::testing::ExplainMatchResult(::testing::Ge(t1), now, result_listener);
413 ok &= ::testing::ExplainMatchResult(::testing::Lt(t2), now, result_listener);
414 return ok;
415 }
416
417 TEST_P(LdsRdsTest, DefaultRouteSpecifiesSlashPrefix) {
418 CreateAndStartBackends(1);
419 RouteConfiguration route_config = default_route_config_;
420 route_config.mutable_virtual_hosts(0)
421 ->mutable_routes(0)
422 ->mutable_match()
423 ->set_prefix("/");
424 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
425 route_config);
426 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
427 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
428 // We need to wait for all backends to come online.
429 WaitForAllBackends(DEBUG_LOCATION);
430 }
431
432 // Tests that LDS client ACKs but fails if matching domain can't be found in
433 // the LDS response.
434 TEST_P(LdsRdsTest, NoMatchedDomain) {
435 RouteConfiguration route_config = default_route_config_;
436 route_config.mutable_virtual_hosts(0)->clear_domains();
437 route_config.mutable_virtual_hosts(0)->add_domains("unmatched_domain");
438 SetRouteConfiguration(balancer_.get(), route_config);
439 CheckRpcSendFailure(
440 DEBUG_LOCATION, StatusCode::UNAVAILABLE,
441 absl::StrCat(
442 (GetParam().enable_rds_testing() ? kDefaultRouteConfigurationName
443 : kServerName),
444 ": UNAVAILABLE: could not find VirtualHost for ", kServerName,
445 " in RouteConfiguration"));
446 // Do a bit of polling, to allow the ACK to get to the ADS server.
447 channel_->WaitForConnected(grpc_timeout_milliseconds_to_deadline(100));
448 auto response_state = RouteConfigurationResponseState(balancer_.get());
449 ASSERT_TRUE(response_state.has_value());
450 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
451 }
452
453 // Tests that LDS client should choose the virtual host with matching domain
454 // if multiple virtual hosts exist in the LDS response.
455 TEST_P(LdsRdsTest, ChooseMatchedDomain) {
456 RouteConfiguration route_config = default_route_config_;
457 *(route_config.add_virtual_hosts()) = route_config.virtual_hosts(0);
458 route_config.mutable_virtual_hosts(0)->clear_domains();
459 route_config.mutable_virtual_hosts(0)->add_domains("unmatched_domain");
460 SetRouteConfiguration(balancer_.get(), route_config);
461 (void)SendRpc();
462 auto response_state = RouteConfigurationResponseState(balancer_.get());
463 ASSERT_TRUE(response_state.has_value());
464 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
465 }
466
467 // Tests that LDS client should choose the last route in the virtual host if
468 // multiple routes exist in the LDS response.
469 TEST_P(LdsRdsTest, ChooseLastRoute) {
470 RouteConfiguration route_config = default_route_config_;
471 *(route_config.mutable_virtual_hosts(0)->add_routes()) =
472 route_config.virtual_hosts(0).routes(0);
473 route_config.mutable_virtual_hosts(0)
474 ->mutable_routes(0)
475 ->mutable_route()
476 ->mutable_cluster_header();
477 SetRouteConfiguration(balancer_.get(), route_config);
478 (void)SendRpc();
479 auto response_state = RouteConfigurationResponseState(balancer_.get());
480 ASSERT_TRUE(response_state.has_value());
481 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
482 }
483
484 // Tests that LDS client should ignore route which has query_parameters.
485 TEST_P(LdsRdsTest, RouteMatchHasQueryParameters) {
486 RouteConfiguration route_config = default_route_config_;
487 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
488 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
489 route1->mutable_match()->add_query_parameters();
490 SetRouteConfiguration(balancer_.get(), route_config);
491 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
492 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
493 EXPECT_THAT(response_state->error_message,
494 ::testing::HasSubstr("No valid routes specified."));
495 }
496
497 // Tests that LDS client should send a ACK if route match has a prefix
498 // that is either empty or a single slash
499 TEST_P(LdsRdsTest, RouteMatchHasValidPrefixEmptyOrSingleSlash) {
500 RouteConfiguration route_config = default_route_config_;
501 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
502 route1->mutable_match()->set_prefix("");
503 auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
504 default_route->mutable_match()->set_prefix("/");
505 default_route->mutable_route()->set_cluster(kDefaultClusterName);
506 SetRouteConfiguration(balancer_.get(), route_config);
507 (void)SendRpc();
508 const auto response_state = RouteConfigurationResponseState(balancer_.get());
509 ASSERT_TRUE(response_state.has_value());
510 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
511 }
512
513 // Tests that LDS client should ignore route which has a path
514 // prefix string does not start with "/".
515 TEST_P(LdsRdsTest, RouteMatchHasInvalidPrefixNoLeadingSlash) {
516 RouteConfiguration route_config = default_route_config_;
517 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
518 route1->mutable_match()->set_prefix("grpc.testing.EchoTest1Service/");
519 SetRouteConfiguration(balancer_.get(), route_config);
520 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
521 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
522 EXPECT_THAT(response_state->error_message,
523 ::testing::HasSubstr("No valid routes specified."));
524 }
525
526 // Tests that LDS client should ignore route which has a prefix
527 // string with more than 2 slashes.
528 TEST_P(LdsRdsTest, RouteMatchHasInvalidPrefixExtraContent) {
529 RouteConfiguration route_config = default_route_config_;
530 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
531 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/Echo1/");
532 SetRouteConfiguration(balancer_.get(), route_config);
533 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
534 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
535 EXPECT_THAT(response_state->error_message,
536 ::testing::HasSubstr("No valid routes specified."));
537 }
538
539 // Tests that LDS client should ignore route which has a prefix
540 // string "//".
541 TEST_P(LdsRdsTest, RouteMatchHasInvalidPrefixDoubleSlash) {
542 RouteConfiguration route_config = default_route_config_;
543 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
544 route1->mutable_match()->set_prefix("//");
545 SetRouteConfiguration(balancer_.get(), route_config);
546 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
547 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
548 EXPECT_THAT(response_state->error_message,
549 ::testing::HasSubstr("No valid routes specified."));
550 }
551
552 // Tests that LDS client should ignore route which has path
553 // but it's empty.
554 TEST_P(LdsRdsTest, RouteMatchHasInvalidPathEmptyPath) {
555 RouteConfiguration route_config = default_route_config_;
556 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
557 route1->mutable_match()->set_path("");
558 SetRouteConfiguration(balancer_.get(), route_config);
559 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
560 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
561 EXPECT_THAT(response_state->error_message,
562 ::testing::HasSubstr("No valid routes specified."));
563 }
564
565 // Tests that LDS client should ignore route which has path
566 // string does not start with "/".
567 TEST_P(LdsRdsTest, RouteMatchHasInvalidPathNoLeadingSlash) {
568 RouteConfiguration route_config = default_route_config_;
569 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
570 route1->mutable_match()->set_path("grpc.testing.EchoTest1Service/Echo1");
571 SetRouteConfiguration(balancer_.get(), route_config);
572 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
573 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
574 EXPECT_THAT(response_state->error_message,
575 ::testing::HasSubstr("No valid routes specified."));
576 }
577
578 // Tests that LDS client should ignore route which has path
579 // string that has too many slashes; for example, ends with "/".
580 TEST_P(LdsRdsTest, RouteMatchHasInvalidPathTooManySlashes) {
581 RouteConfiguration route_config = default_route_config_;
582 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
583 route1->mutable_match()->set_path("/grpc.testing.EchoTest1Service/Echo1/");
584 SetRouteConfiguration(balancer_.get(), route_config);
585 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
586 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
587 EXPECT_THAT(response_state->error_message,
588 ::testing::HasSubstr("No valid routes specified."));
589 }
590
591 // Tests that LDS client should ignore route which has path
592 // string that has only 1 slash: missing "/" between service and method.
593 TEST_P(LdsRdsTest, RouteMatchHasInvalidPathOnlyOneSlash) {
594 RouteConfiguration route_config = default_route_config_;
595 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
596 route1->mutable_match()->set_path("/grpc.testing.EchoTest1Service.Echo1");
597 SetRouteConfiguration(balancer_.get(), route_config);
598 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
599 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
600 EXPECT_THAT(response_state->error_message,
601 ::testing::HasSubstr("No valid routes specified."));
602 }
603
604 // Tests that LDS client should ignore route which has path
605 // string that is missing service.
606 TEST_P(LdsRdsTest, RouteMatchHasInvalidPathMissingService) {
607 RouteConfiguration route_config = default_route_config_;
608 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
609 route1->mutable_match()->set_path("//Echo1");
610 SetRouteConfiguration(balancer_.get(), route_config);
611 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
612 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
613 EXPECT_THAT(response_state->error_message,
614 ::testing::HasSubstr("No valid routes specified."));
615 }
616
617 // Tests that LDS client should ignore route which has path
618 // string that is missing method.
619 TEST_P(LdsRdsTest, RouteMatchHasInvalidPathMissingMethod) {
620 RouteConfiguration route_config = default_route_config_;
621 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
622 route1->mutable_match()->set_path("/grpc.testing.EchoTest1Service/");
623 SetRouteConfiguration(balancer_.get(), route_config);
624 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
625 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
626 EXPECT_THAT(response_state->error_message,
627 ::testing::HasSubstr("No valid routes specified."));
628 }
629
630 // Test that LDS client should reject route which has invalid path regex.
631 TEST_P(LdsRdsTest, RouteMatchHasInvalidPathRegex) {
632 const char* kNewCluster1Name = "new_cluster_1";
633 RouteConfiguration route_config = default_route_config_;
634 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
635 route1->mutable_match()->mutable_safe_regex()->set_regex("a[z-a]");
636 route1->mutable_route()->set_cluster(kNewCluster1Name);
637 SetRouteConfiguration(balancer_.get(), route_config);
638 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
639 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
640 EXPECT_THAT(response_state->error_message,
641 ::testing::HasSubstr(
642 "path matcher: Invalid regex string specified in matcher."));
643 }
644
645 // Tests that LDS client should fail RPCs with UNAVAILABLE status code if the
646 // matching route has an action other than RouteAction.
647 TEST_P(LdsRdsTest, MatchingRouteHasNoRouteAction) {
648 RouteConfiguration route_config = default_route_config_;
649 // Set a route with an inappropriate route action
650 auto* vhost = route_config.mutable_virtual_hosts(0);
651 vhost->mutable_routes(0)->mutable_redirect();
652 // Add another route to make sure that the resolver code actually tries to
653 // match to a route instead of using a shorthand logic to error out.
654 auto* route = vhost->add_routes();
655 route->mutable_match()->set_prefix("");
656 route->mutable_route()->set_cluster(kDefaultClusterName);
657 SetRouteConfiguration(balancer_.get(), route_config);
658 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::UNAVAILABLE,
659 "Matching route has inappropriate action");
660 }
661
662 TEST_P(LdsRdsTest, RouteActionClusterHasEmptyClusterName) {
663 RouteConfiguration route_config = default_route_config_;
664 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
665 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
666 route1->mutable_route()->set_cluster("");
667 auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
668 default_route->mutable_match()->set_prefix("");
669 default_route->mutable_route()->set_cluster(kDefaultClusterName);
670 SetRouteConfiguration(balancer_.get(), route_config);
671 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
672 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
673 EXPECT_THAT(
674 response_state->error_message,
675 ::testing::HasSubstr("RouteAction cluster contains empty cluster name."));
676 }
677
678 TEST_P(LdsRdsTest, RouteActionWeightedTargetHasIncorrectTotalWeightSet) {
679 const size_t kWeight75 = 75;
680 const char* kNewCluster1Name = "new_cluster_1";
681 RouteConfiguration route_config = default_route_config_;
682 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
683 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
684 auto* weighted_cluster1 =
685 route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
686 weighted_cluster1->set_name(kNewCluster1Name);
687 weighted_cluster1->mutable_weight()->set_value(kWeight75);
688 route1->mutable_route()
689 ->mutable_weighted_clusters()
690 ->mutable_total_weight()
691 ->set_value(kWeight75 + 1);
692 auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
693 default_route->mutable_match()->set_prefix("");
694 default_route->mutable_route()->set_cluster(kDefaultClusterName);
695 SetRouteConfiguration(balancer_.get(), route_config);
696 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
697 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
698 EXPECT_THAT(response_state->error_message,
699 ::testing::HasSubstr(
700 "RouteAction weighted_cluster has incorrect total weight"));
701 }
702
703 TEST_P(LdsRdsTest, RouteActionWeightedClusterHasZeroTotalWeight) {
704 const char* kNewCluster1Name = "new_cluster_1";
705 RouteConfiguration route_config = default_route_config_;
706 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
707 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
708 auto* weighted_cluster1 =
709 route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
710 weighted_cluster1->set_name(kNewCluster1Name);
711 weighted_cluster1->mutable_weight()->set_value(0);
712 route1->mutable_route()
713 ->mutable_weighted_clusters()
714 ->mutable_total_weight()
715 ->set_value(0);
716 auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
717 default_route->mutable_match()->set_prefix("");
718 default_route->mutable_route()->set_cluster(kDefaultClusterName);
719 SetRouteConfiguration(balancer_.get(), route_config);
720 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
721 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
722 EXPECT_THAT(
723 response_state->error_message,
724 ::testing::HasSubstr(
725 "RouteAction weighted_cluster has no valid clusters specified."));
726 }
727
728 TEST_P(LdsRdsTest, RouteActionWeightedTargetClusterHasEmptyClusterName) {
729 const size_t kWeight75 = 75;
730 RouteConfiguration route_config = default_route_config_;
731 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
732 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
733 auto* weighted_cluster1 =
734 route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
735 weighted_cluster1->set_name("");
736 weighted_cluster1->mutable_weight()->set_value(kWeight75);
737 route1->mutable_route()
738 ->mutable_weighted_clusters()
739 ->mutable_total_weight()
740 ->set_value(kWeight75);
741 auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
742 default_route->mutable_match()->set_prefix("");
743 default_route->mutable_route()->set_cluster(kDefaultClusterName);
744 SetRouteConfiguration(balancer_.get(), route_config);
745 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
746 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
747 EXPECT_THAT(response_state->error_message,
748 ::testing::HasSubstr("RouteAction weighted_cluster cluster "
749 "contains empty cluster name."));
750 }
751
752 TEST_P(LdsRdsTest, RouteActionWeightedTargetClusterHasNoWeight) {
753 const size_t kWeight75 = 75;
754 const char* kNewCluster1Name = "new_cluster_1";
755 RouteConfiguration route_config = default_route_config_;
756 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
757 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
758 auto* weighted_cluster1 =
759 route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
760 weighted_cluster1->set_name(kNewCluster1Name);
761 route1->mutable_route()
762 ->mutable_weighted_clusters()
763 ->mutable_total_weight()
764 ->set_value(kWeight75);
765 auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
766 default_route->mutable_match()->set_prefix("");
767 default_route->mutable_route()->set_cluster(kDefaultClusterName);
768 SetRouteConfiguration(balancer_.get(), route_config);
769 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
770 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
771 EXPECT_THAT(response_state->error_message,
772 ::testing::HasSubstr(
773 "RouteAction weighted_cluster cluster missing weight"));
774 }
775
776 TEST_P(LdsRdsTest, RouteHeaderMatchInvalidRegex) {
777 const char* kNewCluster1Name = "new_cluster_1";
778 RouteConfiguration route_config = default_route_config_;
779 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
780 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
781 auto* header_matcher1 = route1->mutable_match()->add_headers();
782 header_matcher1->set_name("header1");
783 header_matcher1->mutable_safe_regex_match()->set_regex("a[z-a]");
784 route1->mutable_route()->set_cluster(kNewCluster1Name);
785 SetRouteConfiguration(balancer_.get(), route_config);
786 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
787 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
788 EXPECT_THAT(
789 response_state->error_message,
790 ::testing::HasSubstr(
791 "header matcher: Invalid regex string specified in matcher."));
792 }
793
794 TEST_P(LdsRdsTest, RouteHeaderMatchInvalidRange) {
795 const char* kNewCluster1Name = "new_cluster_1";
796 RouteConfiguration route_config = default_route_config_;
797 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
798 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
799 auto* header_matcher1 = route1->mutable_match()->add_headers();
800 header_matcher1->set_name("header1");
801 header_matcher1->mutable_range_match()->set_start(1001);
802 header_matcher1->mutable_range_match()->set_end(1000);
803 route1->mutable_route()->set_cluster(kNewCluster1Name);
804 SetRouteConfiguration(balancer_.get(), route_config);
805 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
806 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
807 EXPECT_THAT(
808 response_state->error_message,
809 ::testing::HasSubstr(
810 "header matcher: Invalid range specifier specified: end cannot be "
811 "smaller than start."));
812 }
813
814 // Tests that LDS client should choose the default route (with no matching
815 // specified) after unable to find a match with previous routes.
816 TEST_P(LdsRdsTest, XdsRoutingPathMatching) {
817 CreateAndStartBackends(4);
818 const char* kNewCluster1Name = "new_cluster_1";
819 const char* kNewEdsService1Name = "new_eds_service_name_1";
820 const char* kNewCluster2Name = "new_cluster_2";
821 const char* kNewEdsService2Name = "new_eds_service_name_2";
822 const size_t kNumEcho1Rpcs = 10;
823 const size_t kNumEcho2Rpcs = 20;
824 const size_t kNumEchoRpcs = 30;
825 // Populate new EDS resources.
826 EdsResourceArgs args({
827 {"locality0", CreateEndpointsForBackends(0, 2)},
828 });
829 EdsResourceArgs args1({
830 {"locality0", CreateEndpointsForBackends(2, 3)},
831 });
832 EdsResourceArgs args2({
833 {"locality0", CreateEndpointsForBackends(3, 4)},
834 });
835 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
836 balancer_->ads_service()->SetEdsResource(
837 BuildEdsResource(args1, kNewEdsService1Name));
838 balancer_->ads_service()->SetEdsResource(
839 BuildEdsResource(args2, kNewEdsService2Name));
840 // Populate new CDS resources.
841 Cluster new_cluster1 = default_cluster_;
842 new_cluster1.set_name(kNewCluster1Name);
843 new_cluster1.mutable_eds_cluster_config()->set_service_name(
844 kNewEdsService1Name);
845 balancer_->ads_service()->SetCdsResource(new_cluster1);
846 Cluster new_cluster2 = default_cluster_;
847 new_cluster2.set_name(kNewCluster2Name);
848 new_cluster2.mutable_eds_cluster_config()->set_service_name(
849 kNewEdsService2Name);
850 balancer_->ads_service()->SetCdsResource(new_cluster2);
851 // Populating Route Configurations for LDS.
852 RouteConfiguration new_route_config = default_route_config_;
853 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
854 route1->mutable_match()->set_path("/grpc.testing.EchoTest1Service/Echo1");
855 route1->mutable_route()->set_cluster(kNewCluster1Name);
856 auto* route2 = new_route_config.mutable_virtual_hosts(0)->add_routes();
857 route2->mutable_match()->set_path("/grpc.testing.EchoTest2Service/Echo2");
858 route2->mutable_route()->set_cluster(kNewCluster2Name);
859 auto* route3 = new_route_config.mutable_virtual_hosts(0)->add_routes();
860 route3->mutable_match()->set_path("/grpc.testing.EchoTest3Service/Echo3");
861 route3->mutable_route()->set_cluster(kDefaultClusterName);
862 auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
863 default_route->mutable_match()->set_prefix("");
864 default_route->mutable_route()->set_cluster(kDefaultClusterName);
865 SetRouteConfiguration(balancer_.get(), new_route_config);
866 WaitForAllBackends(DEBUG_LOCATION, 0, 2, /*check_status=*/nullptr,
867 WaitForBackendOptions(),
868 RpcOptions().set_timeout_ms(5000));
869 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs,
870 RpcOptions().set_wait_for_ready(true));
871 CheckRpcSendOk(DEBUG_LOCATION, kNumEcho1Rpcs,
872 RpcOptions()
873 .set_rpc_service(SERVICE_ECHO1)
874 .set_rpc_method(METHOD_ECHO1)
875 .set_wait_for_ready(true));
876 CheckRpcSendOk(DEBUG_LOCATION, kNumEcho2Rpcs,
877 RpcOptions()
878 .set_rpc_service(SERVICE_ECHO2)
879 .set_rpc_method(METHOD_ECHO2)
880 .set_wait_for_ready(true));
881 // Make sure RPCs all go to the correct backend.
882 for (size_t i = 0; i < 2; ++i) {
883 EXPECT_EQ(kNumEchoRpcs / 2,
884 backends_[i]->backend_service()->request_count());
885 EXPECT_EQ(0, backends_[i]->backend_service1()->request_count());
886 EXPECT_EQ(0, backends_[i]->backend_service2()->request_count());
887 }
888 EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
889 EXPECT_EQ(kNumEcho1Rpcs, backends_[2]->backend_service1()->request_count());
890 EXPECT_EQ(0, backends_[2]->backend_service2()->request_count());
891 EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
892 EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
893 EXPECT_EQ(kNumEcho2Rpcs, backends_[3]->backend_service2()->request_count());
894 }
895
896 TEST_P(LdsRdsTest, XdsRoutingPathMatchingCaseInsensitive) {
897 CreateAndStartBackends(4);
898 const char* kNewCluster1Name = "new_cluster_1";
899 const char* kNewEdsService1Name = "new_eds_service_name_1";
900 const char* kNewCluster2Name = "new_cluster_2";
901 const char* kNewEdsService2Name = "new_eds_service_name_2";
902 const size_t kNumEcho1Rpcs = 10;
903 const size_t kNumEchoRpcs = 30;
904 // Populate new EDS resources.
905 EdsResourceArgs args({
906 {"locality0", CreateEndpointsForBackends(0, 1)},
907 });
908 EdsResourceArgs args1({
909 {"locality0", CreateEndpointsForBackends(1, 2)},
910 });
911 EdsResourceArgs args2({
912 {"locality0", CreateEndpointsForBackends(2, 3)},
913 });
914 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
915 balancer_->ads_service()->SetEdsResource(
916 BuildEdsResource(args1, kNewEdsService1Name));
917 balancer_->ads_service()->SetEdsResource(
918 BuildEdsResource(args2, kNewEdsService2Name));
919 // Populate new CDS resources.
920 Cluster new_cluster1 = default_cluster_;
921 new_cluster1.set_name(kNewCluster1Name);
922 new_cluster1.mutable_eds_cluster_config()->set_service_name(
923 kNewEdsService1Name);
924 balancer_->ads_service()->SetCdsResource(new_cluster1);
925 Cluster new_cluster2 = default_cluster_;
926 new_cluster2.set_name(kNewCluster2Name);
927 new_cluster2.mutable_eds_cluster_config()->set_service_name(
928 kNewEdsService2Name);
929 balancer_->ads_service()->SetCdsResource(new_cluster2);
930 // Populating Route Configurations for LDS.
931 RouteConfiguration new_route_config = default_route_config_;
932 // First route will not match, since it's case-sensitive.
933 // Second route will match with same path.
934 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
935 route1->mutable_match()->set_path("/GrPc.TeStInG.EcHoTeSt1SErViCe/EcHo1");
936 route1->mutable_route()->set_cluster(kNewCluster1Name);
937 auto* route2 = new_route_config.mutable_virtual_hosts(0)->add_routes();
938 route2->mutable_match()->set_path("/GrPc.TeStInG.EcHoTeSt1SErViCe/EcHo1");
939 route2->mutable_match()->mutable_case_sensitive()->set_value(false);
940 route2->mutable_route()->set_cluster(kNewCluster2Name);
941 auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
942 default_route->mutable_match()->set_prefix("");
943 default_route->mutable_route()->set_cluster(kDefaultClusterName);
944 SetRouteConfiguration(balancer_.get(), new_route_config);
945 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs,
946 RpcOptions().set_wait_for_ready(true));
947 CheckRpcSendOk(DEBUG_LOCATION, kNumEcho1Rpcs,
948 RpcOptions()
949 .set_rpc_service(SERVICE_ECHO1)
950 .set_rpc_method(METHOD_ECHO1)
951 .set_wait_for_ready(true));
952 // Make sure RPCs all go to the correct backend.
953 EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
954 EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
955 EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
956 EXPECT_EQ(0, backends_[1]->backend_service1()->request_count());
957 EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
958 EXPECT_EQ(kNumEcho1Rpcs, backends_[2]->backend_service1()->request_count());
959 }
960
961 TEST_P(LdsRdsTest, XdsRoutingPrefixMatching) {
962 CreateAndStartBackends(4);
963 const char* kNewCluster1Name = "new_cluster_1";
964 const char* kNewEdsService1Name = "new_eds_service_name_1";
965 const char* kNewCluster2Name = "new_cluster_2";
966 const char* kNewEdsService2Name = "new_eds_service_name_2";
967 const size_t kNumEcho1Rpcs = 10;
968 const size_t kNumEcho2Rpcs = 20;
969 const size_t kNumEchoRpcs = 30;
970 // Populate new EDS resources.
971 EdsResourceArgs args({
972 {"locality0", CreateEndpointsForBackends(0, 2)},
973 });
974 EdsResourceArgs args1({
975 {"locality0", CreateEndpointsForBackends(2, 3)},
976 });
977 EdsResourceArgs args2({
978 {"locality0", CreateEndpointsForBackends(3, 4)},
979 });
980 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
981 balancer_->ads_service()->SetEdsResource(
982 BuildEdsResource(args1, kNewEdsService1Name));
983 balancer_->ads_service()->SetEdsResource(
984 BuildEdsResource(args2, kNewEdsService2Name));
985 // Populate new CDS resources.
986 Cluster new_cluster1 = default_cluster_;
987 new_cluster1.set_name(kNewCluster1Name);
988 new_cluster1.mutable_eds_cluster_config()->set_service_name(
989 kNewEdsService1Name);
990 balancer_->ads_service()->SetCdsResource(new_cluster1);
991 Cluster new_cluster2 = default_cluster_;
992 new_cluster2.set_name(kNewCluster2Name);
993 new_cluster2.mutable_eds_cluster_config()->set_service_name(
994 kNewEdsService2Name);
995 balancer_->ads_service()->SetCdsResource(new_cluster2);
996 // Populating Route Configurations for LDS.
997 RouteConfiguration new_route_config = default_route_config_;
998 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
999 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
1000 route1->mutable_route()->set_cluster(kNewCluster1Name);
1001 auto* route2 = new_route_config.mutable_virtual_hosts(0)->add_routes();
1002 route2->mutable_match()->set_prefix("/grpc.testing.EchoTest2Service/");
1003 route2->mutable_route()->set_cluster(kNewCluster2Name);
1004 auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
1005 default_route->mutable_match()->set_prefix("");
1006 default_route->mutable_route()->set_cluster(kDefaultClusterName);
1007 SetRouteConfiguration(balancer_.get(), new_route_config);
1008 WaitForAllBackends(DEBUG_LOCATION, 0, 2);
1009 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs,
1010 RpcOptions().set_wait_for_ready(true));
1011 CheckRpcSendOk(
1012 DEBUG_LOCATION, kNumEcho1Rpcs,
1013 RpcOptions().set_rpc_service(SERVICE_ECHO1).set_wait_for_ready(true));
1014 CheckRpcSendOk(
1015 DEBUG_LOCATION, kNumEcho2Rpcs,
1016 RpcOptions().set_rpc_service(SERVICE_ECHO2).set_wait_for_ready(true));
1017 // Make sure RPCs all go to the correct backend.
1018 for (size_t i = 0; i < 2; ++i) {
1019 EXPECT_EQ(kNumEchoRpcs / 2,
1020 backends_[i]->backend_service()->request_count());
1021 EXPECT_EQ(0, backends_[i]->backend_service1()->request_count());
1022 EXPECT_EQ(0, backends_[i]->backend_service2()->request_count());
1023 }
1024 EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
1025 EXPECT_EQ(kNumEcho1Rpcs, backends_[2]->backend_service1()->request_count());
1026 EXPECT_EQ(0, backends_[2]->backend_service2()->request_count());
1027 EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
1028 EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
1029 EXPECT_EQ(kNumEcho2Rpcs, backends_[3]->backend_service2()->request_count());
1030 }
1031
1032 TEST_P(LdsRdsTest, XdsRoutingPrefixMatchingCaseInsensitive) {
1033 CreateAndStartBackends(3);
1034 const char* kNewCluster1Name = "new_cluster_1";
1035 const char* kNewEdsService1Name = "new_eds_service_name_1";
1036 const char* kNewCluster2Name = "new_cluster_2";
1037 const char* kNewEdsService2Name = "new_eds_service_name_2";
1038 const size_t kNumEcho1Rpcs = 10;
1039 const size_t kNumEchoRpcs = 30;
1040 // Populate new EDS resources.
1041 EdsResourceArgs args({
1042 {"locality0", CreateEndpointsForBackends(0, 1)},
1043 });
1044 EdsResourceArgs args1({
1045 {"locality0", CreateEndpointsForBackends(1, 2)},
1046 });
1047 EdsResourceArgs args2({
1048 {"locality0", CreateEndpointsForBackends(2, 3)},
1049 });
1050 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1051 balancer_->ads_service()->SetEdsResource(
1052 BuildEdsResource(args1, kNewEdsService1Name));
1053 balancer_->ads_service()->SetEdsResource(
1054 BuildEdsResource(args2, kNewEdsService2Name));
1055 // Populate new CDS resources.
1056 Cluster new_cluster1 = default_cluster_;
1057 new_cluster1.set_name(kNewCluster1Name);
1058 new_cluster1.mutable_eds_cluster_config()->set_service_name(
1059 kNewEdsService1Name);
1060 balancer_->ads_service()->SetCdsResource(new_cluster1);
1061 Cluster new_cluster2 = default_cluster_;
1062 new_cluster2.set_name(kNewCluster2Name);
1063 new_cluster2.mutable_eds_cluster_config()->set_service_name(
1064 kNewEdsService2Name);
1065 balancer_->ads_service()->SetCdsResource(new_cluster2);
1066 // Populating Route Configurations for LDS.
1067 RouteConfiguration new_route_config = default_route_config_;
1068 // First route will not match, since it's case-sensitive.
1069 // Second route will match with same path.
1070 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
1071 route1->mutable_match()->set_prefix("/GrPc.TeStInG.EcHoTeSt1SErViCe");
1072 route1->mutable_route()->set_cluster(kNewCluster1Name);
1073 auto* route2 = new_route_config.mutable_virtual_hosts(0)->add_routes();
1074 route2->mutable_match()->set_prefix("/GrPc.TeStInG.EcHoTeSt1SErViCe");
1075 route2->mutable_match()->mutable_case_sensitive()->set_value(false);
1076 route2->mutable_route()->set_cluster(kNewCluster2Name);
1077 auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
1078 default_route->mutable_match()->set_prefix("");
1079 default_route->mutable_route()->set_cluster(kDefaultClusterName);
1080 SetRouteConfiguration(balancer_.get(), new_route_config);
1081 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs,
1082 RpcOptions().set_wait_for_ready(true));
1083 CheckRpcSendOk(DEBUG_LOCATION, kNumEcho1Rpcs,
1084 RpcOptions()
1085 .set_rpc_service(SERVICE_ECHO1)
1086 .set_rpc_method(METHOD_ECHO1)
1087 .set_wait_for_ready(true));
1088 // Make sure RPCs all go to the correct backend.
1089 EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
1090 EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
1091 EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
1092 EXPECT_EQ(0, backends_[1]->backend_service1()->request_count());
1093 EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
1094 EXPECT_EQ(kNumEcho1Rpcs, backends_[2]->backend_service1()->request_count());
1095 }
1096
1097 TEST_P(LdsRdsTest, XdsRoutingPathRegexMatching) {
1098 CreateAndStartBackends(4);
1099 const char* kNewCluster1Name = "new_cluster_1";
1100 const char* kNewEdsService1Name = "new_eds_service_name_1";
1101 const char* kNewCluster2Name = "new_cluster_2";
1102 const char* kNewEdsService2Name = "new_eds_service_name_2";
1103 const size_t kNumEcho1Rpcs = 10;
1104 const size_t kNumEcho2Rpcs = 20;
1105 const size_t kNumEchoRpcs = 30;
1106 // Populate new EDS resources.
1107 EdsResourceArgs args({
1108 {"locality0", CreateEndpointsForBackends(0, 2)},
1109 });
1110 EdsResourceArgs args1({
1111 {"locality0", CreateEndpointsForBackends(2, 3)},
1112 });
1113 EdsResourceArgs args2({
1114 {"locality0", CreateEndpointsForBackends(3, 4)},
1115 });
1116 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1117 balancer_->ads_service()->SetEdsResource(
1118 BuildEdsResource(args1, kNewEdsService1Name));
1119 balancer_->ads_service()->SetEdsResource(
1120 BuildEdsResource(args2, kNewEdsService2Name));
1121 // Populate new CDS resources.
1122 Cluster new_cluster1 = default_cluster_;
1123 new_cluster1.set_name(kNewCluster1Name);
1124 new_cluster1.mutable_eds_cluster_config()->set_service_name(
1125 kNewEdsService1Name);
1126 balancer_->ads_service()->SetCdsResource(new_cluster1);
1127 Cluster new_cluster2 = default_cluster_;
1128 new_cluster2.set_name(kNewCluster2Name);
1129 new_cluster2.mutable_eds_cluster_config()->set_service_name(
1130 kNewEdsService2Name);
1131 balancer_->ads_service()->SetCdsResource(new_cluster2);
1132 // Populating Route Configurations for LDS.
1133 RouteConfiguration new_route_config = default_route_config_;
1134 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
1135 // Will match "/grpc.testing.EchoTest1Service/"
1136 route1->mutable_match()->mutable_safe_regex()->set_regex(".*1.*");
1137 route1->mutable_route()->set_cluster(kNewCluster1Name);
1138 auto* route2 = new_route_config.mutable_virtual_hosts(0)->add_routes();
1139 // Will match "/grpc.testing.EchoTest2Service/"
1140 route2->mutable_match()->mutable_safe_regex()->set_regex(".*2.*");
1141 route2->mutable_route()->set_cluster(kNewCluster2Name);
1142 auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
1143 default_route->mutable_match()->set_prefix("");
1144 default_route->mutable_route()->set_cluster(kDefaultClusterName);
1145 SetRouteConfiguration(balancer_.get(), new_route_config);
1146 WaitForAllBackends(DEBUG_LOCATION, 0, 2);
1147 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs,
1148 RpcOptions().set_wait_for_ready(true));
1149 CheckRpcSendOk(
1150 DEBUG_LOCATION, kNumEcho1Rpcs,
1151 RpcOptions().set_rpc_service(SERVICE_ECHO1).set_wait_for_ready(true));
1152 CheckRpcSendOk(
1153 DEBUG_LOCATION, kNumEcho2Rpcs,
1154 RpcOptions().set_rpc_service(SERVICE_ECHO2).set_wait_for_ready(true));
1155 // Make sure RPCs all go to the correct backend.
1156 for (size_t i = 0; i < 2; ++i) {
1157 EXPECT_EQ(kNumEchoRpcs / 2,
1158 backends_[i]->backend_service()->request_count());
1159 EXPECT_EQ(0, backends_[i]->backend_service1()->request_count());
1160 EXPECT_EQ(0, backends_[i]->backend_service2()->request_count());
1161 }
1162 EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
1163 EXPECT_EQ(kNumEcho1Rpcs, backends_[2]->backend_service1()->request_count());
1164 EXPECT_EQ(0, backends_[2]->backend_service2()->request_count());
1165 EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
1166 EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
1167 EXPECT_EQ(kNumEcho2Rpcs, backends_[3]->backend_service2()->request_count());
1168 }
1169
1170 TEST_P(LdsRdsTest, XdsRoutingWeightedCluster) {
1171 CreateAndStartBackends(3);
1172 const char* kNewCluster1Name = "new_cluster_1";
1173 const char* kNewEdsService1Name = "new_eds_service_name_1";
1174 const char* kNewCluster2Name = "new_cluster_2";
1175 const char* kNewEdsService2Name = "new_eds_service_name_2";
1176 const char* kNotUsedClusterName = "not_used_cluster";
1177 const size_t kNumEchoRpcs = 10; // RPCs that will go to a fixed backend.
1178 const size_t kWeight75 = 75;
1179 const size_t kWeight25 = 25;
1180 const double kErrorTolerance = 0.05;
1181 const double kWeight75Percent = static_cast<double>(kWeight75) / 100;
1182 const double kWeight25Percent = static_cast<double>(kWeight25) / 100;
1183 const size_t kNumEcho1Rpcs =
1184 ComputeIdealNumRpcs(kWeight75Percent, kErrorTolerance);
1185 // Populate new EDS resources.
1186 EdsResourceArgs args({
1187 {"locality0", CreateEndpointsForBackends(0, 1)},
1188 });
1189 EdsResourceArgs args1({
1190 {"locality0", CreateEndpointsForBackends(1, 2)},
1191 });
1192 EdsResourceArgs args2({
1193 {"locality0", CreateEndpointsForBackends(2, 3)},
1194 });
1195 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1196 balancer_->ads_service()->SetEdsResource(
1197 BuildEdsResource(args1, kNewEdsService1Name));
1198 balancer_->ads_service()->SetEdsResource(
1199 BuildEdsResource(args2, kNewEdsService2Name));
1200 // Populate new CDS resources.
1201 Cluster new_cluster1 = default_cluster_;
1202 new_cluster1.set_name(kNewCluster1Name);
1203 new_cluster1.mutable_eds_cluster_config()->set_service_name(
1204 kNewEdsService1Name);
1205 balancer_->ads_service()->SetCdsResource(new_cluster1);
1206 Cluster new_cluster2 = default_cluster_;
1207 new_cluster2.set_name(kNewCluster2Name);
1208 new_cluster2.mutable_eds_cluster_config()->set_service_name(
1209 kNewEdsService2Name);
1210 balancer_->ads_service()->SetCdsResource(new_cluster2);
1211 // Populating Route Configurations for LDS.
1212 RouteConfiguration new_route_config = default_route_config_;
1213 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
1214 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
1215 auto* weighted_cluster1 =
1216 route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
1217 weighted_cluster1->set_name(kNewCluster1Name);
1218 weighted_cluster1->mutable_weight()->set_value(kWeight75);
1219 auto* weighted_cluster2 =
1220 route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
1221 weighted_cluster2->set_name(kNewCluster2Name);
1222 weighted_cluster2->mutable_weight()->set_value(kWeight25);
1223 // Cluster with weight 0 will not be used.
1224 auto* weighted_cluster3 =
1225 route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
1226 weighted_cluster3->set_name(kNotUsedClusterName);
1227 weighted_cluster3->mutable_weight()->set_value(0);
1228 route1->mutable_route()
1229 ->mutable_weighted_clusters()
1230 ->mutable_total_weight()
1231 ->set_value(kWeight75 + kWeight25);
1232 auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
1233 default_route->mutable_match()->set_prefix("");
1234 default_route->mutable_route()->set_cluster(kDefaultClusterName);
1235 SetRouteConfiguration(balancer_.get(), new_route_config);
1236 WaitForAllBackends(DEBUG_LOCATION, 0, 1);
1237 WaitForAllBackends(DEBUG_LOCATION, 1, 3, /*check_status=*/nullptr,
1238 WaitForBackendOptions(),
1239 RpcOptions().set_rpc_service(SERVICE_ECHO1));
1240 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs);
1241 CheckRpcSendOk(DEBUG_LOCATION, kNumEcho1Rpcs,
1242 RpcOptions().set_rpc_service(SERVICE_ECHO1));
1243 // Make sure RPCs all go to the correct backend.
1244 EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
1245 EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
1246 EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
1247 const int weight_75_request_count =
1248 backends_[1]->backend_service1()->request_count();
1249 EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
1250 const int weight_25_request_count =
1251 backends_[2]->backend_service1()->request_count();
1252 gpr_log(GPR_INFO, "target_75 received %d rpcs and target_25 received %d rpcs",
1253 weight_75_request_count, weight_25_request_count);
1254 EXPECT_THAT(static_cast<double>(weight_75_request_count) / kNumEcho1Rpcs,
1255 ::testing::DoubleNear(kWeight75Percent, kErrorTolerance));
1256 EXPECT_THAT(static_cast<double>(weight_25_request_count) / kNumEcho1Rpcs,
1257 ::testing::DoubleNear(kWeight25Percent, kErrorTolerance));
1258 }
1259
1260 TEST_P(LdsRdsTest, RouteActionWeightedTargetDefaultRoute) {
1261 CreateAndStartBackends(3);
1262 const char* kNewCluster1Name = "new_cluster_1";
1263 const char* kNewEdsService1Name = "new_eds_service_name_1";
1264 const char* kNewCluster2Name = "new_cluster_2";
1265 const char* kNewEdsService2Name = "new_eds_service_name_2";
1266 const size_t kWeight75 = 75;
1267 const size_t kWeight25 = 25;
1268 const double kErrorTolerance = 0.05;
1269 const double kWeight75Percent = static_cast<double>(kWeight75) / 100;
1270 const double kWeight25Percent = static_cast<double>(kWeight25) / 100;
1271 const size_t kNumEchoRpcs =
1272 ComputeIdealNumRpcs(kWeight75Percent, kErrorTolerance);
1273 // Populate new EDS resources.
1274 EdsResourceArgs args({
1275 {"locality0", CreateEndpointsForBackends(0, 1)},
1276 });
1277 EdsResourceArgs args1({
1278 {"locality0", CreateEndpointsForBackends(1, 2)},
1279 });
1280 EdsResourceArgs args2({
1281 {"locality0", CreateEndpointsForBackends(2, 3)},
1282 });
1283 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1284 balancer_->ads_service()->SetEdsResource(
1285 BuildEdsResource(args1, kNewEdsService1Name));
1286 balancer_->ads_service()->SetEdsResource(
1287 BuildEdsResource(args2, kNewEdsService2Name));
1288 // Populate new CDS resources.
1289 Cluster new_cluster1 = default_cluster_;
1290 new_cluster1.set_name(kNewCluster1Name);
1291 new_cluster1.mutable_eds_cluster_config()->set_service_name(
1292 kNewEdsService1Name);
1293 balancer_->ads_service()->SetCdsResource(new_cluster1);
1294 Cluster new_cluster2 = default_cluster_;
1295 new_cluster2.set_name(kNewCluster2Name);
1296 new_cluster2.mutable_eds_cluster_config()->set_service_name(
1297 kNewEdsService2Name);
1298 balancer_->ads_service()->SetCdsResource(new_cluster2);
1299 // Populating Route Configurations for LDS.
1300 RouteConfiguration new_route_config = default_route_config_;
1301 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
1302 route1->mutable_match()->set_prefix("");
1303 auto* weighted_cluster1 =
1304 route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
1305 weighted_cluster1->set_name(kNewCluster1Name);
1306 weighted_cluster1->mutable_weight()->set_value(kWeight75);
1307 auto* weighted_cluster2 =
1308 route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
1309 weighted_cluster2->set_name(kNewCluster2Name);
1310 weighted_cluster2->mutable_weight()->set_value(kWeight25);
1311 route1->mutable_route()
1312 ->mutable_weighted_clusters()
1313 ->mutable_total_weight()
1314 ->set_value(kWeight75 + kWeight25);
1315 SetRouteConfiguration(balancer_.get(), new_route_config);
1316 WaitForAllBackends(DEBUG_LOCATION, 1, 3);
1317 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs);
1318 // Make sure RPCs all go to the correct backend.
1319 EXPECT_EQ(0, backends_[0]->backend_service()->request_count());
1320 const int weight_75_request_count =
1321 backends_[1]->backend_service()->request_count();
1322 const int weight_25_request_count =
1323 backends_[2]->backend_service()->request_count();
1324 gpr_log(GPR_INFO, "target_75 received %d rpcs and target_25 received %d rpcs",
1325 weight_75_request_count, weight_25_request_count);
1326 EXPECT_THAT(static_cast<double>(weight_75_request_count) / kNumEchoRpcs,
1327 ::testing::DoubleNear(kWeight75Percent, kErrorTolerance));
1328 EXPECT_THAT(static_cast<double>(weight_25_request_count) / kNumEchoRpcs,
1329 ::testing::DoubleNear(kWeight25Percent, kErrorTolerance));
1330 }
1331
1332 TEST_P(LdsRdsTest, XdsRoutingWeightedClusterUpdateWeights) {
1333 CreateAndStartBackends(4);
1334 const char* kNewCluster1Name = "new_cluster_1";
1335 const char* kNewEdsService1Name = "new_eds_service_name_1";
1336 const char* kNewCluster2Name = "new_cluster_2";
1337 const char* kNewEdsService2Name = "new_eds_service_name_2";
1338 const char* kNewCluster3Name = "new_cluster_3";
1339 const char* kNewEdsService3Name = "new_eds_service_name_3";
1340 const size_t kNumEchoRpcs = 10;
1341 const size_t kWeight75 = 75;
1342 const size_t kWeight25 = 25;
1343 const size_t kWeight50 = 50;
1344 const double kErrorTolerance = 0.05;
1345 const double kWeight75Percent = static_cast<double>(kWeight75) / 100;
1346 const double kWeight25Percent = static_cast<double>(kWeight25) / 100;
1347 const double kWeight50Percent = static_cast<double>(kWeight50) / 100;
1348 const size_t kNumEcho1Rpcs7525 =
1349 ComputeIdealNumRpcs(kWeight75Percent, kErrorTolerance);
1350 const size_t kNumEcho1Rpcs5050 =
1351 ComputeIdealNumRpcs(kWeight50Percent, kErrorTolerance);
1352 // Populate new EDS resources.
1353 EdsResourceArgs args({
1354 {"locality0", CreateEndpointsForBackends(0, 1)},
1355 });
1356 EdsResourceArgs args1({
1357 {"locality0", CreateEndpointsForBackends(1, 2)},
1358 });
1359 EdsResourceArgs args2({
1360 {"locality0", CreateEndpointsForBackends(2, 3)},
1361 });
1362 EdsResourceArgs args3({
1363 {"locality0", CreateEndpointsForBackends(3, 4)},
1364 });
1365 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1366 balancer_->ads_service()->SetEdsResource(
1367 BuildEdsResource(args1, kNewEdsService1Name));
1368 balancer_->ads_service()->SetEdsResource(
1369 BuildEdsResource(args2, kNewEdsService2Name));
1370 balancer_->ads_service()->SetEdsResource(
1371 BuildEdsResource(args3, kNewEdsService3Name));
1372 // Populate new CDS resources.
1373 Cluster new_cluster1 = default_cluster_;
1374 new_cluster1.set_name(kNewCluster1Name);
1375 new_cluster1.mutable_eds_cluster_config()->set_service_name(
1376 kNewEdsService1Name);
1377 balancer_->ads_service()->SetCdsResource(new_cluster1);
1378 Cluster new_cluster2 = default_cluster_;
1379 new_cluster2.set_name(kNewCluster2Name);
1380 new_cluster2.mutable_eds_cluster_config()->set_service_name(
1381 kNewEdsService2Name);
1382 balancer_->ads_service()->SetCdsResource(new_cluster2);
1383 Cluster new_cluster3 = default_cluster_;
1384 new_cluster3.set_name(kNewCluster3Name);
1385 new_cluster3.mutable_eds_cluster_config()->set_service_name(
1386 kNewEdsService3Name);
1387 balancer_->ads_service()->SetCdsResource(new_cluster3);
1388 // Populating Route Configurations.
1389 RouteConfiguration new_route_config = default_route_config_;
1390 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
1391 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
1392 auto* weighted_cluster1 =
1393 route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
1394 weighted_cluster1->set_name(kNewCluster1Name);
1395 weighted_cluster1->mutable_weight()->set_value(kWeight75);
1396 auto* weighted_cluster2 =
1397 route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
1398 weighted_cluster2->set_name(kNewCluster2Name);
1399 weighted_cluster2->mutable_weight()->set_value(kWeight25);
1400 route1->mutable_route()
1401 ->mutable_weighted_clusters()
1402 ->mutable_total_weight()
1403 ->set_value(kWeight75 + kWeight25);
1404 auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
1405 default_route->mutable_match()->set_prefix("");
1406 default_route->mutable_route()->set_cluster(kDefaultClusterName);
1407 SetRouteConfiguration(balancer_.get(), new_route_config);
1408 WaitForAllBackends(DEBUG_LOCATION, 0, 1, /*check_status=*/nullptr,
1409 WaitForBackendOptions(),
1410 RpcOptions().set_timeout_ms(5000));
1411 WaitForAllBackends(
1412 DEBUG_LOCATION, 1, 3, /*check_status=*/nullptr, WaitForBackendOptions(),
1413 RpcOptions().set_rpc_service(SERVICE_ECHO1).set_timeout_ms(5000));
1414 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs,
1415 RpcOptions().set_timeout_ms(5000));
1416 CheckRpcSendOk(
1417 DEBUG_LOCATION, kNumEcho1Rpcs7525,
1418 RpcOptions().set_rpc_service(SERVICE_ECHO1).set_timeout_ms(5000));
1419 // Make sure RPCs all go to the correct backend.
1420 EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
1421 EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
1422 EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
1423 const int weight_75_request_count =
1424 backends_[1]->backend_service1()->request_count();
1425 EXPECT_EQ(0, backends_[1]->backend_service2()->request_count());
1426 EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
1427 const int weight_25_request_count =
1428 backends_[2]->backend_service1()->request_count();
1429 EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
1430 EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
1431 gpr_log(GPR_INFO, "target_75 received %d rpcs and target_25 received %d rpcs",
1432 weight_75_request_count, weight_25_request_count);
1433 EXPECT_THAT(static_cast<double>(weight_75_request_count) / kNumEcho1Rpcs7525,
1434 ::testing::DoubleNear(kWeight75Percent, kErrorTolerance));
1435 EXPECT_THAT(static_cast<double>(weight_25_request_count) / kNumEcho1Rpcs7525,
1436 ::testing::DoubleNear(kWeight25Percent, kErrorTolerance));
1437 // Change Route Configurations: same clusters different weights.
1438 weighted_cluster1->mutable_weight()->set_value(kWeight50);
1439 weighted_cluster2->mutable_weight()->set_value(kWeight50);
1440 // Change default route to a new cluster to help to identify when new
1441 // polices are seen by the client.
1442 default_route->mutable_route()->set_cluster(kNewCluster3Name);
1443 SetRouteConfiguration(balancer_.get(), new_route_config);
1444 ResetBackendCounters();
1445 WaitForAllBackends(DEBUG_LOCATION, 3, 4, /*check_status=*/nullptr,
1446 WaitForBackendOptions(),
1447 RpcOptions().set_timeout_ms(5000));
1448 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs,
1449 RpcOptions().set_timeout_ms(5000));
1450 CheckRpcSendOk(
1451 DEBUG_LOCATION, kNumEcho1Rpcs5050,
1452 RpcOptions().set_rpc_service(SERVICE_ECHO1).set_timeout_ms(5000));
1453 // Make sure RPCs all go to the correct backend.
1454 EXPECT_EQ(0, backends_[0]->backend_service()->request_count());
1455 EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
1456 EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
1457 const int weight_50_request_count_1 =
1458 backends_[1]->backend_service1()->request_count();
1459 EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
1460 const int weight_50_request_count_2 =
1461 backends_[2]->backend_service1()->request_count();
1462 EXPECT_EQ(kNumEchoRpcs, backends_[3]->backend_service()->request_count());
1463 EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
1464 EXPECT_THAT(
1465 static_cast<double>(weight_50_request_count_1) / kNumEcho1Rpcs5050,
1466 ::testing::DoubleNear(kWeight50Percent, kErrorTolerance));
1467 EXPECT_THAT(
1468 static_cast<double>(weight_50_request_count_2) / kNumEcho1Rpcs5050,
1469 ::testing::DoubleNear(kWeight50Percent, kErrorTolerance));
1470 }
1471
1472 TEST_P(LdsRdsTest, XdsRoutingWeightedClusterUpdateClusters) {
1473 CreateAndStartBackends(4);
1474 const char* kNewCluster1Name = "new_cluster_1";
1475 const char* kNewEdsService1Name = "new_eds_service_name_1";
1476 const char* kNewCluster2Name = "new_cluster_2";
1477 const char* kNewEdsService2Name = "new_eds_service_name_2";
1478 const char* kNewCluster3Name = "new_cluster_3";
1479 const char* kNewEdsService3Name = "new_eds_service_name_3";
1480 const size_t kNumEchoRpcs = 10;
1481 const size_t kWeight75 = 75;
1482 const size_t kWeight25 = 25;
1483 const size_t kWeight50 = 50;
1484 const double kErrorTolerance = 0.05;
1485 const double kWeight75Percent = static_cast<double>(kWeight75) / 100;
1486 const double kWeight25Percent = static_cast<double>(kWeight25) / 100;
1487 const double kWeight50Percent = static_cast<double>(kWeight50) / 100;
1488 const size_t kNumEcho1Rpcs7525 =
1489 ComputeIdealNumRpcs(kWeight75Percent, kErrorTolerance);
1490 const size_t kNumEcho1Rpcs5050 =
1491 ComputeIdealNumRpcs(kWeight50Percent, kErrorTolerance);
1492 // Populate new EDS resources.
1493 EdsResourceArgs args({
1494 {"locality0", CreateEndpointsForBackends(0, 1)},
1495 });
1496 EdsResourceArgs args1({
1497 {"locality0", CreateEndpointsForBackends(1, 2)},
1498 });
1499 EdsResourceArgs args2({
1500 {"locality0", CreateEndpointsForBackends(2, 3)},
1501 });
1502 EdsResourceArgs args3({
1503 {"locality0", CreateEndpointsForBackends(3, 4)},
1504 });
1505 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1506 balancer_->ads_service()->SetEdsResource(
1507 BuildEdsResource(args1, kNewEdsService1Name));
1508 balancer_->ads_service()->SetEdsResource(
1509 BuildEdsResource(args2, kNewEdsService2Name));
1510 balancer_->ads_service()->SetEdsResource(
1511 BuildEdsResource(args3, kNewEdsService3Name));
1512 // Populate new CDS resources.
1513 Cluster new_cluster1 = default_cluster_;
1514 new_cluster1.set_name(kNewCluster1Name);
1515 new_cluster1.mutable_eds_cluster_config()->set_service_name(
1516 kNewEdsService1Name);
1517 balancer_->ads_service()->SetCdsResource(new_cluster1);
1518 Cluster new_cluster2 = default_cluster_;
1519 new_cluster2.set_name(kNewCluster2Name);
1520 new_cluster2.mutable_eds_cluster_config()->set_service_name(
1521 kNewEdsService2Name);
1522 balancer_->ads_service()->SetCdsResource(new_cluster2);
1523 Cluster new_cluster3 = default_cluster_;
1524 new_cluster3.set_name(kNewCluster3Name);
1525 new_cluster3.mutable_eds_cluster_config()->set_service_name(
1526 kNewEdsService3Name);
1527 balancer_->ads_service()->SetCdsResource(new_cluster3);
1528 // Populating Route Configurations.
1529 RouteConfiguration new_route_config = default_route_config_;
1530 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
1531 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
1532 auto* weighted_cluster1 =
1533 route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
1534 weighted_cluster1->set_name(kNewCluster1Name);
1535 weighted_cluster1->mutable_weight()->set_value(kWeight75);
1536 auto* weighted_cluster2 =
1537 route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
1538 weighted_cluster2->set_name(kDefaultClusterName);
1539 weighted_cluster2->mutable_weight()->set_value(kWeight25);
1540 route1->mutable_route()
1541 ->mutable_weighted_clusters()
1542 ->mutable_total_weight()
1543 ->set_value(kWeight75 + kWeight25);
1544 auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
1545 default_route->mutable_match()->set_prefix("");
1546 default_route->mutable_route()->set_cluster(kDefaultClusterName);
1547 SetRouteConfiguration(balancer_.get(), new_route_config);
1548 WaitForBackend(DEBUG_LOCATION, 0);
1549 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
1550 WaitForBackendOptions(),
1551 RpcOptions().set_rpc_service(SERVICE_ECHO1));
1552 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs);
1553 CheckRpcSendOk(DEBUG_LOCATION, kNumEcho1Rpcs7525,
1554 RpcOptions().set_rpc_service(SERVICE_ECHO1));
1555 // Make sure RPCs all go to the correct backend.
1556 EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
1557 int weight_25_request_count =
1558 backends_[0]->backend_service1()->request_count();
1559 EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
1560 int weight_75_request_count =
1561 backends_[1]->backend_service1()->request_count();
1562 EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
1563 EXPECT_EQ(0, backends_[2]->backend_service1()->request_count());
1564 EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
1565 EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
1566 gpr_log(GPR_INFO, "target_75 received %d rpcs and target_25 received %d rpcs",
1567 weight_75_request_count, weight_25_request_count);
1568 EXPECT_THAT(static_cast<double>(weight_75_request_count) / kNumEcho1Rpcs7525,
1569 ::testing::DoubleNear(kWeight75Percent, kErrorTolerance));
1570 EXPECT_THAT(static_cast<double>(weight_25_request_count) / kNumEcho1Rpcs7525,
1571 ::testing::DoubleNear(kWeight25Percent, kErrorTolerance));
1572 // Change Route Configurations: new set of clusters with different weights.
1573 weighted_cluster1->mutable_weight()->set_value(kWeight50);
1574 weighted_cluster2->set_name(kNewCluster2Name);
1575 weighted_cluster2->mutable_weight()->set_value(kWeight50);
1576 SetRouteConfiguration(balancer_.get(), new_route_config);
1577 ResetBackendCounters();
1578 WaitForBackend(DEBUG_LOCATION, 2, /*check_status=*/nullptr,
1579 WaitForBackendOptions(),
1580 RpcOptions().set_rpc_service(SERVICE_ECHO1));
1581 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs);
1582 CheckRpcSendOk(DEBUG_LOCATION, kNumEcho1Rpcs5050,
1583 RpcOptions().set_rpc_service(SERVICE_ECHO1));
1584 // Make sure RPCs all go to the correct backend.
1585 EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
1586 EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
1587 EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
1588 const int weight_50_request_count_1 =
1589 backends_[1]->backend_service1()->request_count();
1590 EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
1591 const int weight_50_request_count_2 =
1592 backends_[2]->backend_service1()->request_count();
1593 EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
1594 EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
1595 EXPECT_THAT(
1596 static_cast<double>(weight_50_request_count_1) / kNumEcho1Rpcs5050,
1597 ::testing::DoubleNear(kWeight50Percent, kErrorTolerance));
1598 EXPECT_THAT(
1599 static_cast<double>(weight_50_request_count_2) / kNumEcho1Rpcs5050,
1600 ::testing::DoubleNear(kWeight50Percent, kErrorTolerance));
1601 // Change Route Configurations.
1602 weighted_cluster1->mutable_weight()->set_value(kWeight75);
1603 weighted_cluster2->set_name(kNewCluster3Name);
1604 weighted_cluster2->mutable_weight()->set_value(kWeight25);
1605 SetRouteConfiguration(balancer_.get(), new_route_config);
1606 ResetBackendCounters();
1607 WaitForBackend(DEBUG_LOCATION, 3, /*check_status=*/nullptr,
1608 WaitForBackendOptions(),
1609 RpcOptions().set_rpc_service(SERVICE_ECHO1));
1610 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs);
1611 CheckRpcSendOk(DEBUG_LOCATION, kNumEcho1Rpcs7525,
1612 RpcOptions().set_rpc_service(SERVICE_ECHO1));
1613 // Make sure RPCs all go to the correct backend.
1614 EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
1615 EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
1616 EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
1617 weight_75_request_count = backends_[1]->backend_service1()->request_count();
1618 EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
1619 EXPECT_EQ(0, backends_[2]->backend_service1()->request_count());
1620 EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
1621 weight_25_request_count = backends_[3]->backend_service1()->request_count();
1622 gpr_log(GPR_INFO, "target_75 received %d rpcs and target_25 received %d rpcs",
1623 weight_75_request_count, weight_25_request_count);
1624 EXPECT_THAT(static_cast<double>(weight_75_request_count) / kNumEcho1Rpcs7525,
1625 ::testing::DoubleNear(kWeight75Percent, kErrorTolerance));
1626 EXPECT_THAT(static_cast<double>(weight_25_request_count) / kNumEcho1Rpcs7525,
1627 ::testing::DoubleNear(kWeight25Percent, kErrorTolerance));
1628 }
1629
1630 TEST_P(LdsRdsTest, XdsRoutingClusterUpdateClusters) {
1631 CreateAndStartBackends(2);
1632 const char* kNewClusterName = "new_cluster";
1633 const char* kNewEdsServiceName = "new_eds_service_name";
1634 const size_t kNumEchoRpcs = 5;
1635 // Populate new EDS resources.
1636 EdsResourceArgs args({
1637 {"locality0", CreateEndpointsForBackends(0, 1)},
1638 });
1639 EdsResourceArgs args1({
1640 {"locality0", CreateEndpointsForBackends(1, 2)},
1641 });
1642 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1643 balancer_->ads_service()->SetEdsResource(
1644 BuildEdsResource(args1, kNewEdsServiceName));
1645 // Populate new CDS resources.
1646 Cluster new_cluster = default_cluster_;
1647 new_cluster.set_name(kNewClusterName);
1648 new_cluster.mutable_eds_cluster_config()->set_service_name(
1649 kNewEdsServiceName);
1650 balancer_->ads_service()->SetCdsResource(new_cluster);
1651 // Send Route Configuration.
1652 RouteConfiguration new_route_config = default_route_config_;
1653 SetRouteConfiguration(balancer_.get(), new_route_config);
1654 WaitForAllBackends(DEBUG_LOCATION, 0, 1);
1655 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs);
1656 // Make sure RPCs all go to the correct backend.
1657 EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
1658 // Change Route Configurations: new default cluster.
1659 auto* default_route =
1660 new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
1661 default_route->mutable_route()->set_cluster(kNewClusterName);
1662 SetRouteConfiguration(balancer_.get(), new_route_config);
1663 WaitForAllBackends(DEBUG_LOCATION, 1, 2);
1664 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs);
1665 // Make sure RPCs all go to the correct backend.
1666 EXPECT_EQ(kNumEchoRpcs, backends_[1]->backend_service()->request_count());
1667 }
1668
1669 TEST_P(LdsRdsTest, XdsRoutingClusterUpdateClustersWithPickingDelays) {
1670 // Start with only backend 1 up, but the default cluster pointing to
1671 // backend 0, which is down.
1672 CreateBackends(2);
1673 StartBackend(1);
1674 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}});
1675 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1676 // Start an RPC with wait_for_ready=true and no deadline. This will
1677 // stay pending until backend 0 is reachable.
1678 LongRunningRpc rpc;
1679 rpc.StartRpc(stub_.get(),
1680 RpcOptions().set_wait_for_ready(true).set_timeout_ms(0));
1681 // Send a non-wait_for_ready RPC, which should fail. This tells us
1682 // that the client has received the update and attempted to connect.
1683 constexpr char kErrorMessageRegex[] =
1684 "connections to all backends failing; last error: "
1685 "(UNKNOWN: (ipv6:%5B::1%5D|ipv4:127.0.0.1):[0-9]+: "
1686 "Failed to connect to remote host: Connection refused|"
1687 "UNAVAILABLE: (ipv6:%5B::1%5D|ipv4:127.0.0.1):[0-9]+: "
1688 "Failed to connect to remote host: FD shutdown)";
1689 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::UNAVAILABLE,
1690 kErrorMessageRegex);
1691 // Now create a new cluster, pointing to backend 1.
1692 const char* kNewClusterName = "new_cluster";
1693 const char* kNewEdsServiceName = "new_eds_service_name";
1694 EdsResourceArgs args1({{"locality0", CreateEndpointsForBackends(1, 2)}});
1695 balancer_->ads_service()->SetEdsResource(
1696 BuildEdsResource(args1, kNewEdsServiceName));
1697 // Populate new CDS resources.
1698 Cluster new_cluster = default_cluster_;
1699 new_cluster.set_name(kNewClusterName);
1700 new_cluster.mutable_eds_cluster_config()->set_service_name(
1701 kNewEdsServiceName);
1702 balancer_->ads_service()->SetCdsResource(new_cluster);
1703 // Send a update RouteConfiguration to use backend 1.
1704 RouteConfiguration new_route_config = default_route_config_;
1705 auto* default_route =
1706 new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
1707 default_route->mutable_route()->set_cluster(kNewClusterName);
1708 SetRouteConfiguration(balancer_.get(), new_route_config);
1709 // Wait for RPCs to go to the new backend: 1, this ensures that the client
1710 // has processed the update.
1711 WaitForBackend(
1712 DEBUG_LOCATION, 1,
1713 [&](const RpcResult& result) {
1714 if (!result.status.ok()) {
1715 EXPECT_EQ(result.status.error_code(), StatusCode::UNAVAILABLE);
1716 EXPECT_THAT(result.status.error_message(),
1717 ::testing::MatchesRegex(kErrorMessageRegex));
1718 }
1719 },
1720 WaitForBackendOptions().set_reset_counters(false));
1721 // Bring up the backend 0. Yhis will allow the delayed RPC to finally
1722 // complete.
1723 StartBackend(0);
1724 Status status = rpc.GetStatus();
1725 EXPECT_TRUE(status.ok()) << "code=" << status.error_code()
1726 << " message=" << status.error_message();
1727 // Make sure RPCs went to the correct backends.
1728 EXPECT_EQ(1, backends_[0]->backend_service()->request_count());
1729 EXPECT_EQ(1, backends_[1]->backend_service()->request_count());
1730 }
1731
1732 TEST_P(LdsRdsTest, XdsRoutingApplyXdsTimeout) {
1733 const auto kTimeoutGrpcHeaderMax = grpc_core::Duration::Milliseconds(1500);
1734 const auto kTimeoutMaxStreamDuration =
1735 grpc_core::Duration::Milliseconds(2500);
1736 const auto kTimeoutHttpMaxStreamDuration =
1737 grpc_core::Duration::Milliseconds(3500);
1738 const auto kTimeoutApplication = grpc_core::Duration::Milliseconds(4500);
1739 const char* kNewCluster1Name = "new_cluster_1";
1740 const char* kNewEdsService1Name = "new_eds_service_name_1";
1741 const char* kNewCluster2Name = "new_cluster_2";
1742 const char* kNewEdsService2Name = "new_eds_service_name_2";
1743 const char* kNewCluster3Name = "new_cluster_3";
1744 const char* kNewEdsService3Name = "new_eds_service_name_3";
1745 // Populate new EDS resources.
1746 EdsResourceArgs args({{"locality0", {MakeNonExistantEndpoint()}}});
1747 EdsResourceArgs args1({{"locality0", {MakeNonExistantEndpoint()}}});
1748 EdsResourceArgs args2({{"locality0", {MakeNonExistantEndpoint()}}});
1749 EdsResourceArgs args3({{"locality0", {MakeNonExistantEndpoint()}}});
1750 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1751 balancer_->ads_service()->SetEdsResource(
1752 BuildEdsResource(args1, kNewEdsService1Name));
1753 balancer_->ads_service()->SetEdsResource(
1754 BuildEdsResource(args2, kNewEdsService2Name));
1755 balancer_->ads_service()->SetEdsResource(
1756 BuildEdsResource(args3, kNewEdsService3Name));
1757 // Populate new CDS resources.
1758 Cluster new_cluster1 = default_cluster_;
1759 new_cluster1.set_name(kNewCluster1Name);
1760 new_cluster1.mutable_eds_cluster_config()->set_service_name(
1761 kNewEdsService1Name);
1762 balancer_->ads_service()->SetCdsResource(new_cluster1);
1763 Cluster new_cluster2 = default_cluster_;
1764 new_cluster2.set_name(kNewCluster2Name);
1765 new_cluster2.mutable_eds_cluster_config()->set_service_name(
1766 kNewEdsService2Name);
1767 balancer_->ads_service()->SetCdsResource(new_cluster2);
1768 Cluster new_cluster3 = default_cluster_;
1769 new_cluster3.set_name(kNewCluster3Name);
1770 new_cluster3.mutable_eds_cluster_config()->set_service_name(
1771 kNewEdsService3Name);
1772 balancer_->ads_service()->SetCdsResource(new_cluster3);
1773 // Construct listener.
1774 auto listener = default_listener_;
1775 HttpConnectionManager http_connection_manager;
1776 listener.mutable_api_listener()->mutable_api_listener()->UnpackTo(
1777 &http_connection_manager);
1778 // Set up HTTP max_stream_duration of 3.5 seconds
1779 SetProtoDuration(
1780 kTimeoutHttpMaxStreamDuration,
1781 http_connection_manager.mutable_common_http_protocol_options()
1782 ->mutable_max_stream_duration());
1783 listener.mutable_api_listener()->mutable_api_listener()->PackFrom(
1784 http_connection_manager);
1785 // Construct route config.
1786 RouteConfiguration new_route_config = default_route_config_;
1787 // route 1: Set max_stream_duration of 2.5 seconds, Set
1788 // grpc_timeout_header_max of 1.5
1789 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
1790 route1->mutable_match()->set_path("/grpc.testing.EchoTest1Service/Echo1");
1791 route1->mutable_route()->set_cluster(kNewCluster1Name);
1792 auto* max_stream_duration =
1793 route1->mutable_route()->mutable_max_stream_duration();
1794 SetProtoDuration(kTimeoutMaxStreamDuration,
1795 max_stream_duration->mutable_max_stream_duration());
1796 SetProtoDuration(kTimeoutGrpcHeaderMax,
1797 max_stream_duration->mutable_grpc_timeout_header_max());
1798 // route 2: Set max_stream_duration of 2.5 seconds
1799 auto* route2 = new_route_config.mutable_virtual_hosts(0)->add_routes();
1800 route2->mutable_match()->set_path("/grpc.testing.EchoTest2Service/Echo2");
1801 route2->mutable_route()->set_cluster(kNewCluster2Name);
1802 max_stream_duration = route2->mutable_route()->mutable_max_stream_duration();
1803 SetProtoDuration(kTimeoutMaxStreamDuration,
1804 max_stream_duration->mutable_max_stream_duration());
1805 // route 3: No timeout values in route configuration
1806 auto* route3 = new_route_config.mutable_virtual_hosts(0)->add_routes();
1807 route3->mutable_match()->set_path("/grpc.testing.EchoTestService/Echo");
1808 route3->mutable_route()->set_cluster(kNewCluster3Name);
1809 // Set listener and route config.
1810 SetListenerAndRouteConfiguration(balancer_.get(), std::move(listener),
1811 new_route_config);
1812 // Test grpc_timeout_header_max of 1.5 seconds applied
1813 grpc_core::Timestamp t0 = NowFromCycleCounter();
1814 grpc_core::Timestamp t1 =
1815 t0 + (kTimeoutGrpcHeaderMax * grpc_test_slowdown_factor());
1816 grpc_core::Timestamp t2 =
1817 t0 + (kTimeoutMaxStreamDuration * grpc_test_slowdown_factor());
1818 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED,
1819 "Deadline Exceeded",
1820 RpcOptions()
1821 .set_rpc_service(SERVICE_ECHO1)
1822 .set_rpc_method(METHOD_ECHO1)
1823 .set_wait_for_ready(true)
1824 .set_timeout(kTimeoutApplication));
1825 EXPECT_THAT(NowFromCycleCounter(), AdjustedClockInRange(t1, t2));
1826 // Test max_stream_duration of 2.5 seconds applied
1827 t0 = NowFromCycleCounter();
1828 t1 = t0 + (kTimeoutMaxStreamDuration * grpc_test_slowdown_factor());
1829 t2 = t0 + (kTimeoutHttpMaxStreamDuration * grpc_test_slowdown_factor());
1830 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED,
1831 "Deadline Exceeded",
1832 RpcOptions()
1833 .set_rpc_service(SERVICE_ECHO2)
1834 .set_rpc_method(METHOD_ECHO2)
1835 .set_wait_for_ready(true)
1836 .set_timeout(kTimeoutApplication));
1837 EXPECT_THAT(NowFromCycleCounter(), AdjustedClockInRange(t1, t2));
1838 // Test http_stream_duration of 3.5 seconds applied
1839 t0 = NowFromCycleCounter();
1840 t1 = t0 + (kTimeoutHttpMaxStreamDuration * grpc_test_slowdown_factor());
1841 t2 = t0 + (kTimeoutApplication * grpc_test_slowdown_factor());
1842 CheckRpcSendFailure(
1843 DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED, "Deadline Exceeded",
1844 RpcOptions().set_wait_for_ready(true).set_timeout(kTimeoutApplication));
1845 EXPECT_THAT(NowFromCycleCounter(), AdjustedClockInRange(t1, t2));
1846 }
1847
1848 TEST_P(LdsRdsTest, XdsRoutingApplyApplicationTimeoutWhenXdsTimeoutExplicit) {
1849 const auto kTimeoutMaxStreamDuration =
1850 grpc_core::Duration::Milliseconds(2500);
1851 const auto kTimeoutHttpMaxStreamDuration =
1852 grpc_core::Duration::Milliseconds(3500);
1853 const auto kTimeoutApplication = grpc_core::Duration::Milliseconds(4500);
1854 const char* kNewCluster1Name = "new_cluster_1";
1855 const char* kNewEdsService1Name = "new_eds_service_name_1";
1856 const char* kNewCluster2Name = "new_cluster_2";
1857 const char* kNewEdsService2Name = "new_eds_service_name_2";
1858 // Populate new EDS resources.
1859 EdsResourceArgs args({{"locality0", {MakeNonExistantEndpoint()}}});
1860 EdsResourceArgs args1({{"locality0", {MakeNonExistantEndpoint()}}});
1861 EdsResourceArgs args2({{"locality0", {MakeNonExistantEndpoint()}}});
1862 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1863 balancer_->ads_service()->SetEdsResource(
1864 BuildEdsResource(args1, kNewEdsService1Name));
1865 balancer_->ads_service()->SetEdsResource(
1866 BuildEdsResource(args2, kNewEdsService2Name));
1867 // Populate new CDS resources.
1868 Cluster new_cluster1 = default_cluster_;
1869 new_cluster1.set_name(kNewCluster1Name);
1870 new_cluster1.mutable_eds_cluster_config()->set_service_name(
1871 kNewEdsService1Name);
1872 balancer_->ads_service()->SetCdsResource(new_cluster1);
1873 Cluster new_cluster2 = default_cluster_;
1874 new_cluster2.set_name(kNewCluster2Name);
1875 new_cluster2.mutable_eds_cluster_config()->set_service_name(
1876 kNewEdsService2Name);
1877 balancer_->ads_service()->SetCdsResource(new_cluster2);
1878 // Construct listener.
1879 auto listener = default_listener_;
1880 HttpConnectionManager http_connection_manager;
1881 listener.mutable_api_listener()->mutable_api_listener()->UnpackTo(
1882 &http_connection_manager);
1883 // Set up HTTP max_stream_duration of 3.5 seconds
1884 SetProtoDuration(
1885 kTimeoutHttpMaxStreamDuration,
1886 http_connection_manager.mutable_common_http_protocol_options()
1887 ->mutable_max_stream_duration());
1888 listener.mutable_api_listener()->mutable_api_listener()->PackFrom(
1889 http_connection_manager);
1890 // Construct route config.
1891 RouteConfiguration new_route_config = default_route_config_;
1892 // route 1: Set max_stream_duration of 2.5 seconds, Set
1893 // grpc_timeout_header_max of 0
1894 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
1895 route1->mutable_match()->set_path("/grpc.testing.EchoTest1Service/Echo1");
1896 route1->mutable_route()->set_cluster(kNewCluster1Name);
1897 auto* max_stream_duration =
1898 route1->mutable_route()->mutable_max_stream_duration();
1899 SetProtoDuration(kTimeoutMaxStreamDuration,
1900 max_stream_duration->mutable_max_stream_duration());
1901 SetProtoDuration(grpc_core::Duration::Zero(),
1902 max_stream_duration->mutable_grpc_timeout_header_max());
1903 // route 2: Set max_stream_duration to 0
1904 auto* route2 = new_route_config.mutable_virtual_hosts(0)->add_routes();
1905 route2->mutable_match()->set_path("/grpc.testing.EchoTest2Service/Echo2");
1906 route2->mutable_route()->set_cluster(kNewCluster2Name);
1907 max_stream_duration = route2->mutable_route()->mutable_max_stream_duration();
1908 SetProtoDuration(grpc_core::Duration::Zero(),
1909 max_stream_duration->mutable_max_stream_duration());
1910 // Set listener and route config.
1911 SetListenerAndRouteConfiguration(balancer_.get(), std::move(listener),
1912 new_route_config);
1913 // Test application timeout is applied for route 1
1914 auto t0 = system_clock::now();
1915 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED,
1916 "Deadline Exceeded",
1917 RpcOptions()
1918 .set_rpc_service(SERVICE_ECHO1)
1919 .set_rpc_method(METHOD_ECHO1)
1920 .set_wait_for_ready(true)
1921 .set_timeout(kTimeoutApplication));
1922 auto elapsed_nano_seconds =
1923 std::chrono::duration_cast<std::chrono::nanoseconds>(system_clock::now() -
1924 t0);
1925 EXPECT_GT(
1926 elapsed_nano_seconds.count(),
1927 (kTimeoutApplication * grpc_test_slowdown_factor()).millis() * 1000);
1928 // Test application timeout is applied for route 2
1929 t0 = system_clock::now();
1930 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED,
1931 "Deadline Exceeded",
1932 RpcOptions()
1933 .set_rpc_service(SERVICE_ECHO2)
1934 .set_rpc_method(METHOD_ECHO2)
1935 .set_wait_for_ready(true)
1936 .set_timeout(kTimeoutApplication));
1937 elapsed_nano_seconds = std::chrono::duration_cast<std::chrono::nanoseconds>(
1938 system_clock::now() - t0);
1939 EXPECT_GT(
1940 elapsed_nano_seconds.count(),
1941 (kTimeoutApplication * grpc_test_slowdown_factor()).millis() * 1000);
1942 }
1943
1944 TEST_P(LdsRdsTest, XdsRoutingApplyApplicationTimeoutWhenHttpTimeoutExplicit) {
1945 const auto kTimeoutApplication = grpc_core::Duration::Milliseconds(4500);
1946 // Populate new EDS resources.
1947 EdsResourceArgs args({{"locality0", {MakeNonExistantEndpoint()}}});
1948 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1949 auto listener = default_listener_;
1950 HttpConnectionManager http_connection_manager;
1951 listener.mutable_api_listener()->mutable_api_listener()->UnpackTo(
1952 &http_connection_manager);
1953 // Set up HTTP max_stream_duration to be explicit 0
1954 auto* duration =
1955 http_connection_manager.mutable_common_http_protocol_options()
1956 ->mutable_max_stream_duration();
1957 duration->set_seconds(0);
1958 duration->set_nanos(0);
1959 listener.mutable_api_listener()->mutable_api_listener()->PackFrom(
1960 http_connection_manager);
1961 // Set listener and route config.
1962 SetListenerAndRouteConfiguration(balancer_.get(), std::move(listener),
1963 default_route_config_);
1964 // Test application timeout is applied for route 1
1965 auto t0 = system_clock::now();
1966 CheckRpcSendFailure(
1967 DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED, "Deadline Exceeded",
1968 RpcOptions().set_wait_for_ready(true).set_timeout(kTimeoutApplication));
1969 auto elapsed_nano_seconds =
1970 std::chrono::duration_cast<std::chrono::nanoseconds>(system_clock::now() -
1971 t0);
1972 EXPECT_GT(
1973 elapsed_nano_seconds.count(),
1974 (kTimeoutApplication * grpc_test_slowdown_factor()).millis() * 1000);
1975 }
1976
1977 // Test to ensure application-specified deadline won't be affected when
1978 // the xDS config does not specify a timeout.
1979 TEST_P(LdsRdsTest, XdsRoutingWithOnlyApplicationTimeout) {
1980 const auto kTimeoutApplication = grpc_core::Duration::Milliseconds(4500);
1981 // Populate new EDS resources.
1982 EdsResourceArgs args({{"locality0", {MakeNonExistantEndpoint()}}});
1983 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1984 auto t0 = system_clock::now();
1985 CheckRpcSendFailure(
1986 DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED, "Deadline Exceeded",
1987 RpcOptions().set_wait_for_ready(true).set_timeout(kTimeoutApplication));
1988 auto elapsed_nano_seconds =
1989 std::chrono::duration_cast<std::chrono::nanoseconds>(system_clock::now() -
1990 t0);
1991 EXPECT_GT(
1992 elapsed_nano_seconds.count(),
1993 (kTimeoutApplication * grpc_test_slowdown_factor()).millis() * 1000);
1994 }
1995
1996 TEST_P(LdsRdsTest, XdsRetryPolicyNumRetries) {
1997 CreateAndStartBackends(1);
1998 const size_t kNumRetries = 3;
1999 // Populate new EDS resources.
2000 EdsResourceArgs args({
2001 {"locality0", CreateEndpointsForBackends(0, 1)},
2002 });
2003 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2004 // Construct route config to set retry policy.
2005 RouteConfiguration new_route_config = default_route_config_;
2006 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
2007 auto* retry_policy = route1->mutable_route()->mutable_retry_policy();
2008 retry_policy->set_retry_on(
2009 "5xx,cancelled,deadline-exceeded,internal,resource-exhausted,"
2010 "unavailable");
2011 retry_policy->mutable_num_retries()->set_value(kNumRetries);
2012 SetRouteConfiguration(balancer_.get(), new_route_config);
2013 // Ensure we retried the correct number of times on all supported status.
2014 CheckRpcSendFailure(
2015 DEBUG_LOCATION, StatusCode::CANCELLED, "",
2016 RpcOptions().set_server_expected_error(StatusCode::CANCELLED));
2017 EXPECT_EQ(kNumRetries + 1, backends_[0]->backend_service()->request_count());
2018 ResetBackendCounters();
2019 CheckRpcSendFailure(
2020 DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED, "",
2021 RpcOptions().set_server_expected_error(StatusCode::DEADLINE_EXCEEDED));
2022 EXPECT_EQ(kNumRetries + 1, backends_[0]->backend_service()->request_count());
2023 ResetBackendCounters();
2024 CheckRpcSendFailure(
2025 DEBUG_LOCATION, StatusCode::INTERNAL, "",
2026 RpcOptions().set_server_expected_error(StatusCode::INTERNAL));
2027 EXPECT_EQ(kNumRetries + 1, backends_[0]->backend_service()->request_count());
2028 ResetBackendCounters();
2029 CheckRpcSendFailure(
2030 DEBUG_LOCATION, StatusCode::RESOURCE_EXHAUSTED, "",
2031 RpcOptions().set_server_expected_error(StatusCode::RESOURCE_EXHAUSTED));
2032 EXPECT_EQ(kNumRetries + 1, backends_[0]->backend_service()->request_count());
2033 ResetBackendCounters();
2034 CheckRpcSendFailure(
2035 DEBUG_LOCATION, StatusCode::UNAVAILABLE, "",
2036 RpcOptions().set_server_expected_error(StatusCode::UNAVAILABLE));
2037 EXPECT_EQ(kNumRetries + 1, backends_[0]->backend_service()->request_count());
2038 ResetBackendCounters();
2039 // Ensure we don't retry on an unsupported status.
2040 CheckRpcSendFailure(
2041 DEBUG_LOCATION, StatusCode::UNAUTHENTICATED, "",
2042 RpcOptions().set_server_expected_error(StatusCode::UNAUTHENTICATED));
2043 EXPECT_EQ(1, backends_[0]->backend_service()->request_count());
2044 }
2045
2046 TEST_P(LdsRdsTest, XdsRetryPolicyAtVirtualHostLevel) {
2047 CreateAndStartBackends(1);
2048 const size_t kNumRetries = 3;
2049 // Populate new EDS resources.
2050 EdsResourceArgs args({
2051 {"locality0", CreateEndpointsForBackends(0, 1)},
2052 });
2053 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2054 // Construct route config to set retry policy.
2055 RouteConfiguration new_route_config = default_route_config_;
2056 auto* retry_policy =
2057 new_route_config.mutable_virtual_hosts(0)->mutable_retry_policy();
2058 retry_policy->set_retry_on(
2059 "cancelled,deadline-exceeded,internal,resource-exhausted,unavailable");
2060 retry_policy->mutable_num_retries()->set_value(kNumRetries);
2061 SetRouteConfiguration(balancer_.get(), new_route_config);
2062 // Ensure we retried the correct number of times on a supported status.
2063 CheckRpcSendFailure(
2064 DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED, "",
2065 RpcOptions().set_server_expected_error(StatusCode::DEADLINE_EXCEEDED));
2066 EXPECT_EQ(kNumRetries + 1, backends_[0]->backend_service()->request_count());
2067 }
2068
2069 TEST_P(LdsRdsTest, XdsRetryPolicyLongBackOff) {
2070 CreateAndStartBackends(1);
2071 // Set num retries to 3, but due to longer back off, we expect only 1 retry
2072 // will take place.
2073 const size_t kNumRetries = 3;
2074 // Populate new EDS resources.
2075 EdsResourceArgs args({
2076 {"locality0", CreateEndpointsForBackends(0, 1)},
2077 });
2078 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2079 // Construct route config to set retry policy.
2080 RouteConfiguration new_route_config = default_route_config_;
2081 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
2082 auto* retry_policy = route1->mutable_route()->mutable_retry_policy();
2083 retry_policy->set_retry_on(
2084 "5xx,cancelled,deadline-exceeded,internal,resource-exhausted,"
2085 "unavailable");
2086 retry_policy->mutable_num_retries()->set_value(kNumRetries);
2087 // Set backoff to 1 second, 1/2 of rpc timeout of 2 second.
2088 SetProtoDuration(
2089 grpc_core::Duration::Seconds(1),
2090 retry_policy->mutable_retry_back_off()->mutable_base_interval());
2091 SetRouteConfiguration(balancer_.get(), new_route_config);
2092 // No need to set max interval and just let it be the default of 10x of base.
2093 // We expect 1 retry before the RPC times out with DEADLINE_EXCEEDED.
2094 CheckRpcSendFailure(
2095 DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED, "Deadline Exceeded",
2096 RpcOptions().set_timeout_ms(2500).set_server_expected_error(
2097 StatusCode::CANCELLED));
2098 EXPECT_EQ(1 + 1, backends_[0]->backend_service()->request_count());
2099 }
2100
2101 TEST_P(LdsRdsTest, XdsRetryPolicyMaxBackOff) {
2102 CreateAndStartBackends(1);
2103 // Set num retries to 3, but due to longer back off, we expect only 2 retry
2104 // will take place, while the 2nd one will obey the max backoff.
2105 const size_t kNumRetries = 3;
2106 // Populate new EDS resources.
2107 EdsResourceArgs args({
2108 {"locality0", CreateEndpointsForBackends(0, 1)},
2109 });
2110 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2111 // Construct route config to set retry policy.
2112 RouteConfiguration new_route_config = default_route_config_;
2113 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
2114 auto* retry_policy = route1->mutable_route()->mutable_retry_policy();
2115 retry_policy->set_retry_on(
2116 "5xx,cancelled,deadline-exceeded,internal,resource-exhausted,"
2117 "unavailable");
2118 retry_policy->mutable_num_retries()->set_value(kNumRetries);
2119 // Set backoff to 1 second.
2120 SetProtoDuration(
2121 grpc_core::Duration::Seconds(1),
2122 retry_policy->mutable_retry_back_off()->mutable_base_interval());
2123 // Set max interval to be the same as base, so 2 retries will take 2 seconds
2124 // and both retries will take place before the 2.5 seconds rpc timeout.
2125 // Tested to ensure if max is not set, this test will be the same as
2126 // XdsRetryPolicyLongBackOff and we will only see 1 retry in that case.
2127 SetProtoDuration(
2128 grpc_core::Duration::Seconds(1),
2129 retry_policy->mutable_retry_back_off()->mutable_max_interval());
2130 SetRouteConfiguration(balancer_.get(), new_route_config);
2131 // Send an initial RPC to make sure we get connected (we don't want
2132 // the channel startup time to affect the retry timing).
2133 CheckRpcSendOk(DEBUG_LOCATION);
2134 ResetBackendCounters();
2135 // We expect 2 retry before the RPC times out with DEADLINE_EXCEEDED.
2136 CheckRpcSendFailure(
2137 DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED, "Deadline Exceeded",
2138 RpcOptions().set_timeout_ms(2500).set_server_expected_error(
2139 StatusCode::CANCELLED));
2140 EXPECT_EQ(2 + 1, backends_[0]->backend_service()->request_count());
2141 }
2142
2143 TEST_P(LdsRdsTest, XdsRetryPolicyUnsupportedStatusCode) {
2144 CreateAndStartBackends(1);
2145 const size_t kNumRetries = 3;
2146 // Populate new EDS resources.
2147 EdsResourceArgs args({
2148 {"locality0", CreateEndpointsForBackends(0, 1)},
2149 });
2150 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2151 // Construct route config to set retry policy.
2152 RouteConfiguration new_route_config = default_route_config_;
2153 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
2154 auto* retry_policy = route1->mutable_route()->mutable_retry_policy();
2155 retry_policy->set_retry_on("5xx");
2156 retry_policy->mutable_num_retries()->set_value(kNumRetries);
2157 SetRouteConfiguration(balancer_.get(), new_route_config);
2158 // We expect no retry.
2159 CheckRpcSendFailure(
2160 DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED, "",
2161 RpcOptions().set_server_expected_error(StatusCode::DEADLINE_EXCEEDED));
2162 EXPECT_EQ(1, backends_[0]->backend_service()->request_count());
2163 }
2164
2165 TEST_P(LdsRdsTest,
2166 XdsRetryPolicyUnsupportedStatusCodeWithVirtualHostLevelRetry) {
2167 CreateAndStartBackends(1);
2168 const size_t kNumRetries = 3;
2169 // Populate new EDS resources.
2170 EdsResourceArgs args({
2171 {"locality0", CreateEndpointsForBackends(0, 1)},
2172 });
2173 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2174 // Construct route config to set retry policy with no supported retry_on
2175 // statuses.
2176 RouteConfiguration new_route_config = default_route_config_;
2177 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
2178 auto* retry_policy = route1->mutable_route()->mutable_retry_policy();
2179 retry_policy->set_retry_on("5xx");
2180 retry_policy->mutable_num_retries()->set_value(kNumRetries);
2181 // Construct a virtual host level retry policy with supported statuses.
2182 auto* virtual_host_retry_policy =
2183 new_route_config.mutable_virtual_hosts(0)->mutable_retry_policy();
2184 virtual_host_retry_policy->set_retry_on(
2185 "cancelled,deadline-exceeded,internal,resource-exhausted,unavailable");
2186 virtual_host_retry_policy->mutable_num_retries()->set_value(kNumRetries);
2187 SetRouteConfiguration(balancer_.get(), new_route_config);
2188 // We expect no retry.
2189 CheckRpcSendFailure(
2190 DEBUG_LOCATION, StatusCode::DEADLINE_EXCEEDED, "",
2191 RpcOptions().set_server_expected_error(StatusCode::DEADLINE_EXCEEDED));
2192 EXPECT_EQ(1, backends_[0]->backend_service()->request_count());
2193 }
2194
2195 TEST_P(LdsRdsTest, XdsRetryPolicyInvalidNumRetriesZero) {
2196 CreateAndStartBackends(1);
2197 // Populate new EDS resources.
2198 EdsResourceArgs args({
2199 {"locality0", CreateEndpointsForBackends(0, 1)},
2200 });
2201 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2202 // Construct route config to set retry policy.
2203 RouteConfiguration new_route_config = default_route_config_;
2204 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
2205 auto* retry_policy = route1->mutable_route()->mutable_retry_policy();
2206 retry_policy->set_retry_on("deadline-exceeded");
2207 // Setting num_retries to zero is not valid.
2208 retry_policy->mutable_num_retries()->set_value(0);
2209 SetRouteConfiguration(balancer_.get(), new_route_config);
2210 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2211 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2212 EXPECT_THAT(
2213 response_state->error_message,
2214 ::testing::HasSubstr(
2215 "RouteAction RetryPolicy num_retries set to invalid value 0."));
2216 }
2217
2218 TEST_P(LdsRdsTest, XdsRetryPolicyRetryBackOffMissingBaseInterval) {
2219 CreateAndStartBackends(1);
2220 // Populate new EDS resources.
2221 EdsResourceArgs args({
2222 {"locality0", CreateEndpointsForBackends(0, 1)},
2223 });
2224 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2225 // Construct route config to set retry policy.
2226 RouteConfiguration new_route_config = default_route_config_;
2227 auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
2228 auto* retry_policy = route1->mutable_route()->mutable_retry_policy();
2229 retry_policy->set_retry_on("deadline-exceeded");
2230 retry_policy->mutable_num_retries()->set_value(1);
2231 // RetryBackoff is there but base interval is missing.
2232 SetProtoDuration(
2233 grpc_core::Duration::Milliseconds(250),
2234 retry_policy->mutable_retry_back_off()->mutable_max_interval());
2235 SetRouteConfiguration(balancer_.get(), new_route_config);
2236 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2237 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2238 EXPECT_THAT(
2239 response_state->error_message,
2240 ::testing::HasSubstr(
2241 "RouteAction RetryPolicy RetryBackoff missing base interval."));
2242 }
2243
2244 TEST_P(LdsRdsTest, XdsRoutingHeadersMatching) {
2245 CreateAndStartBackends(2);
2246 const char* kNewClusterName = "new_cluster";
2247 const char* kNewEdsServiceName = "new_eds_service_name";
2248 const size_t kNumEcho1Rpcs = 100;
2249 const size_t kNumEchoRpcs = 5;
2250 // Populate new EDS resources.
2251 EdsResourceArgs args({
2252 {"locality0", CreateEndpointsForBackends(0, 1)},
2253 });
2254 EdsResourceArgs args1({
2255 {"locality0", CreateEndpointsForBackends(1, 2)},
2256 });
2257 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2258 balancer_->ads_service()->SetEdsResource(
2259 BuildEdsResource(args1, kNewEdsServiceName));
2260 // Populate new CDS resources.
2261 Cluster new_cluster = default_cluster_;
2262 new_cluster.set_name(kNewClusterName);
2263 new_cluster.mutable_eds_cluster_config()->set_service_name(
2264 kNewEdsServiceName);
2265 balancer_->ads_service()->SetCdsResource(new_cluster);
2266 // Populating Route Configurations for LDS.
2267 RouteConfiguration route_config = default_route_config_;
2268 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
2269 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
2270 auto* header_matcher1 = route1->mutable_match()->add_headers();
2271 header_matcher1->set_name("header1");
2272 header_matcher1->set_exact_match("POST,PUT,GET");
2273 auto* header_matcher2 = route1->mutable_match()->add_headers();
2274 header_matcher2->set_name("header2");
2275 header_matcher2->mutable_safe_regex_match()->set_regex("[a-z]*");
2276 auto* header_matcher3 = route1->mutable_match()->add_headers();
2277 header_matcher3->set_name("header3");
2278 header_matcher3->mutable_range_match()->set_start(1);
2279 header_matcher3->mutable_range_match()->set_end(1000);
2280 auto* header_matcher4 = route1->mutable_match()->add_headers();
2281 header_matcher4->set_name("header4");
2282 header_matcher4->set_present_match(false);
2283 auto* header_matcher5 = route1->mutable_match()->add_headers();
2284 header_matcher5->set_name("header5");
2285 header_matcher5->set_present_match(true);
2286 auto* header_matcher6 = route1->mutable_match()->add_headers();
2287 header_matcher6->set_name("header6");
2288 header_matcher6->set_prefix_match("/grpc");
2289 auto* header_matcher7 = route1->mutable_match()->add_headers();
2290 header_matcher7->set_name("header7");
2291 header_matcher7->set_suffix_match(".cc");
2292 header_matcher7->set_invert_match(true);
2293 route1->mutable_route()->set_cluster(kNewClusterName);
2294 auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
2295 default_route->mutable_match()->set_prefix("");
2296 default_route->mutable_route()->set_cluster(kDefaultClusterName);
2297 SetRouteConfiguration(balancer_.get(), route_config);
2298 std::vector<std::pair<std::string, std::string>> metadata = {
2299 {"header1", "POST"},
2300 {"header2", "blah"},
2301 {"header3", "1"},
2302 {"header5", "anything"},
2303 {"header6", "/grpc.testing.EchoTest1Service/"},
2304 {"header1", "PUT"},
2305 {"header7", "grpc.java"},
2306 {"header1", "GET"},
2307 };
2308 const auto header_match_rpc_options = RpcOptions()
2309 .set_rpc_service(SERVICE_ECHO1)
2310 .set_rpc_method(METHOD_ECHO1)
2311 .set_metadata(std::move(metadata));
2312 // Make sure all backends are up.
2313 WaitForBackend(DEBUG_LOCATION, 0);
2314 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
2315 WaitForBackendOptions(), header_match_rpc_options);
2316 // Send RPCs.
2317 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs);
2318 CheckRpcSendOk(DEBUG_LOCATION, kNumEcho1Rpcs, header_match_rpc_options);
2319 EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
2320 EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
2321 EXPECT_EQ(0, backends_[0]->backend_service2()->request_count());
2322 EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
2323 EXPECT_EQ(kNumEcho1Rpcs, backends_[1]->backend_service1()->request_count());
2324 EXPECT_EQ(0, backends_[1]->backend_service2()->request_count());
2325 auto response_state = RouteConfigurationResponseState(balancer_.get());
2326 ASSERT_TRUE(response_state.has_value());
2327 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
2328 }
2329
2330 TEST_P(LdsRdsTest, XdsRoutingHeadersMatchingSpecialHeaderContentType) {
2331 CreateAndStartBackends(2);
2332 const char* kNewClusterName = "new_cluster";
2333 const char* kNewEdsServiceName = "new_eds_service_name";
2334 const size_t kNumEchoRpcs = 100;
2335 // Populate new EDS resources.
2336 EdsResourceArgs args({
2337 {"locality0", CreateEndpointsForBackends(0, 1)},
2338 });
2339 EdsResourceArgs args1({
2340 {"locality0", CreateEndpointsForBackends(1, 2)},
2341 });
2342 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2343 balancer_->ads_service()->SetEdsResource(
2344 BuildEdsResource(args1, kNewEdsServiceName));
2345 // Populate new CDS resources.
2346 Cluster new_cluster = default_cluster_;
2347 new_cluster.set_name(kNewClusterName);
2348 new_cluster.mutable_eds_cluster_config()->set_service_name(
2349 kNewEdsServiceName);
2350 balancer_->ads_service()->SetCdsResource(new_cluster);
2351 // Populating Route Configurations for LDS.
2352 RouteConfiguration route_config = default_route_config_;
2353 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
2354 route1->mutable_match()->set_prefix("");
2355 auto* header_matcher1 = route1->mutable_match()->add_headers();
2356 header_matcher1->set_name("content-type");
2357 header_matcher1->set_exact_match("notapplication/grpc");
2358 route1->mutable_route()->set_cluster(kNewClusterName);
2359 auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
2360 default_route->mutable_match()->set_prefix("");
2361 auto* header_matcher2 = default_route->mutable_match()->add_headers();
2362 header_matcher2->set_name("content-type");
2363 header_matcher2->set_exact_match("application/grpc");
2364 default_route->mutable_route()->set_cluster(kDefaultClusterName);
2365 SetRouteConfiguration(balancer_.get(), route_config);
2366 // Make sure the backend is up.
2367 WaitForAllBackends(DEBUG_LOCATION, 0, 1);
2368 // Send RPCs.
2369 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs);
2370 EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
2371 EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
2372 auto response_state = RouteConfigurationResponseState(balancer_.get());
2373 ASSERT_TRUE(response_state.has_value());
2374 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
2375 }
2376
2377 TEST_P(LdsRdsTest, XdsRoutingHeadersMatchingSpecialCasesToIgnore) {
2378 CreateAndStartBackends(2);
2379 const char* kNewCluster1Name = "new_cluster_1";
2380 const char* kNewEdsService1Name = "new_eds_service_name_1";
2381 const size_t kNumEchoRpcs = 100;
2382 // Populate new EDS resources.
2383 EdsResourceArgs args({
2384 {"locality0", CreateEndpointsForBackends(0, 1)},
2385 });
2386 EdsResourceArgs args1({
2387 {"locality0", CreateEndpointsForBackends(1, 2)},
2388 });
2389 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2390 balancer_->ads_service()->SetEdsResource(
2391 BuildEdsResource(args1, kNewEdsService1Name));
2392 // Populate new CDS resources.
2393 Cluster new_cluster1 = default_cluster_;
2394 new_cluster1.set_name(kNewCluster1Name);
2395 new_cluster1.mutable_eds_cluster_config()->set_service_name(
2396 kNewEdsService1Name);
2397 balancer_->ads_service()->SetCdsResource(new_cluster1);
2398 // Populating Route Configurations for LDS.
2399 RouteConfiguration route_config = default_route_config_;
2400 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
2401 route1->mutable_match()->set_prefix("");
2402 auto* header_matcher1 = route1->mutable_match()->add_headers();
2403 header_matcher1->set_name("grpc-foo-bin");
2404 header_matcher1->set_present_match(true);
2405 route1->mutable_route()->set_cluster(kNewCluster1Name);
2406 auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
2407 default_route->mutable_match()->set_prefix("");
2408 default_route->mutable_route()->set_cluster(kDefaultClusterName);
2409 SetRouteConfiguration(balancer_.get(), route_config);
2410 // Send headers which will mismatch each route
2411 std::vector<std::pair<std::string, std::string>> metadata = {
2412 {"grpc-foo-bin", "grpc-foo-bin"},
2413 };
2414 WaitForAllBackends(DEBUG_LOCATION, 0, 1);
2415 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs,
2416 RpcOptions().set_metadata(metadata));
2417 // Verify that only the default backend got RPCs since all previous routes
2418 // were mismatched.
2419 EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
2420 EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
2421 auto response_state = RouteConfigurationResponseState(balancer_.get());
2422 ASSERT_TRUE(response_state.has_value());
2423 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
2424 }
2425
2426 TEST_P(LdsRdsTest, XdsRoutingRuntimeFractionMatching) {
2427 CreateAndStartBackends(2);
2428 const char* kNewClusterName = "new_cluster";
2429 const char* kNewEdsServiceName = "new_eds_service_name";
2430 const double kErrorTolerance = 0.05;
2431 const size_t kRouteMatchNumerator = 25;
2432 const double kRouteMatchPercent =
2433 static_cast<double>(kRouteMatchNumerator) / 100;
2434 const size_t kNumRpcs =
2435 ComputeIdealNumRpcs(kRouteMatchPercent, kErrorTolerance);
2436 // Populate new EDS resources.
2437 EdsResourceArgs args({
2438 {"locality0", CreateEndpointsForBackends(0, 1)},
2439 });
2440 EdsResourceArgs args1({
2441 {"locality0", CreateEndpointsForBackends(1, 2)},
2442 });
2443 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2444 balancer_->ads_service()->SetEdsResource(
2445 BuildEdsResource(args1, kNewEdsServiceName));
2446 // Populate new CDS resources.
2447 Cluster new_cluster = default_cluster_;
2448 new_cluster.set_name(kNewClusterName);
2449 new_cluster.mutable_eds_cluster_config()->set_service_name(
2450 kNewEdsServiceName);
2451 balancer_->ads_service()->SetCdsResource(new_cluster);
2452 // Populating Route Configurations for LDS.
2453 RouteConfiguration route_config = default_route_config_;
2454 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
2455 route1->mutable_match()
2456 ->mutable_runtime_fraction()
2457 ->mutable_default_value()
2458 ->set_numerator(kRouteMatchNumerator);
2459 route1->mutable_route()->set_cluster(kNewClusterName);
2460 auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
2461 default_route->mutable_match()->set_prefix("");
2462 default_route->mutable_route()->set_cluster(kDefaultClusterName);
2463 SetRouteConfiguration(balancer_.get(), route_config);
2464 WaitForAllBackends(DEBUG_LOCATION, 0, 2);
2465 CheckRpcSendOk(DEBUG_LOCATION, kNumRpcs);
2466 const int default_backend_count =
2467 backends_[0]->backend_service()->request_count();
2468 const int matched_backend_count =
2469 backends_[1]->backend_service()->request_count();
2470 EXPECT_THAT(static_cast<double>(default_backend_count) / kNumRpcs,
2471 ::testing::DoubleNear(1 - kRouteMatchPercent, kErrorTolerance));
2472 EXPECT_THAT(static_cast<double>(matched_backend_count) / kNumRpcs,
2473 ::testing::DoubleNear(kRouteMatchPercent, kErrorTolerance));
2474 auto response_state = RouteConfigurationResponseState(balancer_.get());
2475 ASSERT_TRUE(response_state.has_value());
2476 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
2477 }
2478
2479 TEST_P(LdsRdsTest, XdsRoutingHeadersMatchingUnmatchCases) {
2480 CreateAndStartBackends(4);
2481 const char* kNewCluster1Name = "new_cluster_1";
2482 const char* kNewEdsService1Name = "new_eds_service_name_1";
2483 const char* kNewCluster2Name = "new_cluster_2";
2484 const char* kNewEdsService2Name = "new_eds_service_name_2";
2485 const char* kNewCluster3Name = "new_cluster_3";
2486 const char* kNewEdsService3Name = "new_eds_service_name_3";
2487 const size_t kNumEcho1Rpcs = 100;
2488 const size_t kNumEchoRpcs = 5;
2489 // Populate new EDS resources.
2490 EdsResourceArgs args({
2491 {"locality0", CreateEndpointsForBackends(0, 1)},
2492 });
2493 EdsResourceArgs args1({
2494 {"locality0", CreateEndpointsForBackends(1, 2)},
2495 });
2496 EdsResourceArgs args2({
2497 {"locality0", CreateEndpointsForBackends(2, 3)},
2498 });
2499 EdsResourceArgs args3({
2500 {"locality0", CreateEndpointsForBackends(3, 4)},
2501 });
2502 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2503 balancer_->ads_service()->SetEdsResource(
2504 BuildEdsResource(args1, kNewEdsService1Name));
2505 balancer_->ads_service()->SetEdsResource(
2506 BuildEdsResource(args2, kNewEdsService2Name));
2507 balancer_->ads_service()->SetEdsResource(
2508 BuildEdsResource(args3, kNewEdsService3Name));
2509 // Populate new CDS resources.
2510 Cluster new_cluster1 = default_cluster_;
2511 new_cluster1.set_name(kNewCluster1Name);
2512 new_cluster1.mutable_eds_cluster_config()->set_service_name(
2513 kNewEdsService1Name);
2514 balancer_->ads_service()->SetCdsResource(new_cluster1);
2515 Cluster new_cluster2 = default_cluster_;
2516 new_cluster2.set_name(kNewCluster2Name);
2517 new_cluster2.mutable_eds_cluster_config()->set_service_name(
2518 kNewEdsService2Name);
2519 balancer_->ads_service()->SetCdsResource(new_cluster2);
2520 Cluster new_cluster3 = default_cluster_;
2521 new_cluster3.set_name(kNewCluster3Name);
2522 new_cluster3.mutable_eds_cluster_config()->set_service_name(
2523 kNewEdsService3Name);
2524 balancer_->ads_service()->SetCdsResource(new_cluster3);
2525 // Populating Route Configurations for LDS.
2526 RouteConfiguration route_config = default_route_config_;
2527 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
2528 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
2529 auto* header_matcher1 = route1->mutable_match()->add_headers();
2530 header_matcher1->set_name("header1");
2531 header_matcher1->set_exact_match("POST");
2532 route1->mutable_route()->set_cluster(kNewCluster1Name);
2533 auto route2 = route_config.mutable_virtual_hosts(0)->add_routes();
2534 route2->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
2535 auto* header_matcher2 = route2->mutable_match()->add_headers();
2536 header_matcher2->set_name("header2");
2537 header_matcher2->mutable_range_match()->set_start(1);
2538 header_matcher2->mutable_range_match()->set_end(1000);
2539 route2->mutable_route()->set_cluster(kNewCluster2Name);
2540 auto route3 = route_config.mutable_virtual_hosts(0)->add_routes();
2541 route3->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
2542 auto* header_matcher3 = route3->mutable_match()->add_headers();
2543 header_matcher3->set_name("header3");
2544 header_matcher3->mutable_safe_regex_match()->set_regex("[a-z]*");
2545 route3->mutable_route()->set_cluster(kNewCluster3Name);
2546 auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
2547 default_route->mutable_match()->set_prefix("");
2548 default_route->mutable_route()->set_cluster(kDefaultClusterName);
2549 SetRouteConfiguration(balancer_.get(), route_config);
2550 // Send headers which will mismatch each route
2551 std::vector<std::pair<std::string, std::string>> metadata = {
2552 {"header1", "POST"},
2553 {"header2", "1000"},
2554 {"header3", "123"},
2555 {"header1", "GET"},
2556 };
2557 WaitForAllBackends(DEBUG_LOCATION, 0, 1);
2558 CheckRpcSendOk(DEBUG_LOCATION, kNumEchoRpcs,
2559 RpcOptions().set_metadata(metadata));
2560 CheckRpcSendOk(DEBUG_LOCATION, kNumEcho1Rpcs,
2561 RpcOptions()
2562 .set_rpc_service(SERVICE_ECHO1)
2563 .set_rpc_method(METHOD_ECHO1)
2564 .set_metadata(metadata));
2565 // Verify that only the default backend got RPCs since all previous routes
2566 // were mismatched.
2567 for (size_t i = 1; i < 4; ++i) {
2568 EXPECT_EQ(0, backends_[i]->backend_service()->request_count());
2569 EXPECT_EQ(0, backends_[i]->backend_service1()->request_count());
2570 EXPECT_EQ(0, backends_[i]->backend_service2()->request_count());
2571 }
2572 EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
2573 EXPECT_EQ(kNumEcho1Rpcs, backends_[0]->backend_service1()->request_count());
2574 EXPECT_EQ(0, backends_[0]->backend_service2()->request_count());
2575 auto response_state = RouteConfigurationResponseState(balancer_.get());
2576 ASSERT_TRUE(response_state.has_value());
2577 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
2578 }
2579
2580 TEST_P(LdsRdsTest, XdsRoutingChangeRoutesWithoutChangingClusters) {
2581 CreateAndStartBackends(2);
2582 const char* kNewClusterName = "new_cluster";
2583 const char* kNewEdsServiceName = "new_eds_service_name";
2584 // Populate new EDS resources.
2585 EdsResourceArgs args({
2586 {"locality0", CreateEndpointsForBackends(0, 1)},
2587 });
2588 EdsResourceArgs args1({
2589 {"locality0", CreateEndpointsForBackends(1, 2)},
2590 });
2591 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2592 balancer_->ads_service()->SetEdsResource(
2593 BuildEdsResource(args1, kNewEdsServiceName));
2594 // Populate new CDS resources.
2595 Cluster new_cluster = default_cluster_;
2596 new_cluster.set_name(kNewClusterName);
2597 new_cluster.mutable_eds_cluster_config()->set_service_name(
2598 kNewEdsServiceName);
2599 balancer_->ads_service()->SetCdsResource(new_cluster);
2600 // Populating Route Configurations for LDS.
2601 RouteConfiguration route_config = default_route_config_;
2602 auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
2603 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
2604 route1->mutable_route()->set_cluster(kNewClusterName);
2605 auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
2606 default_route->mutable_match()->set_prefix("");
2607 default_route->mutable_route()->set_cluster(kDefaultClusterName);
2608 SetRouteConfiguration(balancer_.get(), route_config);
2609 // Make sure all backends are up and that requests for each RPC
2610 // service go to the right backends.
2611 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
2612 WaitForBackendOptions().set_reset_counters(false));
2613 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
2614 WaitForBackendOptions().set_reset_counters(false),
2615 RpcOptions().set_rpc_service(SERVICE_ECHO1));
2616 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
2617 WaitForBackendOptions().set_reset_counters(false),
2618 RpcOptions().set_rpc_service(SERVICE_ECHO2));
2619 // Requests for services Echo and Echo2 should have gone to backend 0.
2620 EXPECT_EQ(1, backends_[0]->backend_service()->request_count());
2621 EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
2622 EXPECT_EQ(1, backends_[0]->backend_service2()->request_count());
2623 // Requests for service Echo1 should have gone to backend 1.
2624 EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
2625 EXPECT_EQ(1, backends_[1]->backend_service1()->request_count());
2626 EXPECT_EQ(0, backends_[1]->backend_service2()->request_count());
2627 // Now send an update that changes the first route to match a
2628 // different RPC service, and wait for the client to make the change.
2629 route1->mutable_match()->set_prefix("/grpc.testing.EchoTest2Service/");
2630 SetRouteConfiguration(balancer_.get(), route_config);
2631 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
2632 WaitForBackendOptions(),
2633 RpcOptions().set_rpc_service(SERVICE_ECHO2));
2634 // Now repeat the earlier test, making sure all traffic goes to the
2635 // right place.
2636 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
2637 WaitForBackendOptions().set_reset_counters(false));
2638 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
2639 WaitForBackendOptions().set_reset_counters(false),
2640 RpcOptions().set_rpc_service(SERVICE_ECHO1));
2641 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
2642 WaitForBackendOptions().set_reset_counters(false),
2643 RpcOptions().set_rpc_service(SERVICE_ECHO2));
2644 // Requests for services Echo and Echo1 should have gone to backend 0.
2645 EXPECT_EQ(1, backends_[0]->backend_service()->request_count());
2646 EXPECT_EQ(1, backends_[0]->backend_service1()->request_count());
2647 EXPECT_EQ(0, backends_[0]->backend_service2()->request_count());
2648 // Requests for service Echo2 should have gone to backend 1.
2649 EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
2650 EXPECT_EQ(0, backends_[1]->backend_service1()->request_count());
2651 EXPECT_EQ(1, backends_[1]->backend_service2()->request_count());
2652 }
2653
2654 // Test that we NACK unknown filter types in VirtualHost.
2655 TEST_P(LdsRdsTest, RejectsUnknownHttpFilterTypeInVirtualHost) {
2656 RouteConfiguration route_config = default_route_config_;
2657 auto* per_filter_config =
2658 route_config.mutable_virtual_hosts(0)->mutable_typed_per_filter_config();
2659 (*per_filter_config)["unknown"].PackFrom(Listener());
2660 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2661 route_config);
2662 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2663 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2664 EXPECT_THAT(response_state->error_message,
2665 ::testing::HasSubstr("no filter registered for config type "
2666 "envoy.config.listener.v3.Listener"));
2667 }
2668
2669 // Test that we ignore optional unknown filter types in VirtualHost.
2670 TEST_P(LdsRdsTest, IgnoresOptionalUnknownHttpFilterTypeInVirtualHost) {
2671 CreateAndStartBackends(1);
2672 RouteConfiguration route_config = default_route_config_;
2673 auto* per_filter_config =
2674 route_config.mutable_virtual_hosts(0)->mutable_typed_per_filter_config();
2675 ::envoy::config::route::v3::FilterConfig filter_config;
2676 filter_config.mutable_config()->PackFrom(Listener());
2677 filter_config.set_is_optional(true);
2678 (*per_filter_config)["unknown"].PackFrom(filter_config);
2679 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2680 route_config);
2681 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
2682 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2683 WaitForAllBackends(DEBUG_LOCATION);
2684 auto response_state = RouteConfigurationResponseState(balancer_.get());
2685 ASSERT_TRUE(response_state.has_value());
2686 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
2687 }
2688
2689 // Test that we NACK filters without configs in VirtualHost.
2690 TEST_P(LdsRdsTest, RejectsHttpFilterWithoutConfigInVirtualHost) {
2691 RouteConfiguration route_config = default_route_config_;
2692 auto* per_filter_config =
2693 route_config.mutable_virtual_hosts(0)->mutable_typed_per_filter_config();
2694 (*per_filter_config)["unknown"];
2695 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2696 route_config);
2697 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2698 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2699 EXPECT_THAT(response_state->error_message,
2700 ::testing::HasSubstr(
2701 "no filter config specified for filter name unknown"));
2702 }
2703
2704 // Test that we NACK filters without configs in FilterConfig in VirtualHost.
2705 TEST_P(LdsRdsTest, RejectsHttpFilterWithoutConfigInFilterConfigInVirtualHost) {
2706 RouteConfiguration route_config = default_route_config_;
2707 auto* per_filter_config =
2708 route_config.mutable_virtual_hosts(0)->mutable_typed_per_filter_config();
2709 (*per_filter_config)["unknown"].PackFrom(
2710 ::envoy::config::route::v3::FilterConfig());
2711 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2712 route_config);
2713 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2714 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2715 EXPECT_THAT(response_state->error_message,
2716 ::testing::HasSubstr(
2717 "no filter config specified for filter name unknown"));
2718 }
2719
2720 // Test that we ignore optional filters without configs in VirtualHost.
2721 TEST_P(LdsRdsTest, IgnoresOptionalHttpFilterWithoutConfigInVirtualHost) {
2722 CreateAndStartBackends(1);
2723 RouteConfiguration route_config = default_route_config_;
2724 auto* per_filter_config =
2725 route_config.mutable_virtual_hosts(0)->mutable_typed_per_filter_config();
2726 ::envoy::config::route::v3::FilterConfig filter_config;
2727 filter_config.set_is_optional(true);
2728 (*per_filter_config)["unknown"].PackFrom(filter_config);
2729 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2730 route_config);
2731 EdsResourceArgs args({
2732 {"locality0", CreateEndpointsForBackends()},
2733 });
2734 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2735 WaitForAllBackends(DEBUG_LOCATION);
2736 auto response_state = RouteConfigurationResponseState(balancer_.get());
2737 ASSERT_TRUE(response_state.has_value());
2738 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
2739 }
2740
2741 // Test that we NACK unparseable filter types in VirtualHost.
2742 TEST_P(LdsRdsTest, RejectsUnparseableHttpFilterTypeInVirtualHost) {
2743 RouteConfiguration route_config = default_route_config_;
2744 auto* per_filter_config =
2745 route_config.mutable_virtual_hosts(0)->mutable_typed_per_filter_config();
2746 (*per_filter_config)["unknown"].PackFrom(
2747 envoy::extensions::filters::http::router::v3::Router());
2748 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2749 route_config);
2750 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2751 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2752 EXPECT_THAT(
2753 response_state->error_message,
2754 ::testing::HasSubstr("router filter does not support config override"));
2755 }
2756
2757 // Test that we NACK unknown filter types in Route.
2758 TEST_P(LdsRdsTest, RejectsUnknownHttpFilterTypeInRoute) {
2759 RouteConfiguration route_config = default_route_config_;
2760 auto* per_filter_config = route_config.mutable_virtual_hosts(0)
2761 ->mutable_routes(0)
2762 ->mutable_typed_per_filter_config();
2763 (*per_filter_config)["unknown"].PackFrom(Listener());
2764 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2765 route_config);
2766 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2767 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2768 EXPECT_THAT(response_state->error_message,
2769 ::testing::HasSubstr("no filter registered for config type "
2770 "envoy.config.listener.v3.Listener"));
2771 }
2772
2773 // Test that we ignore optional unknown filter types in Route.
2774 TEST_P(LdsRdsTest, IgnoresOptionalUnknownHttpFilterTypeInRoute) {
2775 CreateAndStartBackends(1);
2776 RouteConfiguration route_config = default_route_config_;
2777 auto* per_filter_config = route_config.mutable_virtual_hosts(0)
2778 ->mutable_routes(0)
2779 ->mutable_typed_per_filter_config();
2780 ::envoy::config::route::v3::FilterConfig filter_config;
2781 filter_config.mutable_config()->PackFrom(Listener());
2782 filter_config.set_is_optional(true);
2783 (*per_filter_config)["unknown"].PackFrom(filter_config);
2784 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2785 route_config);
2786 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
2787 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2788 WaitForAllBackends(DEBUG_LOCATION);
2789 auto response_state = RouteConfigurationResponseState(balancer_.get());
2790 ASSERT_TRUE(response_state.has_value());
2791 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
2792 }
2793
2794 // Test that we NACK filters without configs in Route.
2795 TEST_P(LdsRdsTest, RejectsHttpFilterWithoutConfigInRoute) {
2796 RouteConfiguration route_config = default_route_config_;
2797 auto* per_filter_config = route_config.mutable_virtual_hosts(0)
2798 ->mutable_routes(0)
2799 ->mutable_typed_per_filter_config();
2800 (*per_filter_config)["unknown"];
2801 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2802 route_config);
2803 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2804 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2805 EXPECT_THAT(response_state->error_message,
2806 ::testing::HasSubstr(
2807 "no filter config specified for filter name unknown"));
2808 }
2809
2810 // Test that we NACK filters without configs in FilterConfig in Route.
2811 TEST_P(LdsRdsTest, RejectsHttpFilterWithoutConfigInFilterConfigInRoute) {
2812 RouteConfiguration route_config = default_route_config_;
2813 auto* per_filter_config = route_config.mutable_virtual_hosts(0)
2814 ->mutable_routes(0)
2815 ->mutable_typed_per_filter_config();
2816 (*per_filter_config)["unknown"].PackFrom(
2817 ::envoy::config::route::v3::FilterConfig());
2818 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2819 route_config);
2820 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2821 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2822 EXPECT_THAT(response_state->error_message,
2823 ::testing::HasSubstr(
2824 "no filter config specified for filter name unknown"));
2825 }
2826
2827 // Test that we ignore optional filters without configs in Route.
2828 TEST_P(LdsRdsTest, IgnoresOptionalHttpFilterWithoutConfigInRoute) {
2829 CreateAndStartBackends(1);
2830 RouteConfiguration route_config = default_route_config_;
2831 auto* per_filter_config = route_config.mutable_virtual_hosts(0)
2832 ->mutable_routes(0)
2833 ->mutable_typed_per_filter_config();
2834 ::envoy::config::route::v3::FilterConfig filter_config;
2835 filter_config.set_is_optional(true);
2836 (*per_filter_config)["unknown"].PackFrom(filter_config);
2837 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2838 route_config);
2839 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
2840 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2841 WaitForAllBackends(DEBUG_LOCATION);
2842 auto response_state = RouteConfigurationResponseState(balancer_.get());
2843 ASSERT_TRUE(response_state.has_value());
2844 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
2845 }
2846
2847 // Test that we NACK unparseable filter types in Route.
2848 TEST_P(LdsRdsTest, RejectsUnparseableHttpFilterTypeInRoute) {
2849 RouteConfiguration route_config = default_route_config_;
2850 auto* per_filter_config = route_config.mutable_virtual_hosts(0)
2851 ->mutable_routes(0)
2852 ->mutable_typed_per_filter_config();
2853 (*per_filter_config)["unknown"].PackFrom(
2854 envoy::extensions::filters::http::router::v3::Router());
2855 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2856 route_config);
2857 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2858 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2859 EXPECT_THAT(
2860 response_state->error_message,
2861 ::testing::HasSubstr("router filter does not support config override"));
2862 }
2863
2864 // Test that we NACK unknown filter types in ClusterWeight.
2865 TEST_P(LdsRdsTest, RejectsUnknownHttpFilterTypeInClusterWeight) {
2866 RouteConfiguration route_config = default_route_config_;
2867 auto* cluster_weight = route_config.mutable_virtual_hosts(0)
2868 ->mutable_routes(0)
2869 ->mutable_route()
2870 ->mutable_weighted_clusters()
2871 ->add_clusters();
2872 cluster_weight->set_name(kDefaultClusterName);
2873 cluster_weight->mutable_weight()->set_value(100);
2874 auto* per_filter_config = cluster_weight->mutable_typed_per_filter_config();
2875 (*per_filter_config)["unknown"].PackFrom(Listener());
2876 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2877 route_config);
2878 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2879 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2880 EXPECT_THAT(response_state->error_message,
2881 ::testing::HasSubstr("no filter registered for config type "
2882 "envoy.config.listener.v3.Listener"));
2883 }
2884
2885 // Test that we ignore optional unknown filter types in ClusterWeight.
2886 TEST_P(LdsRdsTest, IgnoresOptionalUnknownHttpFilterTypeInClusterWeight) {
2887 CreateAndStartBackends(1);
2888 RouteConfiguration route_config = default_route_config_;
2889 auto* cluster_weight = route_config.mutable_virtual_hosts(0)
2890 ->mutable_routes(0)
2891 ->mutable_route()
2892 ->mutable_weighted_clusters()
2893 ->add_clusters();
2894 cluster_weight->set_name(kDefaultClusterName);
2895 cluster_weight->mutable_weight()->set_value(100);
2896 auto* per_filter_config = cluster_weight->mutable_typed_per_filter_config();
2897 ::envoy::config::route::v3::FilterConfig filter_config;
2898 filter_config.mutable_config()->PackFrom(Listener());
2899 filter_config.set_is_optional(true);
2900 (*per_filter_config)["unknown"].PackFrom(filter_config);
2901 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2902 route_config);
2903 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
2904 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2905 WaitForAllBackends(DEBUG_LOCATION);
2906 auto response_state = RouteConfigurationResponseState(balancer_.get());
2907 ASSERT_TRUE(response_state.has_value());
2908 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
2909 }
2910
2911 // Test that we NACK filters without configs in ClusterWeight.
2912 TEST_P(LdsRdsTest, RejectsHttpFilterWithoutConfigInClusterWeight) {
2913 RouteConfiguration route_config = default_route_config_;
2914 auto* cluster_weight = route_config.mutable_virtual_hosts(0)
2915 ->mutable_routes(0)
2916 ->mutable_route()
2917 ->mutable_weighted_clusters()
2918 ->add_clusters();
2919 cluster_weight->set_name(kDefaultClusterName);
2920 cluster_weight->mutable_weight()->set_value(100);
2921 auto* per_filter_config = cluster_weight->mutable_typed_per_filter_config();
2922 (*per_filter_config)["unknown"];
2923 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2924 route_config);
2925 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2926 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2927 EXPECT_THAT(response_state->error_message,
2928 ::testing::HasSubstr(
2929 "no filter config specified for filter name unknown"));
2930 }
2931
2932 // Test that we NACK filters without configs in FilterConfig in ClusterWeight.
2933 TEST_P(LdsRdsTest,
2934 RejectsHttpFilterWithoutConfigInFilterConfigInClusterWeight) {
2935 RouteConfiguration route_config = default_route_config_;
2936 auto* cluster_weight = route_config.mutable_virtual_hosts(0)
2937 ->mutable_routes(0)
2938 ->mutable_route()
2939 ->mutable_weighted_clusters()
2940 ->add_clusters();
2941 cluster_weight->set_name(kDefaultClusterName);
2942 cluster_weight->mutable_weight()->set_value(100);
2943 auto* per_filter_config = cluster_weight->mutable_typed_per_filter_config();
2944 (*per_filter_config)["unknown"].PackFrom(
2945 ::envoy::config::route::v3::FilterConfig());
2946 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2947 route_config);
2948 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2949 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2950 EXPECT_THAT(response_state->error_message,
2951 ::testing::HasSubstr(
2952 "no filter config specified for filter name unknown"));
2953 }
2954
2955 // Test that we ignore optional filters without configs in ClusterWeight.
2956 TEST_P(LdsRdsTest, IgnoresOptionalHttpFilterWithoutConfigInClusterWeight) {
2957 CreateAndStartBackends(1);
2958 RouteConfiguration route_config = default_route_config_;
2959 auto* cluster_weight = route_config.mutable_virtual_hosts(0)
2960 ->mutable_routes(0)
2961 ->mutable_route()
2962 ->mutable_weighted_clusters()
2963 ->add_clusters();
2964 cluster_weight->set_name(kDefaultClusterName);
2965 cluster_weight->mutable_weight()->set_value(100);
2966 auto* per_filter_config = cluster_weight->mutable_typed_per_filter_config();
2967 ::envoy::config::route::v3::FilterConfig filter_config;
2968 filter_config.set_is_optional(true);
2969 (*per_filter_config)["unknown"].PackFrom(filter_config);
2970 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2971 route_config);
2972 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
2973 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
2974 WaitForAllBackends(DEBUG_LOCATION);
2975 auto response_state = RouteConfigurationResponseState(balancer_.get());
2976 ASSERT_TRUE(response_state.has_value());
2977 EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED);
2978 }
2979
2980 // Test that we NACK unparseable filter types in ClusterWeight.
2981 TEST_P(LdsRdsTest, RejectsUnparseableHttpFilterTypeInClusterWeight) {
2982 RouteConfiguration route_config = default_route_config_;
2983 auto* cluster_weight = route_config.mutable_virtual_hosts(0)
2984 ->mutable_routes(0)
2985 ->mutable_route()
2986 ->mutable_weighted_clusters()
2987 ->add_clusters();
2988 cluster_weight->set_name(kDefaultClusterName);
2989 cluster_weight->mutable_weight()->set_value(100);
2990 auto* per_filter_config = cluster_weight->mutable_typed_per_filter_config();
2991 (*per_filter_config)["unknown"].PackFrom(
2992 envoy::extensions::filters::http::router::v3::Router());
2993 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
2994 route_config);
2995 const auto response_state = WaitForRdsNack(DEBUG_LOCATION);
2996 ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK";
2997 EXPECT_THAT(
2998 response_state->error_message,
2999 ::testing::HasSubstr("router filter does not support config override"));
3000 }
3001
3002 } // namespace
3003 } // namespace testing
3004 } // namespace grpc
3005
3006 int main(int argc, char** argv) {
3007 grpc::testing::TestEnvironment env(&argc, argv);
3008 ::testing::InitGoogleTest(&argc, argv);
3009 // Make the backup poller poll very frequently in order to pick up
3010 // updates from all the subchannels's FDs.
3011 GPR_GLOBAL_CONFIG_SET(grpc_client_channel_backup_poll_interval_ms, 1);
3012 #if TARGET_OS_IPHONE
3013 // Workaround Apple CFStream bug
3014 grpc_core::SetEnv("grpc_cfstream", "0");
3015 #endif
3016 grpc_init();
3017 const auto result = RUN_ALL_TESTS();
3018 grpc_shutdown();
3019 return result;
3020 }