Repository navigation
Expand file tree
/
Copy pathwatcher_linux_test.go
More file actions
384 lines (341 loc) · 10.9 KB
/
Copy pathwatcher_linux_test.go
File metadata and controls
384 lines (341 loc) · 10.9 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
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
//go:build linux
package fswatcher
import (
"context"
"errors"
"os"
"path/filepath"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
)
// TestFanotify_Backend tests the fanotify backend initialization
func TestFanotify_Backend(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("requires CAP_SYS_ADMIN and CAP_DAC_READ_SEARCH")
}
root := canonicalTempDir(t)
for _, dir := range []string{"a/nested", "b"} {
require.NoError(t, os.MkdirAll(filepath.Join(root, dir), 0700))
}
ready := make(chan struct{})
wi, err := New(WithPath(root), WithLinuxPlatform(PlatformFanotify), WithReadyChannel(ready), WithCooldown(time.Millisecond), WithSeverity(SeverityDebug), WithLogFile("stdout"))
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
stopped := make(chan struct{})
var watchErr error
go func() { watchErr = wi.Watch(ctx); close(stopped) }()
defer func() { cancel(); <-stopped }()
select {
case <-ready:
case <-stopped:
if os.Getenv("FSWATCHER_REQUIRE_FANOTIFY") != "1" && (errors.Is(watchErr, unix.EPERM) || errors.Is(watchErr, unix.EOPNOTSUPP) || errors.Is(watchErr, unix.ENOSYS)) {
t.Skipf("fanotify unavailable: %v", watchErr)
}
require.NoError(t, watchErr)
t.Fatal("watcher stopped before ready")
case <-time.After(5 * time.Second):
t.Fatal("fanotify did not become ready")
}
for _, name := range []string{"a/nested/one.txt", "b/two.txt"} {
file := filepath.Join(root, name)
require.NoError(t, os.WriteFile(file, []byte("data"), 0600))
timeout := time.NewTimer(3 * time.Second)
found:
for {
select {
case event := <-wi.Events():
if event.Path == file {
break found
}
case <-timeout.C:
t.Fatalf("no fanotify event for %s", file)
}
}
timeout.Stop()
}
cancel()
<-stopped
require.NoError(t, watchErr)
}
func TestFanotifyRecursiveMarksAndDynamicDirectories(t *testing.T) {
root := t.TempDir()
existing := filepath.Join(root, "existing", "nested")
require.NoError(t, os.MkdirAll(existing, 0o700))
w, err := New(WithPath(root), WithSeverity(SeverityNone))
require.NoError(t, err)
wImpl := w.(*watcher)
marked := make(map[string]int)
markCalls := make(map[string]int)
p := &fanotify{
fd: -1,
paths: make(map[string]struct{}),
roots: make(map[string]*fanotifyRoot),
markRefs: make(map[string]int),
mountCache: make(map[uint64]string),
markFn: func(path string, add bool) error {
if add {
marked[path]++
markCalls["+"+path]++
} else {
marked[path]--
markCalls["-"+path]++
if marked[path] == 0 {
delete(marked, path)
}
}
return nil
},
}
t.Cleanup(func() { wImpl.aggregator.close() })
wp, err := validateWatchPath(root, WatchNested)
require.NoError(t, err)
require.NoError(t, p.addWatch(wImpl, wp))
for _, path := range []string{root, filepath.Join(root, "existing"), existing} {
assert.Equal(t, 1, marked[path], "existing directory should have a fanotify mark")
}
created := filepath.Join(root, "created")
createdChild := filepath.Join(created, "child")
require.NoError(t, os.MkdirAll(createdChild, 0o700))
wImpl.dispatchFanotifyEvent(p, created, unix.FAN_CREATE|unix.FAN_ONDIR)
assert.Equal(t, 1, marked[created], "new directory should be marked")
assert.Equal(t, 1, marked[createdChild], "existing descendants should be marked")
require.NoError(t, os.RemoveAll(created))
wImpl.dispatchFanotifyEvent(p, created, unix.FAN_DELETE|unix.FAN_ONDIR)
assert.NotContains(t, marked, created)
assert.NotContains(t, marked, createdChild)
assert.Equal(t, 1, markCalls["-"+created])
assert.Equal(t, 1, markCalls["-"+createdChild])
hidden := filepath.Join(root, ".hidden")
require.NoError(t, os.Mkdir(hidden, 0o700))
wImpl.dispatchFanotifyEvent(p, hidden, unix.FAN_CREATE|unix.FAN_ONDIR)
assert.NotContains(t, marked, hidden, "hidden directories should stay unmarked by default")
}
func TestFanotifyMarkFailureRollsBackAndReportsCleanupErrors(t *testing.T) {
markErr := errors.New("cannot add mark")
rollbackA := errors.New("cannot remove mark a")
rollbackB := errors.New("cannot remove mark b")
for _, tc := range []struct {
name string
failures map[string]error
}{
{name: "successful rollback"},
{name: "rollback continues after cleanup failures", failures: map[string]error{"a": rollbackA, "b": rollbackB}},
} {
t.Run(tc.name, func(t *testing.T) {
var calls []string
p := &fanotify{
markRefs: map[string]int{"shared": 1},
markFn: func(path string, add bool) error {
if add {
calls = append(calls, "+"+path)
if path == "failed" {
return markErr
}
return nil
}
calls = append(calls, "-"+path)
return tc.failures[path]
},
}
root := &fanotifyRoot{dirs: make(map[string]struct{})}
err := p.addDirectoriesLocked(root, []string{"shared", "a", "b", "c", "failed"})
require.ErrorIs(t, err, markErr)
for path, cleanupErr := range tc.failures {
require.ErrorIs(t, err, cleanupErr)
require.ErrorContains(t, err, `rollback fanotify mark "`+path+`"`)
}
require.Equal(t, []string{"+a", "+b", "+c", "+failed", "-c", "-b", "-a"}, calls,
"rollback must attempt every new mark and preserve the shared mark")
wantRefs := map[string]int{"shared": 1}
wantDirs := make(map[string]struct{})
for path := range tc.failures {
wantRefs[path] = 1
wantDirs[path] = struct{}{}
}
require.Equal(t, wantRefs, p.markRefs)
require.Equal(t, wantDirs, root.dirs)
})
}
}
func TestFanotifyOverflowEmitsEventAndReconcilesMarks(t *testing.T) {
root := t.TempDir()
w, err := New(WithPath(root), WithCooldown(time.Millisecond), WithSeverity(SeverityNone))
require.NoError(t, err)
wImpl := w.(*watcher)
marked := make(map[string]bool)
p := &fanotify{
fd: -1,
paths: make(map[string]struct{}),
roots: make(map[string]*fanotifyRoot),
markRefs: make(map[string]int),
mountCache: make(map[uint64]string),
markFn: func(path string, add bool) error {
if add {
marked[path] = true
} else {
delete(marked, path)
}
return nil
},
}
t.Cleanup(func() { wImpl.aggregator.close() })
wp, err := validateWatchPath(root, WatchNested)
require.NoError(t, err)
require.NoError(t, p.addWatch(wImpl, wp))
missed := filepath.Join(root, "created-before-overflow")
require.NoError(t, os.Mkdir(missed, 0o700))
wImpl.dispatchFanotifyEvent(p, "", unix.FAN_Q_OVERFLOW)
assert.True(t, marked[missed], "overflow reconciliation should restore marks for new directories")
select {
case event := <-w.Events():
assert.Contains(t, event.Types, EventOverflow)
case <-time.After(time.Second):
t.Fatal("expected an overflow event")
}
}
// TestInotify_Backend tests the inotify backend initialization
func TestInotify_Backend(t *testing.T) {
tempDir := t.TempDir()
readyChan := make(chan struct{})
w, err := New(
WithPath(tempDir),
WithLinuxPlatform(PlatformInotify),
WithReadyChannel(readyChan),
WithCooldown(10*time.Millisecond),
WithSeverity(SeverityDebug),
WithLogFile("stdout"),
)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
_ = w.Watch(ctx)
}()
select {
case <-readyChan:
case <-time.After(5 * time.Second):
t.Fatal("Watcher failed to start")
}
wImpl := w.(*watcher)
wImpl.streamMu.Lock()
platform := wImpl.streams["platform"]
wImpl.streamMu.Unlock()
assert.IsType(t, &inotify{}, platform, "Should be using inotify backend")
var events []WatchEvent
var mu sync.Mutex
done := make(chan struct{})
go func() {
for event := range w.Events() {
mu.Lock()
events = append(events, event)
mu.Unlock()
}
close(done)
}()
testFile := filepath.Join(tempDir, "test_inotify.txt")
err = os.WriteFile(testFile, []byte("data"), 0644)
require.NoError(t, err)
time.Sleep(200 * time.Millisecond)
cancel()
<-done
mu.Lock()
defer mu.Unlock()
found := false
for _, e := range events {
if e.Path == testFile {
found = true
break
}
}
assert.True(t, found, "Should have received an event for the file using inotify")
}
func TestInotify_RemoveWatchCleansState(t *testing.T) {
dir := t.TempDir()
w, err := New()
require.NoError(t, err)
wImpl := w.(*watcher)
p, err := newInotify()
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, unix.Close(p.fd)) })
wp, err := validateWatchPath(dir, WatchNested)
require.NoError(t, err)
require.NoError(t, p.addWatch(wImpl, wp))
require.NoError(t, p.removeWatch(wp.Path))
// Re-adding the same path must succeed after removal
require.NoError(t, p.addWatch(wImpl, wp))
}
func TestInotifyAddWatchSkipsHiddenDirectoriesByDefault(t *testing.T) {
root := t.TempDir()
hiddenNested := filepath.Join(root, ".hidden", "nested")
visibleNested := filepath.Join(root, "visible", "nested")
require.NoError(t, os.MkdirAll(hiddenNested, 0o700))
require.NoError(t, os.MkdirAll(visibleNested, 0o700))
for _, tc := range []struct {
name string
includeHidden bool
wantHidden bool
}{
{name: "hidden excluded by default"},
{name: "WithHidden includes hidden", includeHidden: true, wantHidden: true},
} {
t.Run(tc.name, func(t *testing.T) {
options := []WatcherOpt{}
if tc.includeHidden {
options = append(options, WithHidden())
}
w, err := New(options...)
require.NoError(t, err)
p, err := newInotify()
require.NoError(t, err)
t.Cleanup(func() { _ = unix.Close(p.fd) })
wp, err := validateWatchPath(root, WatchNested)
require.NoError(t, err)
require.NoError(t, p.addWatch(w.(*watcher), wp))
isWatched := func(path string) bool {
p.mu.RLock()
defer p.mu.RUnlock()
for _, watched := range p.wds {
if watched.Path == path {
return true
}
}
return false
}
assert.True(t, isWatched(visibleNested), "visible nested directory should be watched")
assert.Equal(t, tc.wantHidden, isWatched(hiddenNested), "hidden nested directory registration")
})
}
}
func TestInotifyHiddenWatchRootCanBeDropped(t *testing.T) {
root := filepath.Join(t.TempDir(), ".hidden-root")
nested := filepath.Join(root, "nested")
require.NoError(t, os.MkdirAll(nested, 0o700))
w, err := New(WithPath(root))
require.NoError(t, err)
wImpl := w.(*watcher)
p, err := newInotify()
require.NoError(t, err)
t.Cleanup(func() { _ = unix.Close(p.fd) })
wImpl.streams = map[string]any{"platform": p}
wp, err := validateWatchPath(root, WatchNested)
require.NoError(t, err)
require.NoError(t, p.addWatch(wImpl, wp))
p.mu.RLock()
rootWatched := false
nestedWatched := false
for _, watched := range p.wds {
rootWatched = rootWatched || watched.Path == root
nestedWatched = nestedWatched || watched.Path == nested
}
p.mu.RUnlock()
assert.True(t, rootWatched, "the explicit hidden root remains removable")
assert.False(t, nestedWatched, "hidden roots are not recursively scanned")
require.NoError(t, wImpl.removeWatch(root))
p.mu.RLock()
defer p.mu.RUnlock()
assert.Empty(t, p.wds, "dropping the hidden root removes its watch")
}