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
|
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package testingexec
import (
"testing"
"k8s.io/utils/exec"
)
// Test that command order is enforced
func TestCommandOrder(t *testing.T) {
fe := getFakeExecWithScripts(true, false)
// If we call "cat" first, it should panic
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
fe.Command("cat")
}
// Test that a command with different number of args panics
func TestDiffNumArgs(t *testing.T) {
fe := getFakeExecWithScripts(true, false)
// If we call "ps -e -f -A" instead of "ps -ef" it should panic
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
fe.Command("ps", "-e", "-f", "-A")
}
// Test that a command with different args panics
func TestDiffArgs(t *testing.T) {
fe := getFakeExecWithScripts(true, false)
// If we call "ps -fe" instead of "ps -ef" it should panic
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
fe.Command("ps", "-fe")
}
// Test that extra commands panics
func TestExtraCommands(t *testing.T) {
fe := getFakeExecWithScripts(false, false)
// If we call mnore calls than scripted, should panic
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
fe.Command("ps")
fe.Command("cat")
fe.Command("unscripted")
}
// Test that calling a command without a script panics if not disabled
func TestNoScriptPanic(t *testing.T) {
fe := &FakeExec{}
// If we call mnore calls than scripted, should panic
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
fe.Command("ps")
}
// Test that calling a command without a script does not panic if scripts
// are disabled
func TestNoScriptNoPanic(t *testing.T) {
fe := &FakeExec{DisableScripts: true}
// If we call mnore calls than scripted, should panic
defer func() {
if r := recover(); r != nil {
t.Errorf("The code panic'd")
}
}()
fe.Command("ps")
}
func getFakeExecWithScripts(exactOrder bool, disableScripts bool) *FakeExec {
scripts := []struct {
cmd string
args []string
}{
{
cmd: "ps",
args: []string{"-ef"},
},
{
cmd: "cat",
args: []string{"/var/log"},
},
}
fakeexec := &FakeExec{ExactOrder: exactOrder, DisableScripts: disableScripts}
for _, s := range scripts {
fakeCmd := &FakeCmd{}
cmdAction := makeFakeCmd(fakeCmd, s.cmd, s.args...)
fakeexec.CommandScript = append(fakeexec.CommandScript, cmdAction)
}
return fakeexec
}
func makeFakeCmd(fakeCmd *FakeCmd, cmd string, args ...string) FakeCommandAction {
c := cmd
a := args
return func(cmd string, args ...string) exec.Cmd {
command := InitFakeCmd(fakeCmd, c, a...)
return command
}
}
|