-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
fsevents_test.go
531 lines (424 loc) · 15.6 KB
/
fsevents_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
package fsevents_test
import (
"fmt"
"math/rand"
"os"
"path"
"reflect"
"runtime"
"testing"
"time"
fsevents "github.com/tywkeene/go-fsevents"
"golang.org/x/sys/unix"
)
type MaskTest struct {
Mask uint32
Args []string
Setup func(...string) error
Action func(...string) error
}
var MaskTests = map[string]MaskTest{
"MovedFrom": {
Mask: fsevents.MovedFrom,
Args: []string{"./test/move-test-file", "./test2/moved-file"},
Setup: writeToFile,
Action: move,
},
"MovedTo": {
Mask: fsevents.MovedTo,
Args: []string{"./test2/move-test-file", "./test/moved-file"},
Setup: writeToFile,
Action: move,
},
"Delete": {
Mask: fsevents.Delete,
Args: []string{"./test/delete-file"},
Setup: writeToFile,
Action: remove,
},
"Open": {
Mask: fsevents.Open,
Args: []string{"./test/open-file-test"},
Setup: writeToFile,
Action: open,
},
"Modified": {
Mask: fsevents.Modified,
Args: []string{"./test/modify-file-test"},
Setup: writeToFile,
Action: modify,
},
"Accessed": {
Mask: fsevents.Accessed,
Args: []string{"./test/accessed-file-test"},
Setup: writeToFile,
Action: access,
},
"AttrChange": {
Mask: fsevents.AttrChange,
Args: []string{"./test/attr-change-file-test"},
Setup: writeToFile,
Action: changeAttr,
},
"CloseWrite": {
Mask: fsevents.CloseWrite,
Args: []string{"./test/close-write-file-test"},
Setup: writeToFile,
Action: writeToFile,
},
"CloseRead": {
Mask: fsevents.CloseRead,
Args: []string{"./test/close-read-file-test"},
Setup: writeToFile,
Action: access,
},
"Move": {
Mask: fsevents.Move,
Args: []string{"./test/move-file-test", "./test/move-file-test2"},
Setup: writeToFile,
Action: move,
},
"Create": {
Mask: fsevents.Create,
Args: []string{"./test/create-file-test"},
Setup: nothing,
Action: writeToFile,
},
/*
{fsevents.RootDelete, []string{"./test"}, nothing, "Root delete"},
{fsevents.RootMove, []string{"./test", "/tmp/test-moved"}, nothing, "Root move"},
*/
}
var (
testRootDir string = "./test"
testRootDir2 string = "./test2"
)
func open(args ...string) error {
_, err := os.Open(args[0])
return err
}
func modify(args ...string) error {
fd, err := os.OpenFile(args[0], os.O_WRONLY, 0644)
if err != nil {
return err
}
defer fd.Close()
fd.Write([]byte("test"))
return nil
}
func access(args ...string) error {
fd, err := os.Open(args[0])
if err != nil {
return err
}
defer fd.Close()
var buffer []byte = make([]byte, 1)
_, err = fd.ReadAt(buffer, 1)
return err
}
func changeAttr(args ...string) error { return os.Chmod(args[0], 0644) }
func move(args ...string) error { return os.Rename(args[0], args[1]) }
func remove(args ...string) error { return os.RemoveAll(args[0]) }
func writeToFile(args ...string) error { return writeRandomFile(args[0]) }
func nothing(args ...string) error { return nil }
func assert(t *testing.T, compare bool, err error) {
if compare == false {
_, _, line, _ := runtime.Caller(1)
t.Logf("Comparison @ [line %d] failed\n", line)
if err != nil {
t.Fatal("Error returned:", err)
} else {
t.Fatal("Exiting")
}
}
}
func writeRandomFile(path string) error {
var random = []byte("ABCDEF1234567890")
rand.Seed(time.Now().UnixNano())
buffer := make([]byte, 16)
for i := range buffer {
buffer[i] = random[rand.Intn(len(random))]
}
fd, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
return err
}
defer fd.Close()
n, err := fd.Write(buffer)
if n < len(buffer) {
return fmt.Errorf("Wrote %d of %d bytes to file %q", n, len(buffer), path)
}
return err
}
func setupDirs(paths []string) {
for _, path := range paths {
os.Mkdir(path, 0777)
}
}
func teardownDirs(paths []string) {
for _, path := range paths {
os.RemoveAll(path)
}
}
func TestMasks(t *testing.T) {
var w *fsevents.Watcher
var d *fsevents.WatchDescriptor
var err error
setupDirs([]string{testRootDir, testRootDir2})
for name, maskTest := range MaskTests {
fmt.Printf("Running test for mask %q\n", name)
err = maskTest.Setup(maskTest.Args...)
assert(t, (err == nil), err)
w, err = fsevents.NewWatcher()
assert(t, (w != nil), fmt.Errorf("NewWatcher should have returned a non-nil Watcher"))
assert(t, (err == nil), err)
d, err = w.AddDescriptor(testRootDir, maskTest.Mask)
assert(t, (d != nil), fmt.Errorf("AddDescriptor should have returned a non-nil descriptor"))
assert(t, (err == nil), err)
err = d.Start()
assert(t, (err == nil), err)
err = maskTest.Action(maskTest.Args...)
assert(t, (err == nil), err)
event, err := w.ReadSingleEvent()
assert(t, (err == nil), err)
// Ensure the event and its data is consistent
assert(t, (event != nil), fmt.Errorf("ReadSingleEvent should have returned a non-nil event"))
assert(t, (event.Name != ""), fmt.Errorf("The Name field in the event should not be empty"))
assert(t, (event.Path != ""), fmt.Errorf("The Event field in the event should not be empty"))
assert(t, (w.GetEventCount() == 1), nil)
assert(t, (fsevents.CheckMask(maskTest.Mask, event.RawEvent.Mask) == true),
fmt.Errorf("Event returned invalid mask: Expected: %d Got: %d\n", maskTest.Mask, event.RawEvent.Mask))
w.StopAll()
w.RemoveDescriptor(testRootDir)
}
teardownDirs([]string{testRootDir, testRootDir2})
}
func TestRemoveWatchDescriptor(t *testing.T) {
var err error
setupDirs([]string{testRootDir})
w, err := fsevents.NewWatcher()
assert(t, (w != nil), fmt.Errorf("NewWatcher should have returned a non-nil Watcher"))
assert(t, (err == nil), err)
d, err := w.AddDescriptor(testRootDir, fsevents.AllEvents)
assert(t, (d != nil), fmt.Errorf("AddDescriptor should have returned non-nil descriptor"))
assert(t, (err != fsevents.ErrDescAlreadyExists), fmt.Errorf("AddDescriptor should have returned error on duplicate descriptor"))
// RemoveDescriptor SHOULD return non-nil when removing an existing descriptor
err = w.RemoveDescriptor(d.Path)
assert(t, (err == nil), fmt.Errorf("RemoveDescriptor should have returned nil error upon removal of existing descriptor"))
teardownDirs([]string{testRootDir})
}
func TestCustomMaskChecks(t *testing.T) {
var events = map[string]*fsevents.FsEvent{
"IsDirEvent": &fsevents.FsEvent{
RawEvent: &unix.InotifyEvent{Mask: fsevents.IsDir},
},
"IsDirChanged": &fsevents.FsEvent{
RawEvent: &unix.InotifyEvent{Mask: fsevents.DirChangedEvent},
},
"IsDirCreated": &fsevents.FsEvent{
RawEvent: &unix.InotifyEvent{Mask: fsevents.DirCreatedEvent},
},
"IsDirRemoved": &fsevents.FsEvent{
RawEvent: &unix.InotifyEvent{Mask: fsevents.DirRemovedEvent},
},
"IsFileCreated": &fsevents.FsEvent{
RawEvent: &unix.InotifyEvent{Mask: fsevents.FileCreatedEvent},
},
"IsFileRemoved": &fsevents.FsEvent{
RawEvent: &unix.InotifyEvent{Mask: fsevents.FileRemovedEvent},
},
"IsFileChanged": &fsevents.FsEvent{
RawEvent: &unix.InotifyEvent{Mask: fsevents.FileChangedEvent},
},
}
// Loop through FsEvent's methods and test each to ensure that each return true
for methodName, event := range events {
t.Log("Running test for FsEvent method:", methodName)
returnVal := reflect.ValueOf(event).MethodByName(methodName).Call(nil)
assert(t, (returnVal[0].Bool() == true), fmt.Errorf("FsEvent method %q should have returned true", methodName))
}
rootDeletedEvent := &fsevents.FsEvent{
Path: testRootDir,
RawEvent: &unix.InotifyEvent{Mask: fsevents.RootDelete},
}
rootMovedEvent := &fsevents.FsEvent{
Path: testRootDir,
RawEvent: &unix.InotifyEvent{Mask: fsevents.RootMove},
}
dirMethodArgs := []reflect.Value{reflect.ValueOf(testRootDir)}
t.Log("Testing FsEvent method: IsRootDeletion")
rootDelVal := reflect.ValueOf(rootDeletedEvent).MethodByName("IsRootDeletion").Call(dirMethodArgs)
assert(t, (rootDelVal[0].Bool() == true), fmt.Errorf("FsEvent method 'IsRootDeletion' should have returned true"))
t.Log("Testing FsEvent method: IsRootMoved")
rootMovVal := reflect.ValueOf(rootMovedEvent).MethodByName("IsRootMoved").Call(dirMethodArgs)
assert(t, (rootMovVal[0].Bool() == true), fmt.Errorf("FsEvent method 'IsRootMoved' should have returned true"))
}
func TestNewWatcher(t *testing.T) {
var err error
setupDirs([]string{testRootDir})
w, err := fsevents.NewWatcher()
assert(t, (err == nil), err)
assert(t, (w != nil), fmt.Errorf("NewWatcher should have returned a non-nil Watcher"))
teardownDirs([]string{testRootDir})
}
func TestStart(t *testing.T) {
var err error
setupDirs([]string{testRootDir})
w, err := fsevents.NewWatcher()
assert(t, (err == nil), err)
assert(t, (w != nil), fmt.Errorf("NewWatcher should have returned a non-nil Watcher"))
d, err := w.AddDescriptor(testRootDir, fsevents.AllEvents)
assert(t, (d != nil), fmt.Errorf("GetDescriptorByPath should have returned non-nil descriptor"))
assert(t, (err == nil), err)
// Trying to start a new descriptor SHOULD NOT return error
err = d.Start()
assert(t, (err == nil), err)
assert(t, (w.GetRunningDescriptors() == 1), fmt.Errorf("GetRunningDescriptor should have returned 1"))
// Trying to start an already running descriptor SHOULD return error
err = d.Start()
assert(t, (err != nil), fmt.Errorf("Start should have returned error %q", fsevents.ErrDescNotStart))
teardownDirs([]string{testRootDir})
}
func TestAddDescriptor(t *testing.T) {
var w *fsevents.Watcher
var err error
setupDirs([]string{testRootDir})
w, err = fsevents.NewWatcher()
assert(t, (err == nil), err)
assert(t, (w != nil), fmt.Errorf("NewWatcher should have returned non-nil Watcher"))
// AddDescriptor SHOULD return ErrDescNotCreated if we try to add a WatchDescriptor for a directory that does not exist
d, err := w.AddDescriptor("not_there/", fsevents.AllEvents)
assert(t, (d == nil), fmt.Errorf("AddDescriptor should have returned nil descriptor"))
expectedErr := fmt.Errorf("%s: %s", fsevents.ErrDescNotCreated, "directory does not exist").Error()
assert(t, (err.Error() == expectedErr), err)
assert(t, (len(w.ListDescriptors()) == 0), fmt.Errorf("len(w.ListDescriptors()) should have returned 0"))
// AddDescriptor SHOULD return a non-nil WatchDescriptor and a non-nil error if we add a WatchDescriptor for a directory
// that exists on disk and does not already have a running watch
d, err = w.AddDescriptor(testRootDir, fsevents.AllEvents)
assert(t, (d != nil), fmt.Errorf("AddDescriptor should have returned non-nil descriptor"))
assert(t, (err != fsevents.ErrDescAlreadyExists), fmt.Errorf("AddDescriptor should have returned error on duplicate descriptor"))
assert(t, (len(w.ListDescriptors()) == 1), fmt.Errorf("len(w.ListDescriptors()) should have returned 1"))
// AddDescriptor SHOULD return error if we add a WatchDescriptor for a directory that exists and is not already watched
d, err = w.AddDescriptor(testRootDir, fsevents.AllEvents)
assert(t, (d == nil), fmt.Errorf("AddDescriptor should have returned nil descriptor"))
assert(t, (err == fsevents.ErrDescAlreadyExists), fmt.Errorf("AddDescriptor should have returned error on duplicate descriptor"))
assert(t, (len(w.ListDescriptors()) == 1), fmt.Errorf("len(w.ListDescriptors()) should have returned 1"))
teardownDirs([]string{testRootDir})
}
func TestGetDescriptorByPath(t *testing.T) {
var err error
setupDirs([]string{testRootDir})
w, err := fsevents.NewWatcher()
assert(t, (err == nil), err)
assert(t, (w != nil), fmt.Errorf("NewWatcher should have returned non-nil Watcher"))
w.AddDescriptor(testRootDir, fsevents.AllEvents)
// GetDescriptorByPath SHOULD return a non-nil descriptor object if the path for the descriptor exists
there := w.GetDescriptorByPath(testRootDir)
assert(t, (there != nil), fmt.Errorf("GetDescriptorByPath should have returned non-nil descriptor"))
// GetDescriptorByPath SHOULD NOT return a non-nil descriptor object if the path for the descriptor DOES NOT exist
notThere := w.GetDescriptorByPath("not_there")
assert(t, (notThere == nil), fmt.Errorf("GetDescriptorByPath should have returned non-nil descriptor"))
teardownDirs([]string{testRootDir})
}
type fileCreatedHandler struct {
Mask uint32
}
func (h *fileCreatedHandler) Handle(w *fsevents.Watcher, event *fsevents.FsEvent) error {
fmt.Println("File created:", event.Path)
return nil
}
func (h *fileCreatedHandler) GetMask() uint32 {
return h.Mask
}
func (h *fileCreatedHandler) Check(event *fsevents.FsEvent) bool {
return event.IsFileCreated()
}
func TestUnregisterEventHandler(t *testing.T) {
var w *fsevents.Watcher
var err error
setupDirs([]string{testRootDir})
w, err = fsevents.NewWatcher()
assert(t, (w != nil), fmt.Errorf("NewWatcher should have returned non-nil Watcher"))
assert(t, (err == nil), err)
// UnregisterEventHandler SHOULD return an error when attempting to remove a non-existant handle
err = w.UnregisterEventHandler(fsevents.FileCreatedEvent)
assert(t, (err != nil), err)
// RegisterEventHandler SHOULD NOT return an error when attempting to register a non-existant handle
err = w.RegisterEventHandler(&fileCreatedHandler{Mask: fsevents.FileCreatedEvent})
assert(t, (err == nil), err)
// RegisterEventHandler SHOULD return an error when attempting to register an existing handle
err = w.RegisterEventHandler(&fileCreatedHandler{Mask: fsevents.FileChangedEvent})
assert(t, (err == nil), err)
err = w.UnregisterEventHandler(fsevents.FileCreatedEvent)
assert(t, (err == nil), err)
teardownDirs([]string{testRootDir})
}
func TestRegisterEventHandler(t *testing.T) {
var w *fsevents.Watcher
var err error
setupDirs([]string{testRootDir})
w, err = fsevents.NewWatcher()
assert(t, (w != nil), fmt.Errorf("NewWatcher should have returned non-nil Watcher"))
assert(t, (err == nil), err)
handle := &fileCreatedHandler{Mask: fsevents.FileCreatedEvent}
err = w.RegisterEventHandler(handle)
assert(t, (err == nil), err)
err = w.RegisterEventHandler(handle)
assert(t, (err != nil), err)
teardownDirs([]string{testRootDir})
}
func TestRecursiveAdd(t *testing.T) {
var w *fsevents.Watcher
var err error
testDirs := []string{
testRootDir,
path.Join(testRootDir, "a"),
path.Join(testRootDir, "a/aa"),
path.Join(testRootDir, "b"),
path.Join(testRootDir, "b/bb"),
}
setupDirs(testDirs)
w, err = fsevents.NewWatcher()
assert(t, (w != nil), fmt.Errorf("NewWatcher should have returned non-nil Watcher"))
assert(t, (err == nil), err)
err = w.RecursiveAdd(testRootDir, fsevents.AllEvents)
assert(t, (err == nil), err)
assert(t, (int(w.GetRunningDescriptors()) == len(testDirs)), fmt.Errorf("Count of running descriptors is not equal to number of directories"))
teardownDirs(testDirs)
}
func TestMultipleEvents(t *testing.T) {
var w *fsevents.Watcher
var d *fsevents.WatchDescriptor
var err error
testPath := testRootDir + "/multiple-event-test"
setupDirs([]string{testRootDir})
err = writeRandomFile(testPath)
assert(t, (err == nil), err)
w, err = fsevents.NewWatcher()
assert(t, (w != nil), fmt.Errorf("NewWatcher should have returned non-nil Watcher"))
assert(t, (err == nil), err)
d, err = w.AddDescriptor(testPath, unix.IN_ALL_EVENTS)
assert(t, (d != nil), fmt.Errorf("AddDescriptor should have returned a non-nil descriptor"))
assert(t, (err == nil), err)
err = d.Start()
assert(t, (err == nil), err)
err = os.Remove(testPath)
assert(t, (err == nil), err)
// As per inotify(7), an unlink triggers 3 separate events on the
// unlinked path: IN_ATTRIB, IN_DELETE_SELF, and IN_IGNORED. Since
// we can't timeout ReadSingleEvent, run it in a goroutine and fail
// the test if we don't read those 3 events in a reasonable amount
// of time.
eventCount := 0
go func() {
for {
event, err := w.ReadSingleEvent()
assert(t, (event != nil), err)
assert(t, (err == nil), err)
eventCount++
}
}()
time.Sleep(1 * time.Millisecond)
assert(t, (eventCount == 3), err)
teardownDirs([]string{testRootDir})
}