| 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 | "fmt" |
| 21 | "os/exec" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | func mkExecError(stderr string) error { |
| 26 | return wrapError(&exec.ExitError{Stderr: []byte(stderr)}) |
| 27 | } |
| 28 | |
| 29 | func TestError(t *testing.T) { |
| 30 | for _, tc := range []struct { |
| 31 | name string |
| 32 | err error |
| 33 | isNotFound bool |
| 34 | isExists bool |
| 35 | }{ |
| 36 | { |
| 37 | name: "generic doesn't exist", |
| 38 | err: mkExecError("Error: No such file or directory\ndelete element ip nosuchtable set {10.0.0.1}\n ^^^^^^^^^^^\n"), |
| 39 | isNotFound: true, |
| 40 | isExists: false, |
| 41 | }, |
| 42 | { |
| 43 | name: "doesn't exist with suggestion", |
| 44 | err: mkExecError("Error: No such file or directory; did you mean table ‘foo’ in family ip?\nblah blah blah\n"), |
| 45 | isNotFound: true, |
| 46 | isExists: false, |
| 47 | }, |
| 48 | { |
| 49 | name: "already exists", |
| 50 | err: mkExecError("Error: Could not process rule: File exists\ncreate table foo\n ^^^"), |
| 51 | isNotFound: false, |
| 52 | isExists: true, |
| 53 | }, |
| 54 | { |
| 55 | name: "wrapped doesn't exist", |
| 56 | err: fmt.Errorf("oh my! %w", mkExecError("Error: No such file or directory")), |
| 57 | isNotFound: true, |
| 58 | isExists: false, |
| 59 | }, |
| 60 | { |
| 61 | name: "misc error", |
| 62 | err: mkExecError("Error: syntax error, unexpected string, expecting '{' or '$'"), |
| 63 | isNotFound: false, |
| 64 | isExists: false, |
| 65 | }, |
| 66 | { |
| 67 | name: "misleading misc error", |
| 68 | err: mkExecError("Error: syntax error, unexpected comment\nadd rule foo chain1 comment \"No such file or directory\" drop\n ^^^^^^^"), |
| 69 | isNotFound: false, |
| 70 | isExists: false, |
| 71 | }, |
| 72 | { |
| 73 | name: "not an ExecError, so not interpreted", |
| 74 | err: wrapError(fmt.Errorf("Error: No such file or directory\ndelete element ip nosuchtable set {10.0.0.1}\n ^^^^^^^^^^^\n")), //nolint:revive |
| 75 | isNotFound: false, |
| 76 | isExists: false, |
| 77 | }, |
| 78 | { |
| 79 | name: "fake not found", |
| 80 | err: notFoundError("not found"), |
| 81 | isNotFound: true, |
| 82 | isExists: false, |
| 83 | }, |
| 84 | { |
| 85 | name: "fake exists", |
| 86 | err: existsError("already exists"), |
| 87 | isNotFound: false, |
| 88 | isExists: true, |
| 89 | }, |
| 90 | } { |
| 91 | t.Run(tc.name, func(t *testing.T) { |
| 92 | if IsNotFound(tc.err) != tc.isNotFound { |
| 93 | t.Errorf("expected IsNotFound %v, got %v", tc.isNotFound, IsNotFound(tc.err)) |
| 94 | } |
| 95 | if IsAlreadyExists(tc.err) != tc.isExists { |
| 96 | t.Errorf("expected IsAlreadyExists %v, got %v", tc.isExists, IsAlreadyExists(tc.err)) |
| 97 | } |
| 98 | }) |
| 99 | } |
| 100 | } |