-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
xds_config_tracker_integration_test.cc
299 lines (251 loc) · 12.2 KB
/
xds_config_tracker_integration_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <atomic>
#include "envoy/config/route/v3/route.pb.h"
#include "envoy/config/subscription.h"
#include "envoy/config/xds_config_tracker.h"
#include "envoy/protobuf/message_validator.h"
#include "envoy/service/discovery/v3/discovery.pb.h"
#include "envoy/service/runtime/v3/rtds.pb.h"
#include "test/common/grpc/grpc_client_integration.h"
#include "test/config/v2_link_hacks.h"
#include "test/integration/http_integration.h"
#include "test/integration/xds_config_tracker_test.pb.h"
#include "test/test_common/registry.h"
#include "test/test_common/utility.h"
#include "absl/synchronization/mutex.h"
#include "gtest/gtest.h"
namespace Envoy {
namespace {
const char kTestKey[] = "test_key";
const char ClusterName1[] = "cluster_1";
const char ClusterName2[] = "cluster_2";
const int UpstreamIndex1 = 1;
const int UpstreamIndex2 = 2;
/**
* All stats for this xds tracker. @see stats_macros.h
*/
#define ALL_TEST_XDS_TRACKER_STATS(COUNTER) \
COUNTER(on_config_accepted) \
COUNTER(on_config_rejected) \
COUNTER(on_config_metadata_read)
/**
* Struct definition for stats. @see stats_macros.h
*/
struct TestXdsTrackerStats {
ALL_TEST_XDS_TRACKER_STATS(GENERATE_COUNTER_STRUCT)
};
/**
* A test implementation of the XdsConfigTracker extension.
* It just increases the test counter when a related method is called.
*/
class TestXdsConfigTracker : public Config::XdsConfigTracker {
public:
TestXdsConfigTracker(Stats::Scope& scope) : stats_(generateStats("test_xds_tracker", scope)) {}
void onConfigAccepted(const absl::string_view,
const std::vector<Config::DecodedResourcePtr>& decoded_resources) override {
stats_.on_config_accepted_.inc();
test::envoy::config::xds::TestTrackerMetadata test_metadata;
for (const auto& resource : decoded_resources) {
if (resource->metadata().has_value()) {
const auto& config_typed_metadata = resource->metadata()->typed_filter_metadata();
if (const auto& metadata_it = config_typed_metadata.find(kTestKey);
metadata_it != config_typed_metadata.end()) {
const auto status = Envoy::MessageUtil::unpackTo(metadata_it->second, test_metadata);
if (!status.ok()) {
continue;
}
stats_.on_config_metadata_read_.inc();
}
}
}
}
void onConfigAccepted(
const absl::string_view,
const Protobuf::RepeatedPtrField<envoy::service::discovery::v3::Resource>& resources,
const Protobuf::RepeatedPtrField<std::string>&) override {
stats_.on_config_accepted_.inc();
test::envoy::config::xds::TestTrackerMetadata test_metadata;
for (const auto& resource : resources) {
if (resource.has_metadata()) {
const auto& config_typed_metadata = resource.metadata().typed_filter_metadata();
if (const auto& metadata_it = config_typed_metadata.find(kTestKey);
metadata_it != config_typed_metadata.end()) {
const auto status = Envoy::MessageUtil::unpackTo(metadata_it->second, test_metadata);
if (!status.ok()) {
continue;
}
stats_.on_config_metadata_read_.inc();
}
}
}
}
void onConfigRejected(const envoy::service::discovery::v3::DiscoveryResponse&,
const absl::string_view) override {
stats_.on_config_rejected_.inc();
}
void onConfigRejected(const envoy::service::discovery::v3::DeltaDiscoveryResponse&,
const absl::string_view) override {
stats_.on_config_rejected_.inc();
}
private:
TestXdsTrackerStats generateStats(const std::string& prefix, Stats::Scope& scope) {
return {ALL_TEST_XDS_TRACKER_STATS(POOL_COUNTER_PREFIX(scope, prefix))};
}
TestXdsTrackerStats stats_;
};
class TestXdsConfigTrackerFactory : public Config::XdsConfigTrackerFactory {
public:
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
return std::make_unique<test::envoy::config::xds::TestXdsConfigTracker>();
}
std::string name() const override { return "envoy.config.xds.test_xds_tracker"; };
Config::XdsConfigTrackerPtr createXdsConfigTracker(const ProtobufWkt::Any&,
ProtobufMessage::ValidationVisitor&,
Api::Api& api, Event::Dispatcher&) override {
return std::make_unique<TestXdsConfigTracker>(api.rootScope());
}
};
class XdsConfigTrackerIntegrationTest : public Grpc::DeltaSotwIntegrationParamTest,
public HttpIntegrationTest {
public:
XdsConfigTrackerIntegrationTest()
: HttpIntegrationTest(Http::CodecType::HTTP2, ipVersion(),
ConfigHelper::clustersNoListenerBootstrap(
sotwOrDelta() == Grpc::SotwOrDelta::Sotw ||
sotwOrDelta() == Grpc::SotwOrDelta::UnifiedSotw
? "GRPC"
: "DELTA_GRPC")) {
use_lds_ = false;
sotw_or_delta_ = sotwOrDelta();
config_helper_.addRuntimeOverride("envoy.reloadable_features.unified_mux",
(this->sotwOrDelta() == Grpc::SotwOrDelta::UnifiedDelta ||
this->sotwOrDelta() == Grpc::SotwOrDelta::UnifiedSotw)
? "true"
: "false");
// Add test xDS config tracer.
config_helper_.addConfigModifier([](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
auto* tracer_extension = bootstrap.mutable_xds_config_tracker_extension();
tracer_extension->set_name("envoy.config.xds.test_xds_tracer");
tracer_extension->mutable_typed_config()->PackFrom(
test::envoy::config::xds::TestXdsConfigTracker());
});
}
void TearDown() override {
if (xds_connection_ != nullptr) {
cleanUpXdsConnection();
}
}
void initialize() override {
// The tests infra expects the xDS server to be the second fake upstream, so
// we need a dummy data plane cluster.
setUpstreamCount(1);
setUpstreamProtocol(Http::CodecType::HTTP2);
HttpIntegrationTest::initialize();
// Create the regular (i.e. not an xDS server) upstreams.
addFakeUpstream(Http::CodecType::HTTP2);
addFakeUpstream(Http::CodecType::HTTP2);
cluster1_ = ConfigHelper::buildStaticCluster(
ClusterName1, fake_upstreams_[UpstreamIndex1]->localAddress()->ip()->port(),
Network::Test::getLoopbackAddressString(ipVersion()));
cluster2_ = ConfigHelper::buildStaticCluster(
ClusterName2, fake_upstreams_[UpstreamIndex2]->localAddress()->ip()->port(),
Network::Test::getLoopbackAddressString(ipVersion()));
acceptXdsConnection();
registerTestServerPorts({});
}
void acceptXdsConnection() {
AssertionResult result = // xds_connection_ is filled with the new FakeHttpConnection.
fake_upstreams_[0]->waitForHttpConnection(*dispatcher_, xds_connection_);
RELEASE_ASSERT(result, result.message());
result = xds_connection_->waitForNewStream(*dispatcher_, xds_stream_);
RELEASE_ASSERT(result, result.message());
xds_stream_->startGrpcStream();
}
envoy::config::cluster::v3::Cluster cluster1_;
envoy::config::cluster::v3::Cluster cluster2_;
};
INSTANTIATE_TEST_SUITE_P(IpVersionsClientType, XdsConfigTrackerIntegrationTest,
DELTA_SOTW_UNIFIED_GRPC_CLIENT_INTEGRATION_PARAMS);
TEST_P(XdsConfigTrackerIntegrationTest, XdsConfigTrackerSuccessCount) {
TestXdsConfigTrackerFactory factory;
Registry::InjectFactory<Config::XdsConfigTrackerFactory> registered(factory);
initialize();
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "", {}, {}, {}, true));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(
Config::TypeUrl::get().Cluster, {cluster1_, cluster2_}, {cluster1_, cluster2_}, {}, "1");
// 3 because the statically specified CDS server itself counts as a cluster.
test_server_->waitForGaugeGe("cluster_manager.active_clusters", 3);
// onConfigAccepted is called when all the resources are accepted.
test_server_->waitForCounterEq("test_xds_tracker.on_config_accepted", 1);
test_server_->waitForCounterEq("test_xds_tracker.on_config_metadata_read", 0);
EXPECT_EQ(1, test_server_->counter("test_xds_tracker.on_config_accepted")->value());
}
// This is to test the Resource wrapper usage with metadata field.
TEST_P(XdsConfigTrackerIntegrationTest, XdsConfigTrackerSuccessCountWithWrapper) {
TestXdsConfigTrackerFactory factory;
Registry::InjectFactory<Config::XdsConfigTrackerFactory> registered(factory);
initialize();
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "", {}, {}, {}, true));
// Add a typed metadata to the Resource wrapper.
test::envoy::config::xds::TestTrackerMetadata test_metadata;
ProtobufWkt::Any packed_value;
packed_value.PackFrom(test_metadata);
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(
Config::TypeUrl::get().Cluster, {cluster1_, cluster2_}, {cluster1_, cluster2_}, {}, "1",
{{kTestKey, packed_value}});
// 3 because the statically specified CDS server itself counts as a cluster.
test_server_->waitForGaugeGe("cluster_manager.active_clusters", 3);
// onConfigAccepted is called when all the resources are accepted.
test_server_->waitForCounterEq("test_xds_tracker.on_config_accepted", 1);
test_server_->waitForCounterEq("test_xds_tracker.on_config_metadata_read", 2);
EXPECT_EQ(1, test_server_->counter("test_xds_tracker.on_config_accepted")->value());
}
TEST_P(XdsConfigTrackerIntegrationTest, XdsConfigTrackerFailureCount) {
TestXdsConfigTrackerFactory factory;
Registry::InjectFactory<Config::XdsConfigTrackerFactory> registered(factory);
initialize();
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "", {}, {}, {}, true));
const auto route_config =
TestUtility::parseYaml<envoy::config::route::v3::RouteConfiguration>(R"EOF(
name: my_route
vhds:
config_source:
resource_api_version: V3
api_config_source:
api_type: GRPC
transport_api_version: V3
grpc_services:
envoy_grpc:
cluster_name: xds_cluster
)EOF");
sendDiscoveryResponse<envoy::config::route::v3::RouteConfiguration>(
Config::TypeUrl::get().Cluster, {route_config}, {route_config}, {}, "3");
// Resources are rejected because Message's TypeUrl != Resource's
test_server_->waitForCounterEq("test_xds_tracker.on_config_rejected", 1);
EXPECT_EQ(1, test_server_->counter("test_xds_tracker.on_config_rejected")->value());
}
TEST_P(XdsConfigTrackerIntegrationTest, XdsConfigTrackerPartialUpdate) {
TestXdsConfigTrackerFactory factory;
Registry::InjectFactory<Config::XdsConfigTrackerFactory> registered(factory);
initialize();
// The first of duplicates has already been successfully applied,
// and a duplicate exception should be threw.
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "", {}, {}, {}, true));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(
Config::TypeUrl::get().Cluster, {cluster1_, cluster1_, cluster2_},
{cluster1_, cluster1_, cluster2_}, {}, "5");
// For Delta, the response will be rejected when checking the message due to the duplication.
// For SotW, both clusters are accepted, but the internal exception is not empty.
if (sotw_or_delta_ == Grpc::SotwOrDelta::Delta ||
sotw_or_delta_ == Grpc::SotwOrDelta::UnifiedDelta) {
test_server_->waitForCounterGe("cluster_manager.cluster_added", 1);
} else {
test_server_->waitForCounterGe("cluster_manager.cluster_added", 3);
}
// onConfigRejected is called if there is any exception even some resources are accepted.
test_server_->waitForCounterEq("test_xds_tracker.on_config_rejected", 1);
EXPECT_EQ(1, test_server_->counter("test_xds_tracker.on_config_rejected")->value());
// onConfigAccepted is called only when all the resources in a response are successfully ingested.
EXPECT_EQ(0, test_server_->counter("test_xds_tracker.on_config_accepted")->value());
}
} // namespace
} // namespace Envoy