-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommander.go
51 lines (43 loc) · 1.08 KB
/
commander.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
package test
import (
"bytes"
"errors"
"strings"
"k8s.io/utils/exec"
testingexec "k8s.io/utils/exec/testing"
)
// FakeCommand is a command wrapper for testing.
type FakeCommand struct {
Command string
StdOut []byte
StdErr []byte
}
// ToCmd converts FakeCommand to FakeCmd.
func (c FakeCommand) ToCmd() testingexec.FakeCmd {
return testingexec.FakeCmd{
Argv: strings.Split(c.Command, " "),
OutputScript: []testingexec.FakeAction{
func() ([]byte, []byte, error) {
if bytes.Equal(c.StdErr, []byte("")) {
return c.StdOut, nil, nil
}
//nolint:goerr113 // allow static errors.
return c.StdOut, nil, errors.New(string(c.StdErr))
},
},
}
}
// NewExecutor is a factory for Commander testing.
func NewExecutor(commands []FakeCommand) *testingexec.FakeExec {
cmdActions := make([]testingexec.FakeCommandAction, 0)
for i := range commands {
fakeCmd := commands[i].ToCmd()
cmdActions = append(cmdActions, func(c string, args ...string) exec.Cmd {
return &fakeCmd
})
}
return &testingexec.FakeExec{
ExactOrder: true,
CommandScript: cmdActions,
}
}