-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathmonitor_test.go
174 lines (142 loc) · 3.82 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
package cgroup
import (
"fmt"
"os"
"testing"
"time"
)
const preExistingFile = "i-was-here"
const sleepDuration = time.Millisecond * 5
func TestMonitor(t *testing.T) {
cases := []struct {
kind string
check func() bool
factory func(path string) (monitor, error)
}{
{
kind: "Monitor",
factory: func(path string) (monitor, error) {
return NewMonitor(path)
},
},
{
kind: "walkerMonitor",
factory: func(path string) (monitor, error) {
return newWalkerMonitor(path)
},
},
{
kind: "fanotifyMonitor",
check: func() bool {
// Only test fanotify under root
return os.Geteuid() == 0
},
factory: func(path string) (monitor, error) {
return newFanotifyMonitor(path)
},
},
}
for _, c := range cases {
t.Run(c.kind, func(t *testing.T) {
path := t.TempDir()
preExistingPath := fmt.Sprintf("%s/%s", path, preExistingFile)
if err := os.Mkdir(preExistingPath, 0755); err != nil {
t.Fatalf("Error creating %q: %v", preExistingPath, err)
}
if c.check != nil && !c.check() {
t.Skip()
}
m, err := c.factory(path)
if err != nil {
t.Fatal(err)
}
if m == nil {
t.Skip()
}
testMonitor(t, m, path, preExistingPath)
})
}
}
func testMonitor(t *testing.T, m monitor, path, preExistingPath string) {
preExistingID, err := inode(preExistingPath)
if err != nil {
t.Fatalf("Error resolving %q: %v", preExistingPath, err)
}
preExistingResolved := m.Resolve(preExistingID)
if preExistingResolved != preExistingPath {
t.Errorf("Expected %d to resolve into pre-existing %q, got %q", preExistingID, preExistingPath, preExistingResolved)
}
// What are the chances?
missingName := m.Resolve(88888888888)
if missingName != "" {
t.Errorf("Expected empty string for a missing inode, got %q", missingName)
}
// Check addition one by one
for i := range 20 {
dir := fmt.Sprintf("%s/lol-%d", path, i)
err = os.Mkdir(dir, 0755)
if err != nil {
t.Fatalf("Error creating %q: %v", dir, err)
}
// Sleep for a short time to let fanotify to process
time.Sleep(sleepDuration)
id, err := inode(dir)
if err != nil {
t.Fatalf("Error resolving %q: %v", dir, err)
}
resolved := m.Resolve(id)
if resolved != dir {
t.Errorf("Expected %q, got %q", dir, resolved)
}
}
// Check burst addition
for i := range 20 {
dir := fmt.Sprintf("%s/lol-%d", path, 20+i)
err = os.Mkdir(dir, 0755)
if err != nil {
t.Fatalf("Error creating %q: %v", dir, err)
}
}
// Sleep for a short time to let fanotify to process
time.Sleep(sleepDuration)
// Continue checking burst addition
for i := range 20 {
dir := fmt.Sprintf("%s/lol-%d", path, 20+i)
id, err := inode(dir)
if err != nil {
t.Fatalf("Error resolving %q: %v", dir, err)
}
resolved := m.Resolve(id)
if resolved != dir {
t.Errorf("Expected %q, got %q", dir, resolved)
}
}
// Check if overwrites are picked up
overwritePath := fmt.Sprintf("%s/%s", path, "lol-22")
idBefore, err := inode(overwritePath)
if err != nil {
t.Fatalf("Error resolving %q (before): %v", overwritePath, err)
}
resolvedBefore := m.Resolve(idBefore)
if resolvedBefore != overwritePath {
t.Errorf("Expected %d to resolve into %q, got %q (before)", idBefore, overwritePath, resolvedBefore)
}
err = os.Remove(overwritePath)
if err != nil {
t.Fatalf("Error removing %q: %v", overwritePath, err)
}
err = os.Mkdir(overwritePath, 0755)
if err != nil {
t.Fatalf("Error re-creating %q: %v", overwritePath, err)
}
idAfter, err := inode(overwritePath)
if err != nil {
t.Fatalf("Error resolving %q (after): %v", overwritePath, err)
}
// Sleep for a short time to let fanotify to process
time.Sleep(sleepDuration)
resolvedAfter := m.Resolve(idAfter)
if resolvedAfter != overwritePath {
t.Errorf("Expected %d to resolve into %q, got %q (after)", idAfter, overwritePath, resolvedAfter)
}
}