-
Notifications
You must be signed in to change notification settings - Fork 748
/
proc_event_test.go
80 lines (67 loc) · 1.36 KB
/
proc_event_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
// +build linux
package netlink
import (
"github.com/vishvananda/netns"
"os"
"os/exec"
"runtime"
"testing"
)
func TestSubscribeProcEvent(t *testing.T) {
skipUnlessRoot(t)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
pid1ns, err := netns.GetFromPid(1)
if err != nil {
panic(err)
}
err = netns.Set(pid1ns)
if err != nil {
panic(err)
}
ch := make(chan ProcEvent)
done := make(chan struct{})
defer close(done)
errChan := make(chan error)
if err := ProcEventMonitor(ch, done, errChan); err != nil {
t.Fatal(err)
}
cmd := exec.Command("false")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
// first we wait for proc - i.e. childTgid is cmd.Process.Pid
for {
e := <-ch
t.Logf("pid: %+v e: %+v", os.Getpid(), e)
if e.Msg.Tgid() == uint32(os.Getpid()) {
if forkEvent, ok := e.Msg.(*ForkProcEvent); ok {
if forkEvent.ChildTgid == uint32(cmd.Process.Pid) {
break
}
}
}
}
// wait for exec event
for {
e := <-ch
if e.Msg.Tgid() == uint32(cmd.Process.Pid) {
if _, ok := e.Msg.(*ExecProcEvent); ok {
break
}
}
}
cmd.Wait()
for {
e := <-ch
if e.Msg.Tgid() == uint32(cmd.Process.Pid) {
if exitEvent, ok := e.Msg.(*ExitProcEvent); ok {
if exitEvent.ExitCode != 256 {
t.Errorf("Expected error code 256 (-1), but got %+v", exitEvent)
}
break
}
}
}
done <- struct{}{}
}