From 7457bf0b935419132a715328ee290fc2766810f0 Mon Sep 17 00:00:00 2001 From: redanthrax Date: Wed, 15 Jun 2022 16:37:35 -0700 Subject: [PATCH] testing for install - in progress --- .vscode/launch.json | 2 +- README.md | 5 +++++ agent/agent_linux.go | 4 ++++ agent/agent_test.go | 35 +++++++++++++++++++++++++++++++ agent/install_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++ agent/rpc.go | 2 +- agent/testargs.json | 4 ++++ 7 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 agent/agent_test.go create mode 100644 agent/install_test.go create mode 100644 agent/testargs.json diff --git a/.vscode/launch.json b/.vscode/launch.json index da5b13e..432fcfd 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "mode": "debug", "env": {}, - "args": ["-m", "svc", "-logto", "stdout"], + "args": ["-m", "svc", "-log", "DEBUG", "-logto", "stdout"], "buildFlags": "-tags=DEBUG", "program": "${workspaceRoot}" } diff --git a/README.md b/README.md index d065784..e5f2642 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,9 @@ https://github.com/amidaware/tacticalrmm env CGO_ENABLED=0 GOOS= GOARCH= go build -ldflags "-s -w" ``` +### tests +Navigate to agent directory +``` +go test -vet=off +``` \ No newline at end of file diff --git a/agent/agent_linux.go b/agent/agent_linux.go index 2b13410..762ce72 100644 --- a/agent/agent_linux.go +++ b/agent/agent_linux.go @@ -132,6 +132,10 @@ func NewAgentConfig() *rmm.AgentConfig { viper.SetConfigType("json") viper.AddConfigPath("/etc/") viper.AddConfigPath(".") + if strings.HasSuffix(os.Args[0], ".test") { + viper.AddConfigPath("../") + } + err := viper.ReadInConfig() if err != nil { diff --git a/agent/agent_test.go b/agent/agent_test.go new file mode 100644 index 0000000..fad5fa0 --- /dev/null +++ b/agent/agent_test.go @@ -0,0 +1,35 @@ +package agent + +import ( + "bytes" + "io" + "os" + "testing" +) + +func captureOutput(f func()) string { + old := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + f() + w.Close() + os.Stdout = old + + var buf bytes.Buffer + io.Copy(&buf, r) + return buf.String() +} + +func TestShowStatus(t *testing.T) { + var ( + version = "2.0.4" + ) + + output := captureOutput(func() { + ShowStatus(version) + }) + + if output != (version + "\n") { + t.Errorf("ShowStatus output not equal to version defined.") + } +} \ No newline at end of file diff --git a/agent/install_test.go b/agent/install_test.go new file mode 100644 index 0000000..2248440 --- /dev/null +++ b/agent/install_test.go @@ -0,0 +1,49 @@ +package agent + +import ( + "testing" + "github.com/sirupsen/logrus" + "github.com/spf13/viper" + "strconv" +) + +func TestInstall(t *testing.T) { + var ( + version = "2.0.4" + log = logrus.New() + ) + + a := New(log, version) + + viper.SetConfigName("testargs.json") + viper.SetConfigType("json") + viper.AddConfigPath(".") + + cid, err := strconv.Atoi(viper.GetString("clientid")) + + if err != nil { + cid = 0 + } + + installer := Installer { + RMM: viper.GetString("api"), + ClientID: cid, + SiteID: *siteID, + Description: *desc, + AgentType: *atype, + Power: *power, + RDP: *rdp, + Ping: *ping, + Token: *token, + LocalMesh: *localMesh, + Cert: *cert, + Proxy: *proxy, + Timeout: *timeout, + Silent: *silent, + NoMesh: *noMesh, + MeshDir: *meshDir, + MeshNodeID: *meshNodeID, + } + + a.Install(&installer) +} diff --git a/agent/rpc.go b/agent/rpc.go index 79f479f..2990135 100644 --- a/agent/rpc.go +++ b/agent/rpc.go @@ -50,7 +50,7 @@ var ( func (a *Agent) RunRPC() { if rmm.DEBUG { - a.Logger.Infoln("DEBUG MODE") + a.Logger.Infoln("DEBUG BUILD STARTED") } a.Logger.Infoln("Agent service started") diff --git a/agent/testargs.json b/agent/testargs.json new file mode 100644 index 0000000..38f27da --- /dev/null +++ b/agent/testargs.json @@ -0,0 +1,4 @@ +{ + "api": "https://api.hothcorp.com:8000", + +} \ No newline at end of file