forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoped_rds_integration_test.cc
777 lines (698 loc) · 32 KB
/
scoped_rds_integration_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
#include "envoy/config/core/v3/config_source.pb.h"
#include "envoy/config/core/v3/grpc_service.pb.h"
#include "envoy/config/route/v3/route.pb.h"
#include "envoy/config/route/v3/scoped_route.pb.h"
#include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h"
#include "envoy/service/discovery/v3/discovery.pb.h"
#include "common/config/api_version.h"
#include "common/config/version_converter.h"
#include "test/common/grpc/grpc_client_integration.h"
#include "test/integration/http_integration.h"
#include "test/test_common/printers.h"
#include "test/test_common/resources.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace Envoy {
namespace {
class ScopedRdsIntegrationTest : public HttpIntegrationTest,
public Grpc::DeltaSotwIntegrationParamTest {
protected:
struct FakeUpstreamInfo {
FakeHttpConnectionPtr connection_;
FakeUpstream* upstream_{};
absl::flat_hash_map<std::string, FakeStreamPtr> stream_by_resource_name_;
};
ScopedRdsIntegrationTest()
: HttpIntegrationTest(Http::CodecClient::Type::HTTP1, ipVersion(), realTime()) {}
~ScopedRdsIntegrationTest() override { resetConnections(); }
void initialize() override {
// Setup two upstream hosts, one for each cluster.
setUpstreamCount(2);
config_helper_.addConfigModifier([](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
// Add the static cluster to serve SRDS.
auto* cluster_1 = bootstrap.mutable_static_resources()->add_clusters();
cluster_1->MergeFrom(bootstrap.static_resources().clusters()[0]);
cluster_1->set_name("cluster_1");
// Add the static cluster to serve SRDS.
auto* scoped_rds_cluster = bootstrap.mutable_static_resources()->add_clusters();
scoped_rds_cluster->MergeFrom(bootstrap.static_resources().clusters()[0]);
scoped_rds_cluster->set_name("srds_cluster");
ConfigHelper::setHttp2(*scoped_rds_cluster);
// Add the static cluster to serve RDS.
auto* rds_cluster = bootstrap.mutable_static_resources()->add_clusters();
rds_cluster->MergeFrom(bootstrap.static_resources().clusters()[0]);
rds_cluster->set_name("rds_cluster");
ConfigHelper::setHttp2(*rds_cluster);
});
config_helper_.addConfigModifier(
[this](
envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
http_connection_manager) {
const std::string& scope_key_builder_config_yaml = R"EOF(
fragments:
- header_value_extractor:
name: Addr
element_separator: ;
element:
key: x-foo-key
separator: =
)EOF";
envoy::extensions::filters::network::http_connection_manager::v3::ScopedRoutes::
ScopeKeyBuilder scope_key_builder;
TestUtility::loadFromYaml(scope_key_builder_config_yaml, scope_key_builder);
auto* scoped_routes = http_connection_manager.mutable_scoped_routes();
scoped_routes->set_name(srds_config_name_);
*scoped_routes->mutable_scope_key_builder() = scope_key_builder;
// Set resource api version for rds.
envoy::config::core::v3::ConfigSource* rds_config_source =
scoped_routes->mutable_rds_config_source();
rds_config_source->set_resource_api_version(envoy::config::core::v3::ApiVersion::V3);
// Set transport api version for rds.
envoy::config::core::v3::ApiConfigSource* rds_api_config_source =
rds_config_source->mutable_api_config_source();
rds_api_config_source->set_transport_api_version(envoy::config::core::v3::ApiVersion::V3);
// Add grpc service for rds.
rds_api_config_source->set_api_type(envoy::config::core::v3::ApiConfigSource::GRPC);
envoy::config::core::v3::GrpcService* grpc_service =
rds_api_config_source->add_grpc_services();
setGrpcService(*grpc_service, "rds_cluster", getRdsFakeUpstream().localAddress());
// Set resource api version for scoped rds.
envoy::config::core::v3::ConfigSource* srds_config_source =
scoped_routes->mutable_scoped_rds()->mutable_scoped_rds_config_source();
srds_config_source->set_resource_api_version(envoy::config::core::v3::ApiVersion::V3);
// Set Transport api version for scoped_rds.
envoy::config::core::v3::ApiConfigSource* srds_api_config_source =
srds_config_source->mutable_api_config_source();
srds_api_config_source->set_transport_api_version(
envoy::config::core::v3::ApiVersion::V3);
// Add grpc service for scoped rds.
if (isDelta()) {
srds_api_config_source->set_api_type(
envoy::config::core::v3::ApiConfigSource::DELTA_GRPC);
} else {
srds_api_config_source->set_api_type(envoy::config::core::v3::ApiConfigSource::GRPC);
}
srds_api_config_source->set_transport_api_version(
envoy::config::core::v3::ApiVersion::V3);
grpc_service = srds_api_config_source->add_grpc_services();
setGrpcService(*grpc_service, "srds_cluster", getScopedRdsFakeUpstream().localAddress());
});
HttpIntegrationTest::initialize();
}
void createUpstreams() override {
HttpIntegrationTest::createUpstreams();
// Create the SRDS upstream.
addFakeUpstream(FakeHttpConnection::Type::HTTP2);
// Create the RDS upstream.
addFakeUpstream(FakeHttpConnection::Type::HTTP2);
}
void resetFakeUpstreamInfo(FakeUpstreamInfo* upstream_info) {
ASSERT(upstream_info->upstream_ != nullptr);
AssertionResult result = upstream_info->connection_->close();
RELEASE_ASSERT(result, result.message());
result = upstream_info->connection_->waitForDisconnect();
RELEASE_ASSERT(result, result.message());
upstream_info->connection_.reset();
}
void resetConnections() {
if (rds_upstream_info_.upstream_ != nullptr) {
resetFakeUpstreamInfo(&rds_upstream_info_);
}
resetFakeUpstreamInfo(&scoped_rds_upstream_info_);
}
FakeUpstream& getRdsFakeUpstream() const { return *fake_upstreams_[3]; }
FakeUpstream& getScopedRdsFakeUpstream() const { return *fake_upstreams_[2]; }
void createStream(FakeUpstreamInfo* upstream_info, FakeUpstream& upstream,
const std::string& resource_name) {
if (upstream_info->upstream_ == nullptr) {
// bind upstream if not yet.
upstream_info->upstream_ = &upstream;
AssertionResult result =
upstream_info->upstream_->waitForHttpConnection(*dispatcher_, upstream_info->connection_);
RELEASE_ASSERT(result, result.message());
}
if (!upstream_info->stream_by_resource_name_.try_emplace(resource_name, nullptr).second) {
RELEASE_ASSERT(false,
fmt::format("stream with resource name '{}' already exists!", resource_name));
}
auto result = upstream_info->connection_->waitForNewStream(
*dispatcher_, upstream_info->stream_by_resource_name_[resource_name]);
RELEASE_ASSERT(result, result.message());
upstream_info->stream_by_resource_name_[resource_name]->startGrpcStream();
}
void createRdsStream(const std::string& resource_name) {
createStream(&rds_upstream_info_, getRdsFakeUpstream(), resource_name);
}
void createScopedRdsStream() {
createStream(&scoped_rds_upstream_info_, getScopedRdsFakeUpstream(), srds_config_name_);
}
void sendRdsResponse(const std::string& route_config, const std::string& version) {
envoy::service::discovery::v3::DiscoveryResponse response;
std::string route_conguration_type_url =
"type.googleapis.com/envoy.config.route.v3.RouteConfiguration";
response.set_version_info(version);
response.set_type_url(route_conguration_type_url);
auto route_configuration =
TestUtility::parseYaml<envoy::config::route::v3::RouteConfiguration>(route_config);
response.add_resources()->PackFrom(route_configuration);
ASSERT(rds_upstream_info_.stream_by_resource_name_[route_configuration.name()] != nullptr);
rds_upstream_info_.stream_by_resource_name_[route_configuration.name()]->sendGrpcMessage(
response);
}
void sendSrdsResponse(const std::vector<std::string>& sotw_list,
const std::vector<std::string>& to_add_list,
const std::vector<std::string>& to_delete_list,
const std::string& version) {
if (isDelta()) {
sendDeltaScopedRdsResponse(to_add_list, to_delete_list, version);
} else {
sendSotwScopedRdsResponse(sotw_list, version);
}
}
void sendDeltaScopedRdsResponse(const std::vector<std::string>& to_add_list,
const std::vector<std::string>& to_delete_list,
const std::string& version) {
ASSERT(scoped_rds_upstream_info_.stream_by_resource_name_[srds_config_name_] != nullptr);
std::string scoped_route_configuration_type_url =
"type.googleapis.com/envoy.config.route.v3.ScopedRouteConfiguration";
envoy::service::discovery::v3::DeltaDiscoveryResponse response;
response.set_system_version_info(version);
response.set_type_url(scoped_route_configuration_type_url);
for (const auto& scope_name : to_delete_list) {
*response.add_removed_resources() = scope_name;
}
for (const auto& resource_proto : to_add_list) {
envoy::config::route::v3::ScopedRouteConfiguration scoped_route_proto;
TestUtility::loadFromYaml(resource_proto, scoped_route_proto);
auto resource = response.add_resources();
resource->set_name(scoped_route_proto.name());
resource->set_version(version);
resource->mutable_resource()->PackFrom(scoped_route_proto);
}
scoped_rds_upstream_info_.stream_by_resource_name_[srds_config_name_]->sendGrpcMessage(
response);
}
void sendSotwScopedRdsResponse(const std::vector<std::string>& resource_protos,
const std::string& version) {
ASSERT(scoped_rds_upstream_info_.stream_by_resource_name_[srds_config_name_] != nullptr);
std::string scoped_route_configuration_type_url =
"type.googleapis.com/envoy.config.route.v3.ScopedRouteConfiguration";
envoy::service::discovery::v3::DiscoveryResponse response;
response.set_version_info(version);
response.set_type_url(scoped_route_configuration_type_url);
for (const auto& resource_proto : resource_protos) {
envoy::config::route::v3::ScopedRouteConfiguration scoped_route_proto;
TestUtility::loadFromYaml(resource_proto, scoped_route_proto);
response.add_resources()->PackFrom(scoped_route_proto);
}
scoped_rds_upstream_info_.stream_by_resource_name_[srds_config_name_]->sendGrpcMessage(
response);
}
bool isDelta() { return sotwOrDelta() == Grpc::SotwOrDelta::Delta; }
const std::string srds_config_name_{"foo-scoped-routes"};
FakeUpstreamInfo scoped_rds_upstream_info_;
FakeUpstreamInfo rds_upstream_info_;
};
INSTANTIATE_TEST_SUITE_P(IpVersionsAndGrpcTypes, ScopedRdsIntegrationTest,
DELTA_SOTW_GRPC_CLIENT_INTEGRATION_PARAMS);
// Test that a SRDS DiscoveryResponse is successfully processed.
TEST_P(ScopedRdsIntegrationTest, BasicSuccess) {
const std::string scope_tmpl = R"EOF(
name: {}
route_configuration_name: {}
key:
fragments:
- string_key: {}
)EOF";
const std::string scope_route1 = fmt::format(scope_tmpl, "foo_scope1", "foo_route1", "foo-route");
const std::string scope_route2 = fmt::format(scope_tmpl, "foo_scope2", "foo_route1", "bar-route");
const std::string route_config_tmpl = R"EOF(
name: {}
virtual_hosts:
- name: integration
domains: ["*"]
routes:
- match: {{ prefix: "/" }}
route: {{ cluster: {} }}
)EOF";
on_server_init_function_ = [&]() {
createScopedRdsStream();
sendSrdsResponse({scope_route1, scope_route2}, {scope_route1, scope_route2}, {}, "1");
createRdsStream("foo_route1");
// CreateRdsStream waits for connection which is fired by RDS subscription.
sendRdsResponse(fmt::format(route_config_tmpl, "foo_route1", "cluster_0"), "1");
};
initialize();
registerTestServerPorts({"http"});
// No scope key matches "xyz-route".
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeHeaderOnlyRequest(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/meh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", "x-foo-key=xyz-route"}});
response->waitForEndStream();
verifyResponse(std::move(response), "404", Http::TestResponseHeaderMapImpl{}, "");
cleanupUpstreamAndDownstream();
// Test "foo-route" and 'bar-route' both gets routed to cluster_0.
test_server_->waitForCounterGe("http.config_test.rds.foo_route1.update_success", 1);
for (const std::string& scope_key : std::vector<std::string>{"foo-route", "bar-route"}) {
sendRequestAndVerifyResponse(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/meh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", fmt::format("x-foo-key={}", scope_key)}},
456, Http::TestResponseHeaderMapImpl{{":status", "200"}, {"service", scope_key}}, 123,
/*cluster_0*/ 0);
}
test_server_->waitForCounterGe("http.config_test.scoped_rds.foo-scoped-routes.update_attempt",
// update_attempt only increase after a response
isDelta() ? 1 : 2);
test_server_->waitForCounterGe("http.config_test.scoped_rds.foo-scoped-routes.update_success", 1);
// The version gauge should be set to xxHash64("1").
test_server_->waitForGaugeEq("http.config_test.scoped_rds.foo-scoped-routes.version",
13237225503670494420UL);
// Add a new scope scope_route3 with a brand new RouteConfiguration foo_route2.
const std::string scope_route3 = fmt::format(scope_tmpl, "foo_scope3", "foo_route2", "baz-route");
sendSrdsResponse({scope_route1, scope_route2, scope_route3}, /*added*/ {scope_route3}, {}, "2");
test_server_->waitForCounterGe("http.config_test.rds.foo_route1.update_attempt", 2);
sendRdsResponse(fmt::format(route_config_tmpl, "foo_route1", "cluster_1"), "3");
test_server_->waitForCounterGe("http.config_test.rds.foo_route1.update_success", 2);
createRdsStream("foo_route2");
test_server_->waitForCounterGe("http.config_test.rds.foo_route2.update_attempt", 1);
sendRdsResponse(fmt::format(route_config_tmpl, "foo_route2", "cluster_0"), "1");
test_server_->waitForCounterGe("http.config_test.rds.foo_route2.update_success", 1);
test_server_->waitForCounterGe("http.config_test.scoped_rds.foo-scoped-routes.update_success", 2);
// The version gauge should be set to xxHash64("2").
test_server_->waitForGaugeEq("http.config_test.scoped_rds.foo-scoped-routes.version",
6927017134761466251UL);
// After RDS update, requests within scope 'foo_scope1' or 'foo_scope2' get routed to
// 'cluster_1'.
for (const std::string& scope_key : std::vector<std::string>{"foo-route", "bar-route"}) {
sendRequestAndVerifyResponse(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/meh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", fmt::format("x-foo-key={}", scope_key)}},
456, Http::TestResponseHeaderMapImpl{{":status", "200"}, {"service", scope_key}}, 123,
/*cluster_1*/ 1);
}
// Now requests within scope 'foo_scope3' get routed to 'cluster_0'.
sendRequestAndVerifyResponse(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/meh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", fmt::format("x-foo-key={}", "baz-route")}},
456, Http::TestResponseHeaderMapImpl{{":status", "200"}, {"service", "bluh"}}, 123,
/*cluster_0*/ 0);
// Delete foo_scope1 and requests within the scope gets 400s.
sendSrdsResponse({scope_route2, scope_route3}, {}, {"foo_scope1"}, "3");
test_server_->waitForCounterGe("http.config_test.scoped_rds.foo-scoped-routes.update_success", 3);
codec_client_ = makeHttpConnection(lookupPort("http"));
response = codec_client_->makeHeaderOnlyRequest(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/meh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", "x-foo-key=foo-route"}});
response->waitForEndStream();
verifyResponse(std::move(response), "404", Http::TestResponseHeaderMapImpl{}, "");
cleanupUpstreamAndDownstream();
// Add a new scope foo_scope4.
const std::string& scope_route4 =
fmt::format(scope_tmpl, "foo_scope4", "foo_route4", "xyz-route");
sendSrdsResponse({scope_route3, scope_route2, scope_route4}, {scope_route4}, {}, "4");
test_server_->waitForCounterGe("http.config_test.scoped_rds.foo-scoped-routes.update_success", 4);
codec_client_ = makeHttpConnection(lookupPort("http"));
response = codec_client_->makeHeaderOnlyRequest(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/meh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", "x-foo-key=xyz-route"}});
response->waitForEndStream();
// Get 404 because RDS hasn't pushed route configuration "foo_route4" yet.
// But scope is found and the Router::NullConfigImpl is returned.
verifyResponse(std::move(response), "404", Http::TestResponseHeaderMapImpl{}, "");
cleanupUpstreamAndDownstream();
// RDS updated foo_route4, requests with scope key "xyz-route" now hit cluster_1.
test_server_->waitForCounterGe("http.config_test.rds.foo_route4.update_attempt", 1);
createRdsStream("foo_route4");
sendRdsResponse(fmt::format(route_config_tmpl, "foo_route4", "cluster_1"), "3");
test_server_->waitForCounterGe("http.config_test.rds.foo_route4.update_success", 1);
sendRequestAndVerifyResponse(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/meh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", "x-foo-key=xyz-route"}},
456, Http::TestResponseHeaderMapImpl{{":status", "200"}, {"service", "xyz-route"}}, 123,
/*cluster_1 */ 1);
}
// Test that a bad config update updates the corresponding stats.
TEST_P(ScopedRdsIntegrationTest, ConfigUpdateFailure) {
// 'name' will fail to validate due to empty string.
const std::string scope_route1 = R"EOF(
name:
route_configuration_name: foo_route1
key:
fragments:
- string_key: foo
)EOF";
on_server_init_function_ = [this, &scope_route1]() {
createScopedRdsStream();
sendSrdsResponse({scope_route1}, {scope_route1}, {}, "1");
};
initialize();
test_server_->waitForCounterGe("http.config_test.scoped_rds.foo-scoped-routes.update_rejected",
1);
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeHeaderOnlyRequest(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/meh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", "x-foo-key=foo"}});
response->waitForEndStream();
verifyResponse(std::move(response), "404", Http::TestResponseHeaderMapImpl{}, "");
cleanupUpstreamAndDownstream();
// SRDS update fixed the problem.
const std::string scope_route2 = R"EOF(
name: foo_scope1
route_configuration_name: foo_route1
key:
fragments:
- string_key: foo
)EOF";
sendSrdsResponse({scope_route2}, {scope_route2}, {}, "1");
test_server_->waitForCounterGe("http.config_test.rds.foo_route1.update_attempt", 1);
createRdsStream("foo_route1");
const std::string route_config_tmpl = R"EOF(
name: {}
virtual_hosts:
- name: integration
domains: ["*"]
routes:
- match: {{ prefix: "/" }}
route: {{ cluster: {} }}
)EOF";
sendRdsResponse(fmt::format(route_config_tmpl, "foo_route1", "cluster_0"), "1");
test_server_->waitForCounterGe("http.config_test.rds.foo_route1.update_success", 1);
sendRequestAndVerifyResponse(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/meh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", "x-foo-key=foo"}},
456, Http::TestResponseHeaderMapImpl{{":status", "200"}, {"service", "bluh"}}, 123,
/*cluster_0*/ 0);
cleanupUpstreamAndDownstream();
}
// Test that a scoped route config update is performed on demand and http request will succeed.
TEST_P(ScopedRdsIntegrationTest, OnDemandUpdateSuccess) {
config_helper_.addFilter(R"EOF(
name: envoy.filters.http.on_demand
)EOF");
const std::string scope_route1 = R"EOF(
name: foo_scope1
route_configuration_name: foo_route1
on_demand: true
key:
fragments:
- string_key: foo
)EOF";
on_server_init_function_ = [this, &scope_route1]() {
createScopedRdsStream();
sendSrdsResponse({scope_route1}, {scope_route1}, {}, "1");
};
initialize();
registerTestServerPorts({"http"});
const std::string route_config_tmpl = R"EOF(
name: {}
virtual_hosts:
- name: integration
domains: ["*"]
routes:
- match: {{ prefix: "/" }}
route: {{ cluster: {} }}
)EOF";
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
// Request that match lazily loaded scope will trigger on demand loading.
auto response = codec_client_->makeHeaderOnlyRequest(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/meh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", "x-foo-key=foo"}});
createRdsStream("foo_route1");
sendRdsResponse(fmt::format(route_config_tmpl, "foo_route1", "cluster_0"), "1");
test_server_->waitForCounterGe("http.config_test.rds.foo_route1.update_success", 1);
waitForNextUpstreamRequest();
// Send response headers, and end_stream if there is no response body.
upstream_request_->encodeHeaders(default_response_headers_, true);
response->waitForHeaders();
EXPECT_EQ("200", response->headers().Status()->value().getStringView());
cleanupUpstreamAndDownstream();
}
// With on demand update filter configured, scope not match should still return 404
TEST_P(ScopedRdsIntegrationTest, OnDemandUpdateScopeNotMatch) {
config_helper_.addFilter(R"EOF(
name: envoy.filters.http.on_demand
)EOF");
const std::string scope_tmpl = R"EOF(
name: {}
route_configuration_name: {}
key:
fragments:
- string_key: {}
)EOF";
const std::string scope_route1 = fmt::format(scope_tmpl, "foo_scope1", "foo_route1", "foo-route");
const std::string route_config_tmpl = R"EOF(
name: {}
virtual_hosts:
- name: integration
domains: ["*"]
routes:
- match: {{ prefix: "/meh" }}
route: {{ cluster: {} }}
)EOF";
on_server_init_function_ = [&]() {
createScopedRdsStream();
sendSrdsResponse({scope_route1}, {scope_route1}, {}, "1");
createRdsStream("foo_route1");
// CreateRdsStream waits for connection which is fired by RDS subscription.
sendRdsResponse(fmt::format(route_config_tmpl, "foo_route1", "cluster_0"), "1");
};
initialize();
registerTestServerPorts({"http"});
// No scope key matches "bar".
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeHeaderOnlyRequest(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/meh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", "x-foo-key=bar"}});
response->waitForEndStream();
verifyResponse(std::move(response), "404", Http::TestResponseHeaderMapImpl{}, "");
cleanupUpstreamAndDownstream();
}
// With on demand update filter configured, scope match but virtual host don't match, should still
// return 404
TEST_P(ScopedRdsIntegrationTest, OnDemandUpdatePrimaryVirtualHostNotMatch) {
config_helper_.addFilter(R"EOF(
name: envoy.filters.http.on_demand
)EOF");
const std::string scope_tmpl = R"EOF(
name: {}
route_configuration_name: {}
key:
fragments:
- string_key: {}
)EOF";
const std::string scope_route1 = fmt::format(scope_tmpl, "foo_scope1", "foo_route1", "foo-route");
const std::string route_config_tmpl = R"EOF(
name: {}
virtual_hosts:
- name: integration
domains: ["*"]
routes:
- match: {{ prefix: "/meh" }}
route: {{ cluster: {} }}
)EOF";
on_server_init_function_ = [&]() {
createScopedRdsStream();
sendSrdsResponse({scope_route1}, {scope_route1}, {}, "1");
createRdsStream("foo_route1");
// CreateRdsStream waits for connection which is fired by RDS subscription.
sendRdsResponse(fmt::format(route_config_tmpl, "foo_route1", "cluster_0"), "1");
};
initialize();
registerTestServerPorts({"http"});
// No virtual host matches "neh".
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeHeaderOnlyRequest(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/neh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", "x-foo-key=foo"}});
response->waitForEndStream();
verifyResponse(std::move(response), "404", Http::TestResponseHeaderMapImpl{}, "");
cleanupUpstreamAndDownstream();
}
// With on demand update filter configured, scope match but virtual host don't match, should still
// return 404
TEST_P(ScopedRdsIntegrationTest, OnDemandUpdateVirtualHostNotMatch) {
config_helper_.addFilter(R"EOF(
name: envoy.filters.http.on_demand
)EOF");
const std::string scope_route1 = R"EOF(
name: foo_scope
route_configuration_name: foo_route1
key:
fragments:
- string_key: foo
)EOF";
const std::string scope_route2 = R"EOF(
name: bar_scope
route_configuration_name: foo_route1
on_demand: true
key:
fragments:
- string_key: bar
)EOF";
const std::string route_config_tmpl = R"EOF(
name: {}
virtual_hosts:
- name: integration
domains: ["*"]
routes:
- match: {{ prefix: "/meh" }}
route: {{ cluster: {} }}
)EOF";
on_server_init_function_ = [&]() {
createScopedRdsStream();
sendSrdsResponse({scope_route1, scope_route2}, {scope_route1, scope_route2}, {}, "1");
createRdsStream("foo_route1");
// CreateRdsStream waits for connection which is fired by RDS subscription.
sendRdsResponse(fmt::format(route_config_tmpl, "foo_route1", "cluster_0"), "1");
};
initialize();
registerTestServerPorts({"http"});
// No scope key matches "bar".
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeHeaderOnlyRequest(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/neh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", "x-foo-key=bar"}});
response->waitForEndStream();
verifyResponse(std::move(response), "404", Http::TestResponseHeaderMapImpl{}, "");
cleanupUpstreamAndDownstream();
}
// Eager and lazy scopes share the same route configuration
TEST_P(ScopedRdsIntegrationTest, DifferentPriorityScopeShareRoute) {
config_helper_.addFilter(R"EOF(
name: envoy.filters.http.on_demand
)EOF");
const std::string scope_route1 = R"EOF(
name: foo_scope
route_configuration_name: foo_route1
key:
fragments:
- string_key: foo
)EOF";
const std::string scope_route2 = R"EOF(
name: bar_scope
route_configuration_name: foo_route1
on_demand: true
key:
fragments:
- string_key: bar
)EOF";
const std::string route_config_tmpl = R"EOF(
name: {}
virtual_hosts:
- name: integration
domains: ["*"]
routes:
- match: {{ prefix: "/" }}
route: {{ cluster: {} }}
)EOF";
on_server_init_function_ = [&]() {
createScopedRdsStream();
sendSrdsResponse({scope_route1, scope_route2}, {scope_route1, scope_route2}, {}, "1");
createRdsStream("foo_route1");
// CreateRdsStream waits for connection which is fired by RDS subscription.
sendRdsResponse(fmt::format(route_config_tmpl, "foo_route1", "cluster_0"), "1");
};
initialize();
registerTestServerPorts({"http"});
codec_client_ = makeHttpConnection(lookupPort("http"));
test_server_->waitForCounterGe("http.config_test.rds.foo_route1.update_success", 1);
cleanupUpstreamAndDownstream();
// "foo" request should succeed because the foo scope is loaded eagerly by default.
// "bar" request will initialize rds provider on demand and also succeed.
for (const std::string& scope_key : std::vector<std::string>{"foo", "bar"}) {
sendRequestAndVerifyResponse(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/meh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", fmt::format("x-foo-key={}", scope_key)}},
456, Http::TestResponseHeaderMapImpl{{":status", "200"}, {"service", scope_key}}, 123, 0);
}
}
TEST_P(ScopedRdsIntegrationTest, OnDemandUpdateAfterActiveStreamDestroyed) {
config_helper_.addFilter(R"EOF(
name: envoy.filters.http.on_demand
)EOF");
const std::string scope_route1 = R"EOF(
name: foo_scope1
route_configuration_name: foo_route1
on_demand: true
key:
fragments:
- string_key: foo
)EOF";
on_server_init_function_ = [this, &scope_route1]() {
createScopedRdsStream();
sendSrdsResponse({scope_route1}, {scope_route1}, {}, "1");
};
initialize();
registerTestServerPorts({"http"});
const std::string route_config_tmpl = R"EOF(
name: {}
virtual_hosts:
- name: integration
domains: ["*"]
routes:
- match: {{ prefix: "/" }}
route: {{ cluster: {} }}
)EOF";
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
// A request that match lazily loaded scope will trigger on demand loading.
auto response = codec_client_->makeHeaderOnlyRequest(
Http::TestRequestHeaderMapImpl{{":method", "GET"},
{":path", "/meh"},
{":authority", "host"},
{":scheme", "http"},
{"Addr", "x-foo-key=foo"}});
test_server_->waitForCounterGe("http.config_test.rds.foo_route1.update_attempt", 1);
// Close the connection and destroy the active stream.
cleanupUpstreamAndDownstream();
// Push rds update, on demand updated callback is post to worker thread.
// There is no exception thrown even when active stream is dead because weak_ptr can't be locked.
createRdsStream("foo_route1");
sendRdsResponse(fmt::format(route_config_tmpl, "foo_route1", "cluster_0"), "1");
test_server_->waitForCounterGe("http.config_test.rds.foo_route1.update_success", 1);
}
} // namespace
} // namespace Envoy