func TestLinuxConnections(t *testing.T) { fs_hook.Mock(mockFS) defer fs_hook.Restore() scanner := NewConnectionScanner(process.NewWalker("/proc")) defer scanner.Stop() // let the background scanner finish its first pass time.Sleep(1 * time.Second) iter, err := scanner.Connections(true) if err != nil { t.Fatal(err) } have := iter.Next() want := &Connection{ LocalAddress: net.ParseIP("0.0.0.0").To4(), LocalPort: 42688, RemoteAddress: net.ParseIP("0.0.0.0").To4(), RemotePort: 0, inode: 5107, Proc: Proc{ PID: 1, Name: "foo", }, } if !reflect.DeepEqual(want, have) { t.Fatal(test.Diff(want, have)) } if have := iter.Next(); have != nil { t.Fatal(have) } }
func TestLinuxConnections(t *testing.T) { fs_hook.Mock(mockFS) defer fs_hook.Restore() iter, err := cbConnections(true, process.NewWalker("/proc")) if err != nil { t.Fatal(err) } have := iter.Next() want := &Connection{ LocalAddress: net.ParseIP("0.0.0.0").To4(), LocalPort: 42688, RemoteAddress: net.ParseIP("0.0.0.0").To4(), RemotePort: 0, inode: 5107, Proc: Proc{ PID: 1, Name: "foo", }, } if !reflect.DeepEqual(want, have) { t.Fatal(test.Diff(want, have)) } if have := iter.Next(); have != nil { t.Fatal(have) } }
func setup(t *testing.T, sockets ...fs.Entry) fs.Entry { mockFS := fs.Dir("", fs.Dir("plugins", sockets...)) fs_hook.Mock( mockFS) stubTransport(func(socket string, timeout time.Duration) (http.RoundTripper, error) { f, err := mockFS.Open(socket) return readWriteCloseRoundTripper{f}, err }) return mockFS }
func TestWalkProcPid(t *testing.T) { fs_hook.Mock(mockFS) defer fs_hook.Restore() buf := bytes.Buffer{} have, err := walkProcPid(&buf, process.NewWalker(procRoot)) if err != nil { t.Fatal(err) } want := map[uint64]*Proc{ 5107: { PID: 1, Name: "foo", }, } if !reflect.DeepEqual(want, have) { t.Fatalf("%+v", have) } }
func TestWalker(t *testing.T) { fs_hook.Mock(mockFS) defer fs_hook.Restore() want := map[int]process.Process{ 3: {PID: 3, PPID: 2, Name: "curl", Cmdline: "curl google.com", Threads: 1}, 2: {PID: 2, PPID: 1, Name: "bash", Cmdline: "bash", Threads: 1}, 4: {PID: 4, PPID: 3, Name: "apache", Cmdline: "apache", Threads: 1}, 1: {PID: 1, PPID: 0, Name: "init", Cmdline: "init", Threads: 1}, } have := map[int]process.Process{} walker := process.NewWalker("/proc") err := walker.Walk(func(p, _ process.Process) { have[p.PID] = p }) if err != nil || !reflect.DeepEqual(want, have) { t.Errorf("%v (%v)", test.Diff(want, have), err) } }
func TestWalkProcPid(t *testing.T) { fs_hook.Mock(mockFS) defer fs_hook.Restore() buf := bytes.Buffer{} walker := process.NewWalker(procRoot) ticker := time.NewTicker(time.Millisecond) defer ticker.Stop() pWalker := newPidWalker(walker, ticker.C, 1) have, err := pWalker.walk(&buf) if err != nil { t.Fatal(err) } want := map[uint64]*Proc{ 5107: { PID: 1, Name: "foo", }, } if !reflect.DeepEqual(want, have) { t.Fatalf("%+v", have) } }