diff --git a/internal/ftop/renderprocesses.go b/internal/ftop/renderprocesses.go index 8fece4d..2079692 100644 --- a/internal/ftop/renderprocesses.go +++ b/internal/ftop/renderprocesses.go @@ -6,6 +6,7 @@ import ( "github.com/walles/ftop/internal/processes" "github.com/walles/ftop/internal/themes" "github.com/walles/ftop/internal/ui" + "github.com/walles/ftop/internal/util" "github.com/walles/moor/v2/twin" ) @@ -305,7 +306,7 @@ func renderProcesses(screen twin.Screen, theme themes.Theme, x0, y0, x1, y1 int, userColumn0 := x0 + 1 + widths[0] + 1 + widths[1] // Screen column userColumnN := userColumn0 + widths[2] - 1 // Screen column - currentUsername := getCurrentUsername() + currentUsername := util.GetCurrentUsername() commandColumn0 := x0 + 1 + widths[0] + 1 // Screen column. x0 + 1 for left border, then PID column, then a space separator commandColumnN := commandColumn0 + widths[1] - 1 // Screen column diff --git a/internal/ftop/renderprocessesperuser.go b/internal/ftop/renderprocessesperuser.go index 15f28ae..c7d38f0 100644 --- a/internal/ftop/renderprocessesperuser.go +++ b/internal/ftop/renderprocessesperuser.go @@ -5,6 +5,7 @@ import ( "github.com/walles/ftop/internal/themes" "github.com/walles/ftop/internal/ui" + "github.com/walles/ftop/internal/util" "github.com/walles/moor/v2/twin" ) @@ -27,7 +28,7 @@ func renderPerUser(screen twin.Screen, theme themes.Theme, x0, y0, x1, y1 int, t usernameColumn0 := x0 + 1 // Screen column usernameColumnN := usernameColumn0 + widths[0] - 1 // Screen column - currentUsername := getCurrentUsername() + currentUsername := util.GetCurrentUsername() // If y0 = 0 and y1 = 1, then there would be 0 content rows between the // borders diff --git a/internal/processes/process_test.go b/internal/processes/process_test.go new file mode 100644 index 0000000..0748d17 --- /dev/null +++ b/internal/processes/process_test.go @@ -0,0 +1,80 @@ +package processes + +import ( + "os" + "strings" + "testing" + "time" + + "github.com/walles/ftop/internal/assert" + "github.com/walles/ftop/internal/util" +) + +const TEN_MB = 10 * 1024 * 1024 + +func TestGetAll(t *testing.T) { + procs, err := GetAll() + assert.Equal(t, err, nil) + assert.Equal(t, true, len(procs) > 0) + + var self *Process + for _, proc := range procs { + if proc.Pid == os.Getpid() { + self = proc + break + } + } + assert.Equal(t, false, self == nil) + + // Validate Pid field + assert.Equal(t, os.Getpid(), self.Pid) + + // Validate ppid field + assert.Equal(t, os.Getppid(), self.ppid) + + assert.Equal(t, true, self.Command == "processes.test" || strings.Contains(self.Command, "debug")) + assert.Equal(t, self.lowercaseCommand, strings.ToLower(self.Command)) + + // Validate Username field + assert.Equal(t, self.Username, util.GetCurrentUsername()) + + // Validate RssKb field + assert.Equal(t, true, self.RssKb > 0) + + // If this is not enough, feel free to increase it. But it should be low + // enough to catch unreasonable values. Locally on my machine I have seen + // 30MB. + assert.Equal(t, true, (self.RssKb*1024) < 5*TEN_MB) + + // If this is too little, feel free to increase it. But it should be low + // enough to catch unreasonable values. + assert.Equal(t, true, time.Since(self.startTime) < 30*time.Second) + + assert.Equal(t, false, self.CpuTime == nil) + assert.Equal(t, true, *self.CpuTime >= 0) + assert.Equal(t, true, *self.CpuTime < 30*time.Second) + + // Assume that only one test is running + assert.Equal(t, self.DeduplicationSuffix, "") + + assert.SlicesEqual(t, self.children, []*Process{}) + + assert.Equal(t, true, len(self.cmdline) > 0) + + assert.Equal(t, true, *self.cpuPercent >= 0) + + assert.Equal(t, true, *self.memoryPercent >= 0) + + // Trace parents all the way up... + proc := self + for proc.parent != nil { + proc = proc.parent + } + init := proc + + // ... and validate the init process. + assert.Equal(t, 1, init.Pid) + assert.Equal(t, 0, init.ppid) + assert.Equal(t, true, len(init.children) > 0) + assert.Equal(t, true, init.startTime.Before(self.startTime)) +} diff --git a/internal/ftop/util.go b/internal/util/username.go similarity index 84% rename from internal/ftop/util.go rename to internal/util/username.go index 04e84d5..bcb29b2 100644 --- a/internal/ftop/util.go +++ b/internal/util/username.go @@ -1,4 +1,4 @@ -package ftop +package util import ( "os/user" @@ -7,7 +7,7 @@ import ( ) // Or the empty string if lookup fails -func getCurrentUsername() string { +func GetCurrentUsername() string { currentUser, err := user.Current() if err != nil { log.Infof("Failed to look up current user: %v", err)