github.com/vishvananda/netlink@v1.3.0/proc_event_test.go (about) 1 // +build linux 2 3 package netlink 4 5 import ( 6 "github.com/vishvananda/netns" 7 "os" 8 "os/exec" 9 "runtime" 10 "testing" 11 ) 12 13 func TestSubscribeProcEvent(t *testing.T) { 14 skipUnlessRoot(t) 15 runtime.LockOSThread() 16 defer runtime.UnlockOSThread() 17 18 pid1ns, err := netns.GetFromPid(1) 19 if err != nil { 20 panic(err) 21 } 22 23 err = netns.Set(pid1ns) 24 if err != nil { 25 panic(err) 26 } 27 28 ch := make(chan ProcEvent) 29 done := make(chan struct{}) 30 defer close(done) 31 32 errChan := make(chan error) 33 34 if err := ProcEventMonitor(ch, done, errChan); err != nil { 35 t.Fatal(err) 36 } 37 38 cmd := exec.Command("false") 39 if err := cmd.Start(); err != nil { 40 t.Fatal(err) 41 } 42 43 // first we wait for proc - i.e. childTgid is cmd.Process.Pid 44 for { 45 e := <-ch 46 t.Logf("pid: %+v e: %+v", os.Getpid(), e) 47 if e.Msg.Tgid() == uint32(os.Getpid()) { 48 if forkEvent, ok := e.Msg.(*ForkProcEvent); ok { 49 if forkEvent.ChildTgid == uint32(cmd.Process.Pid) { 50 break 51 } 52 } 53 } 54 } 55 56 // wait for exec event 57 for { 58 e := <-ch 59 if e.Msg.Tgid() == uint32(cmd.Process.Pid) { 60 if _, ok := e.Msg.(*ExecProcEvent); ok { 61 break 62 } 63 } 64 } 65 66 cmd.Wait() 67 for { 68 e := <-ch 69 if e.Msg.Tgid() == uint32(cmd.Process.Pid) { 70 if exitEvent, ok := e.Msg.(*ExitProcEvent); ok { 71 if exitEvent.ExitCode != 256 { 72 t.Errorf("Expected error code 256 (-1), but got %+v", exitEvent) 73 } 74 break 75 } 76 } 77 } 78 79 done <- struct{}{} 80 }