From 1f52ae340c86574bb8c399f281844322d5efb739 Mon Sep 17 00:00:00 2001 From: Dominic Breuker Date: Wed, 14 Mar 2018 08:49:54 +0100 Subject: [PATCH] add tests and remove unused method in inotify --- internal/fswatcher/inotify/inotify.go | 12 ----- internal/psscanner/psscanner_test.go | 63 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 internal/psscanner/psscanner_test.go diff --git a/internal/fswatcher/inotify/inotify.go b/internal/fswatcher/inotify/inotify.go index f60b28d..476769d 100644 --- a/internal/fswatcher/inotify/inotify.go +++ b/internal/fswatcher/inotify/inotify.go @@ -130,18 +130,6 @@ func (i *Inotify) NumWatchers() int { return len(i.Watchers) } -func (i *Inotify) String() string { - if len(i.Watchers) < 20 { - dirs := make([]string, 0) - for _, w := range i.Watchers { - dirs = append(dirs, w.Dir) - } - return fmt.Sprintf("Watching: %v", dirs) - } else { - return fmt.Sprintf("Watching %d directories", len(i.Watchers)) - } -} - func getMaxWatchers() (int, error) { b, err := ioutil.ReadFile(maximumWatchersFile) if err != nil { diff --git a/internal/psscanner/psscanner_test.go b/internal/psscanner/psscanner_test.go new file mode 100644 index 0000000..7f2c44e --- /dev/null +++ b/internal/psscanner/psscanner_test.go @@ -0,0 +1,63 @@ +package psscanner + +import ( + "testing" + "time" +) + +const timeout = 100 * time.Millisecond + +// refresh + +func TestRun(t *testing.T) { + tests := []struct { + pids []int + events []string + }{ + {pids: []int{1, 2, 3}, events: []string{ + "UID=??? PID=3 | the-command", + "UID=??? PID=2 | the-command", + "UID=??? PID=1 | the-command", + }}, + } + + for _, tt := range tests { + restoreGetPIDs := mockPidList(tt.pids) + restoreCmdLineReader := mockCmdLineReader([]byte("the-command"), nil) + restoreProcStatusReader := mockProcStatusReader([]byte(""), nil) // don't mock read value since it's not worth it + + pss := NewPSScanner() + triggerCh := make(chan struct{}) + eventCh, errCh := pss.Run(triggerCh) + + // does nothing without triggering + select { + case e := <-eventCh: + t.Errorf("Received event before trigger: %s", e) + case err := <-errCh: + t.Errorf("Received error before trigger: %v", err) + case <-time.After(timeout): + // ok + } + + triggerCh <- struct{}{} + + // received event after the trigger + for i := 0; i < 3; i++ { + select { + case <-time.After(timeout): + t.Errorf("did not receive event in time") + case e := <-eventCh: + if e != tt.events[i] { + t.Errorf("Wrong event received: got '%s' but wanted '%s'", e, tt.events[i]) + } + case err := <-errCh: + t.Errorf("Received unexpected error: %v", err) + } + } + + restoreProcStatusReader() + restoreCmdLineReader() + restoreGetPIDs() + } +}