tests and api fix
This commit is contained in:
parent
c36ee74c5a
commit
66aca05028
5 changed files with 118 additions and 22 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package choco
|
package choco
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/amidaware/rmmagent/agent/system"
|
"github.com/amidaware/rmmagent/agent/system"
|
||||||
|
|
@ -9,7 +10,7 @@ import (
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InstallChoco() {
|
func InstallChoco() error {
|
||||||
config := config.NewAgentConfig()
|
config := config.NewAgentConfig()
|
||||||
var result ChocoInstalled
|
var result ChocoInstalled
|
||||||
result.AgentID = config.AgentID
|
result.AgentID = config.AgentID
|
||||||
|
|
@ -25,27 +26,30 @@ func InstallChoco() {
|
||||||
r, err := rClient.R().Get("https://chocolatey.org/install.ps1")
|
r, err := rClient.R().Get("https://chocolatey.org/install.ps1")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.PostPayload(result, url)
|
api.PostPayload(result, url)
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.IsError() {
|
if r.IsError() {
|
||||||
api.PostPayload(result, url)
|
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 {
|
if err != nil {
|
||||||
api.PostPayload(result, url)
|
api.PostPayload(result, url)
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if exitcode != 0 {
|
if exitcode != 0 {
|
||||||
api.PostPayload(result, url)
|
api.PostPayload(result, url)
|
||||||
return
|
return fmt.Errorf("exit code: %d", exitcode)
|
||||||
}
|
}
|
||||||
|
|
||||||
result.Installed = true
|
result.Installed = true
|
||||||
api.PostPayload(result, url)
|
err = api.PostPayload(result, url)
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func InstallWithChoco(name string) (string, error) {
|
func InstallWithChoco(name string) (string, error) {
|
||||||
59
agent/choco/choco_windows_test.go
Normal file
59
agent/choco/choco_windows_test.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,15 +9,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetDisks(t *testing.T) {
|
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 {
|
testTable := []struct {
|
||||||
name string
|
name string
|
||||||
expected []disk.Disk
|
expected []disk.Disk
|
||||||
|
|
@ -26,7 +17,7 @@ func TestGetDisks(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Get Disks",
|
name: "Get Disks",
|
||||||
expected: []disk.Disk{exampleDisk},
|
expected: []disk.Disk{},
|
||||||
atLeast: 1,
|
atLeast: 1,
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
30
agent/network/network_test.go
Normal file
30
agent/network/network_test.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var restyC resty.Client
|
var restyC *resty.Client
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
ac := config.NewAgentConfig()
|
ac := config.NewAgentConfig()
|
||||||
|
|
@ -19,7 +19,7 @@ func init() {
|
||||||
headers["Authorization"] = fmt.Sprintf("Token %s", ac.Token)
|
headers["Authorization"] = fmt.Sprintf("Token %s", ac.Token)
|
||||||
}
|
}
|
||||||
|
|
||||||
restyC := resty.New()
|
restyC = resty.New()
|
||||||
restyC.SetBaseURL(ac.BaseURL)
|
restyC.SetBaseURL(ac.BaseURL)
|
||||||
restyC.SetCloseConnection(true)
|
restyC.SetCloseConnection(true)
|
||||||
restyC.SetHeaders(headers)
|
restyC.SetHeaders(headers)
|
||||||
|
|
@ -37,7 +37,11 @@ func init() {
|
||||||
|
|
||||||
func PostPayload(payload interface{}, url string) error {
|
func PostPayload(payload interface{}, url string) error {
|
||||||
_, err := restyC.R().SetBody(payload).Post("/api/v3/syncmesh/")
|
_, 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) {
|
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 {
|
func Patch(payload interface{}, url string) error {
|
||||||
_, err := restyC.R().SetBody(payload).Patch(url)
|
_, err := restyC.R().SetBody(payload).Patch(url)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Put(payload interface{}, url string) error {
|
func Put(payload interface{}, url string) error {
|
||||||
_, err := restyC.R().SetBody(payload).Put(url)
|
_, err := restyC.R().SetBody(payload).Put(url)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue