azure-container-networking/platform/mockexec.go

92 строки
2.1 KiB
Go
Исходник Постоянная ссылка Обычный вид История

package platform
import (
"context"
"errors"
"time"
)
2023-04-30 00:50:44 +03:00
type MockExecClient struct {
returnError bool
setExecRawCommand execRawCommandValidator
setExecCommand execCommandValidator
powershellCommandResponder powershellCommandResponder
}
type (
execRawCommandValidator func(string) (string, error)
execCommandValidator func(string, ...string) (string, error)
powershellCommandResponder func(string) (string, error)
)
2023-04-30 00:50:44 +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{
returnError: returnErr,
}
}
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 {
return e.setExecCommand(cmd, args...)
2023-04-30 00:50:44 +03:00
}
if e.returnError {
return "", ErrMockExec
}
return "", nil
}
2023-04-30 00:50:44 +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
}
func (e *MockExecClient) SetPowershellCommandResponder(fn powershellCommandResponder) {
e.powershellCommandResponder = fn
}
func (e *MockExecClient) ClearNetworkConfiguration() (bool, error) {
return true, nil
}
func (e *MockExecClient) ExecutePowershellCommand(cmd string) (string, error) {
if e.powershellCommandResponder != nil {
return e.powershellCommandResponder(cmd)
}
return "", nil
}
func (e *MockExecClient) GetLastRebootTime() (time.Time, error) {
return time.Time{}, nil
}
func (e *MockExecClient) KillProcessByName(_ string) error {
return nil
}
func (e *MockExecClient) ExecutePowershellCommandWithContext(_ context.Context, cmd string) (string, error) {
if e.powershellCommandResponder != nil {
return e.powershellCommandResponder(cmd)
}
return "", nil
}