forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem_subscription_impl_test.cc
306 lines (286 loc) · 12.2 KB
/
filesystem_subscription_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
#include "envoy/config/core/v3/config_source.pb.h"
#include "envoy/config/endpoint/v3/endpoint.pb.h"
#include "envoy/config/listener/v3/listener.pb.h"
#include "envoy/config/listener/v3/listener.pb.validate.h"
#include "test/common/config/filesystem_subscription_test_harness.h"
#include "test/mocks/event/mocks.h"
#include "test/mocks/filesystem/mocks.h"
#include "test/test_common/logging.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using testing::Return;
using testing::Throw;
namespace Envoy {
namespace Config {
namespace {
class FilesystemSubscriptionImplTest : public testing::Test,
public FilesystemSubscriptionTestHarness {};
// Validate that the client can recover from bad JSON responses.
TEST_F(FilesystemSubscriptionImplTest, BadJsonRecovery) {
startSubscription({"cluster0", "cluster1"});
EXPECT_TRUE(statsAre(1, 0, 0, 0, 0, 0, 0, ""));
EXPECT_CALL(callbacks_,
onConfigUpdateFailed(Envoy::Config::ConfigUpdateFailureReason::UpdateRejected, _));
updateFile(";!@#badjso n");
EXPECT_TRUE(statsAre(2, 0, 0, 1, 0, 0, 0, ""));
deliverConfigUpdate({"cluster0", "cluster1"}, "0", true);
EXPECT_TRUE(statsAre(3, 1, 0, 1, 0, TEST_TIME_MILLIS, 7148434200721666028, "0"));
}
// Validate that a file that is initially available results in a successful update.
TEST_F(FilesystemSubscriptionImplTest, InitialFile) {
updateFile("{\"versionInfo\": \"0\", \"resources\": []}", false);
startSubscription({"cluster0", "cluster1"});
EXPECT_TRUE(statsAre(1, 1, 0, 0, 0, TEST_TIME_MILLIS, 7148434200721666028, "0"));
}
// Validate that if we fail to set a watch, we get a sensible warning.
TEST(MiscFilesystemSubscriptionImplTest, BadWatch) {
Event::MockDispatcher dispatcher;
Stats::MockIsolatedStatsStore stats_store;
NiceMock<ProtobufMessage::MockValidationVisitor> validation_visitor;
Api::ApiPtr api = Api::createApiForTest(stats_store);
SubscriptionStats stats{Utility::generateStats(stats_store)};
auto* watcher = new Filesystem::MockWatcher();
EXPECT_CALL(dispatcher, createFilesystemWatcher_()).WillOnce(Return(watcher));
EXPECT_CALL(*watcher, addWatch(_, _, _)).WillOnce(Throw(EnvoyException("bad path")));
NiceMock<Config::MockSubscriptionCallbacks> callbacks;
NiceMock<Config::MockOpaqueResourceDecoder> resource_decoder;
EXPECT_THROW_WITH_MESSAGE(
FilesystemSubscriptionImpl(dispatcher, makePathConfigSource("##!@/dev/null"), callbacks,
resource_decoder, stats, validation_visitor, *api),
EnvoyException, "bad path");
}
// Validate that the update_time statistic isn't changed when the configuration update gets
// rejected.
TEST_F(FilesystemSubscriptionImplTest, UpdateTimeNotChangedOnUpdateReject) {
startSubscription({"cluster0", "cluster1"});
EXPECT_TRUE(statsAre(1, 0, 0, 0, 0, 0, 0, ""));
EXPECT_CALL(callbacks_,
onConfigUpdateFailed(Envoy::Config::ConfigUpdateFailureReason::UpdateRejected, _));
updateFile(";!@#badjso n");
EXPECT_TRUE(statsAre(2, 0, 0, 1, 0, 0, 0, ""));
}
// Validate that the update_time statistic is changed after a trivial configuration update
// (update that resulted in no change).
TEST_F(FilesystemSubscriptionImplTest, UpdateTimeChangedOnUpdateSuccess) {
startSubscription({"cluster0", "cluster1"});
EXPECT_TRUE(statsAre(1, 0, 0, 0, 0, 0, 0, ""));
deliverConfigUpdate({"cluster0", "cluster1"}, "0", true);
EXPECT_TRUE(statsAre(2, 1, 0, 0, 0, TEST_TIME_MILLIS, 7148434200721666028, "0"));
// Advance the simulated time.
simTime().setSystemTime(SystemTime(std::chrono::milliseconds(TEST_TIME_MILLIS + 1)));
deliverConfigUpdate({"cluster0", "cluster1"}, "0", true);
EXPECT_TRUE(statsAre(3, 2, 0, 0, 0, TEST_TIME_MILLIS + 1, 7148434200721666028, "0"));
}
// TODO(htuch): Add generic test harness support for collection subscriptions so that we can test
// gRPC/HTTP transports similar to below.
class FilesystemCollectionSubscriptionImplTest : public testing::Test,
Event::TestUsingSimulatedTime {
public:
FilesystemCollectionSubscriptionImplTest()
: path_(makePathConfigSource(TestEnvironment::temporaryPath("lds.yaml"))),
stats_(Utility::generateStats(stats_store_)),
api_(Api::createApiForTest(stats_store_, simTime())), dispatcher_(setupDispatcher()),
subscription_(*dispatcher_, path_, callbacks_, resource_decoder_, stats_,
ProtobufMessage::getStrictValidationVisitor(), *api_) {}
~FilesystemCollectionSubscriptionImplTest() override {
TestEnvironment::removePath(path_.path());
}
Event::DispatcherPtr setupDispatcher() {
auto dispatcher = std::make_unique<Event::MockDispatcher>();
EXPECT_CALL(*dispatcher, createFilesystemWatcher_()).WillOnce(InvokeWithoutArgs([this] {
Filesystem::MockWatcher* mock_watcher = new Filesystem::MockWatcher();
EXPECT_CALL(*mock_watcher, addWatch(path_.path(), Filesystem::Watcher::Events::MovedTo, _))
.WillOnce(Invoke([this](absl::string_view, uint32_t,
Filesystem::Watcher::OnChangedCb cb) { on_changed_cb_ = cb; }));
return mock_watcher;
}));
return dispatcher;
}
void updateFile(const std::string& yaml) {
// Write YAML contents to file, rename to path_ and invoke on change callback
const std::string temp_path = TestEnvironment::writeStringToFileForTest("lds.yaml.tmp", yaml);
TestEnvironment::renameFile(temp_path, path_.path());
on_changed_cb_(Filesystem::Watcher::Events::MovedTo);
}
AssertionResult statsAre(uint32_t attempt, uint32_t success, uint32_t rejected, uint32_t failure,
uint64_t version, absl::string_view version_text) {
if (attempt != stats_.update_attempt_.value()) {
return testing::AssertionFailure() << "update_attempt: expected " << attempt << ", got "
<< stats_.update_attempt_.value();
}
if (success != stats_.update_success_.value()) {
return testing::AssertionFailure() << "update_success: expected " << success << ", got "
<< stats_.update_success_.value();
}
if (rejected != stats_.update_rejected_.value()) {
return testing::AssertionFailure() << "update_rejected: expected " << rejected << ", got "
<< stats_.update_rejected_.value();
}
// The first attempt always fail.
if (1 + failure != stats_.update_failure_.value()) {
return testing::AssertionFailure() << "update_failure: expected " << 1 + failure << ", got "
<< stats_.update_failure_.value();
}
if (version != stats_.version_.value()) {
return testing::AssertionFailure()
<< "version: expected " << version << ", got " << stats_.version_.value();
}
if (version_text != stats_.version_text_.value()) {
return testing::AssertionFailure()
<< "version_text: expected " << version << ", got " << stats_.version_text_.value();
}
return testing::AssertionSuccess();
}
const envoy::config::core::v3::PathConfigSource path_;
Stats::IsolatedStoreImpl stats_store_;
SubscriptionStats stats_;
Api::ApiPtr api_;
Event::DispatcherPtr dispatcher_;
Filesystem::Watcher::OnChangedCb on_changed_cb_;
NiceMock<Config::MockSubscriptionCallbacks> callbacks_;
TestUtility::TestOpaqueResourceDecoderImpl<envoy::config::listener::v3::Listener>
resource_decoder_{"name"};
FilesystemCollectionSubscriptionImpl subscription_;
};
// Validate that an initial collection load succeeds, followed by a successful update, for inline
// entries.
TEST_F(FilesystemCollectionSubscriptionImplTest, InlineEntrySuccess) {
TestUtility::TestOpaqueResourceDecoderImpl<envoy::config::listener::v3::Listener>
resource_decoder("name");
subscription_.start({});
EXPECT_TRUE(statsAre(1, 0, 0, 0, 0, ""));
// Initial config load.
const auto inline_entry =
TestUtility::parseYaml<xds::core::v3::CollectionEntry::InlineEntry>(R"EOF(
name: foo
version: resource.1
resource:
"@type": type.googleapis.com/envoy.config.listener.v3.Listener
name: foo
address:
socket_address:
protocol: TCP
address: 0.0.0.0
port_value: 10000
)EOF");
const std::string resource =
fmt::format(R"EOF(
version: system.1
resource:
"@type": type.googleapis.com/envoy.config.listener.v3.ListenerCollection
entries:
- inline_entry: {}
)EOF",
MessageUtil::getJsonStringFromMessageOrDie(inline_entry));
DecodedResourcesWrapper decoded_resources;
decoded_resources.pushBack(std::make_unique<DecodedResourceImpl>(resource_decoder, inline_entry));
EXPECT_CALL(callbacks_,
onConfigUpdate(DecodedResourcesEq(decoded_resources.refvec_), "system.1"));
updateFile(resource);
EXPECT_TRUE(statsAre(2, 1, 0, 0, 1471442407191366964, "system.1"));
// Update.
const auto inline_entry_2 =
TestUtility::parseYaml<xds::core::v3::CollectionEntry::InlineEntry>(R"EOF(
name: foo
version: resource.2
resource:
"@type": type.googleapis.com/envoy.config.listener.v3.Listener
name: foo
address:
socket_address:
protocol: TCP
address: 0.0.0.1
port_value: 10001
)EOF");
const std::string resource_2 =
fmt::format(R"EOF(
version: system.2
resource:
"@type": type.googleapis.com/envoy.config.listener.v3.ListenerCollection
entries:
- inline_entry: {}
)EOF",
MessageUtil::getJsonStringFromMessageOrDie(inline_entry_2));
{
DecodedResourcesWrapper decoded_resources_2;
decoded_resources_2.pushBack(
std::make_unique<DecodedResourceImpl>(resource_decoder, inline_entry_2));
EXPECT_CALL(callbacks_,
onConfigUpdate(DecodedResourcesEq(decoded_resources_2.refvec_), "system.2"));
updateFile(resource_2);
}
EXPECT_TRUE(statsAre(3, 2, 0, 0, 17889017004055064037ULL, "system.2"));
}
// Validate handling of invalid resource wrappers
TEST_F(FilesystemCollectionSubscriptionImplTest, BadEnvelope) {
subscription_.start({});
EXPECT_TRUE(statsAre(1, 0, 0, 0, 0, ""));
EXPECT_CALL(callbacks_, onConfigUpdateFailed(ConfigUpdateFailureReason::UpdateRejected, _));
// Unknown collection type.
updateFile("{}");
EXPECT_TRUE(statsAre(2, 0, 0, 1, 0, ""));
const std::string resource = R"EOF(
version: system.1
resource:
"@type": type.googleapis.com/envoy.config.listener.v3.Listener
)EOF";
EXPECT_CALL(callbacks_, onConfigUpdateFailed(ConfigUpdateFailureReason::UpdateRejected, _));
// Invalid collection type structure.
updateFile(resource);
EXPECT_TRUE(statsAre(3, 0, 0, 2, 0, ""));
}
// Validate handling of unknown fields.
TEST_F(FilesystemCollectionSubscriptionImplTest, UnknownFields) {
subscription_.start({});
EXPECT_TRUE(statsAre(1, 0, 0, 0, 0, ""));
const std::string resource = R"EOF(
version: system.1
resource:
"@type": type.googleapis.com/envoy.config.listener.v3.ListenerCollection
entries:
- inline_entry:
name: foo
version: resource.1
resource:
"@type": type.googleapis.com/envoy.config.listener.v3.Listener
name: foo
unknown_bar: baz
address:
socket_address:
protocol: TCP
address: 0.0.0.0
port_value: 10000
)EOF";
EXPECT_CALL(callbacks_, onConfigUpdateFailed(ConfigUpdateFailureReason::UpdateRejected, _));
updateFile(resource);
EXPECT_TRUE(statsAre(2, 0, 1, 0, 0, ""));
}
// Validate handling of rejected config.
TEST_F(FilesystemCollectionSubscriptionImplTest, ConfigRejection) {
subscription_.start({});
EXPECT_TRUE(statsAre(1, 0, 0, 0, 0, ""));
const std::string resource = R"EOF(
version: system.1
resource:
"@type": type.googleapis.com/envoy.config.listener.v3.ListenerCollection
entries:
- inline_entry:
name: foo
version: resource.1
resource:
"@type": type.googleapis.com/envoy.config.listener.v3.Listener
name: foo
address:
socket_address:
protocol: TCP
address: 0.0.0.0
port_value: 10000
)EOF";
EXPECT_CALL(callbacks_, onConfigUpdate(_, _)).WillOnce(Throw(EnvoyException("blah")));
EXPECT_CALL(callbacks_, onConfigUpdateFailed(ConfigUpdateFailureReason::UpdateRejected, _));
updateFile(resource);
EXPECT_TRUE(statsAre(2, 0, 1, 0, 0, ""));
}
} // namespace
} // namespace Config
} // namespace Envoy