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 (
"errors"
"strings"
"testing"
"github.com/sirupsen/logrus"
@ -62,13 +63,24 @@ func TestRunScript(t *testing.T){
}{
{
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",
args: []string{},
timeout: 30,
expectedStdout: "",
expectedStderr: "",
expectedExitcode: 0,
expectedStderr: "exec format error",
expectedExitcode: -1,
expectedError: nil,
},
}
@ -80,15 +92,15 @@ func TestRunScript(t *testing.T){
t.Errorf("expected %s, got %s", tt.expectedStdout, stdout)
}
if tt.expectedStderr != stderr {
t.Errorf("expected %s, got %s", tt.expectedStderr, stderr)
if !strings.Contains(stderr, tt.expectedStderr) {
t.Errorf("expected stderr to contain %s, got %s", tt.expectedStderr, stderr)
}
if 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)
}
})

View file

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