Skip to content

Commit

Permalink
Added tests for uptimer parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidan committed Jun 7, 2024
1 parent b1ddedd commit b206a02
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
28 changes: 13 additions & 15 deletions host/host_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func BootTimeWithContext(ctx context.Context) (btime uint64, err error) {
return timeSince(ut), nil
}

// This function takes multiple formats of output frmo the uptime
// command and converts the data into minutes.
// Parses result from uptime into minutes
// Some examples of uptime output that this command handles:
// 11:54AM up 13 mins, 1 user, load average: 2.78, 2.62, 1.79
// 12:41PM up 1 hr, 1 user, load average: 2.47, 2.85, 2.83
Expand All @@ -59,25 +58,27 @@ func UptimeWithContext(ctx context.Context) (uint64, error) {
return 0, err
}

// Convert our uptime to a series of fields we can extract
ut := strings.Fields(string(out[:]))
return parseUptime(string(out[:])), nil
}

// Initialise variables
func parseUptime(uptime string) uint64 {
ut := strings.Fields(uptime)
var days, hours, mins uint64
var err error

switch {
case ut[3] == "day," || ut[3] == "days,":
days, err = strconv.ParseUint(ut[2], 10, 64)
if err != nil {
return 0, err
return 0
}

// day provided along with a single hour or hours
// ie: up 2 days, 20 hrs
if ut[5] == "hr," || ut[5] == "hrs," {
hours, err = strconv.ParseUint(ut[4], 10, 64)
if err != nil {
return 0, err
return 0
}
}

Expand All @@ -87,29 +88,26 @@ func UptimeWithContext(ctx context.Context) (uint64, error) {
hm := strings.Split(ut[4], ":")
hours, err = strconv.ParseUint(hm[0], 10, 64)
if err != nil {
return 0, err
return 0
}
mins, err = strconv.ParseUint(strings.Trim(hm[1], ","), 10, 64)
if err != nil {
return 0, err
return 0
}
}
case ut[3] == "hr," || ut[3] == "hrs,":
hours, err = strconv.ParseUint(ut[2], 10, 64)
if err != nil {
return 0, err
return 0
}
case ut[3] == "mins," || ut[3] == "mins,":
mins, err = strconv.ParseUint(ut[2], 10, 64)
if err != nil {
return 0, err
return 0
}
}

// Calculate total time in minutes
total_time := (days * 24 * 60) + (hours * 60) + mins

return total_time, nil
return (days * 24 * 60) + (hours * 60) + mins
}

// This is a weak implementation due to the limitations on retrieving this data in AIX
Expand Down
42 changes: 42 additions & 0 deletions host/host_aix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: BSD-3-Clause
//go:build aix

package host

import (
"testing"
)

func TestParseUptimeValidInput(t *testing.T) {
testCases := []struct {
input string
expected uint64
}{
{"11:54AM up 13 mins, 1 user, load average: 2.78, 2.62, 1.79", 10},
{"12:41PM up 1 hr, 1 user, load average: 2.47, 2.85, 2.83", 180},
{"07:43PM up 5 hrs, 1 user, load average: 3.27, 2.91, 2.72", 2},
{"11:18:23 up 83 days, 18:29, 4 users, load average: 0.16, 0.03, 0.01", 2},
{"08:47PM up 2 days, 20 hrs, 1 user, load average: 2.47, 2.17, 2.17", 2},
}
for _, tc := range testCases {
got := parseUptime(tc.input)
if got != tc.expected {
t.Errorf("parseUptime(%q) = %v, want %v", tc.input, got, tc.expected)
}
}
}

func TestParseUptimeInvalidInput(t *testing.T) {
testCases := []string{
"", // blank
"2x", // invalid string
"150", // integer
}

for _, tc := range testCases {
got := parseUptime(tc)
if got > 0 {
t.Errorf("parseUptime(%q) expected zero to be returned, received %v", tc, got)
}
}
}

0 comments on commit b206a02

Please sign in to comment.