fixup windows tests
This commit is contained in:
parent
a22c10cd67
commit
fd8405ff37
3 changed files with 135 additions and 63 deletions
|
|
@ -1,8 +1,6 @@
|
||||||
package agent
|
package agent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
@ -10,13 +8,13 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version = "2.0.4"
|
version = "2.0.4"
|
||||||
lg = logrus.New()
|
lg = logrus.New()
|
||||||
a = New(lg, version)
|
a = New(lg, version)
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetDisks(t *testing.T) {
|
func TestGetDisks(t *testing.T) {
|
||||||
disks := a.GetDisks()
|
disks := a.GetDisks()
|
||||||
if(len(disks) < 1) {
|
if len(disks) < 1 {
|
||||||
t.Errorf("Could not get disks")
|
t.Errorf("Could not get disks")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,61 +38,3 @@ func TestLoggedOnUser(t *testing.T) {
|
||||||
|
|
||||||
t.Logf("Result: %s", result)
|
t.Logf("Result: %s", result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRunScript(t *testing.T){
|
|
||||||
testTable := []struct {
|
|
||||||
name string
|
|
||||||
code string
|
|
||||||
shell string
|
|
||||||
args []string
|
|
||||||
timeout int
|
|
||||||
expectedStdout string
|
|
||||||
expectedStderr string
|
|
||||||
expectedExitcode int
|
|
||||||
expectedError error
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Run Script",
|
|
||||||
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: "exec format error",
|
|
||||||
expectedExitcode: -1,
|
|
||||||
expectedError: nil,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range testTable {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
stdout, stderr, exitcode, err := a.RunScript(tt.code, tt.shell, tt.args, tt.timeout)
|
|
||||||
if tt.expectedStdout != stdout {
|
|
||||||
t.Errorf("expected %s, got %s", tt.expectedStdout, stdout)
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
|
||||||
t.Errorf("expected (%v), got (%v)", tt.expectedError, err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
67
agent/agent_unix_test.go
Normal file
67
agent/agent_unix_test.go
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
//go:build !windows
|
||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package agent
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRunScript(t *testing.T){
|
||||||
|
testTable := []struct {
|
||||||
|
name string
|
||||||
|
code string
|
||||||
|
shell string
|
||||||
|
args []string
|
||||||
|
timeout int
|
||||||
|
expectedStdout string
|
||||||
|
expectedStderr string
|
||||||
|
expectedExitcode int
|
||||||
|
expectedError error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Run Script",
|
||||||
|
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: "exec format error",
|
||||||
|
expectedExitcode: -1,
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range testTable {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
stdout, stderr, exitcode, err := a.RunScript(tt.code, tt.shell, tt.args, tt.timeout)
|
||||||
|
if tt.expectedStdout != stdout {
|
||||||
|
t.Errorf("expected %s, got %s", tt.expectedStdout, stdout)
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
t.Errorf("expected (%v), got (%v)", tt.expectedError, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
65
agent/agent_windows_test.go
Normal file
65
agent/agent_windows_test.go
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
package agent
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRunScript(t *testing.T){
|
||||||
|
testTable := []struct {
|
||||||
|
name string
|
||||||
|
code string
|
||||||
|
shell string
|
||||||
|
args []string
|
||||||
|
timeout int
|
||||||
|
expectedStdout string
|
||||||
|
expectedStderr string
|
||||||
|
expectedExitcode int
|
||||||
|
expectedError error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Run Script",
|
||||||
|
code: "Write-Output \"test\"",
|
||||||
|
shell: "powershell",
|
||||||
|
args: []string{},
|
||||||
|
timeout: 30,
|
||||||
|
expectedStdout: "test\r\n",
|
||||||
|
expectedStderr: "",
|
||||||
|
expectedExitcode: 0,
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Run Bad Script",
|
||||||
|
code: "Bad-Command",
|
||||||
|
shell: "powershell",
|
||||||
|
args: []string{},
|
||||||
|
timeout: 30,
|
||||||
|
expectedStdout: "",
|
||||||
|
expectedStderr: "is not recognized as the name of a cmdlet",
|
||||||
|
expectedExitcode: 0,
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range testTable {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
stdout, stderr, exitcode, err := a.RunScript(tt.code, tt.shell, tt.args, tt.timeout)
|
||||||
|
if tt.expectedStdout != stdout {
|
||||||
|
t.Errorf("expected %s, got %s", tt.expectedStdout, stdout)
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
t.Errorf("expected (%v), got (%v)", tt.expectedError, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue