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.AddConfigPath("/etc/")
viper.AddConfigPath(".")
if strings.HasSuffix(os.Args[0], ".test") {
viper.AddConfigPath("../")
}
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
import (
"bytes"
"io"
"os"
"testing"
"github.com/sirupsen/logrus"
)
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 (
var (
version = "2.0.4"
)
lg = logrus.New()
)
output := captureOutput(func() {
ShowStatus(version)
})
if output != (version + "\n") {
t.Errorf("ShowStatus output not equal to version defined.")
func TestAgentId(t *testing.T) {
a := New(lg, version)
if a.AgentID == "" {
t.Error("AgentID not set")
} else {
t.Logf("AgentID: %s", a.AgentID)
}
}

View File

@ -13,14 +13,12 @@ package agent
import (
"log"
"os"
"strings"
"github.com/spf13/viper"
)
const (
etcConfig = "/etc/tacticalagent"
)
func (a *Agent) checkExistingAndRemove(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("meshdir", meshdir)
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 {
log.Fatalln("createAgentConfig", err)
}

View File

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