forked from cloudfoundry/gosigar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsnotify_test.go
345 lines (270 loc) · 6.49 KB
/
psnotify_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
// +build darwin freebsd linux netbsd openbsd
package psnotify
import (
"fmt"
"os"
"os/exec"
"runtime"
"sync"
"syscall"
"testing"
"time"
)
type anyEvent struct {
exits []int
forks []int
execs []int
errors []error
done chan bool
m sync.Mutex
}
func (a *anyEvent) append(eventType string, event interface{}) {
a.m.Lock()
defer a.m.Unlock()
switch eventType {
case "exits":
a.exits = append(a.exits, event.(int))
case "forks":
a.forks = append(a.forks, event.(int))
case "execs":
a.execs = append(a.execs, event.(int))
case "errors":
a.errors = append(a.errors, event.(error))
}
}
func (a *anyEvent) getExits() []int {
a.m.Lock()
defer a.m.Unlock()
r := make([]int, len(a.exits))
for i, v := range a.exits {
r[i] = v
}
return r
}
func (a *anyEvent) getForks() []int {
a.m.Lock()
defer a.m.Unlock()
r := make([]int, len(a.forks))
for i, v := range a.forks {
r[i] = v
}
return r
}
func (a *anyEvent) getExecs() []int {
a.m.Lock()
defer a.m.Unlock()
r := make([]int, len(a.execs))
for i, v := range a.execs {
r[i] = v
}
return r
}
func (a *anyEvent) getErrors() []error {
a.m.Lock()
defer a.m.Unlock()
r := make([]error, len(a.errors))
for i, v := range a.errors {
r[i] = v
}
return r
}
type testWatcher struct {
t *testing.T
watcher *Watcher
events *anyEvent
}
// General purpose Watcher wrapper for all tests
func newTestWatcher(t *testing.T) *testWatcher {
watcher, err := NewWatcher()
if err != nil {
t.Fatal(err)
}
events := &anyEvent{
done: make(chan bool, 1),
}
tw := &testWatcher{
t: t,
watcher: watcher,
events: events,
}
go func() {
for {
select {
case <-events.done:
return
case ev := <-watcher.Fork:
events.append("forks", ev.ParentPid)
case ev := <-watcher.Exec:
events.append("execs", ev.Pid)
case ev := <-watcher.Exit:
events.append("exits", ev.Pid)
case err := <-watcher.Error:
events.append("errors", err)
}
}
}()
return tw
}
func (tw *testWatcher) close() {
pause := 100 * time.Millisecond
time.Sleep(pause)
tw.events.done <- true
tw.watcher.Close()
time.Sleep(pause)
}
func skipTest(t *testing.T) bool {
if runtime.GOOS == "linux" && os.Getuid() != 0 {
fmt.Println("SKIP: test must be run as root on linux")
return true
}
return false
}
func startSleepCommand(t *testing.T) *exec.Cmd {
cmd := exec.Command("sh", "-c", "sleep 100")
if err := cmd.Start(); err != nil {
t.Error(err)
}
return cmd
}
func runCommand(t *testing.T, name string) *exec.Cmd {
cmd := exec.Command(name)
if err := cmd.Run(); err != nil {
t.Error(err)
}
return cmd
}
func expectEvents(t *testing.T, num int, name string, pids []int) bool {
if len(pids) != num {
t.Errorf("Expected %d %s events, got=%v", num, name, pids)
return false
}
return true
}
func expectEventPid(t *testing.T, name string, expect int, pid int) bool {
if expect != pid {
t.Errorf("Expected %s pid=%d, received=%d", name, expect, pid)
return false
}
return true
}
func TestWatchFork(t *testing.T) {
if skipTest(t) {
return
}
pid := os.Getpid()
tw := newTestWatcher(t)
// no watches added yet, so this fork event will no be captured
runCommand(t, "date")
// watch fork events for this process
if err := tw.watcher.Watch(pid, PROC_EVENT_FORK); err != nil {
t.Error(err)
}
// this fork event will be captured,
// the exec and exit events will not be captured
runCommand(t, "cal")
tw.close()
if expectEvents(t, 1, "forks", tw.events.getForks()) {
expectEventPid(t, "fork", pid, tw.events.getForks()[0])
}
expectEvents(t, 0, "execs", tw.events.getExecs())
expectEvents(t, 0, "exits", tw.events.getExits())
}
func TestWatchExit(t *testing.T) {
if skipTest(t) {
return
}
tw := newTestWatcher(t)
cmd := startSleepCommand(t)
childPid := cmd.Process.Pid
// watch for exit event of our child process
if err := tw.watcher.Watch(childPid, PROC_EVENT_EXIT); err != nil {
t.Error(err)
}
// kill our child process, triggers exit event
syscall.Kill(childPid, syscall.SIGTERM)
cmd.Wait()
tw.close()
expectEvents(t, 0, "forks", tw.events.getForks())
expectEvents(t, 0, "execs", tw.events.getExecs())
if expectEvents(t, 1, "exits", tw.events.getExits()) {
expectEventPid(t, "exit", childPid, tw.events.getExits()[0])
}
}
// combined version of TestWatchFork() and TestWatchExit()
func TestWatchForkAndExit(t *testing.T) {
if skipTest(t) {
return
}
pid := os.Getpid()
tw := newTestWatcher(t)
if err := tw.watcher.Watch(pid, PROC_EVENT_FORK); err != nil {
t.Error(err)
}
cmd := startSleepCommand(t)
childPid := cmd.Process.Pid
if err := tw.watcher.Watch(childPid, PROC_EVENT_EXIT); err != nil {
t.Error(err)
}
syscall.Kill(childPid, syscall.SIGTERM)
cmd.Wait()
tw.close()
if expectEvents(t, 1, "forks", tw.events.getForks()) {
expectEventPid(t, "fork", pid, tw.events.getForks()[0])
}
expectEvents(t, 0, "execs", tw.events.getExecs())
if expectEvents(t, 1, "exits", tw.events.getExits()) {
expectEventPid(t, "exit", childPid, tw.events.getExits()[0])
}
}
func TestWatchFollowFork(t *testing.T) {
if skipTest(t) {
return
}
// Darwin is not able to follow forks, as the kqueue fork event
// does not provide the child pid.
if runtime.GOOS != "linux" {
fmt.Println("SKIP: test follow forks is linux only")
return
}
pid := os.Getpid()
tw := newTestWatcher(t)
// watch for all process events related to this process
if err := tw.watcher.Watch(pid, PROC_EVENT_ALL); err != nil {
t.Error(err)
}
commands := []string{"date", "cal"}
childPids := make([]int, len(commands))
// triggers fork/exec/exit events for each command
for i, name := range commands {
cmd := runCommand(t, name)
childPids[i] = cmd.Process.Pid
}
// remove watch for this process
tw.watcher.RemoveWatch(pid)
// run commands again to make sure we don't receive any unwanted events
for _, name := range commands {
runCommand(t, name)
}
tw.close()
// run commands again to make sure nothing panics after
// closing the watcher
for _, name := range commands {
runCommand(t, name)
}
num := len(commands)
if expectEvents(t, num, "forks", tw.events.getForks()) {
for _, epid := range tw.events.getForks() {
expectEventPid(t, "fork", pid, epid)
}
}
if expectEvents(t, num, "execs", tw.events.getExecs()) {
for i, epid := range tw.events.getExecs() {
expectEventPid(t, "exec", childPids[i], epid)
}
}
if expectEvents(t, num, "exits", tw.events.getExits()) {
for i, epid := range tw.events.getExits() {
expectEventPid(t, "exit", childPids[i], epid)
}
}
}