testing setup

This commit is contained in:
redanthrax 2022-06-15 18:26:10 -07:00
parent 7457bf0b93
commit 7c46970b67
7 changed files with 109 additions and 59 deletions

View File

@ -132,9 +132,6 @@ func NewAgentConfig() *rmm.AgentConfig {
viper.SetConfigType("json") viper.SetConfigType("json")
viper.AddConfigPath("/etc/") viper.AddConfigPath("/etc/")
viper.AddConfigPath(".") viper.AddConfigPath(".")
if strings.HasSuffix(os.Args[0], ".test") {
viper.AddConfigPath("../")
}
err := viper.ReadInConfig() err := viper.ReadInConfig()

45
agent/agent_linux_test.go Normal file
View File

@ -0,0 +1,45 @@
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.")
}
}
func TestOsString(t *testing.T) {
a := New(lg, version)
osString := a.osString()
if osString == "" {
t.Errorf("Could not get OS String")
} else {
t.Logf("Got OS String: %s", osString)
}
}

View File

@ -1,35 +1,20 @@
package agent package agent
import ( import (
"bytes"
"io"
"os"
"testing" "testing"
"github.com/sirupsen/logrus"
) )
func captureOutput(f func()) string { var (
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" version = "2.0.4"
) lg = logrus.New()
)
output := captureOutput(func() { func TestAgentId(t *testing.T) {
ShowStatus(version) a := New(lg, version)
}) if a.AgentID == "" {
t.Error("AgentID not set")
if output != (version + "\n") { } else {
t.Errorf("ShowStatus output not equal to version defined.") t.Logf("AgentID: %s", a.AgentID)
} }
} }

View File

@ -13,14 +13,12 @@ package agent
import ( import (
"log" "log"
"os"
"strings"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
const (
etcConfig = "/etc/tacticalagent"
)
func (a *Agent) checkExistingAndRemove(silent bool) {} func (a *Agent) checkExistingAndRemove(silent bool) {}
func (a *Agent) installerMsg(msg, alert string, silent bool) { func (a *Agent) installerMsg(msg, alert string, silent bool) {
@ -42,7 +40,13 @@ func createAgentConfig(baseurl, agentid, apiurl, token, agentpk, cert, proxy, me
viper.Set("proxy", proxy) viper.Set("proxy", proxy)
viper.Set("meshdir", meshdir) viper.Set("meshdir", meshdir)
viper.SetConfigPermissions(0660) viper.SetConfigPermissions(0660)
err := viper.SafeWriteConfigAs(etcConfig) configLocation := "/etc/tacticalagent"
if strings.HasSuffix(os.Args[0], ".test") {
configLocation = "tacticalagent"
}
err := viper.SafeWriteConfigAs(configLocation)
if err != nil { if err != nil {
log.Fatalln("createAgentConfig", err) log.Fatalln("createAgentConfig", err)
} }

View File

@ -4,7 +4,6 @@ import (
"testing" "testing"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
"strconv"
) )
func TestInstall(t *testing.T) { func TestInstall(t *testing.T) {
@ -18,31 +17,26 @@ func TestInstall(t *testing.T) {
viper.SetConfigName("testargs.json") viper.SetConfigName("testargs.json")
viper.SetConfigType("json") viper.SetConfigType("json")
viper.AddConfigPath(".") viper.AddConfigPath(".")
viper.ReadInConfig()
cid, err := strconv.Atoi(viper.GetString("clientid"))
if err != nil {
cid = 0
}
installer := Installer { installer := Installer {
RMM: viper.GetString("api"), RMM: viper.GetString("api"),
ClientID: cid, ClientID: viper.GetInt("clientid"),
SiteID: *siteID, SiteID: viper.GetInt("siteid"),
Description: *desc, Description: viper.GetString("description"),
AgentType: *atype, AgentType: viper.GetString("agenttype"),
Power: *power, Power: viper.GetBool("power"),
RDP: *rdp, RDP: viper.GetBool("rdp"),
Ping: *ping, Ping: viper.GetBool("ping"),
Token: *token, Token: viper.GetString("token"),
LocalMesh: *localMesh, LocalMesh: viper.GetString("localmesh"),
Cert: *cert, Cert: viper.GetString("cert"),
Proxy: *proxy, Proxy: viper.GetString("proxy"),
Timeout: *timeout, Timeout: viper.GetDuration("timeout"),
Silent: *silent, Silent: viper.GetBool("silent"),
NoMesh: *noMesh, NoMesh: viper.GetBool("nomesh"),
MeshDir: *meshDir, MeshDir: viper.GetString("meshdir"),
MeshNodeID: *meshNodeID, MeshNodeID: viper.GetString("meshnodeid"),
} }
a.Install(&installer) a.Install(&installer)

10
agent/rpc_test.go Normal file
View File

@ -0,0 +1,10 @@
package agent
import (
"testing"
)
func TestRunRPC(t *testing.T) {
a := New(lg, version)
a.RunRPC()
}

View File

@ -1,4 +1,19 @@
{ {
"api": "https://api.hothcorp.com:8000", "api": "http://api.hothcorp.com:8000",
"clientid": 1,
"siteid": 1,
"description": "",
"agenttype": "workstation",
"power": false,
"rdp": false,
"ping": false,
"token": "ffcc2c22de2aaf3870623c6a2622c06faa56b7d74f80e6c3dd5d24da36a80aaa",
"localmesh": "",
"cert": "",
"proxy": "",
"timeout": 30,
"silent": true,
"nomesh": true,
"meshdir": "",
"meshnodeid": ""
} }