-
Notifications
You must be signed in to change notification settings - Fork 36
/
fake_cmd_runner.go
241 lines (180 loc) · 5.75 KB
/
fake_cmd_runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package fakes
import (
"fmt"
"io"
"strings"
"sync"
"time"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type FakeCmdRunner struct {
commandResults map[string][]FakeCmdResult
commandResultsLock sync.Mutex
processes map[string][]*FakeProcess
processesLock sync.Mutex
RunComplexCommands []boshsys.Command
RunCommands [][]string
RunCommandsWithInput [][]string
RunCommandsQuietly [][]string
runCommandCallbacks map[string]FakeCmdCallback
CommandExistsValue bool
AvailableCommands map[string]bool
}
type FakeCmdCallback func()
type FakeCmdResult struct {
Stdout string
Stderr string
ExitStatus int
Error error
Sticky bool // Set to true if this result should ALWAYS be returned for the given command
}
type FakeProcess struct {
StartErr error
WaitCh chan boshsys.Result
Waited bool
WaitResult boshsys.Result
TerminatedNicely bool
TerminatedNicelyCallBack func(*FakeProcess)
TerminateNicelyKillGracePeriod time.Duration
TerminateNicelyErr error
Stdout io.Writer
Stderr io.Writer
}
func (p *FakeProcess) Wait() <-chan boshsys.Result {
if p.Waited {
panic("Cannot Wait() on process multiple times")
}
p.Waited = true
p.WaitCh = make(chan boshsys.Result, 1)
if p.TerminatedNicelyCallBack == nil {
p.WaitCh <- p.WaitResult
}
return p.WaitCh
}
func (p *FakeProcess) TerminateNicely(killGracePeriod time.Duration) error {
p.TerminateNicelyKillGracePeriod = killGracePeriod
p.TerminatedNicely = true
if p.TerminatedNicelyCallBack != nil {
p.TerminatedNicelyCallBack(p)
}
return p.TerminateNicelyErr
}
func NewFakeCmdRunner() *FakeCmdRunner {
return &FakeCmdRunner{
AvailableCommands: map[string]bool{},
commandResults: map[string][]FakeCmdResult{},
runCommandCallbacks: map[string]FakeCmdCallback{},
processes: map[string][]*FakeProcess{},
}
}
func (r *FakeCmdRunner) RunComplexCommand(cmd boshsys.Command) (string, string, int, error) {
r.commandResultsLock.Lock()
defer r.commandResultsLock.Unlock()
r.RunComplexCommands = append(r.RunComplexCommands, cmd)
runCmd := append([]string{cmd.Name}, cmd.Args...)
r.runCallbackForCmd(runCmd)
stdout, stderr, exitstatus, err := r.getOutputsForCmd(runCmd)
if cmd.Stdout != nil {
cmd.Stdout.Write([]byte(stdout)) //nolint:errcheck
}
if cmd.Stderr != nil {
cmd.Stderr.Write([]byte(stderr)) //nolint:errcheck
}
return stdout, stderr, exitstatus, err
}
func (r *FakeCmdRunner) RunComplexCommandAsync(cmd boshsys.Command) (boshsys.Process, error) {
r.processesLock.Lock()
defer r.processesLock.Unlock()
r.RunComplexCommands = append(r.RunComplexCommands, cmd)
runCmd := append([]string{cmd.Name}, cmd.Args...)
r.runCallbackForCmd(runCmd)
fullCmd := strings.Join(runCmd, " ")
results, found := r.processes[fullCmd]
if !found {
panic(fmt.Sprintf("Failed to find process for %s", fullCmd))
}
results[0].Stdout = cmd.Stdout
results[0].Stderr = cmd.Stderr
for _, proc := range results {
if !proc.Waited {
return proc, proc.StartErr
}
}
panic(fmt.Sprintf("Failed to find available process for %s", fullCmd))
}
func (r *FakeCmdRunner) RunCommand(cmdName string, args ...string) (string, string, int, error) {
r.commandResultsLock.Lock()
defer r.commandResultsLock.Unlock()
runCmd := append([]string{cmdName}, args...)
r.RunCommands = append(r.RunCommands, runCmd)
r.runCallbackForCmd(runCmd)
return r.getOutputsForCmd(runCmd)
}
func (r *FakeCmdRunner) ClearCommandHistory() {
r.commandResultsLock.Lock()
defer r.commandResultsLock.Unlock()
r.RunCommands = [][]string{}
r.RunCommandsQuietly = [][]string{}
r.RunCommandsWithInput = [][]string{}
}
func (r *FakeCmdRunner) RunCommandQuietly(cmdName string, args ...string) (string, string, int, error) {
r.commandResultsLock.Lock()
defer r.commandResultsLock.Unlock()
runCmd := append([]string{cmdName}, args...)
r.RunCommandsQuietly = append(r.RunCommandsQuietly, runCmd)
r.runCallbackForCmd(runCmd)
return r.getOutputsForCmd(runCmd)
}
func (r *FakeCmdRunner) RunCommandWithInput(input, cmdName string, args ...string) (string, string, int, error) {
r.commandResultsLock.Lock()
defer r.commandResultsLock.Unlock()
runCmd := append([]string{input, cmdName}, args...)
r.RunCommandsWithInput = append(r.RunCommandsWithInput, runCmd)
r.runCallbackForCmd(runCmd)
return r.getOutputsForCmd(runCmd)
}
func (r *FakeCmdRunner) CommandExists(cmdName string) bool {
return r.CommandExistsValue || r.AvailableCommands[cmdName]
}
func (r *FakeCmdRunner) AddCmdResult(fullCmd string, result FakeCmdResult) {
r.commandResultsLock.Lock()
defer r.commandResultsLock.Unlock()
results := r.commandResults[fullCmd]
r.commandResults[fullCmd] = append(results, result)
}
func (r *FakeCmdRunner) AddProcess(fullCmd string, process *FakeProcess) {
r.processesLock.Lock()
defer r.processesLock.Unlock()
processes := r.processes[fullCmd]
r.processes[fullCmd] = append(processes, process)
}
func (r *FakeCmdRunner) SetCmdCallback(fullCmd string, callback FakeCmdCallback) {
r.runCommandCallbacks[fullCmd] = callback
}
func (r *FakeCmdRunner) getOutputsForCmd(runCmd []string) (string, string, int, error) {
fullCmd := strings.Join(runCmd, " ")
results, found := r.commandResults[fullCmd]
if found {
result := results[0]
newResults := []FakeCmdResult{}
if len(results) > 1 {
newResults = results[1:]
}
if !result.Sticky {
if len(newResults) == 0 {
delete(r.commandResults, fullCmd)
} else {
r.commandResults[fullCmd] = newResults
}
}
return result.Stdout, result.Stderr, result.ExitStatus, result.Error
}
return "", "", -1, nil
}
func (r *FakeCmdRunner) runCallbackForCmd(runCmd []string) {
fullCmd := strings.Join(runCmd, " ")
runCmdCallback, found := r.runCommandCallbacks[fullCmd]
if found {
runCmdCallback()
}
}