| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package knftables |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"reflect" |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
|
| 26 |
"github.com/google/go-cmp/cmp" |
| 27 |
"github.com/lithammer/dedent" |
| 28 |
) |
| 29 |
|
| 30 |
func newTestInterface(t *testing.T, family Family, tableName string) (Interface, *fakeExec, error) { |
| 31 |
fexec := newFakeExec(t) |
| 32 |
fexec.expected = append(fexec.expected, |
| 33 |
expectedCmd{ |
| 34 |
args: []string{"/nft", "--version"}, |
| 35 |
stdout: "nftables v1.0.7 (Old Doc Yak)\n", |
| 36 |
}, |
| 37 |
expectedCmd{ |
| 38 |
args: []string{"/nft", "--check", "-f", "-"}, |
| 39 |
stdin: fmt.Sprintf("add table %s %s { comment \"test\" ; }\n", family, tableName), |
| 40 |
}, |
| 41 |
) |
| 42 |
nft, err := newInternal(family, tableName, fexec) |
| 43 |
return nft, fexec, err |
| 44 |
} |
| 45 |
|
| 46 |
func TestListBad(t *testing.T) { |
| 47 |
for _, tc := range []struct { |
| 48 |
name string |
| 49 |
nftOutput string |
| 50 |
nftError string |
| 51 |
listError string |
| 52 |
}{ |
| 53 |
{ |
| 54 |
name: "empty", |
| 55 |
nftOutput: ``, |
| 56 |
listError: "could not parse nft output", |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "nft failure", |
| 60 |
nftOutput: ``, |
| 61 |
nftError: "blah blah blah", |
| 62 |
listError: "failed to run nft: blah blah blah", |
| 63 |
}, |
| 64 |
{ |
| 65 |
name: "bad format", |
| 66 |
nftOutput: `{"foo": "bar"}`, |
| 67 |
listError: "could not parse nft output", |
| 68 |
}, |
| 69 |
{ |
| 70 |
name: "no result", |
| 71 |
nftOutput: `{"foo": []}`, |
| 72 |
listError: "could not find result", |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "no result (2)", |
| 76 |
nftOutput: `{"nftables":[]}`, |
| 77 |
listError: "could not find result", |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "no metadata", |
| 81 |
nftOutput: `{"nftables":[{"foo":{}}]}`, |
| 82 |
listError: "could not find metadata", |
| 83 |
}, |
| 84 |
{ |
| 85 |
name: "no schema info", |
| 86 |
nftOutput: `{"nftables":[{"metainfo":{}}]}`, |
| 87 |
listError: "could not find supported json_schema_version", |
| 88 |
}, |
| 89 |
{ |
| 90 |
name: "bad version", |
| 91 |
nftOutput: `{"nftables":[{"metainfo":{"json_schema_version":2}}]}`, |
| 92 |
listError: "could not find supported json_schema_version", |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "bad version (2)", |
| 96 |
nftOutput: `{"nftables":[{"metainfo":{"json_schema_version":"one"}}]}`, |
| 97 |
listError: "could not find supported json_schema_version", |
| 98 |
}, |
| 99 |
} { |
| 100 |
t.Run(tc.name, func(t *testing.T) { |
| 101 |
var nftErr error |
| 102 |
if tc.nftError != "" { |
| 103 |
nftErr = fmt.Errorf(tc.nftError) |
| 104 |
} |
| 105 |
|
| 106 |
nft, fexec, _ := newTestInterface(t, IPv6Family, "testing") |
| 107 |
|
| 108 |
fexec.expected = append(fexec.expected, |
| 109 |
expectedCmd{ |
| 110 |
args: []string{"/nft", "--json", "list", "chains", "ip6"}, |
| 111 |
stdout: tc.nftOutput, |
| 112 |
err: nftErr, |
| 113 |
}, |
| 114 |
) |
| 115 |
result, err := nft.List(context.Background(), "chains") |
| 116 |
if result != nil { |
| 117 |
t.Errorf("unexpected non-nil result: %v", result) |
| 118 |
} |
| 119 |
if !strings.Contains(err.Error(), tc.listError) { |
| 120 |
t.Errorf("unexpected error: wanted %q got %q", tc.listError, err.Error()) |
| 121 |
} |
| 122 |
}) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func TestList(t *testing.T) { |
| 127 |
for _, tc := range []struct { |
| 128 |
name string |
| 129 |
objType string |
| 130 |
nftOutput string |
| 131 |
listOutput []string |
| 132 |
}{ |
| 133 |
{ |
| 134 |
name: "empty list", |
| 135 |
objType: "chains", |
| 136 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}]}`, |
| 137 |
listOutput: nil, |
| 138 |
}, |
| 139 |
{ |
| 140 |
name: "singular objType", |
| 141 |
objType: "chain", |
| 142 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}, {"chain": {"family": "ip", "table": "testing", "name": "prerouting", "handle": 1, "type": "nat", "hook": "prerouting", "prio": -100, "policy": "accept"}}, {"chain": {"family": "ip", "table": "testing", "name": "output", "handle": 3, "type": "nat", "hook": "output", "prio": 0, "policy": "accept"}}, {"chain": {"family": "ip", "table": "testing", "name": "postrouting", "handle": 7, "type": "nat", "hook": "postrouting", "prio": 100, "policy": "accept"}}, {"chain": {"family": "ip", "table": "testing", "name": "KUBE-SERVICES", "handle": 11}}, {"chain": {"family": "ip", "table": "filter", "name": "INPUT", "handle": 1, "type": "filter", "hook": "input", "prio": 0, "policy": "accept"}}, {"chain": {"family": "ip", "table": "filter", "name": "FOO", "handle": 3}}]}`, |
| 143 |
listOutput: []string{"prerouting", "output", "postrouting", "KUBE-SERVICES"}, |
| 144 |
}, |
| 145 |
{ |
| 146 |
name: "plural objType", |
| 147 |
objType: "chains", |
| 148 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}, {"chain": {"family": "ip", "table": "testing", "name": "prerouting", "handle": 1, "type": "nat", "hook": "prerouting", "prio": -100, "policy": "accept"}}, {"chain": {"family": "ip", "table": "testing", "name": "output", "handle": 3, "type": "nat", "hook": "output", "prio": 0, "policy": "accept"}}, {"chain": {"family": "ip", "table": "testing", "name": "postrouting", "handle": 7, "type": "nat", "hook": "postrouting", "prio": 100, "policy": "accept"}}, {"chain": {"family": "ip", "table": "testing", "name": "KUBE-SERVICES", "handle": 11}}, {"chain": {"family": "ip", "table": "filter", "name": "INPUT", "handle": 1, "type": "filter", "hook": "input", "prio": 0, "policy": "accept"}}, {"chain": {"family": "ip", "table": "filter", "name": "FOO", "handle": 3}}]}`, |
| 149 |
listOutput: []string{"prerouting", "output", "postrouting", "KUBE-SERVICES"}, |
| 150 |
}, |
| 151 |
} { |
| 152 |
t.Run(tc.name, func(t *testing.T) { |
| 153 |
nft, fexec, _ := newTestInterface(t, IPv4Family, "testing") |
| 154 |
|
| 155 |
fexec.expected = append(fexec.expected, |
| 156 |
expectedCmd{ |
| 157 |
args: []string{"/nft", "--json", "list", "chains", "ip"}, |
| 158 |
stdout: tc.nftOutput, |
| 159 |
}, |
| 160 |
) |
| 161 |
result, err := nft.List(context.Background(), tc.objType) |
| 162 |
if err != nil { |
| 163 |
t.Errorf("unexpected error: %v", err) |
| 164 |
} |
| 165 |
if !reflect.DeepEqual(result, tc.listOutput) { |
| 166 |
t.Errorf("unexpected result: wanted %v got %v", tc.listOutput, result) |
| 167 |
} |
| 168 |
}) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
func TestRun(t *testing.T) { |
| 173 |
nft, fexec, _ := newTestInterface(t, IPv4Family, "kube-proxy") |
| 174 |
|
| 175 |
tx := nft.NewTransaction() |
| 176 |
|
| 177 |
tx.Add(&Table{}) |
| 178 |
tx.Add(&Chain{ |
| 179 |
Name: "chain", |
| 180 |
Comment: PtrTo("foo"), |
| 181 |
}) |
| 182 |
tx.Add(&Rule{ |
| 183 |
Chain: "chain", |
| 184 |
Rule: "ip daddr 10.0.0.0/8 drop", |
| 185 |
}) |
| 186 |
tx.Add(&Flowtable{ |
| 187 |
Name: "flowtable", |
| 188 |
Priority: PtrTo(FilterIngressPriority), |
| 189 |
Devices: []string{"eth0", "eth1"}, |
| 190 |
}) |
| 191 |
expected := strings.TrimPrefix(dedent.Dedent(` |
| 192 |
add table ip kube-proxy |
| 193 |
add chain ip kube-proxy chain { comment "foo" ; } |
| 194 |
add rule ip kube-proxy chain ip daddr 10.0.0.0/8 drop |
| 195 |
add flowtable ip kube-proxy flowtable { hook ingress priority filter ; devices = { eth0, eth1 } ; } |
| 196 |
`), "\n") |
| 197 |
fexec.expected = append(fexec.expected, |
| 198 |
expectedCmd{ |
| 199 |
args: []string{"/nft", "-f", "-"}, |
| 200 |
stdin: expected, |
| 201 |
}, |
| 202 |
) |
| 203 |
|
| 204 |
err := nft.Run(context.Background(), tx) |
| 205 |
if err != nil { |
| 206 |
t.Errorf("unexpected error from Run: %v", err) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
func TestListRules(t *testing.T) { |
| 211 |
for _, tc := range []struct { |
| 212 |
name string |
| 213 |
chain string |
| 214 |
nftOutput string |
| 215 |
nftError string |
| 216 |
listOutput []*Rule |
| 217 |
}{ |
| 218 |
{ |
| 219 |
name: "no such chain", |
| 220 |
chain: "testchain", |
| 221 |
nftError: "Error: No such file or directory\nlist chain ip testing testchain\n ^^^^^^^^^\n", |
| 222 |
}, |
| 223 |
{ |
| 224 |
name: "no rules", |
| 225 |
chain: "testchain", |
| 226 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}, {"chain": {"family": "ip", "table": "testing", "name": "testchain", "handle": 21}}]}`, |
| 227 |
listOutput: []*Rule{}, |
| 228 |
}, |
| 229 |
{ |
| 230 |
name: "normal output", |
| 231 |
chain: "testchain", |
| 232 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}, {"chain": {"family": "ip", "table": "testing", "name": "testchain", "handle": 165}}, {"rule": {"family": "ip", "table": "testing", "chain": "testchain", "handle": 169, "expr": [{"match": {"op": "==", "left": {"ct": {"key": "state"}}, "right": {"set": ["established", "related"]}}}, {"accept": null}]}}, {"rule": {"family": "ip", "table": "testing", "chain": "testchain", "handle": 170, "comment": "This rule does something", "expr": [{"match": {"op": "in", "left": {"ct": {"key": "status"}}, "right": "dnat"}}, {"accept": null}]}}, {"rule": {"family": "ip", "table": "testing", "chain": "testchain", "handle": 171, "expr": [{"match": {"op": "==", "left": {"meta": {"key": "iifname"}}, "right": "lo"}}, {"accept": null}]}}]}`, |
| 233 |
listOutput: []*Rule{ |
| 234 |
{ |
| 235 |
Chain: "testchain", |
| 236 |
Handle: PtrTo(169), |
| 237 |
}, |
| 238 |
{ |
| 239 |
Chain: "testchain", |
| 240 |
Comment: PtrTo("This rule does something"), |
| 241 |
Handle: PtrTo(170), |
| 242 |
}, |
| 243 |
{ |
| 244 |
Chain: "testchain", |
| 245 |
Handle: PtrTo(171), |
| 246 |
}, |
| 247 |
}, |
| 248 |
}, |
| 249 |
{ |
| 250 |
name: "all rules in table", |
| 251 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.2", "release_name": "Lester Gooch", "json_schema_version": 1}}, {"table": {"family": "ip", "name": "testing", "handle": 3}}, {"chain": {"family": "ip", "table": "testing", "name": "chain1", "handle": 1}}, {"rule": {"family": "ip", "table": "testing", "chain": "chain1", "handle": 3, "expr": [{"match": {"op": "==", "left": {"payload": {"protocol": "ip", "field": "daddr"}}, "right": "8.8.8.8"}}, {"counter": {"packets": 0, "bytes": 0}}]}}, {"chain": {"family": "ip", "table": "testing", "name": "chain2", "handle": 2}}, {"rule": {"family": "ip", "table": "testing", "chain": "chain2", "handle": 4, "expr": [{"match": {"op": "==", "left": {"payload": {"protocol": "ip", "field": "daddr"}}, "right": "1.2.3.4"}}, {"counter": {"packets": 0, "bytes": 0}}]}}]}`, |
| 252 |
listOutput: []*Rule{ |
| 253 |
{ |
| 254 |
Chain: "chain1", |
| 255 |
Handle: PtrTo(3), |
| 256 |
}, |
| 257 |
{ |
| 258 |
Chain: "chain2", |
| 259 |
Handle: PtrTo(4), |
| 260 |
}, |
| 261 |
}, |
| 262 |
}, |
| 263 |
} { |
| 264 |
t.Run(tc.name, func(t *testing.T) { |
| 265 |
nft, fexec, _ := newTestInterface(t, IPv4Family, "testing") |
| 266 |
|
| 267 |
var err error |
| 268 |
if tc.nftError != "" { |
| 269 |
err = fmt.Errorf(tc.nftError) |
| 270 |
} |
| 271 |
if tc.chain == "" { |
| 272 |
fexec.expected = append(fexec.expected, |
| 273 |
expectedCmd{ |
| 274 |
args: []string{"/nft", "--json", "list", "table", "ip", "testing"}, |
| 275 |
stdout: strings.TrimSpace(dedent.Dedent(tc.nftOutput)), |
| 276 |
err: err, |
| 277 |
}, |
| 278 |
) |
| 279 |
} else { |
| 280 |
fexec.expected = append(fexec.expected, |
| 281 |
expectedCmd{ |
| 282 |
args: []string{"/nft", "--json", "list", "chain", "ip", "testing", "testchain"}, |
| 283 |
stdout: strings.TrimSpace(dedent.Dedent(tc.nftOutput)), |
| 284 |
err: err, |
| 285 |
}, |
| 286 |
) |
| 287 |
} |
| 288 |
result, err := nft.ListRules(context.Background(), tc.chain) |
| 289 |
if err != nil { |
| 290 |
if tc.nftError == "" { |
| 291 |
t.Errorf("unexpected error: %v", err) |
| 292 |
} |
| 293 |
return |
| 294 |
} else if tc.nftError != "" { |
| 295 |
t.Errorf("unexpected non-error") |
| 296 |
return |
| 297 |
} |
| 298 |
|
| 299 |
diff := cmp.Diff(tc.listOutput, result) |
| 300 |
if diff != "" { |
| 301 |
t.Errorf("unexpected result:\n%s", diff) |
| 302 |
} |
| 303 |
}) |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
func TestListElements(t *testing.T) { |
| 308 |
for _, tc := range []struct { |
| 309 |
name string |
| 310 |
objectType string |
| 311 |
nftOutput string |
| 312 |
nftError string |
| 313 |
listOutput []*Element |
| 314 |
}{ |
| 315 |
{ |
| 316 |
name: "no such set", |
| 317 |
objectType: "set", |
| 318 |
nftError: "Error: No such file or directory\nlist set ip testing test\n ^^^^\n", |
| 319 |
}, |
| 320 |
{ |
| 321 |
name: "no elements", |
| 322 |
objectType: "set", |
| 323 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}, {"set": {"family": "ip", "name": "test", "table": "testing", "type": "inet_proto", "handle": 16}}]}`, |
| 324 |
listOutput: []*Element{}, |
| 325 |
}, |
| 326 |
{ |
| 327 |
name: "no elements", |
| 328 |
objectType: "set", |
| 329 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}, {"set": {"family": "ip", "name": "test", "table": "testing", "type": "inet_proto", "handle": 16, "elem": []}}]}`, |
| 330 |
listOutput: []*Element{}, |
| 331 |
}, |
| 332 |
{ |
| 333 |
name: "simple type", |
| 334 |
objectType: "set", |
| 335 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}, {"set": {"family": "ip", "name": "test", "table": "testing", "type": "ipv4_addr", "handle": 12, "elem": ["192.168.1.1", "192.168.1.2", {"elem": {"val": "192.168.1.3", "comment": "with a comment"}}]}}]}`, |
| 336 |
listOutput: []*Element{ |
| 337 |
{ |
| 338 |
Set: "test", |
| 339 |
Key: []string{"192.168.1.1"}, |
| 340 |
}, |
| 341 |
{ |
| 342 |
Set: "test", |
| 343 |
Key: []string{"192.168.1.2"}, |
| 344 |
}, |
| 345 |
{ |
| 346 |
Set: "test", |
| 347 |
Key: []string{"192.168.1.3"}, |
| 348 |
Comment: PtrTo("with a comment"), |
| 349 |
}, |
| 350 |
}, |
| 351 |
}, |
| 352 |
{ |
| 353 |
name: "concatenated type", |
| 354 |
objectType: "set", |
| 355 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}, {"set": {"family": "ip", "name": "test", "table": "testing", "type": ["ipv4_addr", "inet_proto", "inet_service"], "handle": 13, "elem": [{"concat": ["192.168.1.3", "tcp", 80]}, {"concat": ["192.168.1.4", "udp", 80]}, {"elem": {"val": {"concat": ["192.168.1.5", "tcp", 443]}, "comment": "foo"}}]}}]}`, |
| 356 |
listOutput: []*Element{ |
| 357 |
{ |
| 358 |
Set: "test", |
| 359 |
Key: []string{"192.168.1.3", "tcp", "80"}, |
| 360 |
}, |
| 361 |
{ |
| 362 |
Set: "test", |
| 363 |
Key: []string{"192.168.1.4", "udp", "80"}, |
| 364 |
}, |
| 365 |
{ |
| 366 |
Set: "test", |
| 367 |
Key: []string{"192.168.1.5", "tcp", "443"}, |
| 368 |
Comment: PtrTo("foo"), |
| 369 |
}, |
| 370 |
}, |
| 371 |
}, |
| 372 |
{ |
| 373 |
name: "prefix type", |
| 374 |
objectType: "set", |
| 375 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}, {"set": {"family": "ip", "name": "test", "table": "testing", "type": ["ipv4_addr"], "handle": 13, "flags": ["interval"], "elem": [{"prefix": {"addr": "192.168.0.0", "len": 16}}]}}]}`, |
| 376 |
listOutput: []*Element{ |
| 377 |
{ |
| 378 |
Set: "test", |
| 379 |
Key: []string{"192.168.0.0/16"}, |
| 380 |
}, |
| 381 |
}, |
| 382 |
}, |
| 383 |
{ |
| 384 |
name: "prefix type - bad len value", |
| 385 |
objectType: "set", |
| 386 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}, {"set": {"family": "ip", "name": "test", "table": "testing", "type": ["ipv4_addr"], "handle": 13, "flags": ["interval"], "elem": [{"prefix": {"addr": "192.168.0.0", "len": "16"}}]}}]}`, |
| 387 |
nftError: `could not parse 'len' value as number: map["addr":"192.168.0.0" "len":"16"]`, |
| 388 |
}, |
| 389 |
{ |
| 390 |
name: "prefix type - missing addr", |
| 391 |
objectType: "set", |
| 392 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}, {"set": {"family": "ip", "name": "test", "table": "testing", "type": ["ipv4_addr"], "handle": 13, "flags": ["interval"], "elem": [{"prefix": {"len": "16"}}]}}]}`, |
| 393 |
nftError: `could not parse 'addr' value as string: map["len":"16"]`, |
| 394 |
}, |
| 395 |
{ |
| 396 |
name: "simple map", |
| 397 |
objectType: "map", |
| 398 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}, {"map": {"family": "ip", "name": "test", "table": "testing", "type": "ipv4_addr", "handle": 14, "map": "inet_service", "elem": [["10.0.0.1", 80], [{"elem": {"val": "10.0.0.2", "comment": "a comment"}}, 443]]}}]}`, |
| 399 |
listOutput: []*Element{ |
| 400 |
{ |
| 401 |
Map: "test", |
| 402 |
Key: []string{"10.0.0.1"}, |
| 403 |
Value: []string{"80"}, |
| 404 |
}, |
| 405 |
{ |
| 406 |
Map: "test", |
| 407 |
Key: []string{"10.0.0.2"}, |
| 408 |
Value: []string{"443"}, |
| 409 |
Comment: PtrTo("a comment"), |
| 410 |
}, |
| 411 |
}, |
| 412 |
}, |
| 413 |
{ |
| 414 |
name: "verdict map, concatenated key", |
| 415 |
objectType: "map", |
| 416 |
nftOutput: `{"nftables": [{"metainfo": {"version": "1.0.1", "release_name": "Fearless Fosdick #3", "json_schema_version": 1}}, {"map": {"family": "ip", "name": "test", "table": "testing", "type": ["ipv4_addr", "inet_proto"], "handle": 15, "map": "verdict", "elem": [[{"concat": ["192.168.1.1", "tcp"]}, {"drop": null}], [{"elem": {"val": {"concat": ["192.168.1.3", "tcp"]}, "comment": "foo"}}, {"return": null}], [{"concat": ["192.168.1.2", "udp"]}, {"goto": {"target": "test"}}]]}}]}`, |
| 417 |
listOutput: []*Element{ |
| 418 |
{ |
| 419 |
Map: "test", |
| 420 |
Key: []string{"192.168.1.1", "tcp"}, |
| 421 |
Value: []string{"drop"}, |
| 422 |
}, |
| 423 |
{ |
| 424 |
Map: "test", |
| 425 |
Key: []string{"192.168.1.3", "tcp"}, |
| 426 |
Value: []string{"return"}, |
| 427 |
Comment: PtrTo("foo"), |
| 428 |
}, |
| 429 |
{ |
| 430 |
Map: "test", |
| 431 |
Key: []string{"192.168.1.2", "udp"}, |
| 432 |
Value: []string{"goto test"}, |
| 433 |
}, |
| 434 |
}, |
| 435 |
}, |
| 436 |
} { |
| 437 |
t.Run(tc.name, func(t *testing.T) { |
| 438 |
nft, fexec, _ := newTestInterface(t, IPv4Family, "testing") |
| 439 |
|
| 440 |
var err error |
| 441 |
if tc.nftError != "" { |
| 442 |
err = fmt.Errorf(tc.nftError) |
| 443 |
} |
| 444 |
fexec.expected = append(fexec.expected, |
| 445 |
expectedCmd{ |
| 446 |
args: []string{"/nft", "--json", "list", tc.objectType, "ip", "testing", "test"}, |
| 447 |
stdout: strings.TrimSpace(dedent.Dedent(tc.nftOutput)), |
| 448 |
err: err, |
| 449 |
}, |
| 450 |
) |
| 451 |
|
| 452 |
result, err := nft.ListElements(context.Background(), tc.objectType, "test") |
| 453 |
if err != nil { |
| 454 |
if tc.nftError == "" { |
| 455 |
t.Errorf("unexpected error: %v", err) |
| 456 |
} |
| 457 |
return |
| 458 |
} else if tc.nftError != "" { |
| 459 |
t.Errorf("unexpected non-error") |
| 460 |
return |
| 461 |
} |
| 462 |
|
| 463 |
diff := cmp.Diff(tc.listOutput, result) |
| 464 |
if diff != "" { |
| 465 |
t.Errorf("unexpected result:\n%s", diff) |
| 466 |
} |
| 467 |
}) |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
func TestFeatures(t *testing.T) { |
| 472 |
for _, tc := range []struct { |
| 473 |
name string |
| 474 |
commands []expectedCmd |
| 475 |
result *nftContext |
| 476 |
}{ |
| 477 |
{ |
| 478 |
name: "old nftables", |
| 479 |
commands: []expectedCmd{ |
| 480 |
{ |
| 481 |
args: []string{ |
| 482 |
"/nft", "--version", |
| 483 |
}, |
| 484 |
stdout: "nftables v0.9.3 (Topsy)\n", |
| 485 |
}, |
| 486 |
}, |
| 487 |
result: nil, |
| 488 |
}, |
| 489 |
{ |
| 490 |
name: "all features", |
| 491 |
commands: []expectedCmd{ |
| 492 |
{ |
| 493 |
args: []string{ |
| 494 |
"/nft", "--version", |
| 495 |
}, |
| 496 |
stdout: "nftables v1.0.7 (Old Doc Yak)\n", |
| 497 |
}, |
| 498 |
{ |
| 499 |
args: []string{"/nft", "--check", "-f", "-"}, |
| 500 |
stdin: "add table ip testing { comment \"test\" ; }\n", |
| 501 |
}, |
| 502 |
}, |
| 503 |
result: &nftContext{ |
| 504 |
family: IPv4Family, |
| 505 |
table: "testing", |
| 506 |
}, |
| 507 |
}, |
| 508 |
{ |
| 509 |
name: "noObjectComments", |
| 510 |
commands: []expectedCmd{ |
| 511 |
{ |
| 512 |
args: []string{ |
| 513 |
"/nft", "--version", |
| 514 |
}, |
| 515 |
stdout: "nftables v1.0.7 (Old Doc Yak)\n", |
| 516 |
}, |
| 517 |
{ |
| 518 |
args: []string{"/nft", "--check", "-f", "-"}, |
| 519 |
stdin: "add table ip testing { comment \"test\" ; }\n", |
| 520 |
err: fmt.Errorf("Error: syntax error, unexpected comment"), |
| 521 |
}, |
| 522 |
{ |
| 523 |
args: []string{"/nft", "--check", "-f", "-"}, |
| 524 |
stdin: "add table ip testing\n", |
| 525 |
}, |
| 526 |
}, |
| 527 |
result: &nftContext{ |
| 528 |
family: IPv4Family, |
| 529 |
table: "testing", |
| 530 |
|
| 531 |
noObjectComments: true, |
| 532 |
}, |
| 533 |
}, |
| 534 |
} { |
| 535 |
t.Run(tc.name, func(t *testing.T) { |
| 536 |
fexec := newFakeExec(t) |
| 537 |
fexec.expected = tc.commands |
| 538 |
nft, err := newInternal(IPv4Family, "testing", fexec) |
| 539 |
if err != nil { |
| 540 |
if tc.result != nil { |
| 541 |
t.Fatalf("Unexpected error creating Interface: %v", err) |
| 542 |
} |
| 543 |
} else { |
| 544 |
result := nft.(*realNFTables).nftContext |
| 545 |
if tc.result != nil { |
| 546 |
if !reflect.DeepEqual(*tc.result, result) { |
| 547 |
t.Errorf("Expected %#v, got %#v", *tc.result, result) |
| 548 |
} |
| 549 |
} else { |
| 550 |
t.Fatalf("Expected failure, got %#v", result) |
| 551 |
} |
| 552 |
} |
| 553 |
}) |
| 554 |
} |
| 555 |
} |