fixed run script tests

This commit is contained in:
redanthrax 2022-06-24 09:11:35 -07:00
parent b778f4058c
commit 0f72dbf958
2 changed files with 27 additions and 13 deletions

View file

@ -2,6 +2,7 @@ package agent
import ( import (
"errors" "errors"
"strings"
"testing" "testing"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -62,13 +63,24 @@ func TestRunScript(t *testing.T){
}{ }{
{ {
name: "Run Script", name: "Run Script",
code: "ls", code: "#!/bin/sh\necho 'test'",
shell: "/bin/sh",
args: []string{},
timeout: 30,
expectedStdout: "test\n",
expectedStderr: "",
expectedExitcode: 0,
expectedError: nil,
},
{
name: "Run Bad Script No Hash Bang",
code: "echo 'test'",
shell: "/bin/sh", shell: "/bin/sh",
args: []string{}, args: []string{},
timeout: 30, timeout: 30,
expectedStdout: "", expectedStdout: "",
expectedStderr: "", expectedStderr: "exec format error",
expectedExitcode: 0, expectedExitcode: -1,
expectedError: nil, expectedError: nil,
}, },
} }
@ -80,15 +92,15 @@ func TestRunScript(t *testing.T){
t.Errorf("expected %s, got %s", tt.expectedStdout, stdout) t.Errorf("expected %s, got %s", tt.expectedStdout, stdout)
} }
if tt.expectedStderr != stderr { if !strings.Contains(stderr, tt.expectedStderr) {
t.Errorf("expected %s, got %s", tt.expectedStderr, stderr) t.Errorf("expected stderr to contain %s, got %s", tt.expectedStderr, stderr)
} }
if tt.expectedExitcode != exitcode { if tt.expectedExitcode != exitcode {
t.Errorf("expected exit %d, got exit %d", tt.expectedExitcode, exitcode) t.Errorf("expected exit %d, got exit %d", tt.expectedExitcode, exitcode)
} }
if errors.Is(tt.expectedError, err) { if !errors.Is(tt.expectedError, err) {
t.Errorf("expected (%v), got (%v)", tt.expectedError, err) t.Errorf("expected (%v), got (%v)", tt.expectedError, err)
} }
}) })

View file

@ -1,22 +1,24 @@
//go:build !windows //go:build !windows
// +build !windows // +build !windows
package system package system_test
import ( import (
"testing" "testing"
"github.com/amidaware/rmmagent/agent/system"
"github.com/amidaware/rmmagent/agent/utils" "github.com/amidaware/rmmagent/agent/utils"
) )
func TestNewCMDOpts(t *testing.T) { func TestNewCMDOpts(t *testing.T) {
opts := NewCMDOpts() opts := system.NewCMDOpts()
if opts.Shell != "/bin/bash" { if opts.Shell != "/bin/bash" {
t.Fatalf("Expected /bin/bash, got %s", opts.Shell) t.Fatalf("Expected /bin/bash, got %s", opts.Shell)
} }
} }
func TestSystemRebootRequired(t *testing.T) { func TestSystemRebootRequired(t *testing.T) {
required, err := SystemRebootRequired() required, err := system.SystemRebootRequired()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -26,7 +28,7 @@ func TestSystemRebootRequired(t *testing.T) {
func TestShowStatus(t *testing.T) { func TestShowStatus(t *testing.T) {
output := utils.CaptureOutput(func() { output := utils.CaptureOutput(func() {
ShowStatus("1.0.0") system.ShowStatus("1.0.0")
}); });
if output != "1.0.0\n" { if output != "1.0.0\n" {
@ -35,7 +37,7 @@ func TestShowStatus(t *testing.T) {
} }
func TestLoggedOnUser(t *testing.T) { func TestLoggedOnUser(t *testing.T) {
user := LoggedOnUser() user := system.LoggedOnUser()
if user == "" { if user == "" {
t.Fatalf("Expected a user, got empty") t.Fatalf("Expected a user, got empty")
} }
@ -44,7 +46,7 @@ func TestLoggedOnUser(t *testing.T) {
} }
func TestOsString(t *testing.T) { func TestOsString(t *testing.T) {
osString := OsString() osString := system.OsString()
if osString == "error getting host info" { if osString == "error getting host info" {
t.Fatalf("Unable to get OS string") t.Fatalf("Unable to get OS string")
} }
@ -53,7 +55,7 @@ func TestOsString(t *testing.T) {
} }
func TestRunScript(t *testing.T) { func TestRunScript(t *testing.T) {
stdout, stderr, exitcode, err := RunScript("#!/bin/sh\ncat /etc/os-release", "/bin/sh", nil, 30) stdout, stderr, exitcode, err := system.RunScript("#!/bin/sh\ncat /etc/os-release", "/bin/sh", nil, 30)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }