forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility_test.cc
565 lines (492 loc) · 24.8 KB
/
utility_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
#include "envoy/api/v2/cluster.pb.h"
#include "envoy/common/exception.h"
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
#include "envoy/config/cluster/v3/cluster.pb.h"
#include "envoy/config/core/v3/config_source.pb.h"
#include "envoy/config/core/v3/grpc_service.pb.h"
#include "envoy/extensions/filters/http/cors/v3/cors.pb.h"
#include "source/common/common/fmt.h"
#include "source/common/config/api_version.h"
#include "source/common/config/utility.h"
#include "source/common/config/well_known_names.h"
#include "source/common/protobuf/protobuf.h"
#include "test/mocks/config/mocks.h"
#include "test/mocks/grpc/mocks.h"
#include "test/mocks/local_info/mocks.h"
#include "test/mocks/stats/mocks.h"
#include "test/mocks/upstream/cluster_manager.h"
#include "test/mocks/upstream/thread_local_cluster.h"
#include "test/test_common/environment.h"
#include "test/test_common/logging.h"
#include "test/test_common/utility.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "udpa/type/v1/typed_struct.pb.h"
#include "xds/type/v3/typed_struct.pb.h"
using testing::Ref;
using testing::Return;
namespace Envoy {
namespace Config {
namespace {
TEST(UtilityTest, ComputeHashedVersion) {
EXPECT_EQ("hash_2e1472b57af294d1", Utility::computeHashedVersion("{}").first);
EXPECT_EQ("hash_33bf00a859c4ba3f", Utility::computeHashedVersion("foo").first);
}
TEST(UtilityTest, ApiConfigSourceRefreshDelay) {
envoy::config::core::v3::ApiConfigSource api_config_source;
api_config_source.mutable_refresh_delay()->CopyFrom(
Protobuf::util::TimeUtil::MillisecondsToDuration(1234));
EXPECT_EQ(1234, Utility::apiConfigSourceRefreshDelay(api_config_source).count());
}
TEST(UtilityTest, ApiConfigSourceDefaultRequestTimeout) {
envoy::config::core::v3::ApiConfigSource api_config_source;
EXPECT_EQ(1000, Utility::apiConfigSourceRequestTimeout(api_config_source).count());
}
TEST(UtilityTest, ApiConfigSourceRequestTimeout) {
envoy::config::core::v3::ApiConfigSource api_config_source;
api_config_source.mutable_request_timeout()->CopyFrom(
Protobuf::util::TimeUtil::MillisecondsToDuration(1234));
EXPECT_EQ(1234, Utility::apiConfigSourceRequestTimeout(api_config_source).count());
}
TEST(UtilityTest, ConfigSourceDefaultInitFetchTimeout) {
envoy::config::core::v3::ConfigSource config_source;
EXPECT_EQ(15000, Utility::configSourceInitialFetchTimeout(config_source).count());
}
TEST(UtilityTest, ConfigSourceInitFetchTimeout) {
envoy::config::core::v3::ConfigSource config_source;
config_source.mutable_initial_fetch_timeout()->CopyFrom(
Protobuf::util::TimeUtil::MillisecondsToDuration(654));
EXPECT_EQ(654, Utility::configSourceInitialFetchTimeout(config_source).count());
}
TEST(UtilityTest, TranslateApiConfigSource) {
envoy::config::core::v3::ApiConfigSource api_config_source_rest_legacy;
Utility::translateApiConfigSource("test_rest_legacy_cluster", 10000, ApiType::get().Rest,
api_config_source_rest_legacy);
EXPECT_EQ(envoy::config::core::v3::ApiConfigSource::REST,
api_config_source_rest_legacy.api_type());
EXPECT_EQ(10000,
DurationUtil::durationToMilliseconds(api_config_source_rest_legacy.refresh_delay()));
EXPECT_EQ("test_rest_legacy_cluster", api_config_source_rest_legacy.cluster_names(0));
envoy::config::core::v3::ApiConfigSource api_config_source_rest;
Utility::translateApiConfigSource("test_rest_cluster", 20000, ApiType::get().Rest,
api_config_source_rest);
EXPECT_EQ(envoy::config::core::v3::ApiConfigSource::REST, api_config_source_rest.api_type());
EXPECT_EQ(20000, DurationUtil::durationToMilliseconds(api_config_source_rest.refresh_delay()));
EXPECT_EQ("test_rest_cluster", api_config_source_rest.cluster_names(0));
envoy::config::core::v3::ApiConfigSource api_config_source_grpc;
Utility::translateApiConfigSource("test_grpc_cluster", 30000, ApiType::get().Grpc,
api_config_source_grpc);
EXPECT_EQ(envoy::config::core::v3::ApiConfigSource::GRPC, api_config_source_grpc.api_type());
EXPECT_EQ(30000, DurationUtil::durationToMilliseconds(api_config_source_grpc.refresh_delay()));
EXPECT_EQ("test_grpc_cluster",
api_config_source_grpc.grpc_services(0).envoy_grpc().cluster_name());
}
TEST(UtilityTest, createTagProducer) {
envoy::config::bootstrap::v3::Bootstrap bootstrap;
auto producer = Utility::createTagProducer(bootstrap, {});
ASSERT_TRUE(producer != nullptr);
Stats::TagVector tags;
auto extracted_name = producer->produceTags("http.config_test.rq_total", tags);
ASSERT_EQ(extracted_name, "http.rq_total");
ASSERT_EQ(tags.size(), 1);
}
TEST(UtilityTest, createTagProducerWithDefaultTgs) {
envoy::config::bootstrap::v3::Bootstrap bootstrap;
auto producer = Utility::createTagProducer(bootstrap, {{"foo", "bar"}});
ASSERT_TRUE(producer != nullptr);
Stats::TagVector tags;
auto extracted_name = producer->produceTags("http.config_test.rq_total", tags);
EXPECT_EQ(extracted_name, "http.rq_total");
EXPECT_EQ(tags.size(), 2);
}
TEST(UtilityTest, CheckFilesystemSubscriptionBackingPath) {
Api::ApiPtr api = Api::createApiForTest();
EXPECT_THROW_WITH_MESSAGE(
Utility::checkFilesystemSubscriptionBackingPath("foo", *api), EnvoyException,
"paths must refer to an existing path in the system: 'foo' does not exist");
std::string test_path = TestEnvironment::temporaryDirectory();
Utility::checkFilesystemSubscriptionBackingPath(test_path, *api);
}
TEST(UtilityTest, ParseDefaultRateLimitSettings) {
envoy::config::core::v3::ApiConfigSource api_config_source;
const RateLimitSettings& rate_limit_settings = Utility::parseRateLimitSettings(api_config_source);
EXPECT_EQ(false, rate_limit_settings.enabled_);
EXPECT_EQ(100, rate_limit_settings.max_tokens_);
EXPECT_EQ(10, rate_limit_settings.fill_rate_);
}
TEST(UtilityTest, ParseEmptyRateLimitSettings) {
envoy::config::core::v3::ApiConfigSource api_config_source;
api_config_source.mutable_rate_limit_settings();
const RateLimitSettings& rate_limit_settings = Utility::parseRateLimitSettings(api_config_source);
EXPECT_EQ(true, rate_limit_settings.enabled_);
EXPECT_EQ(100, rate_limit_settings.max_tokens_);
EXPECT_EQ(10, rate_limit_settings.fill_rate_);
}
TEST(UtilityTest, ParseRateLimitSettings) {
envoy::config::core::v3::ApiConfigSource api_config_source;
envoy::config::core::v3::RateLimitSettings* rate_limits =
api_config_source.mutable_rate_limit_settings();
rate_limits->mutable_max_tokens()->set_value(500);
rate_limits->mutable_fill_rate()->set_value(4);
const RateLimitSettings& rate_limit_settings = Utility::parseRateLimitSettings(api_config_source);
EXPECT_EQ(true, rate_limit_settings.enabled_);
EXPECT_EQ(500, rate_limit_settings.max_tokens_);
EXPECT_EQ(4, rate_limit_settings.fill_rate_);
}
// TEST(UtilityTest, FactoryForGrpcApiConfigSource) should catch misconfigured
// API configs along the dimension of ApiConfigSource type.
TEST(UtilityTest, FactoryForGrpcApiConfigSource) {
NiceMock<Grpc::MockAsyncClientManager> async_client_manager;
Stats::MockStore scope;
{
envoy::config::core::v3::ApiConfigSource api_config_source;
api_config_source.set_api_type(envoy::config::core::v3::ApiConfigSource::GRPC);
EXPECT_THROW_WITH_REGEX(
Utility::factoryForGrpcApiConfigSource(async_client_manager, api_config_source, scope,
false),
EnvoyException, "API configs must have either a gRPC service or a cluster name defined:");
}
{
envoy::config::core::v3::ApiConfigSource api_config_source;
api_config_source.set_api_type(envoy::config::core::v3::ApiConfigSource::GRPC);
api_config_source.add_grpc_services();
api_config_source.add_grpc_services();
EXPECT_THROW_WITH_REGEX(Utility::factoryForGrpcApiConfigSource(async_client_manager,
api_config_source, scope, false),
EnvoyException,
fmt::format("{}::.DELTA_.GRPC must have a single gRPC service "
"specified:",
api_config_source.GetTypeName()));
}
{
envoy::config::core::v3::ApiConfigSource api_config_source;
api_config_source.set_api_type(envoy::config::core::v3::ApiConfigSource::GRPC);
api_config_source.add_cluster_names();
// this also logs a warning for setting REST cluster names for a gRPC API config.
EXPECT_THROW_WITH_REGEX(Utility::factoryForGrpcApiConfigSource(async_client_manager,
api_config_source, scope, false),
EnvoyException,
fmt::format("{}::.DELTA_.GRPC must not have a cluster name "
"specified:",
api_config_source.GetTypeName()));
}
{
envoy::config::core::v3::ApiConfigSource api_config_source;
api_config_source.set_api_type(envoy::config::core::v3::ApiConfigSource::GRPC);
api_config_source.add_cluster_names();
api_config_source.add_cluster_names();
EXPECT_THROW_WITH_REGEX(Utility::factoryForGrpcApiConfigSource(async_client_manager,
api_config_source, scope, false),
EnvoyException,
fmt::format("{}::.DELTA_.GRPC must not have a cluster name "
"specified:",
api_config_source.GetTypeName()));
}
{
envoy::config::core::v3::ApiConfigSource api_config_source;
api_config_source.set_api_type(envoy::config::core::v3::ApiConfigSource::REST);
api_config_source.add_grpc_services()->mutable_envoy_grpc()->set_cluster_name("foo");
// this also logs a warning for configuring gRPC clusters for a REST API config.
EXPECT_THROW_WITH_REGEX(Utility::factoryForGrpcApiConfigSource(async_client_manager,
api_config_source, scope, false),
EnvoyException,
fmt::format("{}, if not a gRPC type, must not have a gRPC service "
"specified:",
api_config_source.GetTypeName()));
}
{
envoy::config::core::v3::ApiConfigSource api_config_source;
api_config_source.set_api_type(envoy::config::core::v3::ApiConfigSource::REST);
api_config_source.add_cluster_names("foo");
EXPECT_THROW_WITH_REGEX(Utility::factoryForGrpcApiConfigSource(async_client_manager,
api_config_source, scope, false),
EnvoyException,
fmt::format("{} type must be gRPC:", api_config_source.GetTypeName()));
}
{
envoy::config::core::v3::ApiConfigSource api_config_source;
api_config_source.set_api_type(envoy::config::core::v3::ApiConfigSource::GRPC);
api_config_source.add_grpc_services()->mutable_envoy_grpc()->set_cluster_name("foo");
envoy::config::core::v3::GrpcService expected_grpc_service;
expected_grpc_service.mutable_envoy_grpc()->set_cluster_name("foo");
EXPECT_CALL(async_client_manager,
factoryForGrpcService(ProtoEq(expected_grpc_service), Ref(scope), false));
Utility::factoryForGrpcApiConfigSource(async_client_manager, api_config_source, scope, false);
}
{
envoy::config::core::v3::ApiConfigSource api_config_source;
api_config_source.set_api_type(envoy::config::core::v3::ApiConfigSource::GRPC);
api_config_source.add_grpc_services()->mutable_envoy_grpc()->set_cluster_name("foo");
EXPECT_CALL(
async_client_manager,
factoryForGrpcService(ProtoEq(api_config_source.grpc_services(0)), Ref(scope), true));
Utility::factoryForGrpcApiConfigSource(async_client_manager, api_config_source, scope, true);
}
}
TEST(UtilityTest, PrepareDnsRefreshStrategy) {
NiceMock<Random::MockRandomGenerator> random;
{
// dns_failure_refresh_rate not set.
envoy::config::cluster::v3::Cluster cluster;
BackOffStrategyPtr strategy =
Utility::prepareDnsRefreshStrategy<envoy::config::cluster::v3::Cluster>(cluster, 5000,
random);
EXPECT_NE(nullptr, dynamic_cast<FixedBackOffStrategy*>(strategy.get()));
}
{
// dns_failure_refresh_rate set.
envoy::config::cluster::v3::Cluster cluster;
cluster.mutable_dns_failure_refresh_rate()->mutable_base_interval()->set_seconds(7);
cluster.mutable_dns_failure_refresh_rate()->mutable_max_interval()->set_seconds(10);
BackOffStrategyPtr strategy =
Utility::prepareDnsRefreshStrategy<envoy::config::cluster::v3::Cluster>(cluster, 5000,
random);
EXPECT_NE(nullptr, dynamic_cast<JitteredExponentialBackOffStrategy*>(strategy.get()));
}
{
// dns_failure_refresh_rate set with invalid max_interval.
envoy::config::cluster::v3::Cluster cluster;
cluster.mutable_dns_failure_refresh_rate()->mutable_base_interval()->set_seconds(7);
cluster.mutable_dns_failure_refresh_rate()->mutable_max_interval()->set_seconds(2);
EXPECT_THROW_WITH_REGEX(Utility::prepareDnsRefreshStrategy<envoy::config::cluster::v3::Cluster>(
cluster, 5000, random),
EnvoyException,
"dns_failure_refresh_rate must have max_interval greater than "
"or equal to the base_interval");
}
}
// Validate that an opaque config of the wrong type throws during conversion.
TEST(UtilityTest, AnyWrongType) {
ProtobufWkt::Duration source_duration;
source_duration.set_seconds(42);
ProtobufWkt::Any typed_config;
typed_config.PackFrom(source_duration);
ProtobufWkt::Timestamp out;
EXPECT_THROW_WITH_REGEX(
Utility::translateOpaqueConfig(typed_config, ProtobufMessage::getStrictValidationVisitor(),
out),
EnvoyException,
R"(Unable to unpack as google.protobuf.Timestamp: \[type.googleapis.com/google.protobuf.Duration\] .*)");
}
TEST(UtilityTest, TranslateAnyWrongToFactoryConfig) {
ProtobufWkt::Duration source_duration;
source_duration.set_seconds(42);
ProtobufWkt::Any typed_config;
typed_config.PackFrom(source_duration);
MockTypedFactory factory;
EXPECT_CALL(factory, createEmptyConfigProto()).WillOnce(Invoke([]() -> ProtobufTypes::MessagePtr {
return ProtobufTypes::MessagePtr{new ProtobufWkt::Timestamp()};
}));
EXPECT_THROW_WITH_REGEX(
Utility::translateAnyToFactoryConfig(typed_config,
ProtobufMessage::getStrictValidationVisitor(), factory),
EnvoyException,
R"(Unable to unpack as google.protobuf.Timestamp: \[type.googleapis.com/google.protobuf.Duration\] .*)");
}
TEST(UtilityTest, TranslateAnyToFactoryConfig) {
ProtobufWkt::Duration source_duration;
source_duration.set_seconds(42);
ProtobufWkt::Any typed_config;
typed_config.PackFrom(source_duration);
MockTypedFactory factory;
EXPECT_CALL(factory, createEmptyConfigProto()).WillOnce(Invoke([]() -> ProtobufTypes::MessagePtr {
return ProtobufTypes::MessagePtr{new ProtobufWkt::Duration()};
}));
auto config = Utility::translateAnyToFactoryConfig(
typed_config, ProtobufMessage::getStrictValidationVisitor(), factory);
EXPECT_THAT(*config, ProtoEq(source_duration));
}
template <typename T> class UtilityTypedStructTest : public ::testing::Test {
public:
static void packTypedStructIntoAny(ProtobufWkt::Any& typed_config,
const Protobuf::Message& inner) {
T typed_struct;
(*typed_struct.mutable_type_url()) =
absl::StrCat("type.googleapis.com/", inner.GetDescriptor()->full_name());
MessageUtil::jsonConvert(inner, *typed_struct.mutable_value());
typed_config.PackFrom(typed_struct);
}
};
using TypedStructTypes = ::testing::Types<xds::type::v3::TypedStruct, udpa::type::v1::TypedStruct>;
TYPED_TEST_SUITE(UtilityTypedStructTest, TypedStructTypes);
// Verify that TypedStruct can be translated into google.protobuf.Struct
TYPED_TEST(UtilityTypedStructTest, TypedStructToStruct) {
ProtobufWkt::Any typed_config;
ProtobufWkt::Struct untyped_struct;
(*untyped_struct.mutable_fields())["foo"].set_string_value("bar");
this->packTypedStructIntoAny(typed_config, untyped_struct);
ProtobufWkt::Struct out;
Utility::translateOpaqueConfig(typed_config, ProtobufMessage::getStrictValidationVisitor(), out);
EXPECT_THAT(out, ProtoEq(untyped_struct));
}
// Verify that TypedStruct can be translated into an arbitrary message of correct type
// (v2 API, no upgrading).
TYPED_TEST(UtilityTypedStructTest, TypedStructToClusterV2) {
ProtobufWkt::Any typed_config;
API_NO_BOOST(envoy::api::v2::Cluster) cluster;
const std::string cluster_config_yaml = R"EOF(
drain_connections_on_host_removal: true
)EOF";
TestUtility::loadFromYaml(cluster_config_yaml, cluster);
this->packTypedStructIntoAny(typed_config, cluster);
{
API_NO_BOOST(envoy::api::v2::Cluster) out;
Utility::translateOpaqueConfig(typed_config, ProtobufMessage::getNullValidationVisitor(), out);
EXPECT_THAT(out, ProtoEq(cluster));
}
{
API_NO_BOOST(envoy::api::v2::Cluster) out;
Utility::translateOpaqueConfig(typed_config, ProtobufMessage::getStrictValidationVisitor(),
out);
EXPECT_THAT(out, ProtoEq(cluster));
}
}
// Verify that TypedStruct can be translated into an arbitrary message of correct type
// (v3 API, upgrading).
TYPED_TEST(UtilityTypedStructTest, TypedStructToClusterV3) {
ProtobufWkt::Any typed_config;
API_NO_BOOST(envoy::config::cluster::v3::Cluster) cluster;
const std::string cluster_config_yaml = R"EOF(
ignore_health_on_host_removal: true
)EOF";
TestUtility::loadFromYaml(cluster_config_yaml, cluster);
this->packTypedStructIntoAny(typed_config, cluster);
{
API_NO_BOOST(envoy::config::cluster::v3::Cluster) out;
Utility::translateOpaqueConfig(typed_config, ProtobufMessage::getNullValidationVisitor(), out);
EXPECT_THAT(out, ProtoEq(cluster));
}
{
API_NO_BOOST(envoy::config::cluster::v3::Cluster) out;
Utility::translateOpaqueConfig(typed_config, ProtobufMessage::getStrictValidationVisitor(),
out);
EXPECT_THAT(out, ProtoEq(cluster));
}
}
// Verify that translation from TypedStruct into message of incorrect type fails
TYPED_TEST(UtilityTypedStructTest, TypedStructToInvalidType) {
ProtobufWkt::Any typed_config;
envoy::config::bootstrap::v3::Bootstrap bootstrap;
const std::string bootstrap_config_yaml = R"EOF(
admin:
access_log:
- name: envoy.access_loggers.file
typed_config:
"@type": type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog
path: /dev/null
address:
pipe:
path: "/"
)EOF";
TestUtility::loadFromYaml(bootstrap_config_yaml, bootstrap);
this->packTypedStructIntoAny(typed_config, bootstrap);
ProtobufWkt::Any out;
EXPECT_THROW_WITH_REGEX(Utility::translateOpaqueConfig(
typed_config, ProtobufMessage::getStrictValidationVisitor(), out),
EnvoyException, "Unable to parse JSON as proto");
}
// Verify that Any can be translated into an arbitrary message of correct type
// (v2 API, no upgrading).
TEST(UtilityTest, AnyToClusterV2) {
ProtobufWkt::Any typed_config;
API_NO_BOOST(envoy::api::v2::Cluster) cluster;
const std::string cluster_config_yaml = R"EOF(
drain_connections_on_host_removal: true
)EOF";
TestUtility::loadFromYaml(cluster_config_yaml, cluster);
typed_config.PackFrom(cluster);
API_NO_BOOST(envoy::api::v2::Cluster) out;
Utility::translateOpaqueConfig(typed_config, ProtobufMessage::getStrictValidationVisitor(), out);
EXPECT_THAT(out, ProtoEq(cluster));
}
// Verify that Any can be translated into an arbitrary message of correct type
// (v3 API, upgrading).
TEST(UtilityTest, AnyToClusterV3) {
ProtobufWkt::Any typed_config;
API_NO_BOOST(envoy::config::cluster::v3::Cluster) cluster;
const std::string cluster_config_yaml = R"EOF(
ignore_health_on_host_removal: true
)EOF";
TestUtility::loadFromYaml(cluster_config_yaml, cluster);
typed_config.PackFrom(cluster);
API_NO_BOOST(envoy::config::cluster::v3::Cluster) out;
Utility::translateOpaqueConfig(typed_config, ProtobufMessage::getStrictValidationVisitor(), out);
EXPECT_THAT(out, ProtoEq(cluster));
}
// Verify that ProtobufWkt::Empty can load into a typed factory with an empty config proto
TEST(UtilityTest, EmptyToEmptyConfig) {
ProtobufWkt::Any typed_config;
ProtobufWkt::Empty empty_config;
typed_config.PackFrom(empty_config);
envoy::extensions::filters::http::cors::v3::Cors out;
Utility::translateOpaqueConfig(typed_config, ProtobufMessage::getStrictValidationVisitor(), out);
EXPECT_THAT(out, ProtoEq(envoy::extensions::filters::http::cors::v3::Cors()));
}
TEST(CheckApiConfigSourceSubscriptionBackingClusterTest, GrpcClusterTestAcrossTypes) {
envoy::config::core::v3::ConfigSource config;
auto* api_config_source = config.mutable_api_config_source();
Upstream::ClusterManager::ClusterSet primary_clusters;
// API of type GRPC
api_config_source->set_api_type(envoy::config::core::v3::ApiConfigSource::GRPC);
// GRPC cluster without GRPC services.
EXPECT_THROW_WITH_REGEX(
Utility::checkApiConfigSourceSubscriptionBackingCluster(primary_clusters, *api_config_source),
EnvoyException, "API configs must have either a gRPC service or a cluster name defined:");
// Non-existent cluster.
api_config_source->add_grpc_services()->mutable_envoy_grpc()->set_cluster_name("foo_cluster");
EXPECT_THROW_WITH_MESSAGE(
Utility::checkApiConfigSourceSubscriptionBackingCluster(primary_clusters, *api_config_source),
EnvoyException,
fmt::format("{} must have a statically defined non-EDS cluster: "
"'foo_cluster' does not exist, was added via api, or is an EDS cluster",
api_config_source->GetTypeName()));
// All ok.
primary_clusters.insert("foo_cluster");
Utility::checkApiConfigSourceSubscriptionBackingCluster(primary_clusters, *api_config_source);
// API with cluster_names set should be rejected.
api_config_source->add_cluster_names("foo_cluster");
EXPECT_THROW_WITH_REGEX(
Utility::checkApiConfigSourceSubscriptionBackingCluster(primary_clusters, *api_config_source),
EnvoyException,
fmt::format("{}::.DELTA_.GRPC must not have a cluster name "
"specified:",
api_config_source->GetTypeName()));
}
TEST(CheckApiConfigSourceSubscriptionBackingClusterTest, RestClusterTestAcrossTypes) {
envoy::config::core::v3::ConfigSource config;
auto* api_config_source = config.mutable_api_config_source();
Upstream::ClusterManager::ClusterSet primary_clusters;
api_config_source->set_api_type(envoy::config::core::v3::ApiConfigSource::REST);
// Non-existent cluster.
api_config_source->add_cluster_names("foo_cluster");
EXPECT_THROW_WITH_MESSAGE(
Utility::checkApiConfigSourceSubscriptionBackingCluster(primary_clusters, *api_config_source),
EnvoyException,
fmt::format("{} must have a statically defined non-EDS cluster: "
"'foo_cluster' does not exist, was added via api, or is an EDS cluster",
api_config_source->GetTypeName()));
// All ok.
primary_clusters.insert("foo_cluster");
Utility::checkApiConfigSourceSubscriptionBackingCluster(primary_clusters, *api_config_source);
}
// Validates CheckCluster functionality.
TEST(UtilityTest, CheckCluster) {
NiceMock<Upstream::MockClusterManager> cm;
// Validate that proper error is thrown, when cluster is not available.
EXPECT_THROW_WITH_MESSAGE(Utility::checkCluster("prefix", "foo", cm, false), EnvoyException,
"prefix: unknown cluster 'foo'");
// Validate that proper error is thrown, when dynamic cluster is passed when it is not expected.
cm.initializeClusters({"foo"}, {});
ON_CALL(*cm.active_clusters_["foo"]->info_, addedViaApi()).WillByDefault(Return(true));
EXPECT_THROW_WITH_MESSAGE(Utility::checkCluster("prefix", "foo", cm, false), EnvoyException,
"prefix: invalid cluster 'foo': currently only "
"static (non-CDS) clusters are supported");
EXPECT_NO_THROW(Utility::checkCluster("prefix", "foo", cm, true));
// Validate that bootstrap cluster does not throw any exceptions.
ON_CALL(*cm.active_clusters_["foo"]->info_, addedViaApi()).WillByDefault(Return(false));
EXPECT_NO_THROW(Utility::checkCluster("prefix", "foo", cm, true));
EXPECT_NO_THROW(Utility::checkCluster("prefix", "foo", cm, false));
}
} // namespace
} // namespace Config
} // namespace Envoy