forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatcher_impl_test.cc
More file actions
308 lines (252 loc) · 13.3 KB
/
watcher_impl_test.cc
File metadata and controls
308 lines (252 loc) · 13.3 KB
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
#include <cstdint>
#include <fstream>
#include "envoy/common/exception.h"
#include "source/common/common/assert.h"
#include "source/common/event/dispatcher_impl.h"
#include "source/common/filesystem/watcher_impl.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"
namespace Envoy {
namespace Filesystem {
class WatcherImplTest : public testing::Test {
protected:
WatcherImplTest()
: api_(Api::createApiForTest()), dispatcher_(api_->allocateDispatcher("test_thread")) {}
Api::ApiPtr api_;
Event::DispatcherPtr dispatcher_;
};
class WatchCallback {
public:
MOCK_METHOD(void, called, (uint32_t));
};
TEST_F(WatcherImplTest, All) {
Filesystem::WatcherPtr watcher = dispatcher_->createFilesystemWatcher();
unlink(TestEnvironment::temporaryPath("envoy_test/watcher_target").c_str());
unlink(TestEnvironment::temporaryPath("envoy_test/watcher_link").c_str());
unlink(TestEnvironment::temporaryPath("envoy_test/watcher_new_target").c_str());
unlink(TestEnvironment::temporaryPath("envoy_test/watcher_new_link").c_str());
TestEnvironment::createPath(TestEnvironment::temporaryPath("envoy_test"));
{ std::ofstream file(TestEnvironment::temporaryPath("envoy_test/watcher_target")); }
TestEnvironment::createSymlink(TestEnvironment::temporaryPath("envoy_test/watcher_target"),
TestEnvironment::temporaryPath("envoy_test/watcher_link"));
{ std::ofstream file(TestEnvironment::temporaryPath("envoy_test/watcher_new_target")); }
TestEnvironment::createSymlink(TestEnvironment::temporaryPath("envoy_test/watcher_new_target"),
TestEnvironment::temporaryPath("envoy_test/watcher_new_link"));
WatchCallback callback;
EXPECT_CALL(callback, called(Watcher::Events::MovedTo)).Times(2);
ASSERT_TRUE(watcher
->addWatch(TestEnvironment::temporaryPath("envoy_test/watcher_link"),
Watcher::Events::MovedTo,
[&](uint32_t events) {
callback.called(events);
dispatcher_->exit();
return absl::OkStatus();
})
.ok());
TestEnvironment::renameFile(TestEnvironment::temporaryPath("envoy_test/watcher_new_link"),
TestEnvironment::temporaryPath("envoy_test/watcher_link"));
dispatcher_->run(Event::Dispatcher::RunType::Block);
TestEnvironment::createSymlink(TestEnvironment::temporaryPath("envoy_test/watcher_new_target"),
TestEnvironment::temporaryPath("envoy_test/watcher_new_link"));
TestEnvironment::renameFile(TestEnvironment::temporaryPath("envoy_test/watcher_new_link"),
TestEnvironment::temporaryPath("envoy_test/watcher_link"));
dispatcher_->run(Event::Dispatcher::RunType::Block);
}
TEST_F(WatcherImplTest, Create) {
Filesystem::WatcherPtr watcher = dispatcher_->createFilesystemWatcher();
unlink(TestEnvironment::temporaryPath("envoy_test/watcher_target").c_str());
unlink(TestEnvironment::temporaryPath("envoy_test/watcher_link").c_str());
unlink(TestEnvironment::temporaryPath("envoy_test/watcher_new_link").c_str());
unlink(TestEnvironment::temporaryPath("envoy_test/other_file").c_str());
TestEnvironment::createPath(TestEnvironment::temporaryPath("envoy_test"));
{ std::ofstream file(TestEnvironment::temporaryPath("envoy_test/watcher_target")); }
WatchCallback callback;
ASSERT_TRUE(watcher
->addWatch(TestEnvironment::temporaryPath("envoy_test/watcher_link"),
Watcher::Events::MovedTo,
[&](uint32_t events) {
callback.called(events);
dispatcher_->exit();
return absl::OkStatus();
})
.ok());
{ std::ofstream file(TestEnvironment::temporaryPath("envoy_test/other_file")); }
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
EXPECT_CALL(callback, called(Watcher::Events::MovedTo));
TestEnvironment::createSymlink(TestEnvironment::temporaryPath("envoy_test/watcher_target"),
TestEnvironment::temporaryPath("envoy_test/watcher_new_link"));
TestEnvironment::renameFile(TestEnvironment::temporaryPath("envoy_test/watcher_new_link"),
TestEnvironment::temporaryPath("envoy_test/watcher_link"));
dispatcher_->run(Event::Dispatcher::RunType::Block);
}
TEST_F(WatcherImplTest, Modify) {
Filesystem::WatcherPtr watcher = dispatcher_->createFilesystemWatcher();
TestEnvironment::createPath(TestEnvironment::temporaryPath("envoy_test"));
std::ofstream file(TestEnvironment::temporaryPath("envoy_test/watcher_target"));
WatchCallback callback;
ASSERT_TRUE(watcher
->addWatch(TestEnvironment::temporaryPath("envoy_test/watcher_target"),
Watcher::Events::Modified,
[&](uint32_t events) {
callback.called(events);
dispatcher_->exit();
return absl::OkStatus();
})
.ok());
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
file << "text" << std::flush;
file.close();
EXPECT_CALL(callback, called(Watcher::Events::Modified));
dispatcher_->run(Event::Dispatcher::RunType::Block);
}
TEST_F(WatcherImplTest, BadPath) {
Filesystem::WatcherPtr watcher = dispatcher_->createFilesystemWatcher();
EXPECT_FALSE(watcher
->addWatch("this_is_not_a_file", Watcher::Events::MovedTo,
[&](uint32_t) { return absl::OkStatus(); })
.ok());
EXPECT_FALSE(watcher
->addWatch("this_is_not_a_dir/file", Watcher::Events::MovedTo,
[&](uint32_t) { return absl::OkStatus(); })
.ok());
}
TEST_F(WatcherImplTest, ParentDirectoryRemoved) {
Filesystem::WatcherPtr watcher = dispatcher_->createFilesystemWatcher();
TestEnvironment::createPath(TestEnvironment::temporaryPath("envoy_test_empty"));
WatchCallback callback;
EXPECT_CALL(callback, called(testing::_)).Times(0);
ASSERT_TRUE(watcher
->addWatch(TestEnvironment::temporaryPath("envoy_test_empty/watcher_link"),
Watcher::Events::MovedTo,
[&](uint32_t events) {
callback.called(events);
return absl::OkStatus();
})
.ok());
int rc = rmdir(TestEnvironment::temporaryPath("envoy_test_empty").c_str());
EXPECT_EQ(0, rc);
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
}
TEST_F(WatcherImplTest, RootDirectoryPath) {
Filesystem::WatcherPtr watcher = dispatcher_->createFilesystemWatcher();
#ifndef WIN32
EXPECT_TRUE(
watcher->addWatch("/", Watcher::Events::MovedTo, [&](uint32_t) { return absl::OkStatus(); })
.ok());
#else
EXPECT_TRUE(
watcher
->addWatch("c:\\", Watcher::Events::MovedTo, [&](uint32_t) { return absl::OkStatus(); })
.ok());
#endif
}
// Skipping this test on Windows as there is no Windows API able to atomically move a
// directory/symlink when the new name is a non-empty directory
#ifndef WIN32
TEST_F(WatcherImplTest, SymlinkAtomicRename) {
Filesystem::WatcherPtr watcher = dispatcher_->createFilesystemWatcher();
TestEnvironment::createPath(TestEnvironment::temporaryPath("envoy_test"));
TestEnvironment::createPath(TestEnvironment::temporaryPath("envoy_test/..timestamp1"));
{ std::ofstream file(TestEnvironment::temporaryPath("envoy_test/..timestamp1/watched_file")); }
TestEnvironment::createSymlink(TestEnvironment::temporaryPath("envoy_test/..timestamp1"),
TestEnvironment::temporaryPath("envoy_test/..data"));
TestEnvironment::createSymlink(TestEnvironment::temporaryPath("envoy_test/..data/watched_file"),
TestEnvironment::temporaryPath("envoy_test/watched_file"));
WatchCallback callback;
EXPECT_CALL(callback, called(Watcher::Events::MovedTo));
ASSERT_TRUE(watcher
->addWatch(TestEnvironment::temporaryPath("envoy_test/"),
Watcher::Events::MovedTo,
[&](uint32_t events) {
callback.called(events);
dispatcher_->exit();
return absl::OkStatus();
})
.ok());
TestEnvironment::createPath(TestEnvironment::temporaryPath("envoy_test/..timestamp2"));
{ std::ofstream file(TestEnvironment::temporaryPath("envoy_test/..timestamp2/watched_file")); }
TestEnvironment::createSymlink(TestEnvironment::temporaryPath("envoy_test/..timestamp2"),
TestEnvironment::temporaryPath("envoy_test/..tmp"));
TestEnvironment::renameFile(TestEnvironment::temporaryPath("envoy_test/..tmp"),
TestEnvironment::temporaryPath("envoy_test/..data"));
dispatcher_->run(Event::Dispatcher::RunType::Block);
}
#endif
// Test that callback returning error status is logged and doesn't crash.
TEST_F(WatcherImplTest, CallbackReturnsErrorStatus) {
Filesystem::WatcherPtr watcher = dispatcher_->createFilesystemWatcher();
TestEnvironment::createPath(TestEnvironment::temporaryPath("envoy_test"));
std::ofstream file(TestEnvironment::temporaryPath("envoy_test/watcher_target"));
WatchCallback callback;
EXPECT_CALL(callback, called(Watcher::Events::Modified));
ASSERT_TRUE(watcher
->addWatch(TestEnvironment::temporaryPath("envoy_test/watcher_target"),
Watcher::Events::Modified,
[&](uint32_t events) {
callback.called(events);
dispatcher_->exit();
// Return an error status - should be logged but not crash.
return absl::InternalError("simulated callback error");
})
.ok());
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
EXPECT_LOG_CONTAINS("warn", "Filesystem watch callback for", file << "text" << std::flush;
file.close(); dispatcher_->run(Event::Dispatcher::RunType::Block););
}
// Test that callback throwing exception is caught and logged.
TEST_F(WatcherImplTest, CallbackThrowsException) {
Filesystem::WatcherPtr watcher = dispatcher_->createFilesystemWatcher();
TestEnvironment::createPath(TestEnvironment::temporaryPath("envoy_test"));
std::ofstream file(TestEnvironment::temporaryPath("envoy_test/watcher_target"));
WatchCallback callback;
EXPECT_CALL(callback, called(Watcher::Events::Modified));
ASSERT_TRUE(watcher
->addWatch(TestEnvironment::temporaryPath("envoy_test/watcher_target"),
Watcher::Events::Modified,
[&](uint32_t events) -> absl::Status {
callback.called(events);
dispatcher_->exit();
// Throw an exception - should be caught and logged.
throw EnvoyException("simulated callback exception");
})
.ok());
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
EXPECT_LOG_CONTAINS("warn", "threw exception", file << "text" << std::flush; file.close();
dispatcher_->run(Event::Dispatcher::RunType::Block););
}
// Test that multiple callbacks can fail without affecting each other.
TEST_F(WatcherImplTest, MultipleCallbacksWithErrors) {
Filesystem::WatcherPtr watcher = dispatcher_->createFilesystemWatcher();
TestEnvironment::createPath(TestEnvironment::temporaryPath("envoy_test"));
std::ofstream file(TestEnvironment::temporaryPath("envoy_test/watcher_target"));
int callback_count = 0;
ASSERT_TRUE(watcher
->addWatch(TestEnvironment::temporaryPath("envoy_test/watcher_target"),
Watcher::Events::Modified,
[&](uint32_t) {
callback_count++;
if (callback_count >= 2) {
dispatcher_->exit();
}
// First callback returns error, second returns OK.
if (callback_count == 1) {
return absl::InternalError("first callback error");
}
return absl::OkStatus();
})
.ok());
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
// Trigger first modification. The first callback returns error, but watcher continues.
file << "text1" << std::flush;
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
// Trigger second modification. It should still work.
file << "text2" << std::flush;
file.close();
dispatcher_->run(Event::Dispatcher::RunType::Block);
EXPECT_EQ(2, callback_count);
}
} // namespace Filesystem
} // namespace Envoy