Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/ftop/renderprocesses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/ftop/renderprocessesperuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down
80 changes: 80 additions & 0 deletions internal/processes/process_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
4 changes: 2 additions & 2 deletions internal/ftop/util.go → internal/util/username.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ftop
package util

import (
"os/user"
Expand All @@ -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)
Expand Down