| 1 |
/* |
| 2 |
Copyright 2016 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 ebtables |
| 18 |
|
| 19 |
import ( |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"k8s.io/utils/exec" |
| 24 |
fakeexec "k8s.io/utils/exec/testing" |
| 25 |
) |
| 26 |
|
| 27 |
func TestEnsureChain(t *testing.T) { |
| 28 |
fcmd := fakeexec.FakeCmd{ |
| 29 |
CombinedOutputScript: []fakeexec.FakeAction{ |
| 30 |
// Does not Exists |
| 31 |
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 1} }, |
| 32 |
// Success |
| 33 |
func() ([]byte, []byte, error) { return []byte{}, nil, nil }, |
| 34 |
// Exists |
| 35 |
func() ([]byte, []byte, error) { return nil, nil, nil }, |
| 36 |
// Does not Exists |
| 37 |
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 1} }, |
| 38 |
// Fail to create chain |
| 39 |
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 2} }, |
| 40 |
}, |
| 41 |
} |
| 42 |
fexec := fakeexec.FakeExec{ |
| 43 |
CommandScript: []fakeexec.FakeCommandAction{ |
| 44 |
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, |
| 45 |
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, |
| 46 |
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, |
| 47 |
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, |
| 48 |
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, |
| 49 |
}, |
| 50 |
} |
| 51 |
|
| 52 |
runner := New(&fexec) |
| 53 |
exists, err := runner.EnsureChain(TableFilter, "TEST-CHAIN") |
| 54 |
if exists { |
| 55 |
t.Errorf("expected exists = false") |
| 56 |
} |
| 57 |
if err != nil { |
| 58 |
t.Errorf("expected err = nil") |
| 59 |
} |
| 60 |
|
| 61 |
exists, err = runner.EnsureChain(TableFilter, "TEST-CHAIN") |
| 62 |
if !exists { |
| 63 |
t.Errorf("expected exists = true") |
| 64 |
} |
| 65 |
if err != nil { |
| 66 |
t.Errorf("expected err = nil") |
| 67 |
} |
| 68 |
|
| 69 |
exists, err = runner.EnsureChain(TableFilter, "TEST-CHAIN") |
| 70 |
if exists { |
| 71 |
t.Errorf("expected exists = false") |
| 72 |
} |
| 73 |
errStr := "Failed to ensure TEST-CHAIN chain: exit 2, output:" |
| 74 |
if err == nil || !strings.Contains(err.Error(), errStr) { |
| 75 |
t.Errorf("expected error: %q", errStr) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
func TestEnsureRule(t *testing.T) { |
| 80 |
fcmd := fakeexec.FakeCmd{ |
| 81 |
CombinedOutputScript: []fakeexec.FakeAction{ |
| 82 |
// Exists |
| 83 |
func() ([]byte, []byte, error) { |
| 84 |
return []byte(`Bridge table: filter |
| 85 |
|
| 86 |
Bridge chain: OUTPUT, entries: 4, policy: ACCEPT |
| 87 |
-j TEST |
| 88 |
`), nil, nil |
| 89 |
}, |
| 90 |
// Does not Exists. |
| 91 |
func() ([]byte, []byte, error) { |
| 92 |
return []byte(`Bridge table: filter |
| 93 |
|
| 94 |
Bridge chain: TEST, entries: 0, policy: ACCEPT`), nil, nil |
| 95 |
}, |
| 96 |
// Fail to create |
| 97 |
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 2} }, |
| 98 |
}, |
| 99 |
} |
| 100 |
fexec := fakeexec.FakeExec{ |
| 101 |
CommandScript: []fakeexec.FakeCommandAction{ |
| 102 |
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, |
| 103 |
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, |
| 104 |
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, |
| 105 |
}, |
| 106 |
} |
| 107 |
|
| 108 |
runner := New(&fexec) |
| 109 |
|
| 110 |
exists, err := runner.EnsureRule(Append, TableFilter, ChainOutput, "-j", "TEST") |
| 111 |
if !exists { |
| 112 |
t.Errorf("expected exists = true") |
| 113 |
} |
| 114 |
if err != nil { |
| 115 |
t.Errorf("expected err = nil") |
| 116 |
} |
| 117 |
|
| 118 |
exists, err = runner.EnsureRule(Append, TableFilter, ChainOutput, "-j", "NEXT-TEST") |
| 119 |
if exists { |
| 120 |
t.Errorf("expected exists = false") |
| 121 |
} |
| 122 |
errStr := "Failed to ensure rule: exit 2, output: " |
| 123 |
if err == nil || err.Error() != errStr { |
| 124 |
t.Errorf("expected error: %q", errStr) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
func TestDeleteRule(t *testing.T) { |
| 129 |
fcmd := fakeexec.FakeCmd{ |
| 130 |
CombinedOutputScript: []fakeexec.FakeAction{ |
| 131 |
// Exists |
| 132 |
func() ([]byte, []byte, error) { |
| 133 |
return []byte(`Bridge table: filter |
| 134 |
|
| 135 |
Bridge chain: OUTPUT, entries: 4, policy: ACCEPT |
| 136 |
-j TEST |
| 137 |
`), nil, nil |
| 138 |
}, |
| 139 |
// Fail to delete |
| 140 |
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 2} }, |
| 141 |
// Does not Exists. |
| 142 |
func() ([]byte, []byte, error) { |
| 143 |
return []byte(`Bridge table: filter |
| 144 |
|
| 145 |
Bridge chain: TEST, entries: 0, policy: ACCEPT`), nil, nil |
| 146 |
}, |
| 147 |
}, |
| 148 |
} |
| 149 |
fexec := fakeexec.FakeExec{ |
| 150 |
CommandScript: []fakeexec.FakeCommandAction{ |
| 151 |
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, |
| 152 |
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, |
| 153 |
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, |
| 154 |
}, |
| 155 |
} |
| 156 |
|
| 157 |
runner := New(&fexec) |
| 158 |
|
| 159 |
err := runner.DeleteRule(TableFilter, ChainOutput, "-j", "TEST") |
| 160 |
errStr := "Failed to delete rule: exit 2, output: " |
| 161 |
if err == nil || err.Error() != errStr { |
| 162 |
t.Errorf("expected error: %q", errStr) |
| 163 |
} |
| 164 |
|
| 165 |
err = runner.DeleteRule(TableFilter, ChainOutput, "-j", "TEST") |
| 166 |
if err != nil { |
| 167 |
t.Errorf("expected err = nil") |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func Test_parseVersion(t *testing.T) { |
| 172 |
tests := []struct { |
| 173 |
name string |
| 174 |
version string |
| 175 |
want string |
| 176 |
wantErr bool |
| 177 |
}{ |
| 178 |
{ |
| 179 |
name: "version starting with `v`", |
| 180 |
version: "v2.0.10", |
| 181 |
want: "2.0.10", |
| 182 |
wantErr: false, |
| 183 |
}, |
| 184 |
{ |
| 185 |
name: "version without containing `v`", |
| 186 |
version: "2.0.10", |
| 187 |
want: "2.0.10", |
| 188 |
wantErr: false, |
| 189 |
}, |
| 190 |
{ |
| 191 |
name: "version containing `v` in between the regex expression match", |
| 192 |
version: "2.0v.10", |
| 193 |
want: "", |
| 194 |
wantErr: true, |
| 195 |
}, |
| 196 |
{ |
| 197 |
name: "version containing `v` after the regex expression match", |
| 198 |
version: "2.0.10v", |
| 199 |
want: "2.0.10", |
| 200 |
wantErr: false, |
| 201 |
}, |
| 202 |
{ |
| 203 |
name: "version starting with `v` and containing a symbol in between", |
| 204 |
version: "v2.0.10-4", |
| 205 |
want: "2.0.10", |
| 206 |
wantErr: false, |
| 207 |
}, |
| 208 |
{ |
| 209 |
name: "version starting with `v` and containing a symbol/alphabets in between", |
| 210 |
version: "v2.0a.10-4", |
| 211 |
want: "", |
| 212 |
wantErr: true, |
| 213 |
}, |
| 214 |
} |
| 215 |
for _, tt := range tests { |
| 216 |
t.Run(tt.name, func(t *testing.T) { |
| 217 |
got, err := parseVersion(tt.version) |
| 218 |
if (err != nil) != tt.wantErr { |
| 219 |
t.Errorf("parseVersion() error = %v, wantErr %v", err, tt.wantErr) |
| 220 |
return |
| 221 |
} |
| 222 |
if got != tt.want { |
| 223 |
t.Errorf("parseVersion() got = %v, want %v", got, tt.want) |
| 224 |
} |
| 225 |
}) |
| 226 |
} |
| 227 |
} |