tests
This commit is contained in:
parent
4e656c6556
commit
91c9de6e34
5 changed files with 155 additions and 42 deletions
|
|
@ -56,18 +56,15 @@ func RunScript(code string, shell string, args []string, timeout int) (stdout, s
|
|||
|
||||
tmpfn, err := ioutil.TempFile(dir, ext)
|
||||
if err != nil {
|
||||
//a.Logger.Errorln(err)
|
||||
return "", err.Error(), 85, err
|
||||
}
|
||||
|
||||
defer os.Remove(tmpfn.Name())
|
||||
if _, err := tmpfn.Write(content); err != nil {
|
||||
//a.Logger.Errorln(err)
|
||||
return "", err.Error(), 85, err
|
||||
}
|
||||
|
||||
if err := tmpfn.Close(); err != nil {
|
||||
//a.Logger.Errorln(err)
|
||||
return "", err.Error(), 85, err
|
||||
}
|
||||
|
||||
|
|
@ -93,7 +90,6 @@ func RunScript(code string, shell string, args []string, timeout int) (stdout, s
|
|||
cmd.Stdout = &outb
|
||||
cmd.Stderr = &errb
|
||||
if cmdErr := cmd.Start(); cmdErr != nil {
|
||||
//a.Logger.Debugln(cmdErr)
|
||||
return "", cmdErr.Error(), 65, cmdErr
|
||||
}
|
||||
|
||||
|
|
@ -474,7 +470,7 @@ func OsString() string {
|
|||
return osFullName
|
||||
}
|
||||
|
||||
func AddDefenderExlusions() {
|
||||
func AddDefenderExlusions() error {
|
||||
code := `
|
||||
Add-MpPreference -ExclusionPath 'C:\Program Files\TacticalAgent\*'
|
||||
Add-MpPreference -ExclusionPath 'C:\Windows\Temp\winagent-v*.exe'
|
||||
|
|
@ -483,6 +479,8 @@ Add-MpPreference -ExclusionPath 'C:\Program Files\Mesh Agent\*'
|
|||
`
|
||||
_, _, _, err := RunScript(code, "powershell", []string{}, 20)
|
||||
if err != nil {
|
||||
//a.Logger.Debugln(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
67
agent/system/system_windows_test.go
Normal file
67
agent/system/system_windows_test.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package system_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/amidaware/rmmagent/agent/system"
|
||||
)
|
||||
|
||||
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: "Test-Path -Path C:\\Windows",
|
||||
shell: "powershell",
|
||||
args: []string{},
|
||||
timeout: 30,
|
||||
expectedStdout: "True\r\n",
|
||||
expectedStderr: "",
|
||||
expectedExitCode: 0,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "Run Error Script",
|
||||
code: "Get-ThisError",
|
||||
shell: "powershell",
|
||||
args: []string{},
|
||||
timeout: 30,
|
||||
expectedStdout: "",
|
||||
expectedStderr: "The term 'Get-ThisError' 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 := system.RunScript(tt.code, tt.shell, tt.args, tt.timeout)
|
||||
if stdout != tt.expectedStdout {
|
||||
t.Errorf("expected stdout %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 exitcode != tt.expectedExitCode {
|
||||
t.Errorf("expected exitcode %d, got %d", tt.expectedExitCode, exitcode)
|
||||
}
|
||||
|
||||
if !errors.Is(err, tt.expectedError) {
|
||||
t.Errorf("expected error (%v), got (%v)", tt.expectedError, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue