From 66aca050282aab658aa3fa27bcf11687e11ae62c Mon Sep 17 00:00:00 2001 From: redanthrax Date: Tue, 21 Jun 2022 17:05:26 -0700 Subject: [PATCH] tests and api fix --- agent/choco/{choco.go => choco_windows.go} | 18 ++++--- agent/choco/choco_windows_test.go | 59 ++++++++++++++++++++++ agent/disk/disk_test.go | 11 +--- agent/network/network_test.go | 30 +++++++++++ agent/tactical/api/api.go | 22 ++++++-- 5 files changed, 118 insertions(+), 22 deletions(-) rename agent/choco/{choco.go => choco_windows.go} (76%) create mode 100644 agent/choco/choco_windows_test.go create mode 100644 agent/network/network_test.go diff --git a/agent/choco/choco.go b/agent/choco/choco_windows.go similarity index 76% rename from agent/choco/choco.go rename to agent/choco/choco_windows.go index 92c48ce..20bc992 100644 --- a/agent/choco/choco.go +++ b/agent/choco/choco_windows.go @@ -1,6 +1,7 @@ package choco import ( + "fmt" "time" "github.com/amidaware/rmmagent/agent/system" @@ -9,7 +10,7 @@ import ( "github.com/go-resty/resty/v2" ) -func InstallChoco() { +func InstallChoco() error { config := config.NewAgentConfig() var result ChocoInstalled result.AgentID = config.AgentID @@ -25,27 +26,30 @@ func InstallChoco() { r, err := rClient.R().Get("https://chocolatey.org/install.ps1") if err != nil { api.PostPayload(result, url) - return + return err } if r.IsError() { api.PostPayload(result, url) - return + return fmt.Errorf("response code: %d", r.StatusCode()) } - _, _, exitcode, err := system.RunScript(string(r.Body()), "powershell", []string{}, 900) + installScript := string(r.Body()) + _, _, exitcode, err := system.RunScript(installScript, "powershell", []string{}, 900) if err != nil { api.PostPayload(result, url) - return + return err } if exitcode != 0 { api.PostPayload(result, url) - return + return fmt.Errorf("exit code: %d", exitcode) } result.Installed = true - api.PostPayload(result, url) + err = api.PostPayload(result, url) + + return err } func InstallWithChoco(name string) (string, error) { diff --git a/agent/choco/choco_windows_test.go b/agent/choco/choco_windows_test.go new file mode 100644 index 0000000..e7bd5ea --- /dev/null +++ b/agent/choco/choco_windows_test.go @@ -0,0 +1,59 @@ +package choco_test + +import ( + "errors" + "strings" + "testing" + + "github.com/amidaware/rmmagent/agent/choco" +) + +func TestInstallChoco(t *testing.T) { + testTable := []struct { + name string + expectedError error + }{ + { + name: "Install Choco", + expectedError: nil, + }, + } + + for _, tt := range testTable { + t.Run(tt.name, func(t *testing.T) { + err := choco.InstallChoco() + if !errors.Is(tt.expectedError, err) { + t.Errorf("expected error (%v), got (%v)", tt.expectedError, err) + } + }) + } +} + +func TestInstallWithChoco(t *testing.T) { + testTable := []struct { + name string + software string + expectedString string + expectedError error + }{ + { + name: "Install With Choco", + software: "adobereader", + expectedString: "The install of adobereader was successful", + expectedError: nil, + }, + } + + for _, tt := range testTable { + t.Run(tt.name, func(t *testing.T) { + result, err := choco.InstallWithChoco(tt.software) + if !errors.Is(tt.expectedError, err) { + t.Errorf("expected (%v), got (%v)", tt.expectedError, err) + } + + if !strings.Contains(result, tt.expectedString) { + t.Errorf("expected %s, got %s", tt.expectedString, result) + } + }) + } +} diff --git a/agent/disk/disk_test.go b/agent/disk/disk_test.go index feb37ce..b03e591 100644 --- a/agent/disk/disk_test.go +++ b/agent/disk/disk_test.go @@ -9,15 +9,6 @@ import ( ) 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 @@ -26,7 +17,7 @@ func TestGetDisks(t *testing.T) { }{ { name: "Get Disks", - expected: []disk.Disk{exampleDisk}, + expected: []disk.Disk{}, atLeast: 1, expectedError: nil, }, diff --git a/agent/network/network_test.go b/agent/network/network_test.go new file mode 100644 index 0000000..1391ecc --- /dev/null +++ b/agent/network/network_test.go @@ -0,0 +1,30 @@ +package network_test + +import ( + "testing" + + "github.com/amidaware/rmmagent/agent/network" +) + +func TestPublicIP(t *testing.T) { + testTable := []struct { + name string + expected string + proxy string + }{ + { + name: "Get Public IP", + expected: network.PublicIP(""), + proxy: "", + }, + } + + for _, tt := range testTable { + t.Run(tt.name, func(t *testing.T) { + result := network.PublicIP(tt.proxy) + if result != tt.expected { + t.Errorf("expected %s, got %s", tt.expected, result) + } + }) + } +} diff --git a/agent/tactical/api/api.go b/agent/tactical/api/api.go index bed3496..be69364 100644 --- a/agent/tactical/api/api.go +++ b/agent/tactical/api/api.go @@ -9,7 +9,7 @@ import ( "github.com/go-resty/resty/v2" ) -var restyC resty.Client +var restyC *resty.Client func init() { ac := config.NewAgentConfig() @@ -19,7 +19,7 @@ func init() { headers["Authorization"] = fmt.Sprintf("Token %s", ac.Token) } - restyC := resty.New() + restyC = resty.New() restyC.SetBaseURL(ac.BaseURL) restyC.SetCloseConnection(true) restyC.SetHeaders(headers) @@ -37,7 +37,11 @@ func init() { func PostPayload(payload interface{}, url string) error { _, err := restyC.R().SetBody(payload).Post("/api/v3/syncmesh/") - return err + if err != nil { + return err + } + + return nil } func GetResult(result interface{}, url string) (*resty.Response, error) { @@ -60,10 +64,18 @@ func Get(url string) (*resty.Response, error) { func Patch(payload interface{}, url string) error { _, err := restyC.R().SetBody(payload).Patch(url) - return err + if err != nil { + return err + } + + return nil } func Put(payload interface{}, url string) error { _, err := restyC.R().SetBody(payload).Put(url) - return err + if err != nil { + return err + } + + return nil }