-
-
Notifications
You must be signed in to change notification settings - Fork 887
/
integration_test.go
703 lines (595 loc) · 15.8 KB
/
integration_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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
//go:build !plan9 && !solaris
// +build !plan9,!solaris
package fsnotify
import (
"errors"
"fmt"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/fsnotify/fsnotify/internal"
)
func TestWatch(t *testing.T) {
tests := []testCase{
{"multiple creates", func(t *testing.T, w *Watcher, tmp string) {
file := filepath.Join(tmp, "file")
addWatch(t, w, tmp)
cat(t, "data", file)
rm(t, file)
touch(t, file) // Recreate the file
cat(t, "data", file) // Modify
cat(t, "data", file) // Modify
}, `
create /file
write /file
remove /file
create /file
write /file
write /file
`},
{"dir only", func(t *testing.T, w *Watcher, tmp string) {
beforeWatch := filepath.Join(tmp, "beforewatch")
file := filepath.Join(tmp, "file")
touch(t, beforeWatch)
addWatch(t, w, tmp)
cat(t, "data", file)
rm(t, file)
rm(t, beforeWatch)
}, `
create /file
write /file
remove /file
remove /beforewatch
`},
{"subdir", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)
file := filepath.Join(tmp, "file")
dir := filepath.Join(tmp, "sub")
dirfile := filepath.Join(tmp, "sub/file2")
mkdir(t, dir) // Create sub-directory
touch(t, file) // Create a file
touch(t, dirfile) // Create a file (Should not see this! we are not watching subdir)
time.Sleep(200 * time.Millisecond)
rmAll(t, dir) // Make sure receive deletes for both file and sub-directory
rm(t, file)
}, `
create /sub
create /file
remove /sub
remove /file
# Windows includes a write for the /sub dir too, two of them even(?)
windows:
create /sub
create /file
write /sub
write /sub
remove /sub
remove /file
`},
{"file in directory is not readable", func(t *testing.T, w *Watcher, tmp string) {
if runtime.GOOS == "windows" {
t.Skip("attributes don't work on Windows")
}
touch(t, tmp, "file-unreadable")
chmod(t, 0, tmp, "file-unreadable")
touch(t, tmp, "file")
addWatch(t, w, tmp)
cat(t, "hello", tmp, "file")
rm(t, tmp, "file")
rm(t, tmp, "file-unreadable")
}, `
WRITE "/file"
REMOVE "/file"
REMOVE "/file-unreadable"
# We never set up a watcher on the unreadable file, so we don't get
# the REMOVE.
kqueue:
WRITE "/file"
REMOVE "/file"
`},
}
for _, tt := range tests {
tt := tt
tt.run(t)
}
}
func TestWatchRename(t *testing.T) {
tests := []testCase{
{"rename file", func(t *testing.T, w *Watcher, tmp string) {
file := filepath.Join(tmp, "file")
addWatch(t, w, tmp)
cat(t, "asd", file)
mv(t, file, tmp, "renamed")
}, `
create /file
write /file
rename /file
create /renamed
`},
{"rename from unwatched directory", func(t *testing.T, w *Watcher, tmp string) {
unwatched := t.TempDir()
addWatch(t, w, tmp)
touch(t, unwatched, "file")
mv(t, filepath.Join(unwatched, "file"), tmp, "file")
}, `
create /file
`},
{"rename to unwatched directory", func(t *testing.T, w *Watcher, tmp string) {
if runtime.GOOS == "netbsd" && isCI() {
t.Skip("fails in CI; see #488")
}
unwatched := t.TempDir()
file := filepath.Join(tmp, "file")
renamed := filepath.Join(unwatched, "renamed")
addWatch(t, w, tmp)
cat(t, "data", file)
mv(t, file, renamed)
cat(t, "data", renamed) // Modify the file outside of the watched dir
touch(t, file) // Recreate the file that was moved
}, `
create /file # cat data >file
write /file # ^
rename /file # mv file ../renamed
create /file # touch file
# Windows has REMOVE /file, rather than CREATE /file
windows:
create /file
write /file
remove /file
create /file
`},
{"rename overwriting existing file", func(t *testing.T, w *Watcher, tmp string) {
touch(t, tmp, "renamed")
addWatch(t, w, tmp)
unwatched := t.TempDir()
file := filepath.Join(unwatched, "file")
touch(t, file)
mv(t, file, tmp, "renamed")
}, `
remove /renamed
create /renamed
# No remove event for inotify; inotify just sends MOVE_SELF.
linux:
create /renamed
`},
{"rename watched directory", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)
dir := filepath.Join(tmp, "dir")
mkdir(t, dir)
addWatch(t, w, dir)
mv(t, dir, tmp, "dir-renamed")
touch(t, tmp, "dir-renamed/file")
}, `
CREATE "/dir" # mkdir
RENAME "/dir" # mv
CREATE "/dir-renamed"
RENAME "/dir"
CREATE "/dir/file" # touch
windows:
CREATE "/dir" # mkdir
RENAME "/dir" # mv
CREATE "/dir-renamed"
CREATE "/dir-renamed/file" # touch
# TODO: no results for the touch; this is probably a bug; windows
# was fixed in #370.
kqueue:
CREATE "/dir" # mkdir
CREATE "/dir-renamed" # mv
REMOVE|RENAME "/dir"
`},
}
for _, tt := range tests {
tt := tt
tt.run(t)
}
}
func TestWatchSymlink(t *testing.T) {
tests := []testCase{
{"create unresolvable symlink", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)
symlink(t, filepath.Join(tmp, "target"), tmp, "link")
}, `
create /link
windows:
create /link
write /link
`},
{"cyclic symlink", func(t *testing.T, w *Watcher, tmp string) {
if runtime.GOOS == "darwin" {
// This test is borked on macOS; it reports events outside the
// watched directory:
//
// create "/private/.../testwatchsymlinkcyclic_symlink3681444267/001/link"
// create "/link"
// write "/link"
// write "/private/.../testwatchsymlinkcyclic_symlink3681444267/001/link"
//
// kqueue.go does a lot of weird things with symlinks that I
// don't think are necessarily correct, but need to test a bit
// more.
t.Skip()
}
symlink(t, ".", tmp, "link")
addWatch(t, w, tmp)
rm(t, tmp, "link")
cat(t, "foo", tmp, "link")
}, `
write /link
create /link
linux, windows:
remove /link
create /link
write /link
`},
}
for _, tt := range tests {
tt := tt
tt.run(t)
}
}
func TestWatchAttrib(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("attributes don't work on Windows")
}
tests := []testCase{
{"chmod", func(t *testing.T, w *Watcher, tmp string) {
file := filepath.Join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, file)
chmod(t, 0o700, file)
}, `
CHMOD "/file"
`},
{"write does not trigger CHMOD", func(t *testing.T, w *Watcher, tmp string) {
file := filepath.Join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, file)
chmod(t, 0o700, file)
cat(t, "more data", file)
}, `
CHMOD "/file"
WRITE "/file"
`},
{"chmod after write", func(t *testing.T, w *Watcher, tmp string) {
file := filepath.Join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, file)
chmod(t, 0o700, file)
cat(t, "more data", file)
chmod(t, 0o600, file)
}, `
CHMOD "/file"
WRITE "/file"
CHMOD "/file"
`},
}
for _, tt := range tests {
tt := tt
tt.run(t)
}
}
func TestWatchRm(t *testing.T) {
tests := []testCase{
{"remove watched directory", func(t *testing.T, w *Watcher, tmp string) {
if runtime.GOOS == "openbsd" || runtime.GOOS == "netbsd" {
t.Skip("behaviour is inconsistent on OpenBSD and NetBSD, and this test is flaky")
}
file := filepath.Join(tmp, "file")
touch(t, file)
addWatch(t, w, tmp)
rmAll(t, tmp)
}, `
# OpenBSD, NetBSD
remove /file
remove|write /
freebsd:
remove|write "/"
remove ""
create "."
darwin:
remove /file
remove|write /
linux:
remove /file
remove /
windows:
remove /file
remove /
`},
}
for _, tt := range tests {
tt := tt
tt.run(t)
}
}
func TestClose(t *testing.T) {
t.Run("close", func(t *testing.T) {
t.Parallel()
w := newWatcher(t)
if err := w.Close(); err != nil {
t.Fatal(err)
}
var done int32
go func() {
w.Close()
atomic.StoreInt32(&done, 1)
}()
eventSeparator()
if atomic.LoadInt32(&done) == 0 {
t.Fatal("double Close() test failed: second Close() call didn't return")
}
if err := w.Add(t.TempDir()); err == nil {
t.Fatal("expected error on Watch() after Close(), got nil")
}
})
// Make sure that Close() works even when the Events channel isn't being
// read.
t.Run("events not read", func(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
w := newWatcher(t, tmp)
touch(t, tmp, "file")
rm(t, tmp, "file")
if err := w.Close(); err != nil {
t.Fatal(err)
}
})
// Make sure that calling Close() while REMOVE events are emitted doesn't race.
t.Run("close while removing files", func(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
files := make([]string, 0, 200)
for i := 0; i < 200; i++ {
f := filepath.Join(tmp, fmt.Sprintf("file-%03d", i))
touch(t, f, noWait)
files = append(files, f)
}
w := newWatcher(t, tmp)
startC, stopC, errC := make(chan struct{}), make(chan struct{}), make(chan error)
go func() {
for {
select {
case <-w.Errors:
case <-w.Events:
case <-stopC:
return
}
}
}()
rmDone := make(chan struct{})
go func() {
<-startC
for _, f := range files {
rm(t, f, noWait)
}
rmDone <- struct{}{}
}()
go func() {
<-startC
errC <- w.Close()
}()
close(startC)
defer close(stopC)
if err := <-errC; err != nil {
t.Fatal(err)
}
<-rmDone
})
// Make sure Close() doesn't race when called more than once; hard to write
// a good reproducible test for this, but running it 150 times seems to
// reproduce it in ~75% of cases and isn't too slow (~0.06s on my system).
t.Run("double close", func(t *testing.T) {
t.Parallel()
for i := 0; i < 150; i++ {
w, err := NewWatcher()
if err != nil {
if strings.Contains(err.Error(), "too many") { // syscall.EMFILE
time.Sleep(100 * time.Millisecond)
continue
}
t.Fatal(err)
}
go w.Close()
go w.Close()
go w.Close()
}
})
}
func TestAdd(t *testing.T) {
t.Run("permission denied", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("attributes don't work on Windows")
}
t.Parallel()
tmp := t.TempDir()
dir := filepath.Join(tmp, "dir-unreadable")
mkdir(t, dir)
touch(t, dir, "/file")
chmod(t, 0, dir)
w := newWatcher(t)
defer func() {
w.Close()
chmod(t, 0o755, dir) // Make TempDir() cleanup work
}()
err := w.Add(dir)
if err == nil {
t.Fatal("error is nil")
}
if !errors.Is(err, internal.UnixEACCES) {
t.Errorf("not unix.EACCESS: %T %#[1]v", err)
}
if !errors.Is(err, internal.SyscallEACCES) {
t.Errorf("not syscall.EACCESS: %T %#[1]v", err)
}
})
}
// TODO: should also check internal state is correct/cleaned up; e.g. no
// left-over file descriptors or whatnot.
func TestRemove(t *testing.T) {
t.Run("works", func(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
touch(t, tmp, "file")
w := newCollector(t)
w.collect(t)
addWatch(t, w.w, tmp)
if err := w.w.Remove(tmp); err != nil {
t.Fatal(err)
}
time.Sleep(200 * time.Millisecond)
cat(t, "data", tmp, "file")
chmod(t, 0o700, tmp, "file")
have := w.stop(t)
if len(have) > 0 {
t.Errorf("received events; expected none:\n%s", have)
}
})
t.Run("remove same dir twice", func(t *testing.T) {
tmp := t.TempDir()
touch(t, tmp, "file")
w := newWatcher(t)
defer w.Close()
addWatch(t, w, tmp)
if err := w.Remove(tmp); err != nil {
t.Fatal(err)
}
if err := w.Remove(tmp); err == nil {
t.Fatal("no error")
}
})
// Make sure that concurrent calls to Remove() don't race.
t.Run("no race", func(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
touch(t, tmp, "file")
for i := 0; i < 10; i++ {
w := newWatcher(t)
defer w.Close()
addWatch(t, w, tmp)
done := make(chan struct{})
go func() {
defer func() { done <- struct{}{} }()
w.Remove(tmp)
}()
go func() {
defer func() { done <- struct{}{} }()
w.Remove(tmp)
}()
<-done
<-done
w.Close()
}
})
}
func TestWatcherRecursive(t *testing.T) {
switch runtime.GOOS {
case "linux":
// Run test.
default:
// Ensure we get correct error.
tmp := t.TempDir()
w := newWatcher(t)
err := w.Add(filepath.Join(tmp, "..."))
if !errors.Is(err, ErrRecursionUnsupported) {
t.Errorf("wrong error: %s", err)
}
return
}
// inotify(7):
// Inotify monitoring of directories is not recursive: to monitor
// subdirectories under a directory, additional watches must be created.
// This can take a significant amount time for large directory trees.
//
// If monitoring an entire directory subtree, and a new subdirectory is
// created in that tree or an existing directory is renamed into that
// tree, be aware that by the time you create a watch for the new
// subdirectory, new files (and subdirectories) may already exist inside
// the subdirectory. Therefore, you may want to scan the contents of the
// subdirectory immediately after adding the watch (and, if desired,
// recursively add watches for any subdirectories that it contains).
tests := []testCase{
// Make a nested directory tree, then write some files there.
{"basic", func(t *testing.T, w *Watcher, tmp string) {
mkdirAll(t, tmp, "/one/two/three/four")
addWatch(t, w, tmp, "/...")
cat(t, "asd", tmp, "/file.txt")
cat(t, "asd", tmp, "/one/two/three/file.txt")
}, `
CREATE "/file.txt"
WRITE "/file.txt"
CREATE "/one/two/three/file.txt"
WRITE "/one/two/three/file.txt"
`},
{"add directory", func(t *testing.T, w *Watcher, tmp string) {
mkdirAll(t, tmp, "/one/two/three/four")
addWatch(t, w, tmp, "/...")
mkdirAll(t, tmp, "/one/two/new/dir")
touch(t, tmp, "/one/two/new/file")
touch(t, tmp, "/one/two/new/dir/file")
}, `
# TODO: don't see the new/dir being created.
CREATE "/one/two/new"
CREATE "/one/two/new/file"
CREATE "/one/two/new/dir/file"
`},
// Remove nested directory
{"remove directory", func(t *testing.T, w *Watcher, tmp string) {
mkdirAll(t, tmp, "/one/two/three/four")
addWatch(t, w, tmp, "...")
cat(t, "asd", tmp, "one/two/three/file.txt")
rmAll(t, tmp, "one/two")
}, `
# TODO: this includes many duplicate events as we get a
# notification both for the watch on the directory itself
# as well as the parent that watches the directory.
CREATE "/one/two/three/file.txt"
WRITE "/one/two/three/file.txt"
REMOVE "/one/two/three/file.txt"
REMOVE "/one/two/three/four"
REMOVE "/one/two/three/four"
REMOVE "/one/two/three"
REMOVE "/one/two/three"
REMOVE "/one/two"
REMOVE "/one/two"
`},
// Rename nested directory
{"rename directory", func(t *testing.T, w *Watcher, tmp string) {
mkdirAll(t, tmp, "/one/two/three/four")
addWatch(t, w, tmp, "...")
mv(t, filepath.Join(tmp, "one"), tmp, "one-rename")
touch(t, tmp, "one-rename/file")
touch(t, tmp, "one-rename/two/three/file")
}, `
# TODO: rename + create + rename doesn't seem quite right?
RENAME "/one"
CREATE "/one-rename"
RENAME "/one-rename"
CREATE "/one-rename/file"
CREATE "/one-rename/two/three/file"
`},
// TODO: rest that Remove doesn't keep watching stuff
}
for _, tt := range tests {
tt := tt
tt.run(t)
// t.Run(tt.name, func(t *testing.T) {
// t.Parallel()
// tmp := t.TempDir()
// w := newCollector(t)
// tt.preWatch(t, tmp)
// addWatch(t, w.w, tmp, "...")
// tt.postWatch(t, tmp)
// w.collect(t)
// have := w.stop(t)
// for i := range have {
// have[i].Name = strings.TrimPrefix(have[i].Name, tmp)
// }
// if have.String() != tt.want.String() {
// t.Errorf("\nhave:\n%s\nwant:\n%s", have, tt.want)
// }
// })
}
}