| 1 | /* |
| 2 | Copyright 2023 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 knftables |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "fmt" |
| 22 | "io" |
| 23 | "os/exec" |
| 24 | "reflect" |
| 25 | "strings" |
| 26 | "testing" |
| 27 | ) |
| 28 | |
| 29 | // fakeExec is a mockable implementation of execer for unit tests |
| 30 | type fakeExec struct { |
| 31 | t *testing.T |
| 32 | |
| 33 | // missingBinaries is the set of binaries for which LookPath should fail |
| 34 | missingBinaries map[string]bool |
| 35 | |
| 36 | // expected is the list of expected Run calls |
| 37 | expected []expectedCmd |
| 38 | |
| 39 | // matched is used internally, to keep track of where we are in expected |
| 40 | matched int |
| 41 | } |
| 42 | |
| 43 | func newFakeExec(t *testing.T) *fakeExec { |
| 44 | return &fakeExec{t: t, missingBinaries: make(map[string]bool)} |
| 45 | } |
| 46 | |
| 47 | func (fe *fakeExec) LookPath(file string) (string, error) { |
| 48 | if fe.missingBinaries[file] { |
| 49 | return "", &exec.Error{Name: file, Err: exec.ErrNotFound} |
| 50 | } |
| 51 | return "/" + file, nil |
| 52 | } |
| 53 | |
| 54 | // expectedCmd details one expected fakeExec Cmd |
| 55 | type expectedCmd struct { |
| 56 | args []string |
| 57 | stdin string |
| 58 | stdout string |
| 59 | err error |
| 60 | } |
| 61 | |
| 62 | func (fe *fakeExec) Run(cmd *exec.Cmd) (string, error) { |
| 63 | if fe.t.Failed() { |
| 64 | return "", fmt.Errorf("unit test failed") |
| 65 | } |
| 66 | |
| 67 | if len(fe.expected) == fe.matched { |
| 68 | fe.t.Errorf("ran out of commands before executing %v", cmd.Args) |
| 69 | return "", fmt.Errorf("unit test failed") |
| 70 | } |
| 71 | expected := &fe.expected[fe.matched] |
| 72 | fe.matched++ |
| 73 | |
| 74 | if !reflect.DeepEqual(expected.args, cmd.Args) { |
| 75 | fe.t.Errorf("incorrect arguments: expected %v, got %v", expected.args, cmd.Args) |
| 76 | return "", fmt.Errorf("unit test failed") |
| 77 | } |
| 78 | |
| 79 | var stdin string |
| 80 | if cmd.Stdin != nil { |
| 81 | inBytes, _ := io.ReadAll(cmd.Stdin) |
| 82 | stdin = string(inBytes) |
| 83 | } |
| 84 | if expected.stdin != stdin { |
| 85 | fe.t.Errorf("incorrect stdin: expected %q, got %q", expected.stdin, stdin) |
| 86 | return "", fmt.Errorf("unit test failed") |
| 87 | } |
| 88 | |
| 89 | return expected.stdout, expected.err |
| 90 | } |
| 91 | |
| 92 | type execTestCase struct { |
| 93 | name string |
| 94 | command []string |
| 95 | stdin string |
| 96 | expectedOut string |
| 97 | expectedErr string |
| 98 | } |
| 99 | |
| 100 | var execTestCases = []execTestCase{ |
| 101 | { |
| 102 | name: "/bin/true", |
| 103 | command: []string{"/bin/true"}, |
| 104 | expectedOut: "", |
| 105 | }, |
| 106 | { |
| 107 | name: "/bin/false", |
| 108 | command: []string{"/bin/false"}, |
| 109 | expectedOut: "", |
| 110 | expectedErr: "exit status 1", |
| 111 | }, |
| 112 | { |
| 113 | name: "echo", |
| 114 | command: []string{"echo", "one", "two", "three"}, |
| 115 | expectedOut: "one two three\n", |
| 116 | }, |
| 117 | { |
| 118 | name: "with stdin", |
| 119 | command: []string{"cat"}, |
| 120 | stdin: "one\ntwo\nthree\n", |
| 121 | expectedOut: "one\ntwo\nthree\n", |
| 122 | }, |
| 123 | { |
| 124 | name: "missing command", |
| 125 | command: []string{"/does/not/exist/command"}, |
| 126 | expectedOut: "", |
| 127 | expectedErr: "no such file or directory", |
| 128 | }, |
| 129 | { |
| 130 | name: "fail", |
| 131 | command: []string{"cat", "."}, |
| 132 | expectedOut: "", |
| 133 | expectedErr: "Is a directory", |
| 134 | }, |
| 135 | } |
| 136 | |
| 137 | func TestRealExec(t *testing.T) { |
| 138 | for _, tc := range execTestCases { |
| 139 | t.Run(tc.name, func(t *testing.T) { |
| 140 | execer := &realExec{} |
| 141 | cmd := exec.Command(tc.command[0], tc.command[1:]...) |
| 142 | if tc.stdin != "" { |
| 143 | cmd.Stdin = bytes.NewBufferString(tc.stdin) |
| 144 | } |
| 145 | out, err := execer.Run(cmd) |
| 146 | if out != tc.expectedOut { |
| 147 | t.Errorf("expected output %q, got %q", tc.expectedOut, out) |
| 148 | } |
| 149 | if err != nil { |
| 150 | if tc.expectedErr == "" { |
| 151 | t.Errorf("expected no error, got %v", err) |
| 152 | } else if !strings.Contains(err.Error(), tc.expectedErr) { |
| 153 | t.Errorf("expected error containing %q, got %v", tc.expectedErr, err) |
| 154 | } |
| 155 | } else if tc.expectedErr != "" { |
| 156 | t.Errorf("expected error containing %q, got no error", tc.expectedErr) |
| 157 | } |
| 158 | }) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | func TestFakeExec(t *testing.T) { |
| 163 | for _, tc := range execTestCases { |
| 164 | t.Run(tc.name, func(t *testing.T) { |
| 165 | execer := newFakeExec(t) |
| 166 | execer.missingBinaries["/does/not/exist/command"] = true |
| 167 | execer.expected = []expectedCmd{{ |
| 168 | args: tc.command, |
| 169 | stdin: tc.stdin, |
| 170 | stdout: tc.expectedOut, |
| 171 | }} |
| 172 | if tc.expectedErr != "" { |
| 173 | execer.expected[0].err = fmt.Errorf(tc.expectedErr) |
| 174 | } |
| 175 | |
| 176 | cmd := exec.Command(tc.command[0], tc.command[1:]...) |
| 177 | if tc.stdin != "" { |
| 178 | cmd.Stdin = bytes.NewBufferString(tc.stdin) |
| 179 | } |
| 180 | out, err := execer.Run(cmd) |
| 181 | if out != tc.expectedOut { |
| 182 | t.Errorf("expected output %q, got %q", tc.expectedOut, out) |
| 183 | } |
| 184 | if err != nil { |
| 185 | if tc.expectedErr == "" { |
| 186 | t.Errorf("expected no error, got %v", err) |
| 187 | } else if !strings.Contains(err.Error(), tc.expectedErr) { |
| 188 | t.Errorf("expected error containing %q, got %v", tc.expectedErr, err) |
| 189 | } |
| 190 | } else if tc.expectedErr != "" { |
| 191 | t.Errorf("expected error containing %q, got no error", tc.expectedErr) |
| 192 | } |
| 193 | }) |
| 194 | } |
| 195 | } |