-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathmonitor_test.go
363 lines (288 loc) · 9.12 KB
/
monitor_test.go
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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// +build !integration
package monitor
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/fsnotify/fsnotify"
"github.com/stretchr/testify/assert"
)
func TestNonRecursive(t *testing.T) {
dir, err := ioutil.TempDir("", "monitor")
assertNoError(t, err)
// under macOS, temp dir has a symlink in the path (/var -> /private/var)
// and the path returned in events has the symlink resolved
if runtime.GOOS == "darwin" {
if dirAlt, err := filepath.EvalSymlinks(dir); err == nil {
dir = dirAlt
}
}
defer os.RemoveAll(dir)
watcher, err := New(false)
assertNoError(t, err)
assertNoError(t, watcher.Add(dir))
assertNoError(t, watcher.Start())
testDirOps(t, dir, watcher)
subdir := filepath.Join(dir, "subdir")
os.Mkdir(subdir, 0750)
ev, err := readTimeout(t, watcher)
assertNoError(t, err)
assert.Equal(t, subdir, ev.Name)
assert.Equal(t, fsnotify.Create, ev.Op)
// subdirs are not watched
subfile := filepath.Join(subdir, "file.dat")
assertNoError(t, ioutil.WriteFile(subfile, []byte("foo"), 0640))
_, err = readTimeout(t, watcher)
assert.Error(t, err)
assert.Equal(t, errReadTimeout, err)
assertNoError(t, watcher.Close())
}
func TestRecursive(t *testing.T) {
if runtime.GOOS == "darwin" {
// This test races on Darwin because internal races in the kqueue
// implementation of fsnotify when a watch is added in response to
// a subdirectory created inside a watched directory.
// This race doesn't affect auditbeat because the file_integrity module
// under Darwin uses fsevents instead of kqueue.
t.Skip("Disabled on Darwin")
}
dir, err := ioutil.TempDir("", "monitor")
assertNoError(t, err)
// under macOS, temp dir has a symlink in the path (/var -> /private/var)
// and the path returned in events has the symlink resolved
if runtime.GOOS == "darwin" {
if dirAlt, err := filepath.EvalSymlinks(dir); err == nil {
dir = dirAlt
}
}
defer os.RemoveAll(dir)
watcher, err := New(true)
assertNoError(t, err)
assertNoError(t, watcher.Add(dir))
assertNoError(t, watcher.Start())
testDirOps(t, dir, watcher)
subdir := filepath.Join(dir, "subdir")
os.Mkdir(subdir, 0750)
ev, err := readTimeout(t, watcher)
assertNoError(t, err)
assert.Equal(t, subdir, ev.Name)
assert.Equal(t, fsnotify.Create, ev.Op)
testDirOps(t, subdir, watcher)
assertNoError(t, watcher.Close())
}
func TestRecursiveNoFollowSymlink(t *testing.T) {
// fsnotify has a bug in darwin were it is following symlinks
// see: https://github.com/fsnotify/fsnotify/issues/227
if runtime.GOOS == "darwin" {
t.Skip("This test fails under macOS due to a bug in fsnotify")
}
// Create a watched dir
dir, err := ioutil.TempDir("", "monitor")
assertNoError(t, err)
// under macOS, temp dir has a symlink in the path (/var -> /private/var)
// and the path returned in events has the symlink resolved
if runtime.GOOS == "darwin" {
if dirAlt, err := filepath.EvalSymlinks(dir); err == nil {
dir = dirAlt
}
}
defer os.RemoveAll(dir)
// Create a separate dir
linkedDir, err := ioutil.TempDir("", "linked")
assertNoError(t, err)
defer os.RemoveAll(linkedDir)
// Add a symbolic link from watched dir to the other
symLink := filepath.Join(dir, "link")
assertNoError(t, os.Symlink(linkedDir, symLink))
// Start the watcher
watcher, err := New(true)
assertNoError(t, err)
assertNoError(t, watcher.Add(dir))
assertNoError(t, watcher.Start())
// Create a file in the other dir
file := filepath.Join(linkedDir, "not.seen")
assertNoError(t, ioutil.WriteFile(file, []byte("hello"), 0640))
// No event is received
ev, err := readTimeout(t, watcher)
assert.Equal(t, errReadTimeout, err)
if err == nil {
t.Fatalf("Expected timeout, got event %+v", ev)
}
assertNoError(t, watcher.Close())
}
func TestRecursiveSubdirPermissions(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping permissions test on Windows")
}
// Create dir to be watched
dir, err := ioutil.TempDir("", "monitor")
assertNoError(t, err)
if runtime.GOOS == "darwin" {
if dirAlt, err := filepath.EvalSymlinks(dir); err == nil {
dir = dirAlt
}
}
defer os.RemoveAll(dir)
// Create not watched dir
outDir, err := ioutil.TempDir("", "non-watched")
assertNoError(t, err)
if runtime.GOOS == "darwin" {
if dirAlt, err := filepath.EvalSymlinks(outDir); err == nil {
outDir = dirAlt
}
}
defer os.RemoveAll(outDir)
// Populate not watched subdir
for _, name := range []string{"a", "b", "c"} {
path := filepath.Join(outDir, name)
assertNoError(t, os.Mkdir(path, 0755))
assertNoError(t, ioutil.WriteFile(filepath.Join(path, name), []byte("Hello"), 0644))
}
// Make a subdir not accessible
assertNoError(t, os.Chmod(filepath.Join(outDir, "b"), 0))
// Setup watches on watched dir
watcher, err := New(true)
assertNoError(t, err)
assertNoError(t, watcher.Start())
assertNoError(t, watcher.Add(dir))
defer func() {
assertNoError(t, watcher.Close())
}()
// No event is received
ev, err := readTimeout(t, watcher)
assert.Equal(t, errReadTimeout, err)
if err != errReadTimeout {
t.Fatalf("Expected timeout, got event %+v", ev)
}
// Move the outside directory into the watched
dest := filepath.Join(dir, "subdir")
assertNoError(t, os.Rename(outDir, dest))
// Receive all events
var evs []fsnotify.Event
for {
// No event is received
ev, err := readTimeout(t, watcher)
if err == errReadTimeout {
break
}
assertNoError(t, err)
evs = append(evs, ev)
}
// Verify that events for all accessible files are received
// File "b/b" is missing because a watch to b couldn't be installed
expected := map[string]fsnotify.Op{
dest: fsnotify.Create,
filepath.Join(dest, "a"): fsnotify.Create,
filepath.Join(dest, "a/a"): fsnotify.Create,
filepath.Join(dest, "b"): fsnotify.Create,
filepath.Join(dest, "c"): fsnotify.Create,
filepath.Join(dest, "c/c"): fsnotify.Create,
}
assert.Len(t, evs, len(expected))
for _, ev := range evs {
op, found := expected[ev.Name]
assert.True(t, found, ev.Name)
assert.Equal(t, op, ev.Op)
}
}
func testDirOps(t *testing.T, dir string, watcher Watcher) {
fpath := filepath.Join(dir, "file.txt")
fpath2 := filepath.Join(dir, "file2.txt")
// Create
assertNoError(t, ioutil.WriteFile(fpath, []byte("hello"), 0640))
ev, err := readTimeout(t, watcher)
assertNoError(t, err)
assert.Equal(t, fpath, ev.Name)
assert.Equal(t, fsnotify.Create, ev.Op)
// Update
// Repeat the write if no event is received. Under macOS often
// the write fails to generate a write event for non-recursive watcher
for i := 0; i < 3; i++ {
f, err := os.OpenFile(fpath, os.O_RDWR|os.O_APPEND, 0640)
assertNoError(t, err)
f.WriteString(" world\n")
f.Sync()
f.Close()
ev, err = readTimeout(t, watcher)
if err == nil || err != errReadTimeout {
break
}
}
assertNoError(t, err)
assert.Equal(t, fpath, ev.Name)
assert.Equal(t, fsnotify.Write, ev.Op)
// Move
err = os.Rename(fpath, fpath2)
assertNoError(t, err)
evRename, err := readTimeout(t, watcher)
assertNoError(t, err)
// Sometimes a duplicate Write can be received under Linux, skip
if evRename.Op == fsnotify.Write {
evRename, err = readTimeout(t, watcher)
}
evCreate, err := readTimeout(t, watcher)
assertNoError(t, err)
if evRename.Op != fsnotify.Rename {
evRename, evCreate = evCreate, evRename
}
assert.Equal(t, fpath, evRename.Name)
assert.Equal(t, fsnotify.Rename, evRename.Op)
assert.Equal(t, fpath2, evCreate.Name)
assert.Equal(t, fsnotify.Create, evCreate.Op)
// Delete
err = os.Remove(fpath2)
assertNoError(t, err)
ev, err = readTimeout(t, watcher)
assertNoError(t, err)
// Windows: A write to the parent directory sneaks in
if ev.Op == fsnotify.Write && ev.Name == dir {
ev, err = readTimeout(t, watcher)
assertNoError(t, err)
}
assert.Equal(t, fpath2, ev.Name)
assert.Equal(t, fsnotify.Remove, ev.Op)
}
var errReadTimeout = errors.New("read timeout")
// helper to read from channel
func readTimeout(tb testing.TB, watcher Watcher) (fsnotify.Event, error) {
timer := time.NewTimer(3 * time.Second)
defer timer.Stop()
for {
select {
case <-timer.C:
return fsnotify.Event{}, errReadTimeout
case msg, ok := <-watcher.EventChannel():
if !ok {
return fsnotify.Event{}, errors.New("channel closed")
}
return msg, nil
case err := <-watcher.ErrorChannel():
tb.Log("readTimeout got error:", err)
}
}
}
func assertNoError(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}