2021-10-16 00:28:37 +03:00
|
|
|
package platform
|
|
|
|
|
2023-10-13 22:08:58 +03:00
|
|
|
import (
|
2024-07-06 02:11:00 +03:00
|
|
|
"context"
|
2023-10-13 22:08:58 +03:00
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
2021-10-16 00:28:37 +03:00
|
|
|
|
2023-04-30 00:50:44 +03:00
|
|
|
type MockExecClient struct {
|
2023-11-01 08:13:11 +03:00
|
|
|
returnError bool
|
2024-08-07 00:36:55 +03:00
|
|
|
setExecRawCommand execRawCommandValidator
|
2023-11-01 08:13:11 +03:00
|
|
|
setExecCommand execCommandValidator
|
|
|
|
powershellCommandResponder powershellCommandResponder
|
2021-10-16 00:28:37 +03:00
|
|
|
}
|
|
|
|
|
2023-11-01 08:13:11 +03:00
|
|
|
type (
|
2024-08-07 00:36:55 +03:00
|
|
|
execRawCommandValidator func(string) (string, error)
|
|
|
|
execCommandValidator func(string, ...string) (string, error)
|
2023-11-01 08:13:11 +03:00
|
|
|
powershellCommandResponder func(string) (string, error)
|
|
|
|
)
|
2023-04-30 00:50:44 +03:00
|
|
|
|
2021-10-16 00:28:37 +03:00
|
|
|
// ErrMockExec - mock exec error
|
|
|
|
var ErrMockExec = errors.New("mock exec error")
|
|
|
|
|
2023-04-30 00:50:44 +03:00
|
|
|
func NewMockExecClient(returnErr bool) *MockExecClient {
|
|
|
|
return &MockExecClient{
|
2021-10-16 00:28:37 +03:00
|
|
|
returnError: returnErr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-07 00:36:55 +03:00
|
|
|
func (e *MockExecClient) ExecuteRawCommand(cmd string) (string, error) {
|
|
|
|
if e.setExecRawCommand != nil {
|
|
|
|
return e.setExecRawCommand(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.returnError {
|
|
|
|
return "", ErrMockExec
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *MockExecClient) ExecuteCommand(_ context.Context, cmd string, args ...string) (string, error) {
|
2023-04-30 00:50:44 +03:00
|
|
|
if e.setExecCommand != nil {
|
2024-08-07 00:36:55 +03:00
|
|
|
return e.setExecCommand(cmd, args...)
|
2023-04-30 00:50:44 +03:00
|
|
|
}
|
|
|
|
|
2021-10-16 00:28:37 +03:00
|
|
|
if e.returnError {
|
|
|
|
return "", ErrMockExec
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
2023-04-30 00:50:44 +03:00
|
|
|
|
2024-08-07 00:36:55 +03:00
|
|
|
func (e *MockExecClient) SetExecRawCommand(fn execRawCommandValidator) {
|
|
|
|
e.setExecRawCommand = fn
|
|
|
|
}
|
|
|
|
|
2023-04-30 00:50:44 +03:00
|
|
|
func (e *MockExecClient) SetExecCommand(fn execCommandValidator) {
|
|
|
|
e.setExecCommand = fn
|
|
|
|
}
|
2023-10-13 22:08:58 +03:00
|
|
|
|
2023-11-01 08:13:11 +03:00
|
|
|
func (e *MockExecClient) SetPowershellCommandResponder(fn powershellCommandResponder) {
|
|
|
|
e.powershellCommandResponder = fn
|
|
|
|
}
|
|
|
|
|
2023-10-13 22:08:58 +03:00
|
|
|
func (e *MockExecClient) ClearNetworkConfiguration() (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-11-01 08:13:11 +03:00
|
|
|
func (e *MockExecClient) ExecutePowershellCommand(cmd string) (string, error) {
|
|
|
|
if e.powershellCommandResponder != nil {
|
|
|
|
return e.powershellCommandResponder(cmd)
|
|
|
|
}
|
2023-10-13 22:08:58 +03:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *MockExecClient) GetLastRebootTime() (time.Time, error) {
|
|
|
|
return time.Time{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *MockExecClient) KillProcessByName(_ string) error {
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-06 02:11:00 +03:00
|
|
|
|
|
|
|
func (e *MockExecClient) ExecutePowershellCommandWithContext(_ context.Context, cmd string) (string, error) {
|
|
|
|
if e.powershellCommandResponder != nil {
|
|
|
|
return e.powershellCommandResponder(cmd)
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|