updated tests and methods

This commit is contained in:
redanthrax 2022-06-23 16:04:48 -07:00
parent de8e795254
commit da1e250ce9
8 changed files with 94 additions and 40 deletions

View file

@ -3,8 +3,6 @@ https://github.com/amidaware/tacticalrmm
#### building the agent - linux
```
go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo
go generate
env CGO_ENABLED=0 GOOS=<GOOS> GOARCH=<GOARCH> go build -ldflags "-s -w -X 'main.version=v2.0.4'"
example: env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -X 'main.version=v2.0.4' -o build/output/rmmagent"
```

View file

@ -7,7 +7,6 @@ import (
"bufio"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"
@ -287,21 +286,4 @@ func CMDShell(shell string, cmdArgs []string, command string, timeout int, detac
func CMD(exe string, args []string, timeout int, detached bool) (output [2]string, e error) {
return [2]string{"", ""}, nil
}
func GetPythonBin() string {
opts := NewCMDOpts()
opts.Command = "which python"
out := CmdV2(opts)
return out.Stdout
}
func GetProgramDirectory() string {
pd := filepath.Join(os.Getenv("ProgramFiles"), ProgFilesName)
return pd
}
func GetProgramBin() string {
exe := filepath.Join(GetProgramDirectory(), winExeName)
return exe
}

View file

@ -0,0 +1,16 @@
package shared_test
import (
"testing"
"github.com/amidaware/rmmagent/agent/tactical/shared"
)
func TestGetPythonBin(t *testing.T) {
pybin := shared.GetPythonBin()
if pybin == "" {
t.Errorf("expected path, got %s", pybin)
}
t.Logf("result: %s", pybin)
}

View file

@ -3,6 +3,34 @@
package shared
import (
"os/exec"
"path/filepath"
"strings"
)
const (
binName = "tacticalagent"
)
func GetPython(force bool) {}
func RunMigrations() {}
func RunMigrations() {}
func GetPythonBin() string {
pybin, err := exec.Command("python", "-c", "import sys; print(sys.executable)").Output()
if err != nil {
return ""
}
return strings.TrimSuffix(string(pybin), "\n")
}
func GetProgramDirectory() string {
return "/usr/local/bin"
}
func GetProgramBin() string {
bin := filepath.Join(GetProgramDirectory(), binName)
return bin
}

View file

@ -1 +1,13 @@
package tactical
type AgentConfig struct {
BaseURL string
AgentID string
APIURL string
Token string
AgentPK string
PK int
Cert string
Proxy string
CustomMeshDir string
}

View file

@ -8,5 +8,9 @@ import (
func TestGetVersion(t *testing.T) {
version := tactical.GetVersion()
if version == "" {
t.Errorf("expected version, got empty version")
}
t.Logf("got version %s", version)
}

View file

@ -1,15 +1,20 @@
//go:build !windows
// +build !windows
package tactical
import (
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"github.com/amidaware/rmmagent/agent/system"
"github.com/amidaware/rmmagent/agent/tactical/mesh"
"github.com/amidaware/rmmagent/agent/tactical/shared"
"github.com/amidaware/rmmagent/agent/utils"
"github.com/amidaware/rmmagent/shared"
"github.com/go-resty/resty/v2"
"github.com/spf13/viper"
"github.com/wh1te909/trmm-shared"
@ -19,7 +24,7 @@ func GetMeshBinary() string {
return "/opt/tacticalmesh/meshagent"
}
func NewAgentConfig() *shared.AgentConfig {
func NewAgentConfig() *AgentConfig {
viper.SetConfigName("tacticalagent")
viper.SetConfigType("json")
viper.AddConfigPath("/etc/")
@ -28,13 +33,13 @@ func NewAgentConfig() *shared.AgentConfig {
err := viper.ReadInConfig()
if err != nil {
return &shared.AgentConfig{}
return &AgentConfig{}
}
agentpk := viper.GetString("agentpk")
pk, _ := strconv.Atoi(agentpk)
ret := &shared.AgentConfig{
ret := &AgentConfig{
BaseURL: viper.GetString("baseurl"),
AgentID: viper.GetString("agentid"),
APIURL: viper.GetString("apiurl"),
@ -49,7 +54,7 @@ func NewAgentConfig() *shared.AgentConfig {
return ret
}
func AgentUpdate(url, inno, version string) bool {
func AgentUpdate(url string, inno string) bool {
self, err := os.Executable()
if err != nil {
return false
@ -61,15 +66,12 @@ func AgentUpdate(url, inno, version string) bool {
}
defer os.Remove(f.Name())
//logger.Infof("Agent updating from %s to %s", a.Version, version)
//logger.Infoln("Downloading agent update from", url)
rClient := resty.New()
rClient.SetCloseConnection(true)
rClient.SetTimeout(15 * time.Minute)
if shared.DEBUG {
rClient.SetDebug(true)
}
//if shared.DEBUG {
//rClient.SetDebug(true)
//}
config := NewAgentConfig()
if len(config.Proxy) > 0 {
@ -184,4 +186,15 @@ func installMesh(meshbin, exe, proxy string) (string, error) {
return "not implemented", nil
}
func SendSoftware() {}
func SendSoftware() {}
func GetVersion() string {
version, err := exec.Command(shared.GetProgramBin(), "-version").Output()
if err != nil {
return ""
}
re := regexp.MustCompile(`Tactical RMM Agent: v([0-9]\.[0-9]\.[0-9])`)
match := re.FindStringSubmatch(string(version))
return match[1]
}

View file

@ -1,11 +1,13 @@
package tactical
package tactical_test
import (
"testing"
"github.com/amidaware/rmmagent/agent/tactical"
)
func TestNewAgentConfig(t *testing.T) {
config := NewAgentConfig()
config := tactical.NewAgentConfig()
if config.BaseURL == "" {
t.Fatal("Could not get config")
}
@ -15,8 +17,8 @@ func TestNewAgentConfig(t *testing.T) {
func TestAgentUpdate(t *testing.T) {
url := "https://github.com/redanthrax/rmmagent/releases/download/v2.0.4/linuxagent"
result := AgentUpdate(url, "", "v2.0.4")
if(!result) {
result := tactical.AgentUpdate(url, "")
if !result {
t.Fatal("Agent update resulted in false")
}
@ -24,7 +26,7 @@ func TestAgentUpdate(t *testing.T) {
}
func TestAgentUninstall(t *testing.T) {
result := AgentUninstall("foo")
result := tactical.AgentUninstall("foo")
if !result {
t.Fatal("Agent uninstall resulted in error")
}
@ -33,11 +35,10 @@ func TestAgentUninstall(t *testing.T) {
}
func TestNixMeshNodeID(t *testing.T) {
nodeid := NixMeshNodeID()
nodeid := tactical.NixMeshNodeID()
if nodeid == "" {
t.Fatal("Unable to get mesh node id")
}
t.Logf("MeshNodeID: %s", nodeid)
}