2016-11-05 17:09:05 -07:00
|
|
|
/*
|
|
|
|
|
Copyright 2016 The Rook Authors. All rights reserved.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
2016-10-12 09:04:07 -07:00
|
|
|
package test
|
|
|
|
|
|
2017-03-23 17:14:05 -07:00
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
2017-09-14 09:40:34 -07:00
|
|
|
"time"
|
2017-03-23 17:14:05 -07:00
|
|
|
)
|
2016-10-28 17:07:31 -07:00
|
|
|
|
2016-10-12 09:04:07 -07:00
|
|
|
// ******************** MockExecutor ********************
|
|
|
|
|
type MockExecutor struct {
|
2017-08-07 10:58:52 -07:00
|
|
|
MockExecuteCommand func(debug bool, actionName string, command string, arg ...string) error
|
|
|
|
|
MockStartExecuteCommand func(debug bool, actionName string, command string, arg ...string) (*exec.Cmd, error)
|
|
|
|
|
MockExecuteCommandWithOutput func(debug bool, actionName string, command string, arg ...string) (string, error)
|
|
|
|
|
MockExecuteCommandWithCombinedOutput func(debug bool, actionName string, command string, arg ...string) (string, error)
|
|
|
|
|
MockExecuteCommandWithOutputFile func(debug bool, actionName string, command, outfileArg string, arg ...string) (string, error)
|
2017-09-14 09:40:34 -07:00
|
|
|
MockExecuteCommandWithTimeout func(debug bool, timeout time.Duration, actionName string, command string, arg ...string) (string, error)
|
2017-03-23 17:14:05 -07:00
|
|
|
MockExecuteStat func(name string) (os.FileInfo, error)
|
2016-10-12 09:04:07 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-07 10:58:52 -07:00
|
|
|
func (e *MockExecutor) ExecuteCommand(debug bool, actionName string, command string, arg ...string) error {
|
2016-10-12 09:04:07 -07:00
|
|
|
if e.MockExecuteCommand != nil {
|
2017-08-07 10:58:52 -07:00
|
|
|
return e.MockExecuteCommand(debug, actionName, command, arg...)
|
2016-10-12 09:04:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 10:58:52 -07:00
|
|
|
func (e *MockExecutor) StartExecuteCommand(debug bool, actionName string, command string, arg ...string) (*exec.Cmd, error) {
|
2016-10-28 17:07:31 -07:00
|
|
|
if e.MockStartExecuteCommand != nil {
|
2017-08-07 10:58:52 -07:00
|
|
|
return e.MockStartExecuteCommand(debug, actionName, command, arg...)
|
2016-10-28 17:07:31 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-13 14:31:27 -08:00
|
|
|
args := []string{command}
|
|
|
|
|
return &exec.Cmd{Args: append(args, arg...)}, nil
|
2016-10-28 17:07:31 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-07 10:58:52 -07:00
|
|
|
func (e *MockExecutor) ExecuteCommandWithOutput(debug bool, actionName string, command string, arg ...string) (string, error) {
|
2016-10-12 09:04:07 -07:00
|
|
|
if e.MockExecuteCommandWithOutput != nil {
|
2017-08-07 10:58:52 -07:00
|
|
|
return e.MockExecuteCommandWithOutput(debug, actionName, command, arg...)
|
2016-10-12 09:04:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
|
}
|
2017-02-03 13:16:36 -08:00
|
|
|
|
2017-09-14 09:40:34 -07:00
|
|
|
func (e *MockExecutor) ExecuteCommandWithTimeout(debug bool, timeout time.Duration, actionName string, command string, arg ...string) (string, error) {
|
|
|
|
|
|
|
|
|
|
if e.MockExecuteCommandWithTimeout != nil {
|
|
|
|
|
return e.MockExecuteCommandWithTimeout(debug, time.Second, actionName, command, arg...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 10:58:52 -07:00
|
|
|
func (e *MockExecutor) ExecuteCommandWithCombinedOutput(debug bool, actionName string, command string, arg ...string) (string, error) {
|
2017-02-03 13:16:36 -08:00
|
|
|
if e.MockExecuteCommandWithCombinedOutput != nil {
|
2017-08-07 10:58:52 -07:00
|
|
|
return e.MockExecuteCommandWithCombinedOutput(debug, actionName, command, arg...)
|
2017-02-03 13:16:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
|
}
|
2017-03-23 17:14:05 -07:00
|
|
|
|
2017-08-07 10:58:52 -07:00
|
|
|
func (e *MockExecutor) ExecuteCommandWithOutputFile(debug bool, actionName string, command, outfileArg string, arg ...string) (string, error) {
|
2017-06-15 18:40:18 -07:00
|
|
|
if e.MockExecuteCommandWithOutputFile != nil {
|
2017-08-07 10:58:52 -07:00
|
|
|
return e.MockExecuteCommandWithOutputFile(debug, actionName, command, outfileArg, arg...)
|
2017-06-15 18:40:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 17:14:05 -07:00
|
|
|
func (e *MockExecutor) ExecuteStat(name string) (os.FileInfo, error) {
|
|
|
|
|
if e.MockExecuteStat != nil {
|
|
|
|
|
return e.MockExecuteStat(name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|