Debian Codegraph @

exec/testing/fake_exec_test.go

raw
1 /*
2 Copyright 2019 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 "testing"
21
22 "k8s.io/utils/exec"
23 )
24
25 // Test that command order is enforced
26 func TestCommandOrder(t *testing.T) {
27 fe := getFakeExecWithScripts(true, false)
28
29 // If we call "cat" first, it should panic
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 // Test that a command with different number of args panics
39 func TestDiffNumArgs(t *testing.T) {
40 fe := getFakeExecWithScripts(true, false)
41
42 // If we call "ps -e -f -A" instead of "ps -ef" it should panic
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 // Test that a command with different args panics
52 func TestDiffArgs(t *testing.T) {
53 fe := getFakeExecWithScripts(true, false)
54
55 // If we call "ps -fe" instead of "ps -ef" it should panic
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 // Test that extra commands panics
65 func TestExtraCommands(t *testing.T) {
66 fe := getFakeExecWithScripts(false, false)
67
68 // If we call mnore calls than scripted, should panic
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 // Test that calling a command without a script panics if not disabled
80 func TestNoScriptPanic(t *testing.T) {
81 fe := &FakeExec{}
82
83 // If we call mnore calls than scripted, should panic
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 // Test that calling a command without a script does not panic if scripts
93 // are disabled
94 func TestNoScriptNoPanic(t *testing.T) {
95 fe := &FakeExec{DisableScripts: true}
96
97 // If we call mnore calls than scripted, should panic
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(_ string, _ ...string) exec.Cmd {
134 command := InitFakeCmd(fakeCmd, c, a...)
135 return command
136 }
137 }