forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch_map_test.cc
682 lines (610 loc) · 31.8 KB
/
watch_map_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
#include <memory>
#include "envoy/common/exception.h"
#include "envoy/config/endpoint/v3/endpoint.pb.h"
#include "envoy/config/endpoint/v3/endpoint.pb.validate.h"
#include "envoy/service/discovery/v3/discovery.pb.h"
#include "envoy/stats/scope.h"
#include "source/common/config/watch_map.h"
#include "test/mocks/config/custom_config_validators.h"
#include "test/mocks/config/mocks.h"
#include "test/test_common/utility.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using ::testing::_;
using ::testing::AtMost;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::NiceMock;
namespace Envoy {
namespace Config {
namespace {
void expectDeltaUpdate(
MockSubscriptionCallbacks& callbacks,
const std::vector<envoy::config::endpoint::v3::ClusterLoadAssignment>& expected_resources,
const std::vector<std::string>& expected_removals, const std::string& version) {
EXPECT_CALL(callbacks, onConfigUpdate(_, _, _))
.WillOnce(Invoke([expected_resources, expected_removals,
version](const std::vector<DecodedResourceRef>& gotten_resources,
const Protobuf::RepeatedPtrField<std::string>& removed_resources,
const std::string&) {
EXPECT_EQ(expected_resources.size(), gotten_resources.size());
for (size_t i = 0; i < expected_resources.size(); i++) {
EXPECT_EQ(gotten_resources[i].get().version(), version);
EXPECT_TRUE(
TestUtility::protoEqual(gotten_resources[i].get().resource(), expected_resources[i]));
}
EXPECT_EQ(expected_removals.size(), removed_resources.size());
for (size_t i = 0; i < expected_removals.size(); i++) {
EXPECT_EQ(expected_removals[i], removed_resources[i]);
}
}));
}
// expectDeltaAndSotwUpdate() EXPECTs two birds with one function call: we want to cover both SotW
// and delta, which, while mechanically different, can behave identically for our testing purposes.
// Specifically, as a simplification for these tests, every still-present resource is updated in
// every update. Therefore, a resource can never show up in the SotW update but not the delta
// update. We can therefore use the same expected_resources for both.
void expectDeltaAndSotwUpdate(
MockSubscriptionCallbacks& callbacks,
const std::vector<envoy::config::endpoint::v3::ClusterLoadAssignment>& expected_resources,
const std::vector<std::string>& expected_removals, const std::string& version) {
EXPECT_CALL(callbacks, onConfigUpdate(_, version))
.WillOnce(Invoke([expected_resources](const std::vector<DecodedResourceRef>& gotten_resources,
const std::string&) {
EXPECT_EQ(expected_resources.size(), gotten_resources.size());
for (size_t i = 0; i < expected_resources.size(); i++) {
EXPECT_TRUE(
TestUtility::protoEqual(gotten_resources[i].get().resource(), expected_resources[i]));
}
}));
expectDeltaUpdate(callbacks, expected_resources, expected_removals, version);
}
void expectNoUpdate(MockSubscriptionCallbacks& callbacks, const std::string& version) {
EXPECT_CALL(callbacks, onConfigUpdate(_, version)).Times(0);
EXPECT_CALL(callbacks, onConfigUpdate(_, _, version)).Times(0);
}
void expectEmptySotwNoDeltaUpdate(MockSubscriptionCallbacks& callbacks,
const std::string& version) {
EXPECT_CALL(callbacks, onConfigUpdate(_, version))
.WillOnce(Invoke([](const std::vector<DecodedResourceRef>& gotten_resources,
const std::string&) { EXPECT_EQ(gotten_resources.size(), 0); }));
EXPECT_CALL(callbacks, onConfigUpdate(_, _, version)).Times(0);
}
Protobuf::RepeatedPtrField<envoy::service::discovery::v3::Resource>
wrapInResource(const Protobuf::RepeatedPtrField<ProtobufWkt::Any>& anys,
const std::string& version) {
Protobuf::RepeatedPtrField<envoy::service::discovery::v3::Resource> ret;
for (const auto& a : anys) {
envoy::config::endpoint::v3::ClusterLoadAssignment cur_endpoint;
a.UnpackTo(&cur_endpoint);
auto* cur_resource = ret.Add();
cur_resource->set_name(cur_endpoint.cluster_name());
cur_resource->mutable_resource()->CopyFrom(a);
cur_resource->set_version(version);
}
return ret;
}
void doDeltaUpdate(WatchMap& watch_map,
const Protobuf::RepeatedPtrField<ProtobufWkt::Any>& sotw_resources,
const std::vector<std::string>& removed_names, const std::string& version) {
Protobuf::RepeatedPtrField<envoy::service::discovery::v3::Resource> delta_resources =
wrapInResource(sotw_resources, version);
Protobuf::RepeatedPtrField<std::string> removed_names_proto;
for (const auto& n : removed_names) {
*removed_names_proto.Add() = n;
}
watch_map.onConfigUpdate(delta_resources, removed_names_proto, version);
}
// Similar to expectDeltaAndSotwUpdate(), but making the onConfigUpdate() happen, rather than
// EXPECT-ing it.
void doDeltaAndSotwUpdate(WatchMap& watch_map,
const Protobuf::RepeatedPtrField<ProtobufWkt::Any>& sotw_resources,
const std::vector<std::string>& removed_names,
const std::string& version) {
watch_map.onConfigUpdate(sotw_resources, version);
doDeltaUpdate(watch_map, sotw_resources, removed_names, version);
}
// Tests the simple case of a single watch. Checks that the watch will not be told of updates to
// resources it doesn't care about. Checks that the watch can later decide it does care about them,
// and then receive subsequent updates to them.
TEST(WatchMapTest, Basic) {
MockSubscriptionCallbacks callbacks;
TestUtility::TestOpaqueResourceDecoderImpl<envoy::config::endpoint::v3::ClusterLoadAssignment>
resource_decoder("cluster_name");
NiceMock<MockCustomConfigValidators> config_validators;
WatchMap watch_map(false, "ClusterLoadAssignmentType", config_validators);
Watch* watch = watch_map.addWatch(callbacks, resource_decoder);
{
// nothing is interested, so become wildcard watch
// should callback with empty resource
expectDeltaAndSotwUpdate(callbacks, {}, {}, "version1");
doDeltaAndSotwUpdate(watch_map, {}, {}, "version1");
}
{
// The watch is interested in Alice and Bob...
absl::flat_hash_set<std::string> update_to({"alice", "bob"});
AddedRemoved added_removed = watch_map.updateWatchInterest(watch, update_to);
EXPECT_EQ(update_to, added_removed.added_);
EXPECT_TRUE(added_removed.removed_.empty());
// ...the update is going to contain Bob and Carol...
Protobuf::RepeatedPtrField<ProtobufWkt::Any> updated_resources;
envoy::config::endpoint::v3::ClusterLoadAssignment bob;
bob.set_cluster_name("bob");
updated_resources.Add()->PackFrom(bob);
envoy::config::endpoint::v3::ClusterLoadAssignment carol;
carol.set_cluster_name("carol");
updated_resources.Add()->PackFrom(carol);
// ...so the watch should receive only Bob.
std::vector<envoy::config::endpoint::v3::ClusterLoadAssignment> expected_resources;
expected_resources.push_back(bob);
expectDeltaAndSotwUpdate(callbacks, expected_resources, {}, "version1");
doDeltaAndSotwUpdate(watch_map, updated_resources, {}, "version1");
}
{
// The watch is now interested in Bob, Carol, Dave, Eve...
absl::flat_hash_set<std::string> update_to({"bob", "carol", "dave", "eve"});
AddedRemoved added_removed = watch_map.updateWatchInterest(watch, update_to);
EXPECT_EQ(absl::flat_hash_set<std::string>({"carol", "dave", "eve"}), added_removed.added_);
EXPECT_EQ(absl::flat_hash_set<std::string>({"alice"}), added_removed.removed_);
// ...the update is going to contain Alice, Carol, Dave...
Protobuf::RepeatedPtrField<ProtobufWkt::Any> updated_resources;
envoy::config::endpoint::v3::ClusterLoadAssignment alice;
alice.set_cluster_name("alice");
updated_resources.Add()->PackFrom(alice);
envoy::config::endpoint::v3::ClusterLoadAssignment carol;
carol.set_cluster_name("carol");
updated_resources.Add()->PackFrom(carol);
envoy::config::endpoint::v3::ClusterLoadAssignment dave;
dave.set_cluster_name("dave");
updated_resources.Add()->PackFrom(dave);
// ...so the watch should receive only Carol and Dave.
std::vector<envoy::config::endpoint::v3::ClusterLoadAssignment> expected_resources;
expected_resources.push_back(carol);
expected_resources.push_back(dave);
expectDeltaAndSotwUpdate(callbacks, expected_resources, {"bob"}, "version2");
doDeltaAndSotwUpdate(watch_map, updated_resources, {"bob"}, "version2");
}
}
// Checks the following:
// First watch on a resource name ==> updateWatchInterest() returns "add it to subscription"
// Second watch on that name ==> updateWatchInterest() returns nothing about that name
// Original watch loses interest ==> nothing
// Second watch also loses interest ==> "remove it from subscription"
// NOTE: we need the resource name "dummy" to keep either watch from ever having no names watched,
// which is treated as interest in all names.
TEST(WatchMapTest, Overlap) {
MockSubscriptionCallbacks callbacks1;
MockSubscriptionCallbacks callbacks2;
TestUtility::TestOpaqueResourceDecoderImpl<envoy::config::endpoint::v3::ClusterLoadAssignment>
resource_decoder("cluster_name");
NiceMock<MockCustomConfigValidators> config_validators;
WatchMap watch_map(false, "ClusterLoadAssignmentType", config_validators);
Watch* watch1 = watch_map.addWatch(callbacks1, resource_decoder);
Watch* watch2 = watch_map.addWatch(callbacks2, resource_decoder);
Protobuf::RepeatedPtrField<ProtobufWkt::Any> updated_resources;
envoy::config::endpoint::v3::ClusterLoadAssignment alice;
alice.set_cluster_name("alice");
updated_resources.Add()->PackFrom(alice);
// First watch becomes interested.
{
absl::flat_hash_set<std::string> update_to({"alice", "dummy"});
AddedRemoved added_removed = watch_map.updateWatchInterest(watch1, update_to);
EXPECT_EQ(update_to, added_removed.added_); // add to subscription
EXPECT_TRUE(added_removed.removed_.empty());
watch_map.updateWatchInterest(watch2, {"dummy"});
// *Only* first watch receives update.
expectDeltaAndSotwUpdate(callbacks1, {alice}, {}, "version1");
expectNoUpdate(callbacks2, "version1");
doDeltaAndSotwUpdate(watch_map, updated_resources, {}, "version1");
}
// Second watch becomes interested.
{
absl::flat_hash_set<std::string> update_to({"alice", "dummy"});
AddedRemoved added_removed = watch_map.updateWatchInterest(watch2, update_to);
EXPECT_TRUE(added_removed.added_.empty()); // nothing happens
EXPECT_TRUE(added_removed.removed_.empty());
// Both watches receive update.
expectDeltaAndSotwUpdate(callbacks1, {alice}, {}, "version2");
expectDeltaAndSotwUpdate(callbacks2, {alice}, {}, "version2");
doDeltaAndSotwUpdate(watch_map, updated_resources, {}, "version2");
}
// First watch loses interest.
{
AddedRemoved added_removed = watch_map.updateWatchInterest(watch1, {"dummy"});
EXPECT_TRUE(added_removed.added_.empty()); // nothing happens
EXPECT_TRUE(added_removed.removed_.empty());
// Both watches receive the update. For watch2, this is obviously desired.
expectDeltaAndSotwUpdate(callbacks2, {alice}, {}, "version3");
// For watch1, it's more subtle: the WatchMap sees that this update has no
// resources watch1 cares about, but also knows that watch1 previously had
// some resources. So, it must inform watch1 that it now has no resources.
// (SotW only: delta's explicit removals avoid the need for this guessing.)
expectEmptySotwNoDeltaUpdate(callbacks1, "version3");
doDeltaAndSotwUpdate(watch_map, updated_resources, {}, "version3");
}
// Second watch loses interest.
{
AddedRemoved added_removed = watch_map.updateWatchInterest(watch2, {"dummy"});
EXPECT_TRUE(added_removed.added_.empty());
EXPECT_EQ(absl::flat_hash_set<std::string>({"alice"}),
added_removed.removed_); // remove from subscription
}
}
// These are regression tests for #11877, validate that when two watches point at the same
// watched resource, and an update to one of the watches removes one or both of them, that
// WatchMap defers deletes and doesn't crash.
class SameWatchRemoval : public testing::Test {
public:
SameWatchRemoval() : watch_map_(false, "ClusterLoadAssignmentType", config_validators) {}
void SetUp() override {
envoy::config::endpoint::v3::ClusterLoadAssignment alice;
alice.set_cluster_name("alice");
updated_resources_.Add()->PackFrom(alice);
watch1_ = watch_map_.addWatch(callbacks1_, resource_decoder_);
watch2_ = watch_map_.addWatch(callbacks2_, resource_decoder_);
watch_map_.updateWatchInterest(watch1_, {"alice"});
watch_map_.updateWatchInterest(watch2_, {"alice"});
}
void removeAllInterest() {
ASSERT_FALSE(watch_cb_invoked_);
watch_cb_invoked_ = true;
watch_map_.removeWatch(watch1_);
watch_map_.removeWatch(watch2_);
}
TestUtility::TestOpaqueResourceDecoderImpl<envoy::config::endpoint::v3::ClusterLoadAssignment>
resource_decoder_{"cluster_name"};
NiceMock<MockCustomConfigValidators> config_validators;
WatchMap watch_map_;
NiceMock<MockSubscriptionCallbacks> callbacks1_;
MockSubscriptionCallbacks callbacks2_;
Protobuf::RepeatedPtrField<ProtobufWkt::Any> updated_resources_;
Watch* watch1_;
Watch* watch2_;
bool watch_cb_invoked_{};
};
TEST_F(SameWatchRemoval, SameWatchRemovalSotw) {
EXPECT_CALL(callbacks1_, onConfigUpdate(_, _))
.Times(AtMost(1))
.WillRepeatedly(InvokeWithoutArgs([this] { removeAllInterest(); }));
EXPECT_CALL(callbacks2_, onConfigUpdate(_, _))
.Times(AtMost(1))
.WillRepeatedly(InvokeWithoutArgs([this] { removeAllInterest(); }));
watch_map_.onConfigUpdate(updated_resources_, "version1");
}
TEST_F(SameWatchRemoval, SameWatchRemovalDeltaAdd) {
Protobuf::RepeatedPtrField<envoy::service::discovery::v3::Resource> delta_resources =
wrapInResource(updated_resources_, "version1");
Protobuf::RepeatedPtrField<std::string> removed_names_proto;
EXPECT_CALL(callbacks1_, onConfigUpdate(_, _, _))
.Times(AtMost(1))
.WillRepeatedly(InvokeWithoutArgs([this] { removeAllInterest(); }));
EXPECT_CALL(callbacks2_, onConfigUpdate(_, _, _))
.Times(AtMost(1))
.WillRepeatedly(InvokeWithoutArgs([this] { removeAllInterest(); }));
watch_map_.onConfigUpdate(delta_resources, removed_names_proto, "version1");
}
TEST_F(SameWatchRemoval, SameWatchRemovalDeltaRemove) {
Protobuf::RepeatedPtrField<std::string> removed_names_proto;
*removed_names_proto.Add() = "alice";
EXPECT_CALL(callbacks1_, onConfigUpdate(_, _, _))
.Times(AtMost(1))
.WillRepeatedly(InvokeWithoutArgs([this] { removeAllInterest(); }));
EXPECT_CALL(callbacks2_, onConfigUpdate(_, _, _))
.Times(AtMost(1))
.WillRepeatedly(InvokeWithoutArgs([this] { removeAllInterest(); }));
watch_map_.onConfigUpdate({}, removed_names_proto, "version1");
}
// Checks the following:
// First watch on a resource name ==> updateWatchInterest() returns "add it to subscription"
// Watch loses interest ==> "remove it from subscription"
// Second watch on that name ==> "add it to subscription"
// NOTE: we need the resource name "dummy" to keep either watch from ever having no names watched,
// which is treated as interest in all names.
TEST(WatchMapTest, AddRemoveAdd) {
MockSubscriptionCallbacks callbacks1;
MockSubscriptionCallbacks callbacks2;
TestUtility::TestOpaqueResourceDecoderImpl<envoy::config::endpoint::v3::ClusterLoadAssignment>
resource_decoder("cluster_name");
NiceMock<MockCustomConfigValidators> config_validators;
WatchMap watch_map(false, "ClusterLoadAssignmentType", config_validators);
Watch* watch1 = watch_map.addWatch(callbacks1, resource_decoder);
Watch* watch2 = watch_map.addWatch(callbacks2, resource_decoder);
Protobuf::RepeatedPtrField<ProtobufWkt::Any> updated_resources;
envoy::config::endpoint::v3::ClusterLoadAssignment alice;
alice.set_cluster_name("alice");
updated_resources.Add()->PackFrom(alice);
// First watch becomes interested.
{
absl::flat_hash_set<std::string> update_to({"alice", "dummy"});
AddedRemoved added_removed = watch_map.updateWatchInterest(watch1, update_to);
EXPECT_EQ(update_to, added_removed.added_); // add to subscription
EXPECT_TRUE(added_removed.removed_.empty());
watch_map.updateWatchInterest(watch2, {"dummy"});
// *Only* first watch receives update.
expectDeltaAndSotwUpdate(callbacks1, {alice}, {}, "version1");
expectNoUpdate(callbacks2, "version1");
doDeltaAndSotwUpdate(watch_map, updated_resources, {}, "version1");
}
// First watch loses interest.
{
AddedRemoved added_removed = watch_map.updateWatchInterest(watch1, {"dummy"});
EXPECT_TRUE(added_removed.added_.empty());
EXPECT_EQ(absl::flat_hash_set<std::string>({"alice"}),
added_removed.removed_); // remove from subscription
// (The xDS client should have responded to updateWatchInterest()'s return value by removing
// Alice from the subscription, so onConfigUpdate() calls should be impossible right now.)
}
// Second watch becomes interested.
{
absl::flat_hash_set<std::string> update_to({"alice", "dummy"});
AddedRemoved added_removed = watch_map.updateWatchInterest(watch2, update_to);
EXPECT_EQ(absl::flat_hash_set<std::string>({"alice"}),
added_removed.added_); // add to subscription
EXPECT_TRUE(added_removed.removed_.empty());
// Both watches receive the update. For watch2, this is obviously desired.
expectDeltaAndSotwUpdate(callbacks2, {alice}, {}, "version2");
// For watch1, it's more subtle: the WatchMap sees that this update has no
// resources watch1 cares about, but also knows that watch1 previously had
// some resources. So, it must inform watch1 that it now has no resources.
// (SotW only: delta's explicit removals avoid the need for this guessing.)
expectEmptySotwNoDeltaUpdate(callbacks1, "version2");
doDeltaAndSotwUpdate(watch_map, updated_resources, {}, "version2");
}
}
// Tests that nothing breaks if an update arrives that we entirely do not care about.
TEST(WatchMapTest, UninterestingUpdate) {
MockSubscriptionCallbacks callbacks;
TestUtility::TestOpaqueResourceDecoderImpl<envoy::config::endpoint::v3::ClusterLoadAssignment>
resource_decoder("cluster_name");
NiceMock<MockCustomConfigValidators> config_validators;
WatchMap watch_map(false, "ClusterLoadAssignmentType", config_validators);
Watch* watch = watch_map.addWatch(callbacks, resource_decoder);
watch_map.updateWatchInterest(watch, {"alice"});
Protobuf::RepeatedPtrField<ProtobufWkt::Any> alice_update;
envoy::config::endpoint::v3::ClusterLoadAssignment alice;
alice.set_cluster_name("alice");
alice_update.Add()->PackFrom(alice);
Protobuf::RepeatedPtrField<ProtobufWkt::Any> bob_update;
envoy::config::endpoint::v3::ClusterLoadAssignment bob;
bob.set_cluster_name("bob");
bob_update.Add()->PackFrom(bob);
// We are watching for alice, and an update for just bob arrives. It should be ignored.
expectNoUpdate(callbacks, "version1");
doDeltaAndSotwUpdate(watch_map, bob_update, {}, "version1");
::testing::Mock::VerifyAndClearExpectations(&callbacks);
// The server sends an update adding alice and removing bob. We pay attention only to alice.
expectDeltaAndSotwUpdate(callbacks, {alice}, {}, "version2");
doDeltaAndSotwUpdate(watch_map, alice_update, {}, "version2");
::testing::Mock::VerifyAndClearExpectations(&callbacks);
// The server sends an update removing alice and adding bob. We pay attention only to alice.
expectDeltaAndSotwUpdate(callbacks, {}, {"alice"}, "version3");
doDeltaAndSotwUpdate(watch_map, bob_update, {"alice"}, "version3");
::testing::Mock::VerifyAndClearExpectations(&callbacks);
// Clean removal of the watch: first update to "interested in nothing", then remove.
watch_map.updateWatchInterest(watch, {});
watch_map.removeWatch(watch);
// Finally, test that calling onConfigUpdate on a map with no watches doesn't break.
doDeltaAndSotwUpdate(watch_map, bob_update, {}, "version4");
}
// Tests that a watch that specifies no particular resource interest is treated as interested in
// everything.
TEST(WatchMapTest, WatchingEverything) {
MockSubscriptionCallbacks callbacks1;
MockSubscriptionCallbacks callbacks2;
TestUtility::TestOpaqueResourceDecoderImpl<envoy::config::endpoint::v3::ClusterLoadAssignment>
resource_decoder("cluster_name");
NiceMock<MockCustomConfigValidators> config_validators;
WatchMap watch_map(false, "ClusterLoadAssignmentType", config_validators);
/*Watch* watch1 = */ watch_map.addWatch(callbacks1, resource_decoder);
Watch* watch2 = watch_map.addWatch(callbacks2, resource_decoder);
// watch1 never specifies any names, and so is treated as interested in everything.
watch_map.updateWatchInterest(watch2, {"alice"});
Protobuf::RepeatedPtrField<ProtobufWkt::Any> updated_resources;
envoy::config::endpoint::v3::ClusterLoadAssignment alice;
alice.set_cluster_name("alice");
updated_resources.Add()->PackFrom(alice);
envoy::config::endpoint::v3::ClusterLoadAssignment bob;
bob.set_cluster_name("bob");
updated_resources.Add()->PackFrom(bob);
std::vector<envoy::config::endpoint::v3::ClusterLoadAssignment> expected_resources1;
expected_resources1.push_back(alice);
expected_resources1.push_back(bob);
std::vector<envoy::config::endpoint::v3::ClusterLoadAssignment> expected_resources2;
expected_resources2.push_back(alice);
expectDeltaAndSotwUpdate(callbacks1, expected_resources1, {}, "version1");
expectDeltaAndSotwUpdate(callbacks2, expected_resources2, {}, "version1");
doDeltaAndSotwUpdate(watch_map, updated_resources, {}, "version1");
}
// Delta onConfigUpdate has some slightly subtle details with how it handles the three cases where
// a watch receives {only updates, updates+removals, only removals} to its resources. This test
// exercise those cases. Also, the removal-only case tests that SotW does call a watch's
// onConfigUpdate even if none of the watch's interested resources are among the updated
// resources. (Which ensures we deliver empty config updates when a resource is dropped.)
TEST(WatchMapTest, DeltaOnConfigUpdate) {
MockSubscriptionCallbacks callbacks1;
MockSubscriptionCallbacks callbacks2;
MockSubscriptionCallbacks callbacks3;
TestUtility::TestOpaqueResourceDecoderImpl<envoy::config::endpoint::v3::ClusterLoadAssignment>
resource_decoder("cluster_name");
NiceMock<MockCustomConfigValidators> config_validators;
WatchMap watch_map(false, "ClusterLoadAssignmentType", config_validators);
Watch* watch1 = watch_map.addWatch(callbacks1, resource_decoder);
Watch* watch2 = watch_map.addWatch(callbacks2, resource_decoder);
Watch* watch3 = watch_map.addWatch(callbacks3, resource_decoder);
watch_map.updateWatchInterest(watch1, {"updated"});
watch_map.updateWatchInterest(watch2, {"updated", "removed"});
watch_map.updateWatchInterest(watch3, {"removed"});
// First, create the "removed" resource. We want to test SotW being handed an empty
// onConfigUpdate. But, if SotW holds no resources, then an update with nothing it cares about
// will just not trigger any onConfigUpdate at all.
{
Protobuf::RepeatedPtrField<ProtobufWkt::Any> prepare_removed;
envoy::config::endpoint::v3::ClusterLoadAssignment will_be_removed_later;
will_be_removed_later.set_cluster_name("removed");
prepare_removed.Add()->PackFrom(will_be_removed_later);
expectDeltaAndSotwUpdate(callbacks2, {will_be_removed_later}, {}, "version0");
expectDeltaAndSotwUpdate(callbacks3, {will_be_removed_later}, {}, "version0");
doDeltaAndSotwUpdate(watch_map, prepare_removed, {}, "version0");
}
Protobuf::RepeatedPtrField<ProtobufWkt::Any> update;
envoy::config::endpoint::v3::ClusterLoadAssignment updated;
updated.set_cluster_name("updated");
update.Add()->PackFrom(updated);
expectDeltaAndSotwUpdate(callbacks1, {updated}, {}, "version1"); // only update
expectDeltaAndSotwUpdate(callbacks2, {updated}, {"removed"}, "version1"); // update+remove
expectDeltaAndSotwUpdate(callbacks3, {}, {"removed"}, "version1"); // only remove
doDeltaAndSotwUpdate(watch_map, update, {"removed"}, "version1");
}
TEST(WatchMapTest, OnConfigUpdateFailed) {
NiceMock<MockCustomConfigValidators> config_validators;
WatchMap watch_map(false, "ClusterLoadAssignmentType", config_validators);
// calling on empty map doesn't break
watch_map.onConfigUpdateFailed(ConfigUpdateFailureReason::UpdateRejected, nullptr);
MockSubscriptionCallbacks callbacks1;
MockSubscriptionCallbacks callbacks2;
TestUtility::TestOpaqueResourceDecoderImpl<envoy::config::endpoint::v3::ClusterLoadAssignment>
resource_decoder("cluster_name");
watch_map.addWatch(callbacks1, resource_decoder);
watch_map.addWatch(callbacks2, resource_decoder);
EXPECT_CALL(callbacks1, onConfigUpdateFailed(ConfigUpdateFailureReason::UpdateRejected, nullptr));
EXPECT_CALL(callbacks2, onConfigUpdateFailed(ConfigUpdateFailureReason::UpdateRejected, nullptr));
watch_map.onConfigUpdateFailed(ConfigUpdateFailureReason::UpdateRejected, nullptr);
}
// Validate watch behavior when subscribed to xdstp:// glob collections.
TEST(WatchMapTest, OnConfigUpdateXdsTpGlobCollections) {
MockSubscriptionCallbacks callbacks;
TestUtility::TestOpaqueResourceDecoderImpl<envoy::config::endpoint::v3::ClusterLoadAssignment>
resource_decoder("cluster_name");
NiceMock<MockCustomConfigValidators> config_validators;
WatchMap watch_map(false, "ClusterLoadAssignmentType", config_validators);
Watch* watch = watch_map.addWatch(callbacks, resource_decoder);
watch_map.updateWatchInterest(watch, {"xdstp://foo/bar/baz/*?some=thing&thing=some"});
// verify update
{
// Verify that we pay attention to all matching resources, no matter the order of context
// params.
Protobuf::RepeatedPtrField<ProtobufWkt::Any> update;
envoy::config::endpoint::v3::ClusterLoadAssignment resource1;
resource1.set_cluster_name("xdstp://foo/bar/baz/a?some=thing&thing=some");
update.Add()->PackFrom(resource1);
envoy::config::endpoint::v3::ClusterLoadAssignment resource2;
resource2.set_cluster_name("xdstp://foo/bar/baz/b?thing=some&some=thing");
update.Add()->PackFrom(resource2);
// Ignore non-matching resources.
envoy::config::endpoint::v3::ClusterLoadAssignment ignored_resource;
ignored_resource.set_cluster_name("xdstp://foo/bar/baz/c?thing=some");
update.Add()->PackFrom(ignored_resource);
ignored_resource.set_cluster_name("xdstp://foo/bar/baz/d");
update.Add()->PackFrom(ignored_resource);
ignored_resource.set_cluster_name("xdstp://blah/bar/baz/e");
update.Add()->PackFrom(ignored_resource);
ignored_resource.set_cluster_name("whatevs");
update.Add()->PackFrom(ignored_resource);
expectDeltaUpdate(callbacks, {resource1, resource2}, {}, "version0");
doDeltaUpdate(watch_map, update, {}, "version0");
}
// verify removal
{
Protobuf::RepeatedPtrField<ProtobufWkt::Any> update;
expectDeltaUpdate(callbacks, {}, {"xdstp://foo/bar/baz/a?thing=some&some=thing"}, "version1");
doDeltaUpdate(
watch_map, update,
{"xdstp://foo/bar/baz/*", "xdstp://foo/bar/baz/a?thing=some&some=thing", "whatevs"},
"version1");
}
}
// Validate watch behavior when subscribed to xdstp:// singletons.
TEST(WatchMapTest, OnConfigUpdateXdsTpSingletons) {
MockSubscriptionCallbacks callbacks;
TestUtility::TestOpaqueResourceDecoderImpl<envoy::config::endpoint::v3::ClusterLoadAssignment>
resource_decoder("cluster_name");
NiceMock<MockCustomConfigValidators> config_validators;
WatchMap watch_map(false, "ClusterLoadAssignmentType", config_validators);
Watch* watch = watch_map.addWatch(callbacks, resource_decoder);
watch_map.updateWatchInterest(watch, {"xdstp://foo/bar/baz?some=thing&thing=some"});
// verify update
{
// Verify that we pay attention to all matching resources, no matter the order of context
// params.
Protobuf::RepeatedPtrField<ProtobufWkt::Any> update;
envoy::config::endpoint::v3::ClusterLoadAssignment resource1;
resource1.set_cluster_name("xdstp://foo/bar/baz?thing=some&some=thing");
update.Add()->PackFrom(resource1);
// Ignore non-matching resources.
envoy::config::endpoint::v3::ClusterLoadAssignment ignored_resource;
ignored_resource.set_cluster_name("xdstp://foo/bar/baz/c?thing=some&some=thing");
update.Add()->PackFrom(ignored_resource);
ignored_resource.set_cluster_name("xdstp://foo/bar/bazd");
update.Add()->PackFrom(ignored_resource);
ignored_resource.set_cluster_name("xdstp://blah/bar/baz/e");
update.Add()->PackFrom(ignored_resource);
ignored_resource.set_cluster_name("whatevs");
update.Add()->PackFrom(ignored_resource);
expectDeltaUpdate(callbacks, {resource1}, {}, "version0");
doDeltaUpdate(watch_map, update, {}, "version0");
}
// verify removal
{
Protobuf::RepeatedPtrField<ProtobufWkt::Any> update;
expectDeltaUpdate(callbacks, {}, {"xdstp://foo/bar/baz?thing=some&some=thing"}, "version1");
doDeltaUpdate(watch_map, update, {"xdstp://foo/bar/baz?thing=some&some=thing", "whatevs"},
"version1");
}
}
TEST(WatchMapTest, OnConfigUpdateUsingNamespaces) {
MockSubscriptionCallbacks callbacks1;
MockSubscriptionCallbacks callbacks2;
MockSubscriptionCallbacks callbacks3;
TestUtility::TestOpaqueResourceDecoderImpl<envoy::config::endpoint::v3::ClusterLoadAssignment>
resource_decoder("cluster_name");
NiceMock<MockCustomConfigValidators> config_validators;
WatchMap watch_map(true, "ClusterLoadAssignmentType", config_validators);
Watch* watch1 = watch_map.addWatch(callbacks1, resource_decoder);
Watch* watch2 = watch_map.addWatch(callbacks2, resource_decoder);
Watch* watch3 = watch_map.addWatch(callbacks3, resource_decoder);
watch_map.updateWatchInterest(watch1, {"ns1"});
watch_map.updateWatchInterest(watch2, {"ns1", "ns2"});
watch_map.updateWatchInterest(watch3, {"ns3"});
// verify update
{
Protobuf::RepeatedPtrField<ProtobufWkt::Any> update;
envoy::config::endpoint::v3::ClusterLoadAssignment resource;
resource.set_cluster_name("ns1/resource1");
update.Add()->PackFrom(resource);
expectDeltaUpdate(callbacks1, {resource}, {}, "version0");
expectDeltaUpdate(callbacks2, {resource}, {}, "version0");
doDeltaUpdate(watch_map, update, {}, "version0");
}
// verify removal
{
Protobuf::RepeatedPtrField<ProtobufWkt::Any> update;
expectDeltaUpdate(callbacks2, {}, {"ns2/removed"}, "version1");
doDeltaUpdate(watch_map, update, {"ns2/removed"}, "version1");
}
// verify a not-found response to an on-demand request: such a response will contain an empty
// resource wrapper with the name and aliases fields containing the alias used in the request.
{
Protobuf::RepeatedPtrField<envoy::service::discovery::v3::Resource> empty_resources;
const auto version = "version3";
const auto not_resolved = "ns3/not_resolved";
auto* cur_resource = empty_resources.Add();
cur_resource->set_version(version);
cur_resource->set_name(not_resolved);
cur_resource->add_aliases(not_resolved);
EXPECT_CALL(callbacks3, onConfigUpdate(_, _, _))
.WillOnce(Invoke([not_resolved, version](
const std::vector<DecodedResourceRef>& gotten_resources,
const Protobuf::RepeatedPtrField<std::string>&, const std::string&) {
EXPECT_EQ(1, gotten_resources.size());
EXPECT_EQ(gotten_resources[0].get().version(), version);
EXPECT_FALSE(gotten_resources[0].get().hasResource());
EXPECT_EQ(gotten_resources[0].get().name(), not_resolved);
EXPECT_EQ(gotten_resources[0].get().aliases(), std::vector<std::string>{not_resolved});
}));
Protobuf::RepeatedPtrField<std::string> removed_names_proto;
watch_map.onConfigUpdate(empty_resources, removed_names_proto, "version2");
}
}
} // namespace
} // namespace Config
} // namespace Envoy