This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
leds_integration_test.cc
819 lines (687 loc) · 40.3 KB
/
leds_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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
#include "envoy/config/cluster/v3/cluster.pb.h"
#include "envoy/config/core/v3/health_check.pb.h"
#include "envoy/config/endpoint/v3/endpoint.pb.h"
#include "envoy/type/v3/http.pb.h"
#include "test/common/grpc/grpc_client_integration.h"
#include "test/config/utility.h"
#include "test/integration/http_integration.h"
#include "test/test_common/network_utility.h"
#include "gtest/gtest.h"
namespace Envoy {
namespace {
// Integration test for LEDS features. CDS is consumed vi filesystem subscription,
// and EDS and LEDS are consumed via using the delta-xDS gRPC protocol.
class LedsIntegrationTest : public Grpc::GrpcClientIntegrationParamTest,
public HttpIntegrationTest {
protected:
struct FakeUpstreamInfo {
FakeHttpConnectionPtr connection_;
FakeUpstream* upstream_{};
absl::flat_hash_map<std::string, FakeStreamPtr> stream_by_resource_name_;
static constexpr char default_stream_name[] = "default";
// Used for cases where only a single stream is needed.
FakeStreamPtr& defaultStream() { return stream_by_resource_name_[default_stream_name]; }
};
LedsIntegrationTest()
: HttpIntegrationTest(Http::CodecClient::Type::HTTP1, ipVersion()),
codec_client_type_(envoy::type::v3::HTTP1) {
use_lds_ = false;
create_xds_upstream_ = false;
// LEDS is only supported by delta-xDS.
sotw_or_delta_ = Grpc::SotwOrDelta::Delta;
}
~LedsIntegrationTest() override {
// First disconnect upstream connections to avoid FIN messages causing unexpected
// disconnects on the fake servers.
for (auto& host_upstream_info : hosts_upstreams_info_) {
resetFakeUpstreamInfo(host_upstream_info);
}
resetFakeUpstreamInfo(leds_upstream_info_);
resetFakeUpstreamInfo(eds_upstream_info_);
}
// A helper function to set the endpoints health status.
void setEndpointsHealthStatus(
const absl::flat_hash_set<uint32_t>& endpoints_idxs,
envoy::config::core::v3::HealthStatus health_status, absl::string_view collection_prefix,
absl::flat_hash_map<std::string, envoy::config::endpoint::v3::LbEndpoint>&
updated_endpoints) {
for (const auto endpoint_idx : endpoints_idxs) {
const std::string endpoint_name = absl::StrCat(collection_prefix, "endpoint", endpoint_idx);
envoy::config::endpoint::v3::LbEndpoint endpoint;
// Shift fake_upstreams_ by 2 (due to EDS and LEDS fake upstreams).
setUpstreamAddress(endpoint_idx + 2, endpoint);
endpoint.set_health_status(health_status);
updated_endpoints.emplace(endpoint_name, endpoint);
}
}
// Sets endpoints in a specific locality (using the LEDS helper).
// We need to supply the endpoints via LEDS to provide health status. Use a
// filesystem delivery to simplify test mechanics.
void setEndpoints(const absl::flat_hash_set<uint32_t>& healthy_endpoints_idxs,
const absl::flat_hash_set<uint32_t>& degraded_endpoints_idxs,
const absl::flat_hash_set<uint32_t>& unhealthy_endpoints_idxs,
const absl::flat_hash_set<uint32_t>& unknown_endpoints_idxs,
const absl::flat_hash_set<uint32_t>& removed_endpoints_idxs,
uint32_t locality_idx = 0, bool await_update = true) {
const auto& collection_prefix = localities_prefixes_[locality_idx];
absl::flat_hash_map<std::string, envoy::config::endpoint::v3::LbEndpoint> updated_endpoints;
std::vector<std::string> removed_endpoints;
setEndpointsHealthStatus(healthy_endpoints_idxs, envoy::config::core::v3::HEALTHY,
collection_prefix, updated_endpoints);
setEndpointsHealthStatus(degraded_endpoints_idxs, envoy::config::core::v3::DEGRADED,
collection_prefix, updated_endpoints);
setEndpointsHealthStatus(unhealthy_endpoints_idxs, envoy::config::core::v3::UNHEALTHY,
collection_prefix, updated_endpoints);
setEndpointsHealthStatus(unknown_endpoints_idxs, envoy::config::core::v3::UNKNOWN,
collection_prefix, updated_endpoints);
for (const auto removed_endpoint_idx : removed_endpoints_idxs) {
const std::string endpoint_name =
absl::StrCat(collection_prefix, "endpoint", removed_endpoint_idx);
removed_endpoints.emplace_back(endpoint_name);
}
sendDeltaLedsResponse(updated_endpoints, removed_endpoints, "7", locality_idx);
if (await_update) {
// Receive LEDS ack.
EXPECT_TRUE(compareDeltaDiscoveryRequest(
Config::TypeUrl::get().LbEndpoint, {}, {},
leds_upstream_info_.stream_by_resource_name_[localities_prefixes_[locality_idx]]));
}
}
// Sends an LEDS response to a specific locality, containing the updated
// endpoints map (resource name to endpoint data), and the list of resource
// names to remove.
void sendDeltaLedsResponse(
const absl::flat_hash_map<std::string, envoy::config::endpoint::v3::LbEndpoint>&
to_update_map,
const std::vector<std::string>& to_delete_list, const std::string& version,
uint32_t locality_idx) {
auto& locality_stream =
leds_upstream_info_.stream_by_resource_name_[localities_prefixes_[locality_idx]];
ASSERT(locality_stream != nullptr);
envoy::service::discovery::v3::DeltaDiscoveryResponse response;
response.set_system_version_info(version);
response.set_type_url(Config::TypeUrl::get().LbEndpoint);
for (const auto& endpoint_name : to_delete_list) {
*response.add_removed_resources() = endpoint_name;
}
for (const auto& [resource_name, lb_endpoint] : to_update_map) {
auto* resource = response.add_resources();
resource->set_name(resource_name);
resource->set_version(version);
resource->mutable_resource()->PackFrom(lb_endpoint);
}
locality_stream->sendGrpcMessage(response);
}
void createUpstreams() override {
// Add the EDS upstream.
eds_upstream_info_.upstream_ = &addFakeUpstream(FakeHttpConnection::Type::HTTP2);
// Add the LEDS upstream.
leds_upstream_info_.upstream_ = &addFakeUpstream(FakeHttpConnection::Type::HTTP2);
// Create backends and initialize their wrapper.
HttpIntegrationTest::createUpstreams();
// Store all hosts upstreams info in a single place so it would be easily
// accessible.
ASSERT(fake_upstreams_.size() == fake_upstreams_count_ + 2);
hosts_upstreams_info_.reserve(fake_upstreams_count_);
// Skip the first 2 fake upstreams as they are reserved for EDS and LEDS.
for (size_t i = 2; i < fake_upstreams_.size(); ++i) {
FakeUpstreamInfo host_info;
host_info.upstream_ = &(*fake_upstreams_[i]);
hosts_upstreams_info_.emplace_back(std::move(host_info));
}
}
// Initialize a gRPC stream of an upstream server.
void initializeStream(FakeUpstreamInfo& upstream_info,
const std::string& resource_name = FakeUpstreamInfo::default_stream_name) {
if (!upstream_info.connection_) {
auto 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();
}
// A specific function to initialize LEDS streams. This was introduced to
// handle the non-deterministic requests order when more than one locality is
// used. This method first establishes the gRPC stream, fetches the first
// request and reads its requested resource name, and then assigns the stream
// to the internal data-structure.
void initializeAllLedsStreams() {
// Create a set of localities that are expected.
absl::flat_hash_set<std::string> expected_localities_prefixes(localities_prefixes_.begin(),
localities_prefixes_.end());
if (!leds_upstream_info_.connection_) {
auto result = leds_upstream_info_.upstream_->waitForHttpConnection(
*dispatcher_, leds_upstream_info_.connection_);
RELEASE_ASSERT(result, result.message());
}
// Wait for the exact number of streams.
for (uint32_t i = 0; i < localities_prefixes_.size(); ++i) {
// Create the stream for the LEDS collection and fetch the name from the
// contents, then validate that this is an expected collection
FakeStreamPtr temp_stream;
envoy::service::discovery::v3::DeltaDiscoveryRequest request;
auto result = leds_upstream_info_.connection_->waitForNewStream(*dispatcher_, temp_stream);
RELEASE_ASSERT(result, result.message());
temp_stream->startGrpcStream();
RELEASE_ASSERT(temp_stream->waitForGrpcMessage(*dispatcher_, request),
"LEDS message did not arrive as expected");
RELEASE_ASSERT(request.resource_names_subscribe().size() == 1,
"Each LEDS request in this test must have a single resource");
// Remove the "*" from the collection name to match against the set
// contents.
const auto request_collection_name = *request.resource_names_subscribe().begin();
const auto pos = request_collection_name.find_last_of('*');
ASSERT(pos != std::string::npos);
const auto request_collection_prefix = request_collection_name.substr(0, pos);
auto set_it = expected_localities_prefixes.find(request_collection_prefix);
ASSERT(set_it != expected_localities_prefixes.end());
// Associate the stream with the locality prefix.
leds_upstream_info_.stream_by_resource_name_[*set_it] = std::move(temp_stream);
// Remove the locality prefix from the expected set.
expected_localities_prefixes.erase(set_it);
}
}
void initializeTest(bool http_active_hc, uint32_t localities_num = 1) {
// Set up a single upstream host for the LEDS cluster using HTTP2 (gRPC).
setUpstreamCount(4);
config_helper_.addConfigModifier([this, http_active_hc](
envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
// Add a static EDS cluster.
auto* eds_cluster = bootstrap.mutable_static_resources()->add_clusters();
eds_cluster->MergeFrom(bootstrap.static_resources().clusters()[0]);
eds_cluster->set_name("eds_cluster");
eds_cluster->mutable_load_assignment()->set_cluster_name("eds_cluster");
ConfigHelper::setHttp2(*eds_cluster);
// Add a static LEDS cluster.
auto* leds_cluster = bootstrap.mutable_static_resources()->add_clusters();
leds_cluster->MergeFrom(bootstrap.static_resources().clusters()[0]);
leds_cluster->set_name("leds_cluster");
leds_cluster->mutable_load_assignment()->set_cluster_name("leds_cluster");
ConfigHelper::setHttp2(*leds_cluster);
// Remove the static cluster (cluster_0) and set up CDS.
bootstrap.mutable_dynamic_resources()->mutable_cds_config()->set_resource_api_version(
envoy::config::core::v3::ApiVersion::V3);
bootstrap.mutable_dynamic_resources()
->mutable_cds_config()
->mutable_path_config_source()
->set_path(cds_helper_.cdsPath());
bootstrap.mutable_static_resources()->mutable_clusters()->erase(
bootstrap.mutable_static_resources()->mutable_clusters()->begin());
// Set the default static cluster to use EDS.
auto& cluster_0 = cluster_;
cluster_0.set_name("cluster_0");
cluster_0.set_type(envoy::config::cluster::v3::Cluster::EDS);
cluster_0.mutable_connect_timeout()->CopyFrom(Protobuf::util::TimeUtil::SecondsToDuration(5));
auto* eds_cluster_config = cluster_0.mutable_eds_cluster_config();
eds_cluster_config->mutable_eds_config()->set_resource_api_version(
envoy::config::core::v3::ApiVersion::V3);
auto* api_config_source =
eds_cluster_config->mutable_eds_config()->mutable_api_config_source();
api_config_source->set_api_type(envoy::config::core::v3::ApiConfigSource::DELTA_GRPC);
api_config_source->set_transport_api_version(envoy::config::core::v3::ApiVersion::V3);
auto* grpc_service = api_config_source->add_grpc_services();
setGrpcService(*grpc_service, "eds_cluster", eds_upstream_info_.upstream_->localAddress());
if (http_active_hc) {
auto* health_check = cluster_0.add_health_checks();
health_check->mutable_timeout()->set_seconds(30);
// TODO(mattklein123): Consider using simulated time here.
health_check->mutable_interval()->CopyFrom(
Protobuf::util::TimeUtil::MillisecondsToDuration(100));
health_check->mutable_no_traffic_interval()->CopyFrom(
Protobuf::util::TimeUtil::MillisecondsToDuration(100));
health_check->mutable_unhealthy_threshold()->set_value(1);
health_check->mutable_healthy_threshold()->set_value(1);
health_check->mutable_http_health_check()->set_path("/healthcheck");
health_check->mutable_http_health_check()->set_codec_client_type(codec_client_type_);
}
// Set the cluster using CDS.
cds_helper_.setCds({cluster_});
});
// Set validate_clusters to false to allow us to reference a CDS cluster.
config_helper_.addConfigModifier(
[](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) { hcm.mutable_route_config()->mutable_validate_clusters()->set_value(false); });
defer_listener_finalization_ = true;
initialize();
EXPECT_EQ(1, test_server_->gauge("cluster_manager.warming_clusters")->value());
EXPECT_EQ(2, test_server_->gauge("cluster_manager.active_clusters")->value());
// Create the EDS connection and stream.
initializeStream(eds_upstream_info_);
// Add the assignment and localities.
cluster_load_assignment_.set_cluster_name("cluster_0");
localities_prefixes_.reserve(localities_num);
for (uint32_t locality_idx = 0; locality_idx < localities_num; ++locality_idx) {
// Setup per locality LEDS config over gRPC.
auto* locality_lb_endpoints = cluster_load_assignment_.add_endpoints();
locality_lb_endpoints->set_priority(locality_idx);
auto* leds_locality_config = locality_lb_endpoints->mutable_leds_cluster_locality_config();
auto* leds_config = leds_locality_config->mutable_leds_config();
leds_config->set_resource_api_version(envoy::config::core::v3::ApiVersion::V3);
auto* api_config_source = leds_config->mutable_api_config_source();
api_config_source->set_api_type(envoy::config::core::v3::ApiConfigSource::DELTA_GRPC);
api_config_source->set_transport_api_version(envoy::config::core::v3::ApiVersion::V3);
auto* grpc_service = api_config_source->add_grpc_services();
setGrpcService(*grpc_service, "leds_cluster", leds_upstream_info_.upstream_->localAddress());
const std::string locality_endpoints_prefix = fmt::format(
"xdstp://test/envoy.config.endpoint.v3.LbEndpoint/cluster0/locality{}/", locality_idx);
localities_prefixes_.push_back(locality_endpoints_prefix);
const std::string locality_endpoints_collection_name =
absl::StrCat(locality_endpoints_prefix, "*");
leds_locality_config->set_leds_collection_name(locality_endpoints_collection_name);
}
EXPECT_EQ(1, test_server_->gauge("cluster_manager.warming_clusters")->value());
EXPECT_EQ(2, test_server_->gauge("cluster_manager.active_clusters")->value());
// Do the initial compareDiscoveryRequest / sendDiscoveryResponse for cluster_'s localities
// (ClusterLoadAssignment).
EXPECT_TRUE(compareDeltaDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment,
{"cluster_0"}, {},
eds_upstream_info_.defaultStream()));
sendDeltaDiscoveryResponse<envoy::config::endpoint::v3::ClusterLoadAssignment>(
Config::TypeUrl::get().ClusterLoadAssignment, {cluster_load_assignment_}, {}, "2",
eds_upstream_info_.defaultStream());
// Receive EDS ack.
EXPECT_TRUE(compareDeltaDiscoveryRequest(Config::TypeUrl::get().ClusterLoadAssignment, {}, {},
eds_upstream_info_.defaultStream()));
EXPECT_EQ(1, test_server_->gauge("cluster_manager.warming_clusters")->value());
EXPECT_EQ(2, test_server_->gauge("cluster_manager.active_clusters")->value());
// Create the LEDS connection and stream(s).
// Wait for all the LEDS streams to be established. Note that if more
// than one locality has issued a LEDS request, the order of the requests
// can be non-deterministic (e.g., the request for "locality1" might be
// received before the request for "locality0"). Therefore we first wait
// for all the streams to be established, and only then verify that all the
// requests arrived as expected.
initializeAllLedsStreams();
EXPECT_EQ(1, test_server_->gauge("cluster_manager.warming_clusters")->value());
EXPECT_EQ(2, test_server_->gauge("cluster_manager.active_clusters")->value());
}
void resetFakeUpstreamInfo(FakeUpstreamInfo& upstream_info) {
if (upstream_info.connection_ == nullptr || upstream_info.upstream_ == nullptr) {
upstream_info.upstream_ = nullptr;
return;
}
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();
upstream_info.upstream_ = nullptr;
}
void waitForHealthCheck(uint32_t upstream_info_idx) {
auto& host_info = hosts_upstreams_info_[upstream_info_idx];
if (host_info.connection_ == nullptr) {
ASSERT_TRUE(host_info.upstream_->waitForHttpConnection(*dispatcher_, host_info.connection_));
}
ASSERT_TRUE(host_info.connection_->waitForNewStream(*dispatcher_, host_info.defaultStream()));
ASSERT_TRUE(host_info.defaultStream()->waitForEndStream(*dispatcher_));
EXPECT_EQ(host_info.defaultStream()->headers().getPathValue(), "/healthcheck");
EXPECT_EQ(host_info.defaultStream()->headers().getMethodValue(), "GET");
}
envoy::type::v3::CodecClientType codec_client_type_{};
CdsHelper cds_helper_;
envoy::config::endpoint::v3::ClusterLoadAssignment cluster_load_assignment_;
envoy::config::cluster::v3::Cluster cluster_;
std::vector<std::string> localities_prefixes_;
std::vector<FakeUpstreamInfo> hosts_upstreams_info_;
FakeUpstreamInfo eds_upstream_info_;
FakeUpstreamInfo leds_upstream_info_;
};
INSTANTIATE_TEST_SUITE_P(IpVersionsClientType, LedsIntegrationTest, GRPC_CLIENT_INTEGRATION_PARAMS,
Grpc::GrpcClientIntegrationParamTest::protocolTestParamsToString);
// Validates basic LEDS request response behavior.
TEST_P(LedsIntegrationTest, BasicLeds) {
initializeTest(true);
// Send an endpoint update with an unknown state using LEDS.
setEndpoints({}, {}, {}, {0}, {});
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 1);
// Waiting for the endpoint to become Healthy to end warming and move the cluster to active.
EXPECT_EQ(1, test_server_->gauge("cluster_manager.warming_clusters")->value());
EXPECT_EQ(2, test_server_->gauge("cluster_manager.active_clusters")->value());
// There should be a single backend in the cluster, and not yet healthy.
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(0, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Wait for the first health-check and verify the host is healthy. This should warm the initial
// cluster.
waitForHealthCheck(0);
hosts_upstreams_info_[0].defaultStream()->encodeHeaders(
Http::TestResponseHeaderMapImpl{{":status", "200"}}, true);
test_server_->waitForGaugeEq("cluster.cluster_0.membership_healthy", 1);
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
// 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"});
// The endpoint sent a valid health-check so the cluster should be active.
test_server_->waitForGaugeEq("cluster_manager.warming_clusters", 0);
test_server_->waitForGaugeEq("cluster_manager.active_clusters", 3);
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
}
// Validates adding endpoints using LEDS.
TEST_P(LedsIntegrationTest, LedsAdd) {
initializeTest(true);
// Send an endpoint update with an unknown state using LEDS.
setEndpoints({}, {}, {}, {0}, {});
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 1);
// Waiting for the endpoint to become Healthy to end warming and move the cluster to active.
EXPECT_EQ(1, test_server_->gauge("cluster_manager.warming_clusters")->value());
EXPECT_EQ(2, test_server_->gauge("cluster_manager.active_clusters")->value());
// There should be a single backend in the cluster, and not yet healthy.
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(0, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Wait for the first health-check and verify the host is healthy. This should warm the initial
// cluster.
waitForHealthCheck(0);
hosts_upstreams_info_[0].defaultStream()->encodeHeaders(
Http::TestResponseHeaderMapImpl{{":status", "200"}}, true);
test_server_->waitForGaugeEq("cluster.cluster_0.membership_healthy", 1);
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
// 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"});
// The cluster should have now a single healthy host.
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Add 2 more endpoints using LEDS in unknown state.
setEndpoints({}, {}, {}, {1, 2}, {});
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 2);
// There should be additional 2 backends in the cluster, and only one healthy.
EXPECT_EQ(3, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Send health-check responses back from the new hosts.
for (int i = 1; i < 3; ++i) {
waitForHealthCheck(i);
hosts_upstreams_info_[i].defaultStream()->encodeHeaders(
Http::TestResponseHeaderMapImpl{{":status", "200"}}, true);
}
// Verify that Envoy observes the healthy endpoints.
test_server_->waitForGaugeEq("cluster.cluster_0.membership_healthy", 3);
EXPECT_EQ(3, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(3, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
}
// Verify that updating the same endpoint doesn't change anything.
TEST_P(LedsIntegrationTest, LedsUpdateSameEndpoint) {
initializeTest(true);
// Send an endpoint update with an unknown state using LEDS.
setEndpoints({}, {}, {}, {0}, {});
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 1);
// Waiting for the endpoint to become Healthy to end warming and move the cluster to active.
EXPECT_EQ(1, test_server_->gauge("cluster_manager.warming_clusters")->value());
EXPECT_EQ(2, test_server_->gauge("cluster_manager.active_clusters")->value());
// There should be a single backend in the cluster, and not yet healthy.
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(0, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Wait for the first health-check and verify the host is healthy. This should warm the initial
// cluster.
waitForHealthCheck(0);
hosts_upstreams_info_[0].defaultStream()->encodeHeaders(
Http::TestResponseHeaderMapImpl{{":status", "200"}}, true);
test_server_->waitForGaugeEq("cluster.cluster_0.membership_healthy", 1);
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
// 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"});
// The cluster should have now a single healthy host.
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// "Update" the endpoint by sending the same state. The endpoint should still
// be healthy, as the active health check cleared it.
setEndpoints({}, {}, {}, {0}, {});
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 2);
// There should be additional 2 backends in the cluster, and only one healthy.
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Verify that Envoy observes the healthy endpoint.
test_server_->waitForGaugeEq("cluster.cluster_0.membership_healthy", 1);
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
}
// Verify endpoint removal using LEDS.
TEST_P(LedsIntegrationTest, EndpointRemoval) {
// Set health-checking to false, so Envoy will remove the endpoint, although it
// is still healthy.
initializeTest(false);
// Send 2 endpoints update with an unknown state using LEDS.
setEndpoints({}, {}, {}, {0, 1}, {});
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 1);
// Waiting for the endpoint to become Healthy to end warming and move the cluster to active.
EXPECT_EQ(3, test_server_->gauge("cluster_manager.active_clusters")->value());
// There should be a single backend in the cluster, and not yet healthy.
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_total")->value());
// 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"});
// Remove one of the endpoints.
setEndpoints({}, {}, {}, {}, {0});
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 2);
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
}
// Verify that a config removing an unknown endpoint is a no-op (similar to CDS).
TEST_P(LedsIntegrationTest, UnknownEndpointRemoval) {
initializeTest(true);
// Send 2 endpoints update with an unknown state using LEDS.
setEndpoints({}, {}, {}, {0}, {});
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 1);
// Waiting for the endpoint to become Healthy to end warming and move the cluster to active.
EXPECT_EQ(1, test_server_->gauge("cluster_manager.warming_clusters")->value());
EXPECT_EQ(2, test_server_->gauge("cluster_manager.active_clusters")->value());
// There should be a single backend in the cluster, and not yet healthy.
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(0, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Wait for the first health-check and verify the host is healthy. This should warm the initial
// cluster.
waitForHealthCheck(0);
hosts_upstreams_info_[0].defaultStream()->encodeHeaders(
Http::TestResponseHeaderMapImpl{{":status", "200"}}, true);
test_server_->waitForGaugeEq("cluster.cluster_0.membership_healthy", 1);
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
// 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"});
// The endpoints sent valid health-checks so the cluster should be active.
test_server_->waitForGaugeEq("cluster_manager.warming_clusters", 0);
test_server_->waitForGaugeEq("cluster_manager.active_clusters", 3);
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Remove one of the endpoints.
setEndpoints({}, {}, {}, {}, {2});
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 2);
EXPECT_EQ(0, test_server_->counter("cluster.cluster_0.leds.update_rejected")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
}
// Validates that endpoints can be added and then moved to other localities without causing crashes
// (Primarily as a regression test for https://github.com/envoyproxy/envoy/issues/8764).
TEST_P(LedsIntegrationTest, MoveEndpointsBetweenLocalities) {
// Create 2 localities in the cluster, no health-check as part of this test.
initializeTest(false, 2);
EXPECT_EQ(1, test_server_->gauge("cluster_manager.warming_clusters")->value());
EXPECT_EQ(2, test_server_->gauge("cluster_manager.active_clusters")->value());
// Send an endpoint update using LEDS for locality 0.
setEndpoints({}, {}, {}, {0}, {}, 0);
// The update only updates the first locality, but the cluster should still be
// in warmed state.
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 1);
EXPECT_EQ(0, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster_manager.warming_clusters")->value());
EXPECT_EQ(2, test_server_->gauge("cluster_manager.active_clusters")->value());
// Send an endpoint update using LEDS for locality 1.
setEndpoints({}, {}, {}, {1, 2}, {}, 1);
// All localities should have endpoints so the cluster warm-up should be over.
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 2);
test_server_->waitForGaugeGe("cluster_manager.active_clusters", 3);
// There should be a single backend in the cluster, all healthy as there isn't
// active health-check.
EXPECT_EQ(3, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(3, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// 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"});
// Move one endpoint from locality1 to locality0.
setEndpoints({}, {}, {}, {0, 2}, {}, 0);
setEndpoints({}, {}, {}, {}, {2}, 1);
// Wait for the additional 2 LEDS updates.
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 4);
EXPECT_EQ(3, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(3, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Move one endpoint from locality0 to locality1, and remove the other endpoint.
setEndpoints({}, {}, {}, {}, {2}, 0);
setEndpoints({}, {}, {}, {0}, {}, 1);
// Wait for the additional 2 LEDS updates.
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 6);
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
}
// Verify that an endpoint can be in 2 localities at the same time.
TEST_P(LedsIntegrationTest, LocalitiesShareEndpoint) {
// Create 2 localities in the cluster, no health-check as part of this test.
initializeTest(false, 2);
EXPECT_EQ(1, test_server_->gauge("cluster_manager.warming_clusters")->value());
EXPECT_EQ(2, test_server_->gauge("cluster_manager.active_clusters")->value());
// Send an endpoint update using LEDS for locality 0.
setEndpoints({}, {}, {}, {0}, {}, 0);
// The update only updates the first locality, but the cluster should still be
// in warmed state.
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 1);
EXPECT_EQ(0, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster_manager.warming_clusters")->value());
EXPECT_EQ(2, test_server_->gauge("cluster_manager.active_clusters")->value());
// Send an endpoint update using LEDS for locality 1 with a different endpoint.
setEndpoints({}, {}, {}, {1}, {}, 1);
// All localities should have endpoints so the cluster warm-up should be over.
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 2);
test_server_->waitForGaugeGe("cluster_manager.active_clusters", 3);
// There should be 2 hosts in the cluster, all healthy as there isn't active health-check.
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// 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"});
// Send a LEDS update to locality 1 with the same endpoint that is in locality 0.
setEndpoints({}, {}, {}, {0}, {}, 1);
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 3);
// There should be 2 hosts in the cluster, all healthy as there isn't active health-check.
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Remove the endpoint from one locality.
setEndpoints({}, {}, {}, {}, {0}, 0);
// Wait for the additional LEDS update.
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 4);
// There are 2 endpoints left in locality 1.
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
}
// Verify that a host stabilized via active health checking which is first removed from LEDS and
// then fails health checking is removed.
TEST_P(LedsIntegrationTest, RemoveAfterHcFail) {
initializeTest(true);
setEndpoints({}, {}, {}, {0}, {});
EXPECT_EQ(1, test_server_->counter("cluster.cluster_0.leds.update_success")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(0, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Wait for the first HC and verify the host is healthy.
waitForHealthCheck(0);
hosts_upstreams_info_[0].defaultStream()->encodeHeaders(
Http::TestResponseHeaderMapImpl{{":status", "200"}}, true);
test_server_->waitForGaugeEq("cluster.cluster_0.membership_healthy", 1);
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
// Clear out the host and verify the host is still healthy.
setEndpoints({}, {}, {}, {}, {0});
EXPECT_EQ(2, test_server_->counter("cluster.cluster_0.leds.update_success")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Fail HC and verify the host is gone.
waitForHealthCheck(0);
hosts_upstreams_info_[0].defaultStream()->encodeHeaders(
Http::TestResponseHeaderMapImpl{{":status", "503"}, {"connection", "close"}}, true);
test_server_->waitForGaugeEq("cluster.cluster_0.membership_healthy", 0);
EXPECT_EQ(0, test_server_->gauge("cluster.cluster_0.membership_total")->value());
}
// Validate that health status updates are consumed from LEDS.
TEST_P(LedsIntegrationTest, HealthUpdate) {
initializeTest(false);
// Initial state, no cluster members.
EXPECT_EQ(0, test_server_->counter("cluster.cluster_0.membership_change")->value());
EXPECT_EQ(0, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(0, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// 2 healthy endpoints.
setEndpoints({0, 1}, {}, {}, {}, {});
EXPECT_EQ(1, test_server_->counter("cluster.cluster_0.membership_change")->value());
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Drop to 0/2 healthy endpoints (2 unknown health state).
setEndpoints({}, {}, {0, 1}, {}, {});
EXPECT_EQ(1, test_server_->counter("cluster.cluster_0.membership_change")->value());
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(0, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Increase to 1/2 healthy endpoints (host 1 will remain unhealthy).
setEndpoints({0}, {}, {1}, {}, {});
EXPECT_EQ(1, test_server_->counter("cluster.cluster_0.membership_change")->value());
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Add host and modify healthy to 2/3 healthy endpoints.
setEndpoints({2}, {}, {1}, {}, {});
EXPECT_EQ(2, test_server_->counter("cluster.cluster_0.membership_change")->value());
EXPECT_EQ(3, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
// Modify healthy to 2/3 healthy and 1/3 degraded.
setEndpoints({}, {1}, {}, {}, {});
EXPECT_EQ(2, test_server_->counter("cluster.cluster_0.membership_change")->value());
EXPECT_EQ(3, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(2, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_degraded")->value());
}
// Validates that in a LEDS response that contains 2 endpoints with the same
// address, only the first will be used.
TEST_P(LedsIntegrationTest, LedsSameAddressEndpoints) {
initializeTest(false);
// Send a response with 2 endpoints with a different resource name but that
// map to the same address.
const auto& collection_prefix = localities_prefixes_[0];
absl::flat_hash_map<std::string, envoy::config::endpoint::v3::LbEndpoint> updated_endpoints;
std::vector<std::string> removed_endpoints;
const std::vector<std::string> endpoints_names{
absl::StrCat(collection_prefix, "endpoint0"),
absl::StrCat(collection_prefix, "endpoint1"),
};
for (const auto& endpoint_name : endpoints_names) {
envoy::config::endpoint::v3::LbEndpoint endpoint;
// Shift fake_upstreams_ by 2 (due to EDS and LEDS fake upstreams).
setUpstreamAddress(2, endpoint);
endpoint.set_health_status(envoy::config::core::v3::HEALTHY);
updated_endpoints.emplace(endpoint_name, endpoint);
}
sendDeltaLedsResponse(updated_endpoints, removed_endpoints, "7", 0);
// Await for update (LEDS Ack).
EXPECT_TRUE(compareDeltaDiscoveryRequest(
Config::TypeUrl::get().LbEndpoint, {}, {},
leds_upstream_info_.stream_by_resource_name_[localities_prefixes_[0]]));
// Verify that the update is successful.
test_server_->waitForCounterEq("cluster.cluster_0.leds.update_success", 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"});
// Verify that only one endpoint was processed.
test_server_->waitForGaugeEq("cluster_manager.warming_clusters", 0);
test_server_->waitForGaugeEq("cluster_manager.active_clusters", 3);
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_total")->value());
EXPECT_EQ(1, test_server_->gauge("cluster.cluster_0.membership_healthy")->value());
}
} // namespace
} // namespace Envoy