big refactor and testing

This commit is contained in:
redanthrax 2022-06-20 16:51:00 -07:00
parent a2ed11b2fb
commit d6b1b90034
26 changed files with 908 additions and 70 deletions

View file

@ -1,14 +0,0 @@
package disk
import (
"testing"
)
func TestGetDisks(t *testing.T) {
disks := GetDisks()
if len(disks) == 0 {
t.Fatalf("Could not get disks on linux system.")
}
t.Logf("Got %d disks on linux system", len(disks))
}

51
agent/disk/disk_test.go Normal file
View file

@ -0,0 +1,51 @@
package disk_test
import (
"errors"
"fmt"
"testing"
"github.com/amidaware/rmmagent/agent/disk"
)
func TestGetDisks(t *testing.T) {
exampleDisk := disk.Disk{
Device: "C:",
Fstype: "NTFS",
Total: "149.9 GB",
Used: "129.2 GB",
Free: "20.7 GB",
Percent: 86,
}
testTable := []struct {
name string
expected []disk.Disk
atLeast int
expectedError error
}{
{
name: "Get Disks",
expected: []disk.Disk{exampleDisk},
atLeast: 1,
expectedError: nil,
},
}
for _, tt := range testTable {
t.Run(tt.name, func(t *testing.T) {
result, err := disk.GetDisks()
if fmt.Sprintf("%T", result) != "[]disk.Disk" {
t.Errorf("expected type %T, got type %T", tt.expected, result)
}
if !errors.Is(err, tt.expectedError) {
t.Errorf("expected error (%v), got error(%v)", tt.expectedError, err)
}
if len(result) < 1 {
t.Errorf("expected count at least %d, got %d", tt.atLeast, len(result))
}
})
}
}

View file

@ -5,7 +5,6 @@ import (
"github.com/amidaware/rmmagent/agent/utils"
"github.com/shirou/gopsutil/disk"
trmm "github.com/wh1te909/trmm-shared"
"golang.org/x/sys/windows"
)
@ -14,12 +13,11 @@ var (
)
// GetDisks returns a list of fixed disks
func GetDisks() []trmm.Disk {
ret := make([]trmm.Disk, 0)
func GetDisks() ([]Disk, error) {
ret := make([]Disk, 0)
partitions, err := disk.Partitions(false)
if err != nil {
//a.Logger.Debugln(err)
return ret
return ret, err
}
for _, p := range partitions {
@ -36,7 +34,7 @@ func GetDisks() []trmm.Disk {
continue
}
d := trmm.Disk{
d := Disk{
Device: p.Device,
Fstype: p.Fstype,
Total: utils.ByteCountSI(usage.Total),
@ -44,7 +42,9 @@ func GetDisks() []trmm.Disk {
Free: utils.ByteCountSI(usage.Free),
Percent: int(usage.UsedPercent),
}
ret = append(ret, d)
}
return ret
}
return ret, err
}

10
agent/disk/structs.go Normal file
View file

@ -0,0 +1,10 @@
package disk
type Disk struct {
Device string `json:"device"`
Fstype string `json:"fstype"`
Total string `json:"total"`
Used string `json:"used"`
Free string `json:"free"`
Percent int `json:"percent"`
}