forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vhds_integration_test.cc
681 lines (586 loc) · 29.7 KB
/
vhds_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
#include "envoy/config/route/v3/route.pb.h"
#include "envoy/config/route/v3/route_components.pb.h"
#include "envoy/grpc/status.h"
#include "envoy/stats/scope.h"
#include "common/config/protobuf_link_hacks.h"
#include "common/protobuf/protobuf.h"
#include "common/protobuf/utility.h"
#include "test/common/grpc/grpc_client_integration.h"
#include "test/integration/http_integration.h"
#include "test/integration/utility.h"
#include "test/test_common/network_utility.h"
#include "test/test_common/resources.h"
#include "test/test_common/simulated_time_system.h"
#include "test/test_common/utility.h"
#include "absl/synchronization/notification.h"
#include "gtest/gtest.h"
using testing::AssertionResult;
namespace Envoy {
namespace {
const char Config[] = R"EOF(
admin:
access_log_path: /dev/null
address:
socket_address:
address: 127.0.0.1
port_value: 0
static_resources:
clusters:
- name: xds_cluster
type: STATIC
http2_protocol_options: {}
load_assignment:
cluster_name: xds_cluster
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 127.0.0.1
port_value: 0
- name: my_service
type: STATIC
http2_protocol_options: {}
load_assignment:
cluster_name: my_service
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 127.0.0.1
port_value: 0
listeners:
- name: http
address:
socket_address:
address: 127.0.0.1
port_value: 0
filter_chains:
- filters:
- name: http
typed_config:
"@type": type.googleapis.com/envoy.config.filter.network.http_connection_manager.v2.HttpConnectionManager
stat_prefix: config_test
http_filters:
- name: envoy.filters.http.on_demand
- name: envoy.filters.http.router
codec_type: HTTP2
rds:
route_config_name: my_route
config_source:
api_config_source:
api_type: GRPC
grpc_services:
envoy_grpc:
cluster_name: xds_cluster
)EOF";
// TODO (dmitri-d) move config yaml into ConfigHelper
const char RdsWithoutVhdsConfig[] = R"EOF(
name: my_route
virtual_hosts:
- name: vhost_rds1
domains: ["vhost.rds.first"]
routes:
- match: { prefix: "/rdsone" }
route: { cluster: my_service }
)EOF";
const char RdsConfig[] = R"EOF(
name: my_route
vhds:
config_source:
api_config_source:
api_type: DELTA_GRPC
grpc_services:
envoy_grpc:
cluster_name: xds_cluster
)EOF";
const char RdsConfigWithVhosts[] = R"EOF(
name: my_route
virtual_hosts:
- name: vhost_rds1
domains: ["vhost.rds.first"]
routes:
- match: { prefix: "/rdsone" }
route: { cluster: my_service }
vhds:
config_source:
api_config_source:
api_type: DELTA_GRPC
grpc_services:
envoy_grpc:
cluster_name: xds_cluster
)EOF";
const std::string RouteConfigName = "my_route";
const char VhostTemplate[] = R"EOF(
name: {}
domains: [{}]
routes:
- match: {{ prefix: "/" }}
route: {{ cluster: "my_service" }}
)EOF";
class VhdsInitializationTest : public HttpIntegrationTest,
public Grpc::GrpcClientIntegrationParamTest {
public:
VhdsInitializationTest()
: HttpIntegrationTest(Http::CodecClient::Type::HTTP2, ipVersion(), realTime(), Config) {
use_lds_ = false;
}
void TearDown() override { cleanUpXdsConnection(); }
// Overridden to insert this stuff into the initialize() at the very beginning of
// HttpIntegrationTest::testRouterRequestAndResponseWithBody().
void initialize() override {
// Controls how many fake_upstreams_.emplace_back(new FakeUpstream) will happen in
// BaseIntegrationTest::createUpstreams() (which is part of initialize()).
// Make sure this number matches the size of the 'clusters' repeated field in the bootstrap
// config that you use!
setUpstreamCount(2); // the CDS cluster
setUpstreamProtocol(FakeHttpConnection::Type::HTTP2); // CDS uses gRPC uses HTTP2.
// BaseIntegrationTest::initialize() does many things:
// 1) It appends to fake_upstreams_ as many as you asked for via setUpstreamCount().
// 2) It updates your bootstrap config with the ports your fake upstreams are actually listening
// on (since you're supposed to leave them as 0).
// 3) It creates and starts an IntegrationTestServer - the thing that wraps the almost-actual
// Envoy used in the tests.
// 4) Bringing up the server usually entails waiting to ensure that any listeners specified in
// the bootstrap config have come up, and registering them in a port map (see lookupPort()).
// However, this test needs to defer all of that to later.
defer_listener_finalization_ = true;
HttpIntegrationTest::initialize();
// Now that the upstream has been created, process Envoy's request to discover it.
// (First, we have to let Envoy establish its connection to the RDS server.)
AssertionResult result = // xds_connection_ is filled with the new FakeHttpConnection.
fake_upstreams_[0]->waitForHttpConnection(*dispatcher_, xds_connection_);
RELEASE_ASSERT(result, result.message());
result = xds_connection_->waitForNewStream(*dispatcher_, xds_stream_);
RELEASE_ASSERT(result, result.message());
xds_stream_->startGrpcStream();
fake_upstreams_[0]->set_allow_unexpected_disconnects(true);
EXPECT_TRUE(compareSotwDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "",
{"my_route"}, true));
sendSotwDiscoveryResponse<envoy::config::route::v3::RouteConfiguration>(
Config::TypeUrl::get().RouteConfiguration,
{TestUtility::parseYaml<envoy::config::route::v3::RouteConfiguration>(
RdsWithoutVhdsConfig)},
"1");
// Wait for our statically specified listener to become ready, and register its port in the
// test framework's downstream listener port map.
test_server_->waitUntilListenersReady();
registerTestServerPorts({"http"});
}
FakeStreamPtr vhds_stream_;
};
INSTANTIATE_TEST_SUITE_P(IpVersionsClientType, VhdsInitializationTest,
GRPC_CLIENT_INTEGRATION_PARAMS);
// tests a scenario when:
// - RouteConfiguration without VHDS is received
// - RouteConfiguration update with VHDS configuration in it is received
// - Upstream makes a request to a VirtualHost in the VHDS update
TEST_P(VhdsInitializationTest, InitializeVhdsAfterRdsHasBeenInitialized) {
// Calls our initialize(), which includes establishing a listener, route, and cluster.
testRouterHeaderOnlyRequestAndResponse(nullptr, 1, "/rdsone", "vhost.rds.first");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// Update RouteConfig, this time include VHDS config
sendSotwDiscoveryResponse<envoy::config::route::v3::RouteConfiguration>(
Config::TypeUrl::get().RouteConfiguration,
{TestUtility::parseYaml<envoy::config::route::v3::RouteConfiguration>(RdsConfigWithVhosts)},
"2");
auto result = xds_connection_->waitForNewStream(*dispatcher_, vhds_stream_, true);
RELEASE_ASSERT(result, result.message());
vhds_stream_->startGrpcStream();
EXPECT_TRUE(
compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost, {}, {}, vhds_stream_));
sendDeltaDiscoveryResponse<envoy::config::route::v3::VirtualHost>(
Config::TypeUrl::get().VirtualHost,
{TestUtility::parseYaml<envoy::config::route::v3::VirtualHost>(
fmt::format(VhostTemplate, "vhost_0", "vhost.first"))},
{}, "1", vhds_stream_);
EXPECT_TRUE(
compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost, {}, {}, vhds_stream_));
// Confirm vhost.first that was configured via VHDS is reachable
testRouterHeaderOnlyRequestAndResponse(nullptr, 1, "/", "vhost.first");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
}
class VhdsIntegrationTest : public HttpIntegrationTest,
public Grpc::GrpcClientIntegrationParamTest {
public:
VhdsIntegrationTest()
: HttpIntegrationTest(Http::CodecClient::Type::HTTP2, ipVersion(), realTime(), Config) {
use_lds_ = false;
}
void TearDown() override { cleanUpXdsConnection(); }
std::string virtualHostYaml(const std::string& name, const std::string& domain) {
return fmt::format(VhostTemplate, name, domain);
}
std::string vhdsRequestResourceName(const std::string& host_header) {
return RouteConfigName + "/" + host_header;
}
envoy::config::route::v3::VirtualHost buildVirtualHost() {
return TestUtility::parseYaml<envoy::config::route::v3::VirtualHost>(
virtualHostYaml("vhost_0", "host"));
}
std::vector<envoy::config::route::v3::VirtualHost> buildVirtualHost1() {
return {TestUtility::parseYaml<envoy::config::route::v3::VirtualHost>(
virtualHostYaml("vhost_1", "vhost.first")),
TestUtility::parseYaml<envoy::config::route::v3::VirtualHost>(
virtualHostYaml("vhost_2", "vhost.second"))};
}
envoy::config::route::v3::VirtualHost buildVirtualHost2() {
return TestUtility::parseYaml<envoy::config::route::v3::VirtualHost>(
virtualHostYaml("vhost_1", "vhost.first"));
}
// Overridden to insert this stuff into the initialize() at the very beginning of
// HttpIntegrationTest::testRouterRequestAndResponseWithBody().
void initialize() override {
// Controls how many fake_upstreams_.emplace_back(new FakeUpstream) will happen in
// BaseIntegrationTest::createUpstreams() (which is part of initialize()).
// Make sure this number matches the size of the 'clusters' repeated field in the bootstrap
// config that you use!
setUpstreamCount(2); // the CDS cluster
setUpstreamProtocol(FakeHttpConnection::Type::HTTP2); // CDS uses gRPC uses HTTP2.
// BaseIntegrationTest::initialize() does many things:
// 1) It appends to fake_upstreams_ as many as you asked for via setUpstreamCount().
// 2) It updates your bootstrap config with the ports your fake upstreams are actually listening
// on (since you're supposed to leave them as 0).
// 3) It creates and starts an IntegrationTestServer - the thing that wraps the almost-actual
// Envoy used in the tests.
// 4) Bringing up the server usually entails waiting to ensure that any listeners specified in
// the bootstrap config have come up, and registering them in a port map (see lookupPort()).
// However, this test needs to defer all of that to later.
defer_listener_finalization_ = true;
HttpIntegrationTest::initialize();
// Now that the upstream has been created, process Envoy's request to discover it.
// (First, we have to let Envoy establish its connection to the RDS server.)
AssertionResult result = // xds_connection_ is filled with the new FakeHttpConnection.
fake_upstreams_[0]->waitForHttpConnection(*dispatcher_, xds_connection_);
RELEASE_ASSERT(result, result.message());
result = xds_connection_->waitForNewStream(*dispatcher_, xds_stream_);
RELEASE_ASSERT(result, result.message());
xds_stream_->startGrpcStream();
fake_upstreams_[0]->set_allow_unexpected_disconnects(true);
EXPECT_TRUE(compareSotwDiscoveryRequest(Config::TypeUrl::get().RouteConfiguration, "",
{"my_route"}, true));
sendSotwDiscoveryResponse<envoy::config::route::v3::RouteConfiguration>(
Config::TypeUrl::get().RouteConfiguration, {rdsConfig()}, "1");
result = xds_connection_->waitForNewStream(*dispatcher_, vhds_stream_, true);
RELEASE_ASSERT(result, result.message());
vhds_stream_->startGrpcStream();
EXPECT_TRUE(
compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost, {}, {}, vhds_stream_));
sendDeltaDiscoveryResponse<envoy::config::route::v3::VirtualHost>(
Config::TypeUrl::get().VirtualHost, {buildVirtualHost()}, {}, "1", vhds_stream_);
EXPECT_TRUE(
compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost, {}, {}, vhds_stream_));
// Wait for our statically specified listener to become ready, and register its port in the
// test framework's downstream listener port map.
test_server_->waitUntilListenersReady();
registerTestServerPorts({"http"});
}
void useRdsWithVhosts() { use_rds_with_vhosts = true; }
const envoy::config::route::v3::RouteConfiguration rdsConfig() const {
return TestUtility::parseYaml<envoy::config::route::v3::RouteConfiguration>(
use_rds_with_vhosts ? RdsConfigWithVhosts : RdsConfig);
}
void notifyAboutAliasResolutionFailure(const std::string& version, FakeStreamPtr& stream,
const std::vector<std::string>& aliases = {}) {
envoy::api::v2::DeltaDiscoveryResponse response;
response.set_system_version_info("system_version_info_this_is_a_test");
response.set_type_url(Config::TypeUrl::get().VirtualHost);
auto* resource = response.add_resources();
resource->set_name("cannot-resolve-alias");
resource->set_version(version);
for (const auto& alias : aliases) {
resource->add_aliases(alias);
}
response.set_nonce("noncense");
stream->sendGrpcMessage(response);
}
void sendDeltaDiscoveryResponseWithUnresolvedAliases(
const std::vector<envoy::config::route::v3::VirtualHost>& added_or_updated,
const std::vector<std::string>& removed, const std::string& version, FakeStreamPtr& stream,
const std::vector<std::string>& aliases, const std::vector<std::string>& unresolved_aliases) {
auto response = createDeltaDiscoveryResponse<envoy::config::route::v3::VirtualHost>(
Config::TypeUrl::get().VirtualHost, added_or_updated, removed, version, aliases);
for (const auto& unresolved_alias : unresolved_aliases) {
auto* resource = response.add_resources();
resource->set_name(unresolved_alias);
resource->set_version(version);
resource->add_aliases(unresolved_alias);
}
stream->sendGrpcMessage(response);
}
// used in VhdsOnDemandUpdateWithResourceNameAsAlias test
// to create a DeltaDiscoveryResponse with a resource name matching the value used to create an
// on-demand request
envoy::api::v2::DeltaDiscoveryResponse createDeltaDiscoveryResponseWithResourceNameUsedAsAlias() {
API_NO_BOOST(envoy::api::v2::DeltaDiscoveryResponse) ret;
ret.set_system_version_info("system_version_info_this_is_a_test");
ret.set_type_url(Config::TypeUrl::get().VirtualHost);
auto* resource = ret.add_resources();
resource->set_name("my_route/vhost_1");
resource->set_version("4");
resource->mutable_resource()->PackFrom(
API_DOWNGRADE(TestUtility::parseYaml<envoy::config::route::v3::VirtualHost>(
virtualHostYaml("my_route/vhost_1", "vhost_1, vhost.first"))));
resource->add_aliases("my_route/vhost.first");
ret.set_nonce("test-nonce-0");
return ret;
}
FakeStreamPtr vhds_stream_;
bool use_rds_with_vhosts{false};
};
INSTANTIATE_TEST_SUITE_P(IpVersionsClientType, VhdsIntegrationTest, GRPC_CLIENT_INTEGRATION_PARAMS);
TEST_P(VhdsIntegrationTest, RdsUpdateWithoutVHDSChangesDoesNotRestartVHDS) {
testRouterHeaderOnlyRequestAndResponse(nullptr, 1, "/", "host");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// Update RouteConfig, but don't change VHDS config
sendSotwDiscoveryResponse<envoy::config::route::v3::RouteConfiguration>(
Config::TypeUrl::get().RouteConfiguration,
{TestUtility::parseYaml<envoy::config::route::v3::RouteConfiguration>(RdsConfigWithVhosts)},
"2");
// Confirm vhost_0 that was originally configured via VHDS is reachable
testRouterHeaderOnlyRequestAndResponse(nullptr, 1, "/", "host");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
}
// tests a scenario when:
// - a spontaneous VHDS DiscoveryResponse adds two virtual hosts
// - the next spontaneous VHDS DiscoveryResponse removes newly added virtual hosts
// - Upstream makes a request to an (now) unknown domain
// - A VHDS DiscoveryResponse received containing update for the domain
// - Upstream receives a 200 response
TEST_P(VhdsIntegrationTest, VhdsVirtualHostAddUpdateRemove) {
// Calls our initialize(), which includes establishing a listener, route, and cluster.
testRouterHeaderOnlyRequestAndResponse(nullptr, 1);
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// A spontaneous VHDS DiscoveryResponse adds two virtual hosts
sendDeltaDiscoveryResponse<envoy::config::route::v3::VirtualHost>(
Config::TypeUrl::get().VirtualHost, buildVirtualHost1(), {}, "2", vhds_stream_);
EXPECT_TRUE(
compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost, {}, {}, vhds_stream_));
testRouterHeaderOnlyRequestAndResponse(nullptr, 1, "/one", "vhost.first");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
testRouterHeaderOnlyRequestAndResponse(nullptr, 1, "/two", "vhost.second");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// A spontaneous VHDS DiscoveryResponse removes newly added virtual hosts
sendDeltaDiscoveryResponse<envoy::config::route::v3::VirtualHost>(
Config::TypeUrl::get().VirtualHost, {}, {"vhost_1", "vhost_2"}, "3", vhds_stream_);
EXPECT_TRUE(
compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost, {}, {}, vhds_stream_));
// an upstream request to an (now) unknown domain
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
Http::TestRequestHeaderMapImpl request_headers{{":method", "GET"},
{":path", "/"},
{":scheme", "http"},
{":authority", "vhost.first"},
{"x-lyft-user-id", "123"}};
IntegrationStreamDecoderPtr response = codec_client_->makeHeaderOnlyRequest(request_headers);
EXPECT_TRUE(compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost,
{vhdsRequestResourceName("vhost.first")}, {},
vhds_stream_));
sendDeltaDiscoveryResponse<envoy::config::route::v3::VirtualHost>(
Config::TypeUrl::get().VirtualHost, {buildVirtualHost2()}, {}, "4", vhds_stream_,
{"my_route/vhost.first"});
waitForNextUpstreamRequest(1);
// 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().getStatusValue());
cleanupUpstreamAndDownstream();
}
// tests a scenario when:
// - an RDS exchange contains a non-empty virtual_hosts array
// - a spontaneous VHDS DiscoveryResponse adds two virtual hosts
// - the next spontaneous VHDS DiscoveryResponse removes newly added virtual hosts
// - Upstream makes a request to an (now) unknown domain
// - A VHDS DiscoveryResponse received containing update for the domain
// - Upstream receives a 200 response
TEST_P(VhdsIntegrationTest, RdsWithVirtualHostsVhdsVirtualHostAddUpdateRemove) {
// RDS exchange with a non-empty virtual_hosts field
useRdsWithVhosts();
testRouterHeaderOnlyRequestAndResponse(nullptr, 1);
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// A spontaneous VHDS DiscoveryResponse adds two virtual hosts
sendDeltaDiscoveryResponse<envoy::config::route::v3::VirtualHost>(
Config::TypeUrl::get().VirtualHost, buildVirtualHost1(), {}, "2", vhds_stream_);
EXPECT_TRUE(
compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost, {}, {}, vhds_stream_));
// verify that rds-based virtual host can be resolved
testRouterHeaderOnlyRequestAndResponse(nullptr, 1, "/rdsone", "vhost.rds.first");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
testRouterHeaderOnlyRequestAndResponse(nullptr, 1, "/one", "vhost.first");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
testRouterHeaderOnlyRequestAndResponse(nullptr, 1, "/two", "vhost.second");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// A spontaneous VHDS DiscoveryResponse removes virtual hosts added via vhds
sendDeltaDiscoveryResponse<envoy::config::route::v3::VirtualHost>(
Config::TypeUrl::get().VirtualHost, {}, {"vhost_1", "vhost_2"}, "3", vhds_stream_);
EXPECT_TRUE(
compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost, {}, {}, vhds_stream_));
// verify rds-based virtual host is still present
testRouterHeaderOnlyRequestAndResponse(nullptr, 1, "/rdsone", "vhost.rds.first");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
Http::TestRequestHeaderMapImpl request_headers{{":method", "GET"},
{":path", "/"},
{":scheme", "http"},
{":authority", "vhost.first"},
{"x-lyft-user-id", "123"}};
IntegrationStreamDecoderPtr response = codec_client_->makeHeaderOnlyRequest(request_headers);
EXPECT_TRUE(compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost,
{vhdsRequestResourceName("vhost.first")}, {},
vhds_stream_));
sendDeltaDiscoveryResponse<envoy::config::route::v3::VirtualHost>(
Config::TypeUrl::get().VirtualHost, {buildVirtualHost2()}, {}, "4", vhds_stream_,
{"my_route/vhost.first"});
waitForNextUpstreamRequest(1);
// 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().getStatusValue());
cleanupUpstreamAndDownstream();
}
// tests a scenario where:
// a Resource received in a DeltaDiscoveryResponse has name that matches the value used in the
// on-demand request
TEST_P(VhdsIntegrationTest, VhdsOnDemandUpdateWithResourceNameAsAlias) {
// RDS exchange with a non-empty virtual_hosts field
useRdsWithVhosts();
testRouterHeaderOnlyRequestAndResponse(nullptr, 1);
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// verify that rds-based virtual host can be resolved
testRouterHeaderOnlyRequestAndResponse(nullptr, 1, "/rdsone", "vhost.rds.first");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// Attempt to make a request to an unknown host
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
Http::TestRequestHeaderMapImpl request_headers{{":method", "GET"},
{":path", "/"},
{":scheme", "http"},
{":authority", "vhost_1"},
{"x-lyft-user-id", "123"}};
IntegrationStreamDecoderPtr response = codec_client_->makeHeaderOnlyRequest(request_headers);
EXPECT_TRUE(compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost,
{vhdsRequestResourceName("vhost_1")}, {}, vhds_stream_));
envoy::api::v2::DeltaDiscoveryResponse vhds_update =
createDeltaDiscoveryResponseWithResourceNameUsedAsAlias();
vhds_stream_->sendGrpcMessage(vhds_update);
waitForNextUpstreamRequest(1);
// 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().getStatusValue());
cleanupUpstreamAndDownstream();
}
// tests a scenario when:
// - an RDS exchange contains a non-empty virtual_hosts array
// - a spontaneous VHDS DiscoveryResponse adds two virtual hosts
// - the next spontaneous VHDS DiscoveryResponse removes newly added virtual hosts
// - Upstream makes a request to an (now) unknown domain
// - A VHDS DiscoveryResponse received but contains no update for the domain (the management server
// couldn't resolve it)
// - Upstream receives a 404 response
TEST_P(VhdsIntegrationTest, VhdsOnDemandUpdateFailToResolveTheAlias) {
// RDS exchange with a non-empty virtual_hosts field
useRdsWithVhosts();
testRouterHeaderOnlyRequestAndResponse(nullptr, 1);
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// verify that rds-based virtual host can be resolved
testRouterHeaderOnlyRequestAndResponse(nullptr, 1, "/rdsone", "vhost.rds.first");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// Attempt to make a request to an unknown host
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
Http::TestRequestHeaderMapImpl request_headers{{":method", "GET"},
{":path", "/"},
{":scheme", "http"},
{":authority", "vhost.third"},
{"x-lyft-user-id", "123"}};
IntegrationStreamDecoderPtr response = codec_client_->makeHeaderOnlyRequest(request_headers);
EXPECT_TRUE(compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost,
{vhdsRequestResourceName("vhost.third")}, {},
vhds_stream_));
// Send an empty response back (the management server isn't aware of vhost.third)
notifyAboutAliasResolutionFailure("4", vhds_stream_, {"my_route/vhost.third"});
response->waitForHeaders();
EXPECT_EQ("404", response->headers().getStatusValue());
cleanupUpstreamAndDownstream();
}
// tests a scenario when:
// - an RDS exchange contains a non-empty virtual_hosts array
// - a spontaneous VHDS DiscoveryResponse adds two virtual hosts
// - the next spontaneous VHDS DiscoveryResponse removes newly added virtual hosts
// - Upstream makes a request to an (now) unknown domain
// - A VHDS DiscoveryResponse received that contains update for vhost.first host, but vhost.third
// couldn't be resolved
// - Upstream receives a 404 response
TEST_P(VhdsIntegrationTest, VhdsOnDemandUpdateFailToResolveOneAliasOutOfSeveral) {
// RDS exchange with a non-empty virtual_hosts field
useRdsWithVhosts();
testRouterHeaderOnlyRequestAndResponse(nullptr, 1);
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// verify that rds-based virtual host can be resolved
testRouterHeaderOnlyRequestAndResponse(nullptr, 1, "/rdsone", "vhost.rds.first");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// Attempt to make a request to an unknown host
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
Http::TestRequestHeaderMapImpl request_headers{{":method", "GET"},
{":path", "/"},
{":scheme", "http"},
{":authority", "vhost.third"},
{"x-lyft-user-id", "123"}};
IntegrationStreamDecoderPtr response = codec_client_->makeHeaderOnlyRequest(request_headers);
EXPECT_TRUE(compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost,
{vhdsRequestResourceName("vhost.third")}, {},
vhds_stream_));
// Send an empty response back (the management server isn't aware of vhost.third)
sendDeltaDiscoveryResponseWithUnresolvedAliases({buildVirtualHost2()}, {}, "4", vhds_stream_,
{"vhost.first"}, {"my_route/vhost.third"});
response->waitForHeaders();
EXPECT_EQ("404", response->headers().getStatusValue());
cleanupUpstreamAndDownstream();
}
// Verify that an vhds update succeeds even when the client closes its connection
TEST_P(VhdsIntegrationTest, VhdsOnDemandUpdateHttpConnectionCloses) {
// RDS exchange with a non-empty virtual_hosts field
useRdsWithVhosts();
testRouterHeaderOnlyRequestAndResponse(nullptr, 1);
cleanupUpstreamAndDownstream();
EXPECT_TRUE(codec_client_->waitForDisconnect());
// Attempt to make a request to an unknown host
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
Http::TestRequestHeaderMapImpl request_headers{{":method", "GET"},
{":path", "/"},
{":scheme", "http"},
{":authority", "vhost_1"},
{"x-lyft-user-id", "123"}};
auto encoder_decoder = codec_client_->startRequest(request_headers);
Http::RequestEncoder& encoder = encoder_decoder.first;
IntegrationStreamDecoderPtr response = std::move(encoder_decoder.second);
EXPECT_TRUE(compareDeltaDiscoveryRequest(Config::TypeUrl::get().VirtualHost,
{vhdsRequestResourceName("vhost_1")}, {}, vhds_stream_));
envoy::api::v2::DeltaDiscoveryResponse vhds_update =
createDeltaDiscoveryResponseWithResourceNameUsedAsAlias();
vhds_stream_->sendGrpcMessage(vhds_update);
codec_client_->sendReset(encoder);
response->waitForReset();
EXPECT_TRUE(codec_client_->connected());
cleanupUpstreamAndDownstream();
}
} // namespace
} // namespace Envoy