summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormatthieu <matthieu.cneude@gmail.com>2020-06-01 18:47:36 +0200
committermatthieu <matthieu.cneude@gmail.com>2020-06-01 18:47:36 +0200
commit533014054ecdc560f8a5a53044bfe9bd09891342 (patch)
tree4f0567fb7e568202b8b601d7c0f814f70545bba4
parentb43eb6ee0079e44b04ba38dd1b3db9fa83b0271b (diff)
A bit of first order function is great for testing ;)
-rw-r--r--internal/host_widget.go6
-rw-r--r--internal/host_widget_test.go30
-rw-r--r--internal/platform/host.go26
-rw-r--r--internal/platform/host_test.go50
4 files changed, 96 insertions, 16 deletions
diff --git a/internal/host_widget.go b/internal/host_widget.go
index 2a7ae4e..cea0c3c 100644
--- a/internal/host_widget.go
+++ b/internal/host_widget.go
@@ -119,13 +119,13 @@ func (ms *HostWidget) boxUptime(widget Widget) (f func() error, err error) {
title = widget.Options[optionTitle]
}
- uptime, err := ms.service.Uptime()
+ uptime, err := platform.HostUptime(ms.service.Runner)
if err != nil {
return nil, err
}
f = func() error {
- return ms.tui.AddTextBox(FormatSeconds(time.Duration(uptime)), title, widget.Options)
+ return ms.tui.AddTextBox(formatSeconds(time.Duration(uptime)), title, widget.Options)
}
return
@@ -358,7 +358,7 @@ func (ms *HostWidget) table(widget Widget) (f func() error, err error) {
return
}
-func FormatSeconds(dur time.Duration) string {
+func formatSeconds(dur time.Duration) string {
dur = dur - (dur % time.Second)
var days int
for dur.Hours() > 24.0 {
diff --git a/internal/host_widget_test.go b/internal/host_widget_test.go
new file mode 100644
index 0000000..e07827e
--- /dev/null
+++ b/internal/host_widget_test.go
@@ -0,0 +1,30 @@
+package internal
+
+import (
+ "testing"
+ "time"
+)
+
+func Test_formatSeconds(t *testing.T) {
+ testCases := []struct {
+ name string
+ expected string
+ duration time.Duration
+ }{
+ {
+ name: "happy case",
+ expected: "4h 46m 40s",
+ duration: time.Duration(17200210000000),
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ actual := formatSeconds(tc.duration)
+
+ if actual != tc.expected {
+ t.Errorf("Expected %v, actual %v", tc.expected, actual)
+ }
+ })
+ }
+}
diff --git a/internal/platform/host.go b/internal/platform/host.go
index 40daf7a..61ae195 100644
--- a/internal/platform/host.go
+++ b/internal/platform/host.go
@@ -46,7 +46,7 @@ func NewHost(username, addr string) (*Host, error) {
}
// Run a command on remote server via SSH or on localhost
-func (s *Host) run(command string) (string, error) {
+func (s *Host) Runner(command string) (string, error) {
if s.localhost {
return runLocalhost(command)
}
@@ -80,9 +80,9 @@ func runLocalhost(command string) (string, error) {
return string(out), nil
}
-func (s *Host) Uptime() (int64, error) {
+func HostUptime(runner func(cmd string) (string, error)) (int64, error) {
command := "/bin/cat /proc/uptime"
- uptime, err := s.run(command)
+ uptime, err := runner(command)
if err != nil {
return 0, err
}
@@ -103,7 +103,7 @@ func (s *Host) Uptime() (int64, error) {
func (s *Host) Load() (string, error) {
command := "/bin/cat /proc/loadavg"
- lines, err := s.run(command)
+ lines, err := s.Runner(command)
if err != nil {
return "", err
}
@@ -121,7 +121,7 @@ func (s *Host) Load() (string, error) {
func (s *Host) Processes() (string, error) {
command := "/bin/cat /proc/loadavg"
- lines, err := s.run(command)
+ lines, err := s.Runner(command)
if err != nil {
return "", err
}
@@ -144,7 +144,7 @@ func (s *Host) Processes() (string, error) {
}
func (s *Host) Memory(metrics []string, unit string) (val []int, err error) {
- lines, err := s.run("/bin/cat /proc/meminfo")
+ lines, err := s.Runner("/bin/cat /proc/meminfo")
if err != nil {
return nil, err
}
@@ -181,7 +181,7 @@ func (s *Host) Memory(metrics []string, unit string) (val []int, err error) {
}
func (s *Host) MemoryRate() (float64, error) {
- lines, err := s.run("/bin/cat /proc/meminfo")
+ lines, err := s.Runner("/bin/cat /proc/meminfo")
if err != nil {
return 0, err
}
@@ -218,7 +218,7 @@ func (s *Host) MemoryRate() (float64, error) {
// TODO to refactor - DRY
func (s *Host) SwapRate() (float64, error) {
- lines, err := s.run("/bin/cat /proc/meminfo")
+ lines, err := s.Runner("/bin/cat /proc/meminfo")
if err != nil {
return 0, err
}
@@ -255,7 +255,7 @@ func (s *Host) SwapRate() (float64, error) {
// See https://www.idnt.net/en-US/kb/941772
func (s *Host) CPURate() (float64, error) {
- raw, err := s.run("/bin/cat /proc/stat")
+ raw, err := s.Runner("/bin/cat /proc/stat")
if err != nil {
return 0, err
}
@@ -307,7 +307,7 @@ func (s *Host) CPURate() (float64, error) {
// GetNetStat returns net stat
func (s *Host) NetIO(unit string) (string, error) {
- lines, err := s.run("/bin/cat /proc/net/dev")
+ lines, err := s.Runner("/bin/cat /proc/net/dev")
if err != nil {
return "", err
}
@@ -341,7 +341,7 @@ func (s *Host) NetIO(unit string) (string, error) {
}
func (s *Host) Table(command string, headers []string) (cells [][]string, err error) {
- lines, err := s.run(command)
+ lines, err := s.Runner(command)
if err != nil {
return nil, err
}
@@ -373,7 +373,7 @@ func (s *Host) Table(command string, headers []string) (cells [][]string, err er
func (s *Host) Disk(headers []string, unit string) ([][]string, error) {
// GetIOStat returns io stat
- lines, err := s.run("/bin/df -x devtmpfs -x tmpfs -x debugfs")
+ lines, err := s.Runner("/bin/df -x devtmpfs -x tmpfs -x debugfs")
if err != nil {
return nil, nil
}
@@ -427,7 +427,7 @@ func (s *Host) Disk(headers []string, unit string) ([][]string, error) {
func (s *Host) DiskIO(unit string) (string, error) {
// GetIOStat returns io stat
- lines, err := s.run("/bin/cat /proc/diskstats")
+ lines, err := s.Runner("/bin/cat /proc/diskstats")
if err != nil {
return "", nil
}
diff --git a/internal/platform/host_test.go b/internal/platform/host_test.go
index 242f70b..664d9fc 100644
--- a/internal/platform/host_test.go
+++ b/internal/platform/host_test.go
@@ -3,6 +3,8 @@ package platform
import (
"reflect"
"testing"
+
+ "github.com/pkg/errors"
)
func Test_formatToTable(t *testing.T) {
@@ -68,3 +70,51 @@ func Test_formatToBar(t *testing.T) {
})
}
}
+
+func Test_HostUptime(t *testing.T) {
+ testCases := []struct {
+ name string
+ expected int64
+ runner func(cmd string) (string, error)
+ wantErr bool
+ }{
+ {
+ name: "happy case",
+ expected: 17200210000000,
+ runner: func(cmd string) (string, error) { return "17200.21 59425.48", nil },
+ wantErr: false,
+ },
+ {
+ name: "Empty result",
+ expected: 0,
+ runner: func(cmd string) (string, error) { return "", nil },
+ wantErr: true,
+ },
+ {
+ name: "Runner return error",
+ expected: 0,
+ runner: func(cmd string) (string, error) { return "", errors.New("Error!") },
+ wantErr: true,
+ },
+ {
+ name: "Runner return impossible number",
+ expected: 0,
+ runner: func(cmd string) (string, error) { return "hello", nil },
+ wantErr: true,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ actual, err := HostUptime(tc.runner)
+ if (err != nil) != tc.wantErr {
+ t.Errorf("Error '%v' even if wantErr is %t", err, tc.wantErr)
+ return
+ }
+
+ if tc.wantErr == false && actual != tc.expected {
+ t.Errorf("Expected %v, actual %v", tc.expected, actual)
+ }
+ })
+ }
+}