-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
rds_impl_test.cc
1207 lines (1078 loc) · 41.8 KB
/
rds_impl_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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <chrono>
#include <memory>
#include <string>
#include "envoy/admin/v3/config_dump_shared.pb.h"
#include "envoy/admin/v3/config_dump_shared.pb.validate.h"
#include "envoy/config/route/v3/route.pb.h"
#include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h"
#include "envoy/service/discovery/v3/discovery.pb.h"
#include "envoy/stats/scope.h"
#include "source/common/config/utility.h"
#include "source/common/json/json_loader.h"
#include "source/common/router/config_impl.h"
#include "source/common/router/rds_impl.h"
#include "source/common/router/route_provider_manager.h"
#ifdef ENVOY_ADMIN_FUNCTIONALITY
#include "source/server/admin/admin.h"
#endif
#include "test/mocks/init/mocks.h"
#include "test/mocks/local_info/mocks.h"
#include "test/mocks/matcher/mocks.h"
#include "test/mocks/protobuf/mocks.h"
#include "test/mocks/server/instance.h"
#include "test/mocks/thread_local/mocks.h"
#include "test/test_common/printers.h"
#include "test/test_common/simulated_time_system.h"
#include "test/test_common/utility.h"
#include "test/test_common/test_runtime.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using testing::_;
using testing::Eq;
using testing::InSequence;
using testing::Invoke;
using testing::Return;
using testing::ReturnRef;
namespace Envoy {
namespace Router {
namespace {
using ::Envoy::Matchers::MockStringMatcher;
using ::Envoy::Matchers::UniversalStringMatcher;
envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager
parseHttpConnectionManagerFromYaml(const std::string& yaml_string) {
envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager
http_connection_manager;
TestUtility::loadFromYaml(yaml_string, http_connection_manager);
return http_connection_manager;
}
class RdsTestBase : public testing::Test {
public:
RdsTestBase() {
// For server_factory_context
ON_CALL(server_factory_context_, scope()).WillByDefault(ReturnRef(*scope_.rootScope()));
ON_CALL(server_factory_context_, messageValidationContext())
.WillByDefault(ReturnRef(validation_context_));
EXPECT_CALL(validation_context_, dynamicValidationVisitor())
.WillRepeatedly(ReturnRef(validation_visitor_));
ON_CALL(outer_init_manager_, add(_)).WillByDefault(Invoke([this](const Init::Target& target) {
init_target_handle_ = target.createHandle("test");
}));
ON_CALL(outer_init_manager_, initialize(_))
.WillByDefault(Invoke(
[this](const Init::Watcher& watcher) { init_target_handle_->initialize(watcher); }));
}
Event::SimulatedTimeSystem& timeSystem() { return time_system_; }
Event::SimulatedTimeSystem time_system_;
NiceMock<ProtobufMessage::MockValidationContext> validation_context_;
NiceMock<ProtobufMessage::MockValidationVisitor> validation_visitor_;
NiceMock<Init::MockManager> outer_init_manager_;
NiceMock<Server::Configuration::MockServerFactoryContext> server_factory_context_;
Init::ExpectableWatcherImpl init_watcher_;
Init::TargetHandlePtr init_target_handle_;
Envoy::Config::SubscriptionCallbacks* rds_callbacks_{};
NiceMock<Stats::MockIsolatedStatsStore> scope_;
};
class RdsImplTest : public RdsTestBase {
public:
RdsImplTest() {
EXPECT_CALL(server_factory_context_.admin_.config_tracker_, add_("routes", _));
route_config_provider_manager_ =
std::make_unique<RouteConfigProviderManagerImpl>(server_factory_context_.admin_);
}
~RdsImplTest() override { server_factory_context_.thread_local_.shutdownThread(); }
RouteConfigProviderSharedPtr create(
const envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
config,
Server::Configuration::ServerFactoryContext& factory_context,
ProtobufMessage::ValidationVisitor& validator, Init::Manager& init_manager,
const std::string& stat_prefix, RouteConfigProviderManager& route_config_provider_manager) {
switch (config.route_specifier_case()) {
case envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager::
RouteSpecifierCase::kRouteConfig:
return route_config_provider_manager.createStaticRouteConfigProvider(
config.route_config(), factory_context, validator);
case envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager::
RouteSpecifierCase::kRds:
return route_config_provider_manager.createRdsRouteConfigProvider(
// At the creation of a RDS route config provider, the factory_context's initManager is
// always valid, though the init manager may go away later when the listener goes away.
config.rds(), factory_context, stat_prefix, init_manager);
case envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager::
RouteSpecifierCase::kScopedRoutes:
FALLTHRU; // PANIC
case envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager::
RouteSpecifierCase::ROUTE_SPECIFIER_NOT_SET:
PANIC("not implemented");
}
PANIC_DUE_TO_CORRUPT_ENUM;
}
void setup(const std::string& override_config = "") {
std::string config_yaml = R"EOF(
rds:
config_source:
api_config_source:
api_type: REST
cluster_names:
- foo_cluster
refresh_delay: 1s
route_config_name: foo_route_config
codec_type: auto
stat_prefix: foo
http_filters:
- name: http_dynamo_filter
typed_config: {}
)EOF";
if (!override_config.empty()) {
config_yaml = override_config;
}
EXPECT_CALL(outer_init_manager_, add(_));
rds_ =
create(parseHttpConnectionManagerFromYaml(config_yaml), server_factory_context_,
validation_visitor_, outer_init_manager_, "foo.", *route_config_provider_manager_);
rds_callbacks_ = server_factory_context_.cluster_manager_.subscription_factory_.callbacks_;
EXPECT_CALL(*server_factory_context_.cluster_manager_.subscription_factory_.subscription_,
start(_));
outer_init_manager_.initialize(init_watcher_);
}
RouteConstSharedPtr route(Http::TestRequestHeaderMapImpl headers) {
NiceMock<Envoy::StreamInfo::MockStreamInfo> stream_info;
headers.addCopy("x-forwarded-proto", "http");
return rds_->configCast()->route(headers, stream_info, 0);
}
NiceMock<Server::MockInstance> server_;
RouteConfigProviderManagerImplPtr route_config_provider_manager_;
RouteConfigProviderSharedPtr rds_;
};
TEST_F(RdsImplTest, RdsAndStatic) {
const std::string config_yaml = R"EOF(
rds: {}
route_config: {}
codec_type: auto
stat_prefix: foo
http_filters:
- name: http_dynamo_filter
config: {}
)EOF";
EXPECT_THROW(create(parseHttpConnectionManagerFromYaml(config_yaml), server_factory_context_,
validation_visitor_, outer_init_manager_, "foo.",
*route_config_provider_manager_),
EnvoyException);
}
TEST_F(RdsImplTest, RdsAndStaticWithUnknownFilterPerVirtualHostConfig) {
const std::string config_yaml = R"EOF(
route_config:
virtual_hosts:
- name: bar
domains: ["*"]
routes:
- match: { prefix: "/" }
typed_per_filter_config:
filter.unknown:
"@type": type.googleapis.com/google.protobuf.Struct
value:
seconds: 123
codec_type: auto
stat_prefix: foo
http_filters:
- name: filter.unknown
)EOF";
EXPECT_THROW_WITH_MESSAGE(
create(parseHttpConnectionManagerFromYaml(config_yaml), server_factory_context_,
validation_visitor_, outer_init_manager_, "foo.", *route_config_provider_manager_),
EnvoyException,
"Didn't find a registered implementation for 'filter.unknown' with type URL: "
"'google.protobuf.Struct'");
}
TEST_F(RdsImplTest, RdsAndStaticWithOptionalUnknownFilterPerVirtualHostConfig) {
const std::string config_yaml = R"EOF(
route_config:
virtual_hosts:
- name: bar
domains: ["*"]
routes:
- match: { prefix: "/" }
typed_per_filter_config:
filter.unknown:
"@type": type.googleapis.com/envoy.config.route.v3.FilterConfig
is_optional: true
config:
"@type": type.googleapis.com/google.protobuf.Struct
value:
seconds: 123
codec_type: auto
stat_prefix: foo
http_filters:
- name: filter.unknown
is_optional: true
)EOF";
create(parseHttpConnectionManagerFromYaml(config_yaml), server_factory_context_,
validation_visitor_, outer_init_manager_, "foo.", *route_config_provider_manager_);
}
TEST_F(RdsImplTest, DestroyDuringInitialize) {
InSequence s;
setup();
// EXPECT_CALL(server_factory_context_, scope());
EXPECT_CALL(init_watcher_, ready());
rds_.reset();
}
TEST_F(RdsImplTest, Basic) {
InSequence s;
Buffer::OwnedImpl empty;
Buffer::OwnedImpl data;
setup();
// Make sure the initial empty route table works.
EXPECT_EQ(nullptr, route(Http::TestRequestHeaderMapImpl{{":authority", "foo"}}));
// Initial request.
const std::string response1_json = R"EOF(
{
"version_info": "1",
"resources": [
{
"@type": "type.googleapis.com/envoy.config.route.v3.RouteConfiguration",
"name": "foo_route_config",
"virtual_hosts": null
}
]
}
)EOF";
auto response1 =
TestUtility::parseYaml<envoy::service::discovery::v3::DiscoveryResponse>(response1_json);
const auto decoded_resources =
TestUtility::decodeResources<envoy::config::route::v3::RouteConfiguration>(response1);
EXPECT_CALL(init_watcher_, ready());
EXPECT_TRUE(
rds_callbacks_->onConfigUpdate(decoded_resources.refvec_, response1.version_info()).ok());
EXPECT_EQ(nullptr, route(Http::TestRequestHeaderMapImpl{{":authority", "foo"}}));
// 2nd request with same response. Based on hash should not reload config.
EXPECT_TRUE(
rds_callbacks_->onConfigUpdate(decoded_resources.refvec_, response1.version_info()).ok());
EXPECT_EQ(nullptr, route(Http::TestRequestHeaderMapImpl{{":authority", "foo"}}));
// Load the config and verified shared count.
// ConfigConstSharedPtr is shared between: RouteConfigUpdateReceiverImpl, rds_ (via tls_), and
// config local var below.
ConfigConstSharedPtr config = rds_->configCast();
EXPECT_EQ(3, config.use_count());
// Third request.
const std::string response2_json = R"EOF(
{
"version_info": "2",
"resources": [
{
"@type": "type.googleapis.com/envoy.config.route.v3.RouteConfiguration",
"name": "foo_route_config",
"virtual_hosts": [
{
"name": "integration",
"domains": [
"*"
],
"routes": [
{
"match": {
"prefix": "/foo"
},
"route": {
"cluster_header": ":authority"
}
}
]
}
]
}
]
}
)EOF";
auto response2 =
TestUtility::parseYaml<envoy::service::discovery::v3::DiscoveryResponse>(response2_json);
const auto decoded_resources_2 =
TestUtility::decodeResources<envoy::config::route::v3::RouteConfiguration>(response2);
// Make sure we don't lookup/verify clusters.
EXPECT_CALL(server_factory_context_.cluster_manager_, getThreadLocalCluster(Eq("bar"))).Times(0);
EXPECT_TRUE(
rds_callbacks_->onConfigUpdate(decoded_resources_2.refvec_, response2.version_info()).ok());
EXPECT_EQ("foo", route(Http::TestRequestHeaderMapImpl{{":authority", "foo"}, {":path", "/foo"}})
->routeEntry()
->clusterName());
// Old config use count should be 1 now.
EXPECT_EQ(1, config.use_count());
EXPECT_EQ(2UL, scope_.counter("foo.rds.foo_route_config.config_reload").value());
EXPECT_TRUE(scope_.findGaugeByString("foo.rds.foo_route_config.config_reload_time_ms"));
}
// validate there will be exception throw when unknown factory found for per virtualhost typed
// config.
TEST_F(RdsImplTest, UnknownFacotryForPerVirtualHostTypedConfig) {
InSequence s;
setup();
const std::string response1_json = R"EOF(
{
"version_info": "1",
"resources": [
{
"@type": "type.googleapis.com/envoy.config.route.v3.RouteConfiguration",
"name": "foo_route_config",
"virtual_hosts": [
{
"name": "integration",
"domains": [
"*"
],
"routes": [
{
"match": {
"prefix": "/foo"
},
"route": {
"cluster_header": ":authority"
}
}
],
"typed_per_filter_config": {
"filter.unknown": {
"@type": "type.googleapis.com/google.protobuf.Struct"
}
}
}
]
}
]
}
)EOF";
auto response1 =
TestUtility::parseYaml<envoy::service::discovery::v3::DiscoveryResponse>(response1_json);
const auto decoded_resources =
TestUtility::decodeResources<envoy::config::route::v3::RouteConfiguration>(response1);
EXPECT_CALL(init_watcher_, ready());
EXPECT_THROW_WITH_MESSAGE(
EXPECT_TRUE(
rds_callbacks_->onConfigUpdate(decoded_resources.refvec_, response1.version_info()).ok()),
EnvoyException,
"Didn't find a registered implementation for 'filter.unknown' with type URL: "
"'google.protobuf.Struct'");
}
// Validate the optional unknown factory will be ignored for per virtualhost typed config.
TEST_F(RdsImplTest, OptionalUnknownFacotryForPerVirtualHostTypedConfig) {
InSequence s;
const std::string config_yaml = R"EOF(
rds:
config_source:
api_config_source:
api_type: REST
cluster_names:
- foo_cluster
refresh_delay: 1s
route_config_name: foo_route_config
codec_type: auto
stat_prefix: foo
http_filters:
- name: filter.unknown
is_optional: true
)EOF";
setup(config_yaml);
const std::string response1_json = R"EOF(
{
"version_info": "1",
"resources": [
{
"@type": "type.googleapis.com/envoy.config.route.v3.RouteConfiguration",
"name": "foo_route_config",
"virtual_hosts": [
{
"name": "integration",
"domains": [
"*"
],
"routes": [
{
"match": {
"prefix": "/foo"
},
"route": {
"cluster_header": ":authority"
}
}
],
"typed_per_filter_config": {
"filter.unknown": {
"@type": "type.googleapis.com/envoy.config.route.v3.FilterConfig",
"is_optional": true,
"config": {
"@type": "type.googleapis.com/google.protobuf.Struct"
}
}
}
}
]
}
]
}
)EOF";
auto response1 =
TestUtility::parseYaml<envoy::service::discovery::v3::DiscoveryResponse>(response1_json);
const auto decoded_resources =
TestUtility::decodeResources<envoy::config::route::v3::RouteConfiguration>(response1);
EXPECT_CALL(init_watcher_, ready());
EXPECT_TRUE(
rds_callbacks_->onConfigUpdate(decoded_resources.refvec_, response1.version_info()).ok());
}
// validate there will be exception throw when unknown factory found for per route typed config.
TEST_F(RdsImplTest, UnknownFacotryForPerRouteTypedConfig) {
InSequence s;
setup();
const std::string response1_json = R"EOF(
{
"version_info": "1",
"resources": [
{
"@type": "type.googleapis.com/envoy.config.route.v3.RouteConfiguration",
"name": "foo_route_config",
"virtual_hosts": [
{
"name": "integration",
"domains": [
"*"
],
"routes": [
{
"match": {
"prefix": "/foo"
},
"route": {
"cluster_header": ":authority"
},
"typed_per_filter_config": {
"filter.unknown": {
"@type": "type.googleapis.com/google.protobuf.Struct"
}
}
}
],
}
]
}
]
}
)EOF";
auto response1 =
TestUtility::parseYaml<envoy::service::discovery::v3::DiscoveryResponse>(response1_json);
const auto decoded_resources =
TestUtility::decodeResources<envoy::config::route::v3::RouteConfiguration>(response1);
EXPECT_CALL(init_watcher_, ready());
EXPECT_THROW_WITH_MESSAGE(
EXPECT_TRUE(
rds_callbacks_->onConfigUpdate(decoded_resources.refvec_, response1.version_info()).ok()),
EnvoyException,
"Didn't find a registered implementation for 'filter.unknown' with type URL: "
"'google.protobuf.Struct'");
}
// Validates behavior when the config is delivered but it fails PGV validation.
// The invalid config won't affect existing valid config.
TEST_F(RdsImplTest, FailureInvalidConfig) {
InSequence s;
setup();
EXPECT_CALL(init_watcher_, ready());
const std::string valid_json = R"EOF(
{
"version_info": "1",
"resources": [
{
"@type": "type.googleapis.com/envoy.config.route.v3.RouteConfiguration",
"name": "foo_route_config",
"virtual_hosts": null
}
]
}
)EOF";
auto response1 =
TestUtility::parseYaml<envoy::service::discovery::v3::DiscoveryResponse>(valid_json);
const auto decoded_resources =
TestUtility::decodeResources<envoy::config::route::v3::RouteConfiguration>(response1);
EXPECT_TRUE(
rds_callbacks_->onConfigUpdate(decoded_resources.refvec_, response1.version_info()).ok());
// Sadly the RdsRouteConfigSubscription privately inherited from
// SubscriptionCallbacks, so we has to use reinterpret_cast here.
RdsRouteConfigSubscription* rds_subscription =
reinterpret_cast<RdsRouteConfigSubscription*>(rds_callbacks_);
auto config_impl_pointer = rds_subscription->routeConfigProvider()->config();
// Now send an invalid config update.
const std::string invalid_json =
R"EOF(
{
"version_info": "1",
"resources": [
{
"@type": "type.googleapis.com/envoy.config.route.v3.RouteConfiguration",
"name": "INVALID_NAME_FOR_route_config",
"virtual_hosts": null
}
]
}
)EOF";
auto response2 =
TestUtility::parseYaml<envoy::service::discovery::v3::DiscoveryResponse>(invalid_json);
const auto decoded_resources_2 =
TestUtility::decodeResources<envoy::config::route::v3::RouteConfiguration>(response2);
EXPECT_EQ(rds_callbacks_->onConfigUpdate(decoded_resources_2.refvec_, response2.version_info())
.message(),
"Unexpected RDS configuration (expecting foo_route_config): "
"INVALID_NAME_FOR_route_config");
// Verify that the config is still the old value.
ASSERT_EQ(config_impl_pointer, rds_subscription->routeConfigProvider()->config());
}
// rds and vhds configurations change together
TEST_F(RdsImplTest, VHDSandRDSupdateTogether) {
setup();
const std::string response1_json = R"EOF(
{
"version_info": "1",
"resources": [
{
"@type": "type.googleapis.com/envoy.config.route.v3.RouteConfiguration",
"name": "foo_route_config",
"virtual_hosts": [
{
"name": "foo",
"domains": [
"foo"
],
"routes": [
{
"match": {
"prefix": "/foo"
},
"route": {
"cluster": "foo"
}
}
]
}
],
"vhds": {
"config_source": {
"resource_api_version": "V3",
"api_config_source": {
"api_type": "DELTA_GRPC",
"transport_api_version": "V3",
"grpc_services": {
"envoy_grpc": {
"cluster_name": "xds_cluster"
}
}
}
}
}
}
]
}
)EOF";
auto response1 =
TestUtility::parseYaml<envoy::service::discovery::v3::DiscoveryResponse>(response1_json);
const auto decoded_resources =
TestUtility::decodeResources<envoy::config::route::v3::RouteConfiguration>(response1);
EXPECT_CALL(init_watcher_, ready());
EXPECT_TRUE(
rds_callbacks_->onConfigUpdate(decoded_resources.refvec_, response1.version_info()).ok());
EXPECT_TRUE(rds_->configCast()->usesVhds());
EXPECT_EQ("foo", route(Http::TestRequestHeaderMapImpl{{":authority", "foo"}, {":path", "/foo"}})
->routeEntry()
->clusterName());
}
// Validate behavior when the config fails delivery at the subscription level.
TEST_F(RdsImplTest, FailureSubscription) {
InSequence s;
setup();
EXPECT_CALL(init_watcher_, ready());
// onConfigUpdateFailed() should not be called for gRPC stream connection failure
rds_callbacks_->onConfigUpdateFailed(Envoy::Config::ConfigUpdateFailureReason::FetchTimedout, {});
}
// Verifies that a queued up request for a virtual host update doesn't crash if
// RdsRouteConfigProvider is deallocated
TEST_F(RdsImplTest, VirtualHostUpdateWhenProviderHasBeenDeallocated) {
const std::string rds_config = R"EOF(
rds:
route_config_name: my_route
config_source:
api_config_source:
api_type: GRPC
grpc_services:
envoy_grpc:
cluster_name: xds_cluster
)EOF";
Event::PostCb post_cb;
testing::NiceMock<Event::MockDispatcher> local_thread_dispatcher;
testing::MockFunction<void(bool)> mock_callback;
{
auto rds =
create(parseHttpConnectionManagerFromYaml(rds_config), server_factory_context_,
validation_visitor_, outer_init_manager_, "foo.", *route_config_provider_manager_);
EXPECT_CALL(server_factory_context_.dispatcher_, post(_))
.WillOnce([&post_cb](Event::PostCb cb) { post_cb = std::move(cb); });
rds->requestVirtualHostsUpdate(
"testing", local_thread_dispatcher,
std::make_shared<Http::RouteConfigUpdatedCallback>(
Http::RouteConfigUpdatedCallback(mock_callback.AsStdFunction())));
}
// Invoke the callback that was scheduled on the main thread
// RdsRouteConfigProvider in rds is out of scope and callback's captured parameters are no longer
// valid
EXPECT_CALL(mock_callback, Call(_)).Times(0);
EXPECT_NO_THROW(post_cb());
}
TEST_F(RdsImplTest, RdsRouteConfigProviderImplSubscriptionSetup) {
setup();
EXPECT_CALL(init_watcher_, ready());
RdsRouteConfigSubscription& subscription =
dynamic_cast<RdsRouteConfigProviderImpl&>(*rds_).subscription();
EXPECT_EQ(rds_.get(), subscription.routeConfigProvider());
}
class RdsRouteConfigSubscriptionTest : public RdsTestBase {
public:
RdsRouteConfigSubscriptionTest() {
EXPECT_CALL(server_factory_context_.admin_.config_tracker_, add_("routes", _));
route_config_provider_manager_ =
std::make_unique<RouteConfigProviderManagerImpl>(server_factory_context_.admin_);
}
~RdsRouteConfigSubscriptionTest() override {
server_factory_context_.thread_local_.shutdownThread();
}
RouteConfigProviderManagerImplPtr route_config_provider_manager_;
};
// Verifies that maybeCreateInitManager() creates a noop init manager if the main init manager is in
// Initialized state already
TEST_F(RdsRouteConfigSubscriptionTest, CreatesNoopInitManager) {
const std::string rds_config = R"EOF(
route_config_name: my_route
config_source:
api_config_source:
api_type: GRPC
grpc_services:
envoy_grpc:
cluster_name: xds_cluster
)EOF";
const auto rds =
TestUtility::parseYaml<envoy::extensions::filters::network::http_connection_manager::v3::Rds>(
rds_config);
const auto route_config_provider = route_config_provider_manager_->createRdsRouteConfigProvider(
rds, server_factory_context_, "stat_prefix", outer_init_manager_);
RdsRouteConfigSubscription& subscription =
(dynamic_cast<RdsRouteConfigProviderImpl*>(route_config_provider.get()))->subscription();
init_watcher_.expectReady(); // The parent_init_target_ will call once.
outer_init_manager_.initialize(init_watcher_);
std::unique_ptr<Init::ManagerImpl> noop_init_manager;
std::unique_ptr<Cleanup> init_vhds;
subscription.maybeCreateInitManager("version_info", noop_init_manager, init_vhds);
// local_init_manager_ is not ready yet as the local_init_target_ is not ready.
EXPECT_EQ(init_vhds, nullptr);
EXPECT_EQ(noop_init_manager, nullptr);
// Now mark local_init_target_ ready by forcing an update failure.
auto* rds_callbacks_ = server_factory_context_.cluster_manager_.subscription_factory_.callbacks_;
EnvoyException e("test");
rds_callbacks_->onConfigUpdateFailed(Envoy::Config::ConfigUpdateFailureReason::UpdateRejected,
&e);
// Now noop init manager will be created as local_init_manager_ is initialized.
subscription.maybeCreateInitManager("version_info", noop_init_manager, init_vhds);
EXPECT_NE(init_vhds, nullptr);
EXPECT_NE(noop_init_manager, nullptr);
}
class RouteConfigProviderManagerImplTest : public RdsTestBase {
public:
void setup() {
// Get a RouteConfigProvider. This one should create an entry in the RouteConfigProviderManager.
rds_.set_route_config_name("foo_route_config");
rds_.mutable_config_source()->set_path("foo_path");
provider_ = route_config_provider_manager_->createRdsRouteConfigProvider(
rds_, server_factory_context_, "foo_prefix.", outer_init_manager_);
rds_callbacks_ = server_factory_context_.cluster_manager_.subscription_factory_.callbacks_;
}
RouteConfigProviderManagerImplTest() {
EXPECT_CALL(server_factory_context_.admin_.config_tracker_, add_("routes", _));
route_config_provider_manager_ =
std::make_unique<RouteConfigProviderManagerImpl>(server_factory_context_.admin_);
}
~RouteConfigProviderManagerImplTest() override {
server_factory_context_.thread_local_.shutdownThread();
}
envoy::extensions::filters::network::http_connection_manager::v3::Rds rds_;
RouteConfigProviderManagerImplPtr route_config_provider_manager_;
RouteConfigProviderSharedPtr provider_;
};
envoy::config::route::v3::RouteConfiguration
parseRouteConfigurationFromV3Yaml(const std::string& yaml) {
envoy::config::route::v3::RouteConfiguration route_config;
TestUtility::loadFromYaml(yaml, route_config);
return route_config;
}
TEST_F(RouteConfigProviderManagerImplTest, ConfigDump) {
UniversalStringMatcher universal_name_matcher;
auto message_ptr =
server_factory_context_.admin_.config_tracker_.config_tracker_callbacks_["routes"](
universal_name_matcher);
const auto& route_config_dump =
TestUtility::downcastAndValidate<const envoy::admin::v3::RoutesConfigDump&>(*message_ptr);
// No routes at all, no last_updated timestamp
envoy::admin::v3::RoutesConfigDump expected_route_config_dump;
TestUtility::loadFromYaml(R"EOF(
static_route_configs:
dynamic_route_configs:
)EOF",
expected_route_config_dump);
EXPECT_EQ(expected_route_config_dump.DebugString(), route_config_dump.DebugString());
const std::string config_yaml = R"EOF(
name: foo
virtual_hosts:
- name: bar
domains: ["*"]
routes:
- match: { prefix: "/" }
route: { cluster: baz }
)EOF";
timeSystem().setSystemTime(std::chrono::milliseconds(1234567891234));
// Only static route.
server_factory_context_.cluster_manager_.initializeClusters({"baz"}, {});
RouteConfigProviderPtr static_config =
route_config_provider_manager_->createStaticRouteConfigProvider(
parseRouteConfigurationFromV3Yaml(config_yaml), server_factory_context_,
validation_visitor_);
message_ptr = server_factory_context_.admin_.config_tracker_.config_tracker_callbacks_["routes"](
universal_name_matcher);
const auto& route_config_dump2 =
TestUtility::downcastAndValidate<const envoy::admin::v3::RoutesConfigDump&>(*message_ptr);
TestUtility::loadFromYaml(R"EOF(
static_route_configs:
- route_config:
"@type": type.googleapis.com/envoy.config.route.v3.RouteConfiguration
name: foo
virtual_hosts:
- name: bar
domains: ["*"]
routes:
- match: { prefix: "/" }
route: { cluster: baz }
last_updated:
seconds: 1234567891
nanos: 234000000
dynamic_route_configs:
)EOF",
expected_route_config_dump);
EXPECT_EQ(expected_route_config_dump.DebugString(), route_config_dump2.DebugString());
// Static + dynamic.
setup();
EXPECT_CALL(*server_factory_context_.cluster_manager_.subscription_factory_.subscription_,
start(_));
outer_init_manager_.initialize(init_watcher_);
const std::string response1_json = R"EOF(
{
"version_info": "1",
"resources": [
{
"@type": "type.googleapis.com/envoy.config.route.v3.RouteConfiguration",
"name": "foo_route_config",
"virtual_hosts": null
}
]
}
)EOF";
auto response1 =
TestUtility::parseYaml<envoy::service::discovery::v3::DiscoveryResponse>(response1_json);
const auto decoded_resources =
TestUtility::decodeResources<envoy::config::route::v3::RouteConfiguration>(response1);
EXPECT_CALL(init_watcher_, ready());
EXPECT_TRUE(
rds_callbacks_->onConfigUpdate(decoded_resources.refvec_, response1.version_info()).ok());
message_ptr = server_factory_context_.admin_.config_tracker_.config_tracker_callbacks_["routes"](
universal_name_matcher);
const auto& route_config_dump3 =
TestUtility::downcastAndValidate<const envoy::admin::v3::RoutesConfigDump&>(*message_ptr);
TestUtility::loadFromYaml(R"EOF(
static_route_configs:
- route_config:
"@type": type.googleapis.com/envoy.config.route.v3.RouteConfiguration
name: foo
virtual_hosts:
- name: bar
domains: ["*"]
routes:
- match: { prefix: "/" }
route: { cluster: baz }
last_updated:
seconds: 1234567891
nanos: 234000000
dynamic_route_configs:
- version_info: "1"
route_config:
"@type": type.googleapis.com/envoy.config.route.v3.RouteConfiguration
name: foo_route_config
virtual_hosts:
last_updated:
seconds: 1234567891
nanos: 234000000
)EOF",
expected_route_config_dump);
EXPECT_EQ(expected_route_config_dump.DebugString(), route_config_dump3.DebugString());
MockStringMatcher mock_name_matcher;
EXPECT_CALL(mock_name_matcher, match("foo")).WillOnce(Return(true));
EXPECT_CALL(mock_name_matcher, match("foo_route_config")).WillOnce(Return(false));
message_ptr = server_factory_context_.admin_.config_tracker_.config_tracker_callbacks_["routes"](
mock_name_matcher);
const auto& route_config_dump4 =
TestUtility::downcastAndValidate<const envoy::admin::v3::RoutesConfigDump&>(*message_ptr);
TestUtility::loadFromYaml(R"EOF(
static_route_configs:
- route_config:
"@type": type.googleapis.com/envoy.config.route.v3.RouteConfiguration
name: foo
virtual_hosts:
- name: bar
domains: ["*"]
routes:
- match: { prefix: "/" }
route: { cluster: baz }
last_updated:
seconds: 1234567891
nanos: 234000000
)EOF",
expected_route_config_dump);
EXPECT_EQ(expected_route_config_dump.DebugString(), route_config_dump4.DebugString());
EXPECT_CALL(mock_name_matcher, match("foo")).WillOnce(Return(false));
EXPECT_CALL(mock_name_matcher, match("foo_route_config")).WillOnce(Return(true));
message_ptr = server_factory_context_.admin_.config_tracker_.config_tracker_callbacks_["routes"](
mock_name_matcher);
const auto& route_config_dump5 =
TestUtility::downcastAndValidate<const envoy::admin::v3::RoutesConfigDump&>(*message_ptr);
TestUtility::loadFromYaml(R"EOF(
dynamic_route_configs:
- version_info: "1"
route_config:
"@type": type.googleapis.com/envoy.config.route.v3.RouteConfiguration
name: foo_route_config
virtual_hosts:
last_updated:
seconds: 1234567891
nanos: 234000000
)EOF",
expected_route_config_dump);
EXPECT_EQ(expected_route_config_dump.DebugString(), route_config_dump5.DebugString());
}
TEST_F(RouteConfigProviderManagerImplTest, Basic) {
Buffer::OwnedImpl data;
// Get a RouteConfigProvider. This one should create an entry in the RouteConfigProviderManager.
setup();
EXPECT_FALSE(provider_->configInfo().has_value());
const auto route_config = parseRouteConfigurationFromV3Yaml(R"EOF(
name: foo_route_config
virtual_hosts:
- name: bar
domains: ["*"]
routes:
- match: { prefix: "/" }
route: { cluster: baz }
)EOF");
const auto decoded_resources = TestUtility::decodeResources({route_config});
EXPECT_TRUE(server_factory_context_.cluster_manager_.subscription_factory_.callbacks_
->onConfigUpdate(decoded_resources.refvec_, "1")
.ok());
RouteConfigProviderSharedPtr provider2 =
route_config_provider_manager_->createRdsRouteConfigProvider(
rds_, server_factory_context_, "foo_prefix", outer_init_manager_);
// provider2 should have route config immediately after create
EXPECT_TRUE(provider2->configInfo().has_value());
EXPECT_EQ(provider_, provider2) << "fail to obtain the same rds config provider object";
// So this means that both provider have same subscription.
EXPECT_EQ(&dynamic_cast<RdsRouteConfigProviderImpl&>(*provider_).subscription(),
&dynamic_cast<RdsRouteConfigProviderImpl&>(*provider2).subscription());
EXPECT_EQ(&provider_->configInfo().value().config_, &provider2->configInfo().value().config_);
envoy::extensions::filters::network::http_connection_manager::v3::Rds rds2;
rds2.set_route_config_name("foo_route_config");
rds2.mutable_config_source()->set_path("bar_path");
RouteConfigProviderSharedPtr provider3 =
route_config_provider_manager_->createRdsRouteConfigProvider(
rds2, server_factory_context_, "foo_prefix", outer_init_manager_);
EXPECT_NE(provider3, provider_);
EXPECT_TRUE(server_factory_context_.cluster_manager_.subscription_factory_.callbacks_
->onConfigUpdate(decoded_resources.refvec_, "provider3")
.ok());
UniversalStringMatcher universal_name_matcher;
EXPECT_EQ(2UL, route_config_provider_manager_->dumpRouteConfigs(universal_name_matcher)
->dynamic_route_configs()
.size());
provider_.reset();
provider2.reset();
// All shared_ptrs to the provider pointed at by provider1, and provider2 have been deleted, so
// now we should only have the provider pointed at by provider3.
auto dynamic_route_configs =
route_config_provider_manager_->dumpRouteConfigs(universal_name_matcher)