| 1 |
/* |
| 2 |
Copyright 2017 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package testingexec |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"sync" |
| 24 |
|
| 25 |
"k8s.io/utils/exec" |
| 26 |
) |
| 27 |
|
| 28 |
// FakeExec is a simple scripted Interface type. |
| 29 |
type FakeExec struct { |
| 30 |
CommandScript []FakeCommandAction |
| 31 |
CommandCalls int |
| 32 |
LookPathFunc func(string) (string, error) |
| 33 |
// ExactOrder enforces that commands are called in the order they are scripted, |
| 34 |
// and with the exact same arguments |
| 35 |
ExactOrder bool |
| 36 |
// DisableScripts removes the requirement that CommandScripts be populated |
| 37 |
// before calling Command(). This makes Command() and subsequent calls to |
| 38 |
// Run() or CombinedOutput() always return success and empty output. |
| 39 |
DisableScripts bool |
| 40 |
|
| 41 |
mu sync.Mutex |
| 42 |
} |
| 43 |
|
| 44 |
var _ exec.Interface = &FakeExec{} |
| 45 |
|
| 46 |
// FakeCommandAction is the function to be executed |
| 47 |
type FakeCommandAction func(cmd string, args ...string) exec.Cmd |
| 48 |
|
| 49 |
// Command returns the next unexecuted command in CommandScripts. |
| 50 |
// This function is safe for concurrent access as long as the underlying |
| 51 |
// FakeExec struct is not modified during execution. |
| 52 |
func (fake *FakeExec) Command(cmd string, args ...string) exec.Cmd { |
| 53 |
if fake.DisableScripts { |
| 54 |
fakeCmd := &FakeCmd{DisableScripts: true} |
| 55 |
return InitFakeCmd(fakeCmd, cmd, args...) |
| 56 |
} |
| 57 |
fakeCmd := fake.nextCommand(cmd, args) |
| 58 |
if fake.ExactOrder { |
| 59 |
argv := append([]string{cmd}, args...) |
| 60 |
fc := fakeCmd.(*FakeCmd) |
| 61 |
if cmd != fc.Argv[0] { |
| 62 |
panic(fmt.Sprintf("received command: %s, expected: %s", cmd, fc.Argv[0])) |
| 63 |
} |
| 64 |
if len(argv) != len(fc.Argv) { |
| 65 |
panic(fmt.Sprintf("command (%s) received with extra/missing arguments. Expected %v, Received %v", cmd, fc.Argv, argv)) |
| 66 |
} |
| 67 |
for i, a := range argv[1:] { |
| 68 |
if a != fc.Argv[i+1] { |
| 69 |
panic(fmt.Sprintf("command (%s) called with unexpected argument. Expected %s, Received %s", cmd, fc.Argv[i+1], a)) |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
return fakeCmd |
| 74 |
} |
| 75 |
|
| 76 |
func (fake *FakeExec) nextCommand(cmd string, args []string) exec.Cmd { |
| 77 |
fake.mu.Lock() |
| 78 |
defer fake.mu.Unlock() |
| 79 |
|
| 80 |
if fake.CommandCalls > len(fake.CommandScript)-1 { |
| 81 |
panic(fmt.Sprintf("ran out of Command() actions. Could not handle command [%d]: %s args: %v", fake.CommandCalls, cmd, args)) |
| 82 |
} |
| 83 |
i := fake.CommandCalls |
| 84 |
fake.CommandCalls++ |
| 85 |
return fake.CommandScript[i](cmd, args...) |
| 86 |
} |
| 87 |
|
| 88 |
// CommandContext wraps arguments into exec.Cmd |
| 89 |
func (fake *FakeExec) CommandContext(_ context.Context, cmd string, args ...string) exec.Cmd { |
| 90 |
return fake.Command(cmd, args...) |
| 91 |
} |
| 92 |
|
| 93 |
// LookPath is for finding the path of a file |
| 94 |
func (fake *FakeExec) LookPath(file string) (string, error) { |
| 95 |
return fake.LookPathFunc(file) |
| 96 |
} |
| 97 |
|
| 98 |
// FakeCmd is a simple scripted Cmd type. |
| 99 |
type FakeCmd struct { |
| 100 |
Argv []string |
| 101 |
CombinedOutputScript []FakeAction |
| 102 |
CombinedOutputCalls int |
| 103 |
CombinedOutputLog [][]string |
| 104 |
OutputScript []FakeAction |
| 105 |
OutputCalls int |
| 106 |
OutputLog [][]string |
| 107 |
RunScript []FakeAction |
| 108 |
RunCalls int |
| 109 |
RunLog [][]string |
| 110 |
Dirs []string |
| 111 |
Stdin io.Reader |
| 112 |
Stdout io.Writer |
| 113 |
Stderr io.Writer |
| 114 |
Env []string |
| 115 |
StdoutPipeResponse FakeStdIOPipeResponse |
| 116 |
StderrPipeResponse FakeStdIOPipeResponse |
| 117 |
WaitResponse error |
| 118 |
StartResponse error |
| 119 |
DisableScripts bool |
| 120 |
} |
| 121 |
|
| 122 |
var _ exec.Cmd = &FakeCmd{} |
| 123 |
|
| 124 |
// InitFakeCmd is for creating a fake exec.Cmd |
| 125 |
func InitFakeCmd(fake *FakeCmd, cmd string, args ...string) exec.Cmd { |
| 126 |
fake.Argv = append([]string{cmd}, args...) |
| 127 |
return fake |
| 128 |
} |
| 129 |
|
| 130 |
// FakeStdIOPipeResponse holds responses to use as fakes for the StdoutPipe and |
| 131 |
// StderrPipe method calls |
| 132 |
type FakeStdIOPipeResponse struct { |
| 133 |
ReadCloser io.ReadCloser |
| 134 |
Error error |
| 135 |
} |
| 136 |
|
| 137 |
// FakeAction is a function type |
| 138 |
type FakeAction func() ([]byte, []byte, error) |
| 139 |
|
| 140 |
// SetDir sets the directory |
| 141 |
func (fake *FakeCmd) SetDir(dir string) { |
| 142 |
fake.Dirs = append(fake.Dirs, dir) |
| 143 |
} |
| 144 |
|
| 145 |
// SetStdin sets the stdin |
| 146 |
func (fake *FakeCmd) SetStdin(in io.Reader) { |
| 147 |
fake.Stdin = in |
| 148 |
} |
| 149 |
|
| 150 |
// SetStdout sets the stdout |
| 151 |
func (fake *FakeCmd) SetStdout(out io.Writer) { |
| 152 |
fake.Stdout = out |
| 153 |
} |
| 154 |
|
| 155 |
// SetStderr sets the stderr |
| 156 |
func (fake *FakeCmd) SetStderr(out io.Writer) { |
| 157 |
fake.Stderr = out |
| 158 |
} |
| 159 |
|
| 160 |
// SetEnv sets the environment variables |
| 161 |
func (fake *FakeCmd) SetEnv(env []string) { |
| 162 |
fake.Env = env |
| 163 |
} |
| 164 |
|
| 165 |
// StdoutPipe returns an injected ReadCloser & error (via StdoutPipeResponse) |
| 166 |
// to be able to inject an output stream on Stdout |
| 167 |
func (fake *FakeCmd) StdoutPipe() (io.ReadCloser, error) { |
| 168 |
return fake.StdoutPipeResponse.ReadCloser, fake.StdoutPipeResponse.Error |
| 169 |
} |
| 170 |
|
| 171 |
// StderrPipe returns an injected ReadCloser & error (via StderrPipeResponse) |
| 172 |
// to be able to inject an output stream on Stderr |
| 173 |
func (fake *FakeCmd) StderrPipe() (io.ReadCloser, error) { |
| 174 |
return fake.StderrPipeResponse.ReadCloser, fake.StderrPipeResponse.Error |
| 175 |
} |
| 176 |
|
| 177 |
// Start mimicks starting the process (in the background) and returns the |
| 178 |
// injected StartResponse |
| 179 |
func (fake *FakeCmd) Start() error { |
| 180 |
return fake.StartResponse |
| 181 |
} |
| 182 |
|
| 183 |
// Wait mimicks waiting for the process to exit returns the |
| 184 |
// injected WaitResponse |
| 185 |
func (fake *FakeCmd) Wait() error { |
| 186 |
return fake.WaitResponse |
| 187 |
} |
| 188 |
|
| 189 |
// Run runs the command |
| 190 |
func (fake *FakeCmd) Run() error { |
| 191 |
if fake.DisableScripts { |
| 192 |
return nil |
| 193 |
} |
| 194 |
if fake.RunCalls > len(fake.RunScript)-1 { |
| 195 |
panic("ran out of Run() actions") |
| 196 |
} |
| 197 |
if fake.RunLog == nil { |
| 198 |
fake.RunLog = [][]string{} |
| 199 |
} |
| 200 |
i := fake.RunCalls |
| 201 |
fake.RunLog = append(fake.RunLog, append([]string{}, fake.Argv...)) |
| 202 |
fake.RunCalls++ |
| 203 |
stdout, stderr, err := fake.RunScript[i]() |
| 204 |
if stdout != nil { |
| 205 |
fake.Stdout.Write(stdout) |
| 206 |
} |
| 207 |
if stderr != nil { |
| 208 |
fake.Stderr.Write(stderr) |
| 209 |
} |
| 210 |
return err |
| 211 |
} |
| 212 |
|
| 213 |
// CombinedOutput returns the output from the command |
| 214 |
func (fake *FakeCmd) CombinedOutput() ([]byte, error) { |
| 215 |
if fake.DisableScripts { |
| 216 |
return []byte{}, nil |
| 217 |
} |
| 218 |
if fake.CombinedOutputCalls > len(fake.CombinedOutputScript)-1 { |
| 219 |
panic("ran out of CombinedOutput() actions") |
| 220 |
} |
| 221 |
if fake.CombinedOutputLog == nil { |
| 222 |
fake.CombinedOutputLog = [][]string{} |
| 223 |
} |
| 224 |
i := fake.CombinedOutputCalls |
| 225 |
fake.CombinedOutputLog = append(fake.CombinedOutputLog, append([]string{}, fake.Argv...)) |
| 226 |
fake.CombinedOutputCalls++ |
| 227 |
stdout, _, err := fake.CombinedOutputScript[i]() |
| 228 |
return stdout, err |
| 229 |
} |
| 230 |
|
| 231 |
// Output is the response from the command |
| 232 |
func (fake *FakeCmd) Output() ([]byte, error) { |
| 233 |
if fake.DisableScripts { |
| 234 |
return []byte{}, nil |
| 235 |
} |
| 236 |
if fake.OutputCalls > len(fake.OutputScript)-1 { |
| 237 |
panic("ran out of Output() actions") |
| 238 |
} |
| 239 |
if fake.OutputLog == nil { |
| 240 |
fake.OutputLog = [][]string{} |
| 241 |
} |
| 242 |
i := fake.OutputCalls |
| 243 |
fake.OutputLog = append(fake.OutputLog, append([]string{}, fake.Argv...)) |
| 244 |
fake.OutputCalls++ |
| 245 |
stdout, _, err := fake.OutputScript[i]() |
| 246 |
return stdout, err |
| 247 |
} |
| 248 |
|
| 249 |
// Stop is to stop the process |
| 250 |
func (fake *FakeCmd) Stop() { |
| 251 |
// no-op |
| 252 |
} |
| 253 |
|
| 254 |
// FakeExitError is a simple fake ExitError type. |
| 255 |
type FakeExitError struct { |
| 256 |
Status int |
| 257 |
} |
| 258 |
|
| 259 |
var _ exec.ExitError = FakeExitError{} |
| 260 |
|
| 261 |
func (fake FakeExitError) String() string { |
| 262 |
return fmt.Sprintf("exit %d", fake.Status) |
| 263 |
} |
| 264 |
|
| 265 |
func (fake FakeExitError) Error() string { |
| 266 |
return fake.String() |
| 267 |
} |
| 268 |
|
| 269 |
// Exited always returns true |
| 270 |
func (fake FakeExitError) Exited() bool { |
| 271 |
return true |
| 272 |
} |
| 273 |
|
| 274 |
// ExitStatus returns the fake status |
| 275 |
func (fake FakeExitError) ExitStatus() int { |
| 276 |
return fake.Status |
| 277 |
} |