...
1
16
17 package testingexec
18
19 import (
20 "testing"
21
22 "k8s.io/utils/exec"
23 )
24
25
26 func TestCommandOrder(t *testing.T) {
27 fe := getFakeExecWithScripts(true, false)
28
29
30 defer func() {
31 if r := recover(); r == nil {
32 t.Errorf("The code did not panic")
33 }
34 }()
35 fe.Command("cat")
36 }
37
38
39 func TestDiffNumArgs(t *testing.T) {
40 fe := getFakeExecWithScripts(true, false)
41
42
43 defer func() {
44 if r := recover(); r == nil {
45 t.Errorf("The code did not panic")
46 }
47 }()
48 fe.Command("ps", "-e", "-f", "-A")
49 }
50
51
52 func TestDiffArgs(t *testing.T) {
53 fe := getFakeExecWithScripts(true, false)
54
55
56 defer func() {
57 if r := recover(); r == nil {
58 t.Errorf("The code did not panic")
59 }
60 }()
61 fe.Command("ps", "-fe")
62 }
63
64
65 func TestExtraCommands(t *testing.T) {
66 fe := getFakeExecWithScripts(false, false)
67
68
69 defer func() {
70 if r := recover(); r == nil {
71 t.Errorf("The code did not panic")
72 }
73 }()
74 fe.Command("ps")
75 fe.Command("cat")
76 fe.Command("unscripted")
77 }
78
79
80 func TestNoScriptPanic(t *testing.T) {
81 fe := &FakeExec{}
82
83
84 defer func() {
85 if r := recover(); r == nil {
86 t.Errorf("The code did not panic")
87 }
88 }()
89 fe.Command("ps")
90 }
91
92
93
94 func TestNoScriptNoPanic(t *testing.T) {
95 fe := &FakeExec{DisableScripts: true}
96
97
98 defer func() {
99 if r := recover(); r != nil {
100 t.Errorf("The code panic'd")
101 }
102 }()
103 fe.Command("ps")
104 }
105
106 func getFakeExecWithScripts(exactOrder bool, disableScripts bool) *FakeExec {
107 scripts := []struct {
108 cmd string
109 args []string
110 }{
111 {
112 cmd: "ps",
113 args: []string{"-ef"},
114 },
115 {
116 cmd: "cat",
117 args: []string{"/var/log"},
118 },
119 }
120
121 fakeexec := &FakeExec{ExactOrder: exactOrder, DisableScripts: disableScripts}
122 for _, s := range scripts {
123 fakeCmd := &FakeCmd{}
124 cmdAction := makeFakeCmd(fakeCmd, s.cmd, s.args...)
125 fakeexec.CommandScript = append(fakeexec.CommandScript, cmdAction)
126 }
127 return fakeexec
128 }
129
130 func makeFakeCmd(fakeCmd *FakeCmd, cmd string, args ...string) FakeCommandAction {
131 c := cmd
132 a := args
133 return func(cmd string, args ...string) exec.Cmd {
134 command := InitFakeCmd(fakeCmd, c, a...)
135 return command
136 }
137 }
138
View as plain text