| 1 | // Copyright 2015-2018 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "encoding/json" |
| 20 | "fmt" |
| 21 | "net" |
| 22 | "os" |
| 23 | "strings" |
| 24 | |
| 25 | "github.com/coreos/go-iptables/iptables" |
| 26 | "github.com/networkplumbing/go-nft/nft" |
| 27 | . "github.com/onsi/ginkgo/v2" |
| 28 | . "github.com/onsi/gomega" |
| 29 | "github.com/vishvananda/netlink" |
| 30 | "github.com/vishvananda/netlink/nl" |
| 31 | "sigs.k8s.io/knftables" |
| 32 | |
| 33 | "github.com/containernetworking/cni/pkg/skel" |
| 34 | "github.com/containernetworking/cni/pkg/types" |
| 35 | types040 "github.com/containernetworking/cni/pkg/types/040" |
| 36 | types100 "github.com/containernetworking/cni/pkg/types/100" |
| 37 | "github.com/containernetworking/plugins/pkg/ip" |
| 38 | "github.com/containernetworking/plugins/pkg/netlinksafe" |
| 39 | "github.com/containernetworking/plugins/pkg/ns" |
| 40 | "github.com/containernetworking/plugins/pkg/testutils" |
| 41 | "github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator" |
| 42 | ) |
| 43 | |
| 44 | const ( |
| 45 | BRNAME = "bridge0" |
| 46 | BRNAMEVLAN = "bridge0.100" |
| 47 | IFNAME = "eth0" |
| 48 | NAMESERVER = "192.0.2.0" |
| 49 | ) |
| 50 | |
| 51 | type Net struct { |
| 52 | Name string `json:"name"` |
| 53 | CNIVersion string `json:"cniVersion"` |
| 54 | Type string `json:"type,omitempty"` |
| 55 | BrName string `json:"bridge"` |
| 56 | IPAM *allocator.IPAMConfig `json:"ipam"` |
| 57 | // RuntimeConfig struct { // The capability arg |
| 58 | // IPRanges []RangeSet `json:"ipRanges,omitempty"` |
| 59 | // Args *struct { |
| 60 | // } `json:"runtimeConfig,omitempty"` |
| 61 | |
| 62 | // A *IPAMArgs `json:"cni"` |
| 63 | DNS types.DNS `json:"dns"` |
| 64 | RawPrevResult map[string]interface{} `json:"prevResult,omitempty"` |
| 65 | PrevResult types100.Result `json:"-"` |
| 66 | } |
| 67 | |
| 68 | // testCase defines the CNI network configuration and the expected |
| 69 | // bridge addresses for a test case. |
| 70 | type testCase struct { |
| 71 | cniVersion string // CNI Version |
| 72 | subnet string // Single subnet config: Subnet CIDR |
| 73 | gateway string // Single subnet config: Gateway |
| 74 | ranges []rangeInfo // Ranges list (multiple subnets config) |
| 75 | resolvConf string // host-local resolvConf file path |
| 76 | isGW bool |
| 77 | isLayer2 bool |
| 78 | expGWCIDRs []string // Expected gateway addresses in CIDR form |
| 79 | vlan int |
| 80 | vlanTrunk []*VlanTrunk |
| 81 | removeDefaultVlan bool |
| 82 | ipMasq bool |
| 83 | ipMasqBackend string |
| 84 | macspoofchk bool |
| 85 | disableContIface bool |
| 86 | portIsolation bool |
| 87 | |
| 88 | AddErr020 string |
| 89 | DelErr020 string |
| 90 | AddErr010 string |
| 91 | DelErr010 string |
| 92 | |
| 93 | envArgs string // CNI_ARGS |
| 94 | runtimeConfig struct { |
| 95 | mac string |
| 96 | } |
| 97 | args struct { |
| 98 | cni struct { |
| 99 | mac string |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Unlike the parameters above, the following parameters |
| 104 | // are expected values to be checked against. |
| 105 | // e.g. the mac address has several sources: CNI_ARGS, Args and RuntimeConfig. |
| 106 | expectedMac string |
| 107 | } |
| 108 | |
| 109 | // Range definition for each entry in the ranges list |
| 110 | type rangeInfo struct { |
| 111 | subnet string |
| 112 | gateway string |
| 113 | } |
| 114 | |
| 115 | // netConf() creates a NetConf structure for a test case. |
| 116 | func (tc testCase) netConf() *NetConf { |
| 117 | return &NetConf{ |
| 118 | NetConf: types.NetConf{ |
| 119 | CNIVersion: tc.cniVersion, |
| 120 | Name: "testConfig", |
| 121 | Type: "bridge", |
| 122 | }, |
| 123 | BrName: BRNAME, |
| 124 | IsGW: tc.isGW, |
| 125 | IPMasq: false, |
| 126 | MTU: 5000, |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Snippets for generating a JSON network configuration string. |
| 131 | const ( |
| 132 | netConfStr = ` |
| 133 | "cniVersion": "%s", |
| 134 | "name": "testConfig", |
| 135 | "type": "bridge", |
| 136 | "bridge": "%s"` |
| 137 | |
| 138 | vlan = `, |
| 139 | "vlan": %d` |
| 140 | |
| 141 | vlanTrunkStartStr = `, |
| 142 | "vlanTrunk": [` |
| 143 | |
| 144 | vlanTrunk = ` |
| 145 | { |
| 146 | "id": %d |
| 147 | }` |
| 148 | |
| 149 | vlanTrunkRange = ` |
| 150 | { |
| 151 | "minID": %d, |
| 152 | "maxID": %d |
| 153 | }` |
| 154 | |
| 155 | vlanTrunkEndStr = ` |
| 156 | ]` |
| 157 | |
| 158 | preserveDefaultVlan = `, |
| 159 | "preserveDefaultVlan": false` |
| 160 | |
| 161 | netDefault = `, |
| 162 | "isDefaultGateway": true` |
| 163 | |
| 164 | disableContainerInterface = `, |
| 165 | "disableContainerInterface": true` |
| 166 | |
| 167 | portIsolation = `, |
| 168 | "portIsolation": true` |
| 169 | |
| 170 | ipamStartStr = `, |
| 171 | "ipam": { |
| 172 | "type": "host-local"` |
| 173 | |
| 174 | ipamDataDirStr = `, |
| 175 | "dataDir": "%s"` |
| 176 | |
| 177 | ipamResolvConfStr = `, |
| 178 | "resolvConf": "%s"` |
| 179 | |
| 180 | ipMasqConfStr = `, |
| 181 | "ipMasq": %t` |
| 182 | |
| 183 | ipMasqBackendConfStr = `, |
| 184 | "ipMasqBackend": "%s"` |
| 185 | |
| 186 | // Single subnet configuration (legacy) |
| 187 | subnetConfStr = `, |
| 188 | "subnet": "%s"` |
| 189 | gatewayConfStr = `, |
| 190 | "gateway": "%s"` |
| 191 | |
| 192 | // Ranges (multiple subnets) configuration |
| 193 | rangesStartStr = `, |
| 194 | "ranges": [` |
| 195 | rangeSubnetConfStr = ` |
| 196 | [{ |
| 197 | "subnet": "%s" |
| 198 | }]` |
| 199 | rangeSubnetGWConfStr = ` |
| 200 | [{ |
| 201 | "subnet": "%s", |
| 202 | "gateway": "%s" |
| 203 | }]` |
| 204 | rangesEndStr = ` |
| 205 | ]` |
| 206 | |
| 207 | ipamEndStr = ` |
| 208 | }` |
| 209 | |
| 210 | macspoofchkFormat = `, |
| 211 | "macspoofchk": %t` |
| 212 | |
| 213 | argsFormat = `, |
| 214 | "args": { |
| 215 | "cni": { |
| 216 | "mac": %q |
| 217 | } |
| 218 | }` |
| 219 | |
| 220 | runtimeConfig = `, |
| 221 | "RuntimeConfig": { |
| 222 | "mac": %q |
| 223 | }` |
| 224 | ) |
| 225 | |
| 226 | // netConfJSON() generates a JSON network configuration string |
| 227 | // for a test case. |
| 228 | func (tc testCase) netConfJSON(dataDir string) string { |
| 229 | conf := fmt.Sprintf(netConfStr, tc.cniVersion, BRNAME) |
| 230 | if tc.vlan != 0 { |
| 231 | conf += fmt.Sprintf(vlan, tc.vlan) |
| 232 | |
| 233 | if tc.removeDefaultVlan { |
| 234 | conf += preserveDefaultVlan |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if tc.isLayer2 && tc.vlanTrunk != nil { |
| 239 | conf += vlanTrunkStartStr |
| 240 | for i, vlan := range tc.vlanTrunk { |
| 241 | if i > 0 { |
| 242 | conf += "," |
| 243 | } |
| 244 | if vlan.ID != nil { |
| 245 | conf += fmt.Sprintf(vlanTrunk, *vlan.ID) |
| 246 | } |
| 247 | if vlan.MinID != nil && vlan.MaxID != nil { |
| 248 | conf += fmt.Sprintf(vlanTrunkRange, *vlan.MinID, *vlan.MaxID) |
| 249 | } |
| 250 | } |
| 251 | conf += vlanTrunkEndStr |
| 252 | } |
| 253 | |
| 254 | if tc.ipMasq { |
| 255 | conf += tc.ipMasqConfig() |
| 256 | } |
| 257 | if tc.ipMasqBackend != "" { |
| 258 | conf += tc.ipMasqBackendConfig() |
| 259 | } |
| 260 | if tc.args.cni.mac != "" { |
| 261 | conf += fmt.Sprintf(argsFormat, tc.args.cni.mac) |
| 262 | } |
| 263 | if tc.runtimeConfig.mac != "" { |
| 264 | conf += fmt.Sprintf(runtimeConfig, tc.runtimeConfig.mac) |
| 265 | } |
| 266 | if tc.macspoofchk { |
| 267 | conf += fmt.Sprintf(macspoofchkFormat, tc.macspoofchk) |
| 268 | } |
| 269 | |
| 270 | if tc.disableContIface { |
| 271 | conf += disableContainerInterface |
| 272 | } |
| 273 | |
| 274 | if tc.portIsolation { |
| 275 | conf += portIsolation |
| 276 | } |
| 277 | |
| 278 | if !tc.isLayer2 { |
| 279 | conf += netDefault |
| 280 | if tc.subnet != "" || tc.ranges != nil { |
| 281 | conf += ipamStartStr |
| 282 | if dataDir != "" { |
| 283 | conf += fmt.Sprintf(ipamDataDirStr, dataDir) |
| 284 | } |
| 285 | if tc.subnet != "" { |
| 286 | conf += tc.subnetConfig() |
| 287 | } |
| 288 | if tc.ranges != nil { |
| 289 | conf += tc.rangesConfig() |
| 290 | } |
| 291 | if tc.resolvConf != "" { |
| 292 | conf += tc.resolvConfConfig() |
| 293 | } |
| 294 | conf += ipamEndStr |
| 295 | } |
| 296 | } else { |
| 297 | conf += `, |
| 298 | "ipam": {}` |
| 299 | } |
| 300 | return "{" + conf + "\n}" |
| 301 | } |
| 302 | |
| 303 | func (tc testCase) subnetConfig() string { |
| 304 | conf := fmt.Sprintf(subnetConfStr, tc.subnet) |
| 305 | if tc.gateway != "" { |
| 306 | conf += fmt.Sprintf(gatewayConfStr, tc.gateway) |
| 307 | } |
| 308 | return conf |
| 309 | } |
| 310 | |
| 311 | func (tc testCase) ipMasqConfig() string { |
| 312 | conf := fmt.Sprintf(ipMasqConfStr, tc.ipMasq) |
| 313 | return conf |
| 314 | } |
| 315 | |
| 316 | func (tc testCase) ipMasqBackendConfig() string { |
| 317 | conf := fmt.Sprintf(ipMasqBackendConfStr, tc.ipMasqBackend) |
| 318 | return conf |
| 319 | } |
| 320 | |
| 321 | func (tc testCase) rangesConfig() string { |
| 322 | conf := rangesStartStr |
| 323 | for i, tcRange := range tc.ranges { |
| 324 | if i > 0 { |
| 325 | conf += "," |
| 326 | } |
| 327 | if tcRange.gateway != "" { |
| 328 | conf += fmt.Sprintf(rangeSubnetGWConfStr, tcRange.subnet, tcRange.gateway) |
| 329 | } else { |
| 330 | conf += fmt.Sprintf(rangeSubnetConfStr, tcRange.subnet) |
| 331 | } |
| 332 | } |
| 333 | return conf + rangesEndStr |
| 334 | } |
| 335 | |
| 336 | func (tc testCase) resolvConfConfig() string { |
| 337 | conf := fmt.Sprintf(ipamResolvConfStr, tc.resolvConf) |
| 338 | return conf |
| 339 | } |
| 340 | |
| 341 | func newResolvConf() (string, error) { |
| 342 | f, err := os.CreateTemp("", "host_local_resolv") |
| 343 | if err != nil { |
| 344 | return "", err |
| 345 | } |
| 346 | defer f.Close() |
| 347 | name := f.Name() |
| 348 | _, err = fmt.Fprintf(f, "nameserver %s", NAMESERVER) |
| 349 | return name, err |
| 350 | } |
| 351 | |
| 352 | func deleteResolvConf(path string) error { |
| 353 | return os.Remove(path) |
| 354 | } |
| 355 | |
| 356 | var counter uint |
| 357 | |
| 358 | // createCmdArgs generates network configuration and creates command |
| 359 | // arguments for a test case. |
| 360 | func (tc testCase) createCmdArgs(targetNS ns.NetNS, dataDir string) *skel.CmdArgs { |
| 361 | conf := tc.netConfJSON(dataDir) |
| 362 | // defer func() { counter += 1 }() |
| 363 | return &skel.CmdArgs{ |
| 364 | ContainerID: fmt.Sprintf("dummy-%d", counter), |
| 365 | Netns: targetNS.Path(), |
| 366 | IfName: IFNAME, |
| 367 | StdinData: []byte(conf), |
| 368 | Args: tc.envArgs, |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // createCheckCmdArgs generates network configuration and creates command |
| 373 | // arguments for a Check test case. |
| 374 | func (tc testCase) createCheckCmdArgs(targetNS ns.NetNS, config *Net) *skel.CmdArgs { |
| 375 | conf, err := json.Marshal(config) |
| 376 | Expect(err).NotTo(HaveOccurred()) |
| 377 | |
| 378 | // TODO Don't we need to use the same counter as before? |
| 379 | // defer func() { counter += 1 }() |
| 380 | return &skel.CmdArgs{ |
| 381 | ContainerID: fmt.Sprintf("dummy-%d", counter), |
| 382 | Netns: targetNS.Path(), |
| 383 | IfName: IFNAME, |
| 384 | StdinData: conf, |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | // expectedCIDRs determines the IPv4 and IPv6 CIDRs in which the resulting |
| 389 | // addresses are expected to be contained. |
| 390 | func (tc testCase) expectedCIDRs() ([]*net.IPNet, []*net.IPNet) { |
| 391 | var cidrsV4, cidrsV6 []*net.IPNet |
| 392 | appendSubnet := func(subnet string) { |
| 393 | ip, cidr, err := net.ParseCIDR(subnet) |
| 394 | Expect(err).NotTo(HaveOccurred()) |
| 395 | if ipVersion(ip) == "4" { |
| 396 | cidrsV4 = append(cidrsV4, cidr) |
| 397 | } else { |
| 398 | cidrsV6 = append(cidrsV6, cidr) |
| 399 | } |
| 400 | } |
| 401 | if tc.subnet != "" { |
| 402 | appendSubnet(tc.subnet) |
| 403 | } |
| 404 | for _, r := range tc.ranges { |
| 405 | appendSubnet(r.subnet) |
| 406 | } |
| 407 | return cidrsV4, cidrsV6 |
| 408 | } |
| 409 | |
| 410 | // delBridgeAddrs() deletes addresses from the bridge |
| 411 | func delBridgeAddrs(testNS ns.NetNS) { |
| 412 | err := testNS.Do(func(ns.NetNS) error { |
| 413 | defer GinkgoRecover() |
| 414 | |
| 415 | br, err := netlinksafe.LinkByName(BRNAME) |
| 416 | Expect(err).NotTo(HaveOccurred()) |
| 417 | addrs, err := netlinksafe.AddrList(br, netlink.FAMILY_ALL) |
| 418 | Expect(err).NotTo(HaveOccurred()) |
| 419 | for _, addr := range addrs { |
| 420 | if !addr.IP.IsLinkLocalUnicast() { |
| 421 | err = netlink.AddrDel(br, &addr) |
| 422 | Expect(err).NotTo(HaveOccurred()) |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | br, err = netlinksafe.LinkByName(BRNAMEVLAN) |
| 427 | if err == nil { |
| 428 | addrs, err = netlinksafe.AddrList(br, netlink.FAMILY_ALL) |
| 429 | Expect(err).NotTo(HaveOccurred()) |
| 430 | for _, addr := range addrs { |
| 431 | if !addr.IP.IsLinkLocalUnicast() { |
| 432 | err = netlink.AddrDel(br, &addr) |
| 433 | Expect(err).NotTo(HaveOccurred()) |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return nil |
| 439 | }) |
| 440 | Expect(err).NotTo(HaveOccurred()) |
| 441 | } |
| 442 | |
| 443 | func delVlanAddrs(testNS ns.NetNS, vlan int) { |
| 444 | err := testNS.Do(func(ns.NetNS) error { |
| 445 | defer GinkgoRecover() |
| 446 | |
| 447 | vlanLink, err := netlinksafe.LinkByName(fmt.Sprintf("%s.%d", BRNAME, vlan)) |
| 448 | Expect(err).NotTo(HaveOccurred()) |
| 449 | addrs, err := netlinksafe.AddrList(vlanLink, netlink.FAMILY_ALL) |
| 450 | Expect(err).NotTo(HaveOccurred()) |
| 451 | for _, addr := range addrs { |
| 452 | if !addr.IP.IsLinkLocalUnicast() { |
| 453 | err = netlink.AddrDel(vlanLink, &addr) |
| 454 | Expect(err).NotTo(HaveOccurred()) |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | return nil |
| 459 | }) |
| 460 | Expect(err).NotTo(HaveOccurred()) |
| 461 | } |
| 462 | |
| 463 | func ipVersion(ip net.IP) string { |
| 464 | if ip.To4() != nil { |
| 465 | return "4" |
| 466 | } |
| 467 | return "6" |
| 468 | } |
| 469 | |
| 470 | func countIPAMIPs(path string) (int, error) { |
| 471 | count := 0 |
| 472 | entries, err := os.ReadDir(path) |
| 473 | if err != nil { |
| 474 | return -1, err |
| 475 | } |
| 476 | for _, entry := range entries { |
| 477 | if entry.IsDir() { |
| 478 | continue |
| 479 | } |
| 480 | |
| 481 | if net.ParseIP(entry.Name()) != nil { |
| 482 | count++ |
| 483 | } |
| 484 | } |
| 485 | return count, nil |
| 486 | } |
| 487 | |
| 488 | func checkVlan(vlanID int, bridgeVlanInfo []*nl.BridgeVlanInfo) bool { |
| 489 | for _, vlan := range bridgeVlanInfo { |
| 490 | if vlan.Vid == uint16(vlanID) { |
| 491 | return true |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | return false |
| 496 | } |
| 497 | |
| 498 | type cmdAddDelTester interface { |
| 499 | cmdAddTest(tc testCase, dataDir string) (types.Result, error) |
| 500 | cmdCheckTest(tc testCase, conf *Net, dataDir string) |
| 501 | cmdDelTest(tc testCase, dataDir string) |
| 502 | } |
| 503 | |
| 504 | type testerBase struct { |
| 505 | testNS ns.NetNS |
| 506 | targetNS ns.NetNS |
| 507 | args *skel.CmdArgs |
| 508 | vethName string |
| 509 | } |
| 510 | |
| 511 | type ( |
| 512 | testerV10x testerBase |
| 513 | testerV04x testerBase |
| 514 | testerV03x testerBase |
| 515 | testerV01xOr02x testerBase |
| 516 | ) |
| 517 | |
| 518 | func newTesterByVersion(version string, testNS, targetNS ns.NetNS) cmdAddDelTester { |
| 519 | switch { |
| 520 | case strings.HasPrefix(version, "1.1."): |
| 521 | return &testerV10x{ |
| 522 | testNS: testNS, |
| 523 | targetNS: targetNS, |
| 524 | } |
| 525 | case strings.HasPrefix(version, "1.0."): |
| 526 | return &testerV10x{ |
| 527 | testNS: testNS, |
| 528 | targetNS: targetNS, |
| 529 | } |
| 530 | case strings.HasPrefix(version, "0.4."): |
| 531 | return &testerV04x{ |
| 532 | testNS: testNS, |
| 533 | targetNS: targetNS, |
| 534 | } |
| 535 | case strings.HasPrefix(version, "0.3."): |
| 536 | return &testerV03x{ |
| 537 | testNS: testNS, |
| 538 | targetNS: targetNS, |
| 539 | } |
| 540 | default: |
| 541 | return &testerV01xOr02x{ |
| 542 | testNS: testNS, |
| 543 | targetNS: targetNS, |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | func (tester *testerV10x) cmdAddTest(tc testCase, dataDir string) (types.Result, error) { |
| 549 | // Generate network config and command arguments |
| 550 | tester.args = tc.createCmdArgs(tester.targetNS, dataDir) |
| 551 | |
| 552 | // Execute cmdADD on the plugin |
| 553 | var result *types100.Result |
| 554 | err := tester.testNS.Do(func(ns.NetNS) error { |
| 555 | defer GinkgoRecover() |
| 556 | |
| 557 | r, raw, err := testutils.CmdAddWithArgs(tester.args, func() error { |
| 558 | return cmdAdd(tester.args) |
| 559 | }) |
| 560 | Expect(err).NotTo(HaveOccurred()) |
| 561 | Expect(strings.Index(string(raw), "\"interfaces\":")).Should(BeNumerically(">", 0)) |
| 562 | |
| 563 | resultType, err := r.GetAsVersion(tc.cniVersion) |
| 564 | Expect(err).NotTo(HaveOccurred()) |
| 565 | result = resultType.(*types100.Result) |
| 566 | |
| 567 | if !tc.isLayer2 && tc.vlan != 0 { |
| 568 | Expect(result.Interfaces).To(HaveLen(4)) |
| 569 | } else { |
| 570 | Expect(result.Interfaces).To(HaveLen(3)) |
| 571 | } |
| 572 | |
| 573 | Expect(result.Interfaces[0].Name).To(Equal(BRNAME)) |
| 574 | Expect(result.Interfaces[0].Mac).To(HaveLen(17)) |
| 575 | |
| 576 | Expect(result.Interfaces[1].Name).To(HavePrefix("veth")) |
| 577 | Expect(result.Interfaces[1].Mac).To(HaveLen(17)) |
| 578 | |
| 579 | Expect(result.Interfaces[2].Name).To(Equal(IFNAME)) |
| 580 | Expect(result.Interfaces[2].Mac).To(HaveLen(17)) |
| 581 | if tc.expectedMac != "" { |
| 582 | Expect(result.Interfaces[2].Mac).To(Equal(tc.expectedMac)) |
| 583 | } |
| 584 | Expect(result.Interfaces[2].Sandbox).To(Equal(tester.targetNS.Path())) |
| 585 | |
| 586 | // Make sure bridge link exists |
| 587 | link, err := netlinksafe.LinkByName(result.Interfaces[0].Name) |
| 588 | Expect(err).NotTo(HaveOccurred()) |
| 589 | Expect(link.Attrs().Name).To(Equal(BRNAME)) |
| 590 | Expect(link).To(BeAssignableToTypeOf(&netlink.Bridge{})) |
| 591 | Expect(link.Attrs().HardwareAddr.String()).To(Equal(result.Interfaces[0].Mac)) |
| 592 | bridgeMAC := link.Attrs().HardwareAddr.String() |
| 593 | |
| 594 | var vlanLink netlink.Link |
| 595 | if !tc.isLayer2 && tc.vlan != 0 { |
| 596 | // Make sure vlan link exists |
| 597 | vlanLink, err = netlinksafe.LinkByName(fmt.Sprintf("%s.%d", BRNAME, tc.vlan)) |
| 598 | Expect(err).NotTo(HaveOccurred()) |
| 599 | Expect(vlanLink.Attrs().Name).To(Equal(fmt.Sprintf("%s.%d", BRNAME, tc.vlan))) |
| 600 | Expect(vlanLink).To(BeAssignableToTypeOf(&netlink.Veth{})) |
| 601 | |
| 602 | // Check the bridge dot vlan interface have the vlan tag |
| 603 | peerLink, err := netlink.LinkByIndex(vlanLink.Attrs().Index - 1) |
| 604 | Expect(err).NotTo(HaveOccurred()) |
| 605 | interfaceMap, err := netlinksafe.BridgeVlanList() |
| 606 | Expect(err).NotTo(HaveOccurred()) |
| 607 | vlans, isExist := interfaceMap[int32(peerLink.Attrs().Index)] |
| 608 | Expect(isExist).To(BeTrue()) |
| 609 | Expect(checkVlan(tc.vlan, vlans)).To(BeTrue()) |
| 610 | if tc.removeDefaultVlan { |
| 611 | Expect(vlans).To(HaveLen(1)) |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // Check the bridge vlan filtering equals true |
| 616 | if tc.vlan != 0 || tc.vlanTrunk != nil { |
| 617 | Expect(*link.(*netlink.Bridge).VlanFiltering).To(BeTrue()) |
| 618 | } else { |
| 619 | Expect(*link.(*netlink.Bridge).VlanFiltering).To(BeFalse()) |
| 620 | } |
| 621 | |
| 622 | // Ensure bridge has expected gateway address(es) |
| 623 | var addrs []netlink.Addr |
| 624 | if tc.vlan == 0 { |
| 625 | addrs, err = netlinksafe.AddrList(link, netlink.FAMILY_ALL) |
| 626 | } else { |
| 627 | addrs, err = netlinksafe.AddrList(vlanLink, netlink.FAMILY_ALL) |
| 628 | } |
| 629 | Expect(err).NotTo(HaveOccurred()) |
| 630 | Expect(addrs).ToNot(BeEmpty()) |
| 631 | for _, cidr := range tc.expGWCIDRs { |
| 632 | ip, subnet, err := net.ParseCIDR(cidr) |
| 633 | Expect(err).NotTo(HaveOccurred()) |
| 634 | |
| 635 | found := false |
| 636 | subnetPrefix, subnetBits := subnet.Mask.Size() |
| 637 | for _, a := range addrs { |
| 638 | aPrefix, aBits := a.IPNet.Mask.Size() |
| 639 | if a.IPNet.IP.Equal(ip) && aPrefix == subnetPrefix && aBits == subnetBits { |
| 640 | found = true |
| 641 | break |
| 642 | } |
| 643 | } |
| 644 | Expect(found).To(BeTrue(), fmt.Sprintf("failed to find %s", cidr)) |
| 645 | } |
| 646 | |
| 647 | // Check for the veth link in the main namespace |
| 648 | links, err := netlinksafe.LinkList() |
| 649 | Expect(err).NotTo(HaveOccurred()) |
| 650 | if !tc.isLayer2 && tc.vlan != 0 { |
| 651 | Expect(links).To(HaveLen(5)) // Bridge, Bridge vlan veth, veth, and loopback |
| 652 | } else { |
| 653 | Expect(links).To(HaveLen(3)) // Bridge, veth, and loopback |
| 654 | } |
| 655 | |
| 656 | link, err = netlinksafe.LinkByName(result.Interfaces[1].Name) |
| 657 | Expect(err).NotTo(HaveOccurred()) |
| 658 | Expect(link).To(BeAssignableToTypeOf(&netlink.Veth{})) |
| 659 | tester.vethName = result.Interfaces[1].Name |
| 660 | |
| 661 | protInfo, err := netlinksafe.LinkGetProtinfo(link) |
| 662 | Expect(err).NotTo(HaveOccurred()) |
| 663 | Expect(protInfo.Isolated).To(Equal(tc.portIsolation), "link isolation should be on when portIsolation is set") |
| 664 | |
| 665 | // check vlan exist on the veth interface |
| 666 | if tc.vlan != 0 { |
| 667 | interfaceMap, err := netlinksafe.BridgeVlanList() |
| 668 | Expect(err).NotTo(HaveOccurred()) |
| 669 | vlans, isExist := interfaceMap[int32(link.Attrs().Index)] |
| 670 | Expect(isExist).To(BeTrue()) |
| 671 | Expect(checkVlan(tc.vlan, vlans)).To(BeTrue()) |
| 672 | if tc.removeDefaultVlan { |
| 673 | Expect(vlans).To(HaveLen(1)) |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | // check VlanTrunks exist on the veth interface |
| 678 | if tc.vlanTrunk != nil { |
| 679 | interfaceMap, err := netlinksafe.BridgeVlanList() |
| 680 | Expect(err).NotTo(HaveOccurred()) |
| 681 | vlans, isExist := interfaceMap[int32(link.Attrs().Index)] |
| 682 | Expect(isExist).To(BeTrue()) |
| 683 | |
| 684 | for _, vlanEntry := range tc.vlanTrunk { |
| 685 | if vlanEntry.ID != nil { |
| 686 | Expect(checkVlan(*vlanEntry.ID, vlans)).To(BeTrue()) |
| 687 | } |
| 688 | if vlanEntry.MinID != nil && vlanEntry.MaxID != nil { |
| 689 | for vid := *vlanEntry.MinID; vid <= *vlanEntry.MaxID; vid++ { |
| 690 | Expect(checkVlan(vid, vlans)).To(BeTrue()) |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | // Check native vlan |
| 696 | nativeVlan := tc.vlan |
| 697 | if tc.vlan == 0 { |
| 698 | nativeVlan = 1 |
| 699 | } |
| 700 | Expect(checkVlan(nativeVlan, vlans)).To(BeTrue()) |
| 701 | } |
| 702 | |
| 703 | // Check that the bridge has a different mac from the veth |
| 704 | // If not, it means the bridge has an unstable mac and will change |
| 705 | // as ifs are added and removed |
| 706 | // this check is not relevant for a layer 2 bridge |
| 707 | if !tc.isLayer2 && tc.vlan == 0 { |
| 708 | Expect(link.Attrs().HardwareAddr.String()).NotTo(Equal(bridgeMAC)) |
| 709 | } |
| 710 | |
| 711 | // Check that resolvConf was used properly |
| 712 | if !tc.isLayer2 && tc.resolvConf != "" { |
| 713 | Expect(result.DNS.Nameservers).To(Equal([]string{NAMESERVER})) |
| 714 | } |
| 715 | |
| 716 | return nil |
| 717 | }) |
| 718 | Expect(err).NotTo(HaveOccurred()) |
| 719 | |
| 720 | // Find the veth peer in the container namespace and the default route |
| 721 | err = tester.targetNS.Do(func(ns.NetNS) error { |
| 722 | defer GinkgoRecover() |
| 723 | |
| 724 | link, err := netlinksafe.LinkByName(IFNAME) |
| 725 | Expect(err).NotTo(HaveOccurred()) |
| 726 | Expect(link.Attrs().Name).To(Equal(IFNAME)) |
| 727 | Expect(link).To(BeAssignableToTypeOf(&netlink.Veth{})) |
| 728 | assertContainerInterfaceLinkState(&tc, link) |
| 729 | |
| 730 | expCIDRsV4, expCIDRsV6 := tc.expectedCIDRs() |
| 731 | addrs, err := netlinksafe.AddrList(link, netlink.FAMILY_V4) |
| 732 | Expect(err).NotTo(HaveOccurred()) |
| 733 | Expect(addrs).To(HaveLen(len(expCIDRsV4))) |
| 734 | addrs, err = netlinksafe.AddrList(link, netlink.FAMILY_V6) |
| 735 | Expect(err).NotTo(HaveOccurred()) |
| 736 | assertIPv6Addresses(&tc, addrs, expCIDRsV6) |
| 737 | |
| 738 | // Ignore link local address which may or may not be |
| 739 | // ready when we read addresses. |
| 740 | var foundAddrs int |
| 741 | for _, addr := range addrs { |
| 742 | if !addr.IP.IsLinkLocalUnicast() { |
| 743 | foundAddrs++ |
| 744 | } |
| 745 | } |
| 746 | Expect(foundAddrs).To(Equal(len(expCIDRsV6))) |
| 747 | |
| 748 | // Ensure the default route(s) |
| 749 | routes, err := netlinksafe.RouteList(link, 0) |
| 750 | Expect(err).NotTo(HaveOccurred()) |
| 751 | |
| 752 | var defaultRouteFound4, defaultRouteFound6 bool |
| 753 | for _, cidr := range tc.expGWCIDRs { |
| 754 | gwIP, _, err := net.ParseCIDR(cidr) |
| 755 | Expect(err).NotTo(HaveOccurred()) |
| 756 | var found *bool |
| 757 | if ipVersion(gwIP) == "4" { |
| 758 | found = &defaultRouteFound4 |
| 759 | } else { |
| 760 | found = &defaultRouteFound6 |
| 761 | } |
| 762 | if *found == true { |
| 763 | continue |
| 764 | } |
| 765 | for _, route := range routes { |
| 766 | *found = (ip.IsIPNetZero(route.Dst) && route.Src == nil && route.Gw.Equal(gwIP)) |
| 767 | if *found { |
| 768 | break |
| 769 | } |
| 770 | } |
| 771 | Expect(*found).To(BeTrue()) |
| 772 | } |
| 773 | |
| 774 | return nil |
| 775 | }) |
| 776 | Expect(err).NotTo(HaveOccurred()) |
| 777 | |
| 778 | return result, nil |
| 779 | } |
| 780 | |
| 781 | func assertContainerInterfaceLinkState(tc *testCase, link netlink.Link) { |
| 782 | linkState := int(link.Attrs().OperState) |
| 783 | if tc.disableContIface { |
| 784 | Expect(linkState).ToNot(Equal(netlink.OperUp)) |
| 785 | } else { |
| 786 | Expect(linkState).To(Equal(netlink.OperUp)) |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | func (tester *testerV10x) cmdCheckTest(tc testCase, conf *Net, _ string) { |
| 791 | // Generate network config and command arguments |
| 792 | tester.args = tc.createCheckCmdArgs(tester.targetNS, conf) |
| 793 | |
| 794 | // Execute cmdCHECK on the plugin |
| 795 | err := tester.testNS.Do(func(ns.NetNS) error { |
| 796 | defer GinkgoRecover() |
| 797 | |
| 798 | err := testutils.CmdCheckWithArgs(tester.args, func() error { |
| 799 | return cmdCheck(tester.args) |
| 800 | }) |
| 801 | Expect(err).NotTo(HaveOccurred()) |
| 802 | |
| 803 | return nil |
| 804 | }) |
| 805 | Expect(err).NotTo(HaveOccurred()) |
| 806 | |
| 807 | // Find the veth peer in the container namespace and the default route |
| 808 | err = tester.targetNS.Do(func(ns.NetNS) error { |
| 809 | defer GinkgoRecover() |
| 810 | |
| 811 | link, err := netlinksafe.LinkByName(IFNAME) |
| 812 | Expect(err).NotTo(HaveOccurred()) |
| 813 | Expect(link.Attrs().Name).To(Equal(IFNAME)) |
| 814 | Expect(link).To(BeAssignableToTypeOf(&netlink.Veth{})) |
| 815 | |
| 816 | expCIDRsV4, expCIDRsV6 := tc.expectedCIDRs() |
| 817 | addrs, err := netlinksafe.AddrList(link, netlink.FAMILY_V4) |
| 818 | Expect(err).NotTo(HaveOccurred()) |
| 819 | Expect(addrs).To(HaveLen(len(expCIDRsV4))) |
| 820 | addrs, err = netlinksafe.AddrList(link, netlink.FAMILY_V6) |
| 821 | Expect(addrs).To(HaveLen(len(expCIDRsV6) + 1)) // add one for the link-local |
| 822 | Expect(err).NotTo(HaveOccurred()) |
| 823 | // Ignore link local address which may or may not be |
| 824 | // ready when we read addresses. |
| 825 | var foundAddrs int |
| 826 | for _, addr := range addrs { |
| 827 | if !addr.IP.IsLinkLocalUnicast() { |
| 828 | foundAddrs++ |
| 829 | } |
| 830 | } |
| 831 | Expect(foundAddrs).To(Equal(len(expCIDRsV6))) |
| 832 | |
| 833 | // Ensure the default route(s) |
| 834 | routes, err := netlinksafe.RouteList(link, 0) |
| 835 | Expect(err).NotTo(HaveOccurred()) |
| 836 | |
| 837 | var defaultRouteFound4, defaultRouteFound6 bool |
| 838 | for _, cidr := range tc.expGWCIDRs { |
| 839 | gwIP, _, err := net.ParseCIDR(cidr) |
| 840 | Expect(err).NotTo(HaveOccurred()) |
| 841 | var found *bool |
| 842 | if ipVersion(gwIP) == "4" { |
| 843 | found = &defaultRouteFound4 |
| 844 | } else { |
| 845 | found = &defaultRouteFound6 |
| 846 | } |
| 847 | if *found == true { |
| 848 | continue |
| 849 | } |
| 850 | for _, route := range routes { |
| 851 | *found = (ip.IsIPNetZero(route.Dst) && route.Src == nil && route.Gw.Equal(gwIP)) |
| 852 | if *found { |
| 853 | break |
| 854 | } |
| 855 | } |
| 856 | Expect(*found).To(BeTrue()) |
| 857 | } |
| 858 | |
| 859 | return nil |
| 860 | }) |
| 861 | Expect(err).NotTo(HaveOccurred()) |
| 862 | } |
| 863 | |
| 864 | func (tester *testerV10x) cmdDelTest(tc testCase, dataDir string) { |
| 865 | tester.args = tc.createCmdArgs(tester.targetNS, dataDir) |
| 866 | err := tester.testNS.Do(func(ns.NetNS) error { |
| 867 | defer GinkgoRecover() |
| 868 | |
| 869 | err := testutils.CmdDelWithArgs(tester.args, func() error { |
| 870 | return cmdDel(tester.args) |
| 871 | }) |
| 872 | Expect(err).NotTo(HaveOccurred()) |
| 873 | return nil |
| 874 | }) |
| 875 | Expect(err).NotTo(HaveOccurred()) |
| 876 | |
| 877 | // Make sure the host veth has been deleted |
| 878 | err = tester.targetNS.Do(func(ns.NetNS) error { |
| 879 | defer GinkgoRecover() |
| 880 | |
| 881 | link, err := netlinksafe.LinkByName(IFNAME) |
| 882 | Expect(err).To(HaveOccurred()) |
| 883 | Expect(link).To(BeNil()) |
| 884 | return nil |
| 885 | }) |
| 886 | Expect(err).NotTo(HaveOccurred()) |
| 887 | |
| 888 | // Make sure the container veth has been deleted |
| 889 | err = tester.testNS.Do(func(ns.NetNS) error { |
| 890 | defer GinkgoRecover() |
| 891 | |
| 892 | link, err := netlinksafe.LinkByName(tester.vethName) |
| 893 | Expect(err).To(HaveOccurred()) |
| 894 | Expect(link).To(BeNil()) |
| 895 | return nil |
| 896 | }) |
| 897 | Expect(err).NotTo(HaveOccurred()) |
| 898 | } |
| 899 | |
| 900 | func (tester *testerV04x) cmdAddTest(tc testCase, dataDir string) (types.Result, error) { |
| 901 | // Generate network config and command arguments |
| 902 | tester.args = tc.createCmdArgs(tester.targetNS, dataDir) |
| 903 | |
| 904 | // Execute cmdADD on the plugin |
| 905 | var result *types040.Result |
| 906 | err := tester.testNS.Do(func(ns.NetNS) error { |
| 907 | defer GinkgoRecover() |
| 908 | |
| 909 | r, raw, err := testutils.CmdAddWithArgs(tester.args, func() error { |
| 910 | return cmdAdd(tester.args) |
| 911 | }) |
| 912 | Expect(err).NotTo(HaveOccurred()) |
| 913 | Expect(strings.Index(string(raw), "\"interfaces\":")).Should(BeNumerically(">", 0)) |
| 914 | |
| 915 | resultType, err := r.GetAsVersion(tc.cniVersion) |
| 916 | Expect(err).NotTo(HaveOccurred()) |
| 917 | result = resultType.(*types040.Result) |
| 918 | |
| 919 | if !tc.isLayer2 && tc.vlan != 0 { |
| 920 | Expect(result.Interfaces).To(HaveLen(4)) |
| 921 | } else { |
| 922 | Expect(result.Interfaces).To(HaveLen(3)) |
| 923 | } |
| 924 | |
| 925 | Expect(result.Interfaces[0].Name).To(Equal(BRNAME)) |
| 926 | Expect(result.Interfaces[0].Mac).To(HaveLen(17)) |
| 927 | |
| 928 | Expect(result.Interfaces[1].Name).To(HavePrefix("veth")) |
| 929 | Expect(result.Interfaces[1].Mac).To(HaveLen(17)) |
| 930 | |
| 931 | Expect(result.Interfaces[2].Name).To(Equal(IFNAME)) |
| 932 | Expect(result.Interfaces[2].Mac).To(HaveLen(17)) |
| 933 | if tc.expectedMac != "" { |
| 934 | Expect(result.Interfaces[2].Mac).To(Equal(tc.expectedMac)) |
| 935 | } |
| 936 | Expect(result.Interfaces[2].Sandbox).To(Equal(tester.targetNS.Path())) |
| 937 | |
| 938 | // Make sure bridge link exists |
| 939 | link, err := netlinksafe.LinkByName(result.Interfaces[0].Name) |
| 940 | Expect(err).NotTo(HaveOccurred()) |
| 941 | Expect(link.Attrs().Name).To(Equal(BRNAME)) |
| 942 | Expect(link).To(BeAssignableToTypeOf(&netlink.Bridge{})) |
| 943 | Expect(link.Attrs().HardwareAddr.String()).To(Equal(result.Interfaces[0].Mac)) |
| 944 | bridgeMAC := link.Attrs().HardwareAddr.String() |
| 945 | |
| 946 | var vlanLink netlink.Link |
| 947 | if !tc.isLayer2 && tc.vlan != 0 { |
| 948 | // Make sure vlan link exists |
| 949 | vlanLink, err = netlinksafe.LinkByName(fmt.Sprintf("%s.%d", BRNAME, tc.vlan)) |
| 950 | Expect(err).NotTo(HaveOccurred()) |
| 951 | Expect(vlanLink.Attrs().Name).To(Equal(fmt.Sprintf("%s.%d", BRNAME, tc.vlan))) |
| 952 | Expect(vlanLink).To(BeAssignableToTypeOf(&netlink.Veth{})) |
| 953 | |
| 954 | // Check the bridge dot vlan interface have the vlan tag |
| 955 | peerLink, err := netlink.LinkByIndex(vlanLink.Attrs().Index - 1) |
| 956 | Expect(err).NotTo(HaveOccurred()) |
| 957 | interfaceMap, err := netlinksafe.BridgeVlanList() |
| 958 | Expect(err).NotTo(HaveOccurred()) |
| 959 | vlans, isExist := interfaceMap[int32(peerLink.Attrs().Index)] |
| 960 | Expect(isExist).To(BeTrue()) |
| 961 | Expect(checkVlan(tc.vlan, vlans)).To(BeTrue()) |
| 962 | if tc.removeDefaultVlan { |
| 963 | Expect(vlans).To(HaveLen(1)) |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | // Check the bridge vlan filtering equals true |
| 968 | if tc.vlan != 0 || tc.vlanTrunk != nil { |
| 969 | Expect(*link.(*netlink.Bridge).VlanFiltering).To(BeTrue()) |
| 970 | } else { |
| 971 | Expect(*link.(*netlink.Bridge).VlanFiltering).To(BeFalse()) |
| 972 | } |
| 973 | |
| 974 | // Ensure bridge has expected gateway address(es) |
| 975 | var addrs []netlink.Addr |
| 976 | if tc.vlan == 0 { |
| 977 | addrs, err = netlinksafe.AddrList(link, netlink.FAMILY_ALL) |
| 978 | } else { |
| 979 | addrs, err = netlinksafe.AddrList(vlanLink, netlink.FAMILY_ALL) |
| 980 | } |
| 981 | Expect(err).NotTo(HaveOccurred()) |
| 982 | Expect(addrs).ToNot(BeEmpty()) |
| 983 | for _, cidr := range tc.expGWCIDRs { |
| 984 | ip, subnet, err := net.ParseCIDR(cidr) |
| 985 | Expect(err).NotTo(HaveOccurred()) |
| 986 | |
| 987 | found := false |
| 988 | subnetPrefix, subnetBits := subnet.Mask.Size() |
| 989 | for _, a := range addrs { |
| 990 | aPrefix, aBits := a.IPNet.Mask.Size() |
| 991 | if a.IPNet.IP.Equal(ip) && aPrefix == subnetPrefix && aBits == subnetBits { |
| 992 | found = true |
| 993 | break |
| 994 | } |
| 995 | } |
| 996 | Expect(found).To(BeTrue()) |
| 997 | } |
| 998 | |
| 999 | // Check for the veth link in the main namespace |
| 1000 | links, err := netlinksafe.LinkList() |
| 1001 | Expect(err).NotTo(HaveOccurred()) |
| 1002 | if !tc.isLayer2 && tc.vlan != 0 { |
| 1003 | Expect(links).To(HaveLen(5)) // Bridge, Bridge vlan veth, veth, and loopback |
| 1004 | } else { |
| 1005 | Expect(links).To(HaveLen(3)) // Bridge, veth, and loopback |
| 1006 | } |
| 1007 | |
| 1008 | link, err = netlinksafe.LinkByName(result.Interfaces[1].Name) |
| 1009 | Expect(err).NotTo(HaveOccurred()) |
| 1010 | Expect(link).To(BeAssignableToTypeOf(&netlink.Veth{})) |
| 1011 | tester.vethName = result.Interfaces[1].Name |
| 1012 | |
| 1013 | // check vlan exist on the veth interface |
| 1014 | if tc.vlan != 0 { |
| 1015 | interfaceMap, err := netlinksafe.BridgeVlanList() |
| 1016 | Expect(err).NotTo(HaveOccurred()) |
| 1017 | vlans, isExist := interfaceMap[int32(link.Attrs().Index)] |
| 1018 | Expect(isExist).To(BeTrue()) |
| 1019 | Expect(checkVlan(tc.vlan, vlans)).To(BeTrue()) |
| 1020 | if tc.removeDefaultVlan { |
| 1021 | Expect(vlans).To(HaveLen(1)) |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | // check VlanTrunks exist on the veth interface |
| 1026 | if tc.vlanTrunk != nil { |
| 1027 | interfaceMap, err := netlinksafe.BridgeVlanList() |
| 1028 | Expect(err).NotTo(HaveOccurred()) |
| 1029 | vlans, isExist := interfaceMap[int32(link.Attrs().Index)] |
| 1030 | Expect(isExist).To(BeTrue()) |
| 1031 | |
| 1032 | for _, vlanEntry := range tc.vlanTrunk { |
| 1033 | if vlanEntry.ID != nil { |
| 1034 | Expect(checkVlan(*vlanEntry.ID, vlans)).To(BeTrue()) |
| 1035 | } |
| 1036 | if vlanEntry.MinID != nil && vlanEntry.MaxID != nil { |
| 1037 | for vid := *vlanEntry.MinID; vid <= *vlanEntry.MaxID; vid++ { |
| 1038 | Expect(checkVlan(vid, vlans)).To(BeTrue()) |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | // Check native vlan |
| 1044 | nativeVlan := tc.vlan |
| 1045 | if tc.vlan == 0 { |
| 1046 | nativeVlan = 1 |
| 1047 | } |
| 1048 | Expect(checkVlan(nativeVlan, vlans)).To(BeTrue()) |
| 1049 | } |
| 1050 | |
| 1051 | // Check that the bridge has a different mac from the veth |
| 1052 | // If not, it means the bridge has an unstable mac and will change |
| 1053 | // as ifs are added and removed |
| 1054 | // this check is not relevant for a layer 2 bridge |
| 1055 | if !tc.isLayer2 && tc.vlan == 0 { |
| 1056 | Expect(link.Attrs().HardwareAddr.String()).NotTo(Equal(bridgeMAC)) |
| 1057 | } |
| 1058 | |
| 1059 | return nil |
| 1060 | }) |
| 1061 | Expect(err).NotTo(HaveOccurred()) |
| 1062 | |
| 1063 | // Find the veth peer in the container namespace and the default route |
| 1064 | err = tester.targetNS.Do(func(ns.NetNS) error { |
| 1065 | defer GinkgoRecover() |
| 1066 | |
| 1067 | link, err := netlinksafe.LinkByName(IFNAME) |
| 1068 | Expect(err).NotTo(HaveOccurred()) |
| 1069 | Expect(link.Attrs().Name).To(Equal(IFNAME)) |
| 1070 | Expect(link).To(BeAssignableToTypeOf(&netlink.Veth{})) |
| 1071 | |
| 1072 | expCIDRsV4, expCIDRsV6 := tc.expectedCIDRs() |
| 1073 | addrs, err := netlinksafe.AddrList(link, netlink.FAMILY_V4) |
| 1074 | Expect(err).NotTo(HaveOccurred()) |
| 1075 | Expect(addrs).To(HaveLen(len(expCIDRsV4))) |
| 1076 | addrs, err = netlinksafe.AddrList(link, netlink.FAMILY_V6) |
| 1077 | Expect(err).NotTo(HaveOccurred()) |
| 1078 | assertIPv6Addresses(&tc, addrs, expCIDRsV6) |
| 1079 | |
| 1080 | // Ignore link local address which may or may not be |
| 1081 | // ready when we read addresses. |
| 1082 | var foundAddrs int |
| 1083 | for _, addr := range addrs { |
| 1084 | if !addr.IP.IsLinkLocalUnicast() { |
| 1085 | foundAddrs++ |
| 1086 | } |
| 1087 | } |
| 1088 | Expect(foundAddrs).To(Equal(len(expCIDRsV6))) |
| 1089 | |
| 1090 | // Ensure the default route(s) |
| 1091 | routes, err := netlinksafe.RouteList(link, 0) |
| 1092 | Expect(err).NotTo(HaveOccurred()) |
| 1093 | |
| 1094 | var defaultRouteFound4, defaultRouteFound6 bool |
| 1095 | for _, cidr := range tc.expGWCIDRs { |
| 1096 | gwIP, _, err := net.ParseCIDR(cidr) |
| 1097 | Expect(err).NotTo(HaveOccurred()) |
| 1098 | var found *bool |
| 1099 | if ipVersion(gwIP) == "4" { |
| 1100 | found = &defaultRouteFound4 |
| 1101 | } else { |
| 1102 | found = &defaultRouteFound6 |
| 1103 | } |
| 1104 | if *found == true { |
| 1105 | continue |
| 1106 | } |
| 1107 | for _, route := range routes { |
| 1108 | *found = (ip.IsIPNetZero(route.Dst) && route.Src == nil && route.Gw.Equal(gwIP)) |
| 1109 | if *found { |
| 1110 | break |
| 1111 | } |
| 1112 | } |
| 1113 | Expect(*found).To(BeTrue()) |
| 1114 | } |
| 1115 | |
| 1116 | return nil |
| 1117 | }) |
| 1118 | Expect(err).NotTo(HaveOccurred()) |
| 1119 | |
| 1120 | return result, nil |
| 1121 | } |
| 1122 | |
| 1123 | func assertIPv6Addresses(tc *testCase, addrs []netlink.Addr, expCIDRsV6 []*net.IPNet) { |
| 1124 | if tc.disableContIface { |
| 1125 | Expect(addrs).To(BeEmpty()) |
| 1126 | } else { |
| 1127 | Expect(addrs).To(HaveLen(len(expCIDRsV6) + 1)) // add one for the link-local |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | func (tester *testerV04x) cmdCheckTest(tc testCase, conf *Net, _ string) { |
| 1132 | // Generate network config and command arguments |
| 1133 | tester.args = tc.createCheckCmdArgs(tester.targetNS, conf) |
| 1134 | |
| 1135 | // Execute cmdCHECK on the plugin |
| 1136 | err := tester.testNS.Do(func(ns.NetNS) error { |
| 1137 | defer GinkgoRecover() |
| 1138 | |
| 1139 | err := testutils.CmdCheckWithArgs(tester.args, func() error { |
| 1140 | return cmdCheck(tester.args) |
| 1141 | }) |
| 1142 | Expect(err).NotTo(HaveOccurred()) |
| 1143 | |
| 1144 | return nil |
| 1145 | }) |
| 1146 | Expect(err).NotTo(HaveOccurred()) |
| 1147 | |
| 1148 | // Find the veth peer in the container namespace and the default route |
| 1149 | err = tester.targetNS.Do(func(ns.NetNS) error { |
| 1150 | defer GinkgoRecover() |
| 1151 | |
| 1152 | link, err := netlinksafe.LinkByName(IFNAME) |
| 1153 | Expect(err).NotTo(HaveOccurred()) |
| 1154 | Expect(link.Attrs().Name).To(Equal(IFNAME)) |
| 1155 | Expect(link).To(BeAssignableToTypeOf(&netlink.Veth{})) |
| 1156 | |
| 1157 | expCIDRsV4, expCIDRsV6 := tc.expectedCIDRs() |
| 1158 | addrs, err := netlinksafe.AddrList(link, netlink.FAMILY_V4) |
| 1159 | Expect(err).NotTo(HaveOccurred()) |
| 1160 | Expect(addrs).To(HaveLen(len(expCIDRsV4))) |
| 1161 | addrs, err = netlinksafe.AddrList(link, netlink.FAMILY_V6) |
| 1162 | Expect(addrs).To(HaveLen(len(expCIDRsV6) + 1)) // add one for the link-local |
| 1163 | Expect(err).NotTo(HaveOccurred()) |
| 1164 | // Ignore link local address which may or may not be |
| 1165 | // ready when we read addresses. |
| 1166 | var foundAddrs int |
| 1167 | for _, addr := range addrs { |
| 1168 | if !addr.IP.IsLinkLocalUnicast() { |
| 1169 | foundAddrs++ |
| 1170 | } |
| 1171 | } |
| 1172 | Expect(foundAddrs).To(Equal(len(expCIDRsV6))) |
| 1173 | |
| 1174 | // Ensure the default route(s) |
| 1175 | routes, err := netlinksafe.RouteList(link, 0) |
| 1176 | Expect(err).NotTo(HaveOccurred()) |
| 1177 | |
| 1178 | var defaultRouteFound4, defaultRouteFound6 bool |
| 1179 | for _, cidr := range tc.expGWCIDRs { |
| 1180 | gwIP, _, err := net.ParseCIDR(cidr) |
| 1181 | Expect(err).NotTo(HaveOccurred()) |
| 1182 | var found *bool |
| 1183 | if ipVersion(gwIP) == "4" { |
| 1184 | found = &defaultRouteFound4 |
| 1185 | } else { |
| 1186 | found = &defaultRouteFound6 |
| 1187 | } |
| 1188 | if *found == true { |
| 1189 | continue |
| 1190 | } |
| 1191 | for _, route := range routes { |
| 1192 | *found = (ip.IsIPNetZero(route.Dst) && route.Src == nil && route.Gw.Equal(gwIP)) |
| 1193 | if *found { |
| 1194 | break |
| 1195 | } |
| 1196 | } |
| 1197 | Expect(*found).To(BeTrue()) |
| 1198 | } |
| 1199 | |
| 1200 | return nil |
| 1201 | }) |
| 1202 | Expect(err).NotTo(HaveOccurred()) |
| 1203 | } |
| 1204 | |
| 1205 | func (tester *testerV04x) cmdDelTest(tc testCase, dataDir string) { |
| 1206 | tester.args = tc.createCmdArgs(tester.targetNS, dataDir) |
| 1207 | err := tester.testNS.Do(func(ns.NetNS) error { |
| 1208 | defer GinkgoRecover() |
| 1209 | |
| 1210 | err := testutils.CmdDelWithArgs(tester.args, func() error { |
| 1211 | return cmdDel(tester.args) |
| 1212 | }) |
| 1213 | Expect(err).NotTo(HaveOccurred()) |
| 1214 | return nil |
| 1215 | }) |
| 1216 | Expect(err).NotTo(HaveOccurred()) |
| 1217 | |
| 1218 | // Make sure the host veth has been deleted |
| 1219 | err = tester.targetNS.Do(func(ns.NetNS) error { |
| 1220 | defer GinkgoRecover() |
| 1221 | |
| 1222 | link, err := netlinksafe.LinkByName(IFNAME) |
| 1223 | Expect(err).To(HaveOccurred()) |
| 1224 | Expect(link).To(BeNil()) |
| 1225 | return nil |
| 1226 | }) |
| 1227 | Expect(err).NotTo(HaveOccurred()) |
| 1228 | |
| 1229 | // Make sure the container veth has been deleted |
| 1230 | err = tester.testNS.Do(func(ns.NetNS) error { |
| 1231 | defer GinkgoRecover() |
| 1232 | |
| 1233 | link, err := netlinksafe.LinkByName(tester.vethName) |
| 1234 | Expect(err).To(HaveOccurred()) |
| 1235 | Expect(link).To(BeNil()) |
| 1236 | return nil |
| 1237 | }) |
| 1238 | Expect(err).NotTo(HaveOccurred()) |
| 1239 | } |
| 1240 | |
| 1241 | func (tester *testerV03x) cmdAddTest(tc testCase, dataDir string) (types.Result, error) { |
| 1242 | // Generate network config and command arguments |
| 1243 | tester.args = tc.createCmdArgs(tester.targetNS, dataDir) |
| 1244 | |
| 1245 | // Execute cmdADD on the plugin |
| 1246 | var result *types040.Result |
| 1247 | err := tester.testNS.Do(func(ns.NetNS) error { |
| 1248 | defer GinkgoRecover() |
| 1249 | |
| 1250 | r, raw, err := testutils.CmdAddWithArgs(tester.args, func() error { |
| 1251 | return cmdAdd(tester.args) |
| 1252 | }) |
| 1253 | Expect(err).NotTo(HaveOccurred()) |
| 1254 | Expect(strings.Index(string(raw), "\"interfaces\":")).Should(BeNumerically(">", 0)) |
| 1255 | |
| 1256 | resultType, err := r.GetAsVersion(tc.cniVersion) |
| 1257 | Expect(err).NotTo(HaveOccurred()) |
| 1258 | result = resultType.(*types040.Result) |
| 1259 | |
| 1260 | if !tc.isLayer2 && tc.vlan != 0 { |
| 1261 | Expect(result.Interfaces).To(HaveLen(4)) |
| 1262 | } else { |
| 1263 | Expect(result.Interfaces).To(HaveLen(3)) |
| 1264 | } |
| 1265 | |
| 1266 | Expect(result.Interfaces[0].Name).To(Equal(BRNAME)) |
| 1267 | Expect(result.Interfaces[0].Mac).To(HaveLen(17)) |
| 1268 | |
| 1269 | Expect(result.Interfaces[1].Name).To(HavePrefix("veth")) |
| 1270 | Expect(result.Interfaces[1].Mac).To(HaveLen(17)) |
| 1271 | |
| 1272 | Expect(result.Interfaces[2].Name).To(Equal(IFNAME)) |
| 1273 | Expect(result.Interfaces[2].Mac).To(HaveLen(17)) |
| 1274 | if tc.expectedMac != "" { |
| 1275 | Expect(result.Interfaces[2].Mac).To(Equal(tc.expectedMac)) |
| 1276 | } |
| 1277 | Expect(result.Interfaces[2].Sandbox).To(Equal(tester.targetNS.Path())) |
| 1278 | |
| 1279 | // Make sure bridge link exists |
| 1280 | link, err := netlinksafe.LinkByName(result.Interfaces[0].Name) |
| 1281 | Expect(err).NotTo(HaveOccurred()) |
| 1282 | Expect(link.Attrs().Name).To(Equal(BRNAME)) |
| 1283 | Expect(link).To(BeAssignableToTypeOf(&netlink.Bridge{})) |
| 1284 | Expect(link.Attrs().HardwareAddr.String()).To(Equal(result.Interfaces[0].Mac)) |
| 1285 | bridgeMAC := link.Attrs().HardwareAddr.String() |
| 1286 | |
| 1287 | var vlanLink netlink.Link |
| 1288 | if !tc.isLayer2 && tc.vlan != 0 { |
| 1289 | // Make sure vlan link exists |
| 1290 | vlanLink, err = netlinksafe.LinkByName(fmt.Sprintf("%s.%d", BRNAME, tc.vlan)) |
| 1291 | Expect(err).NotTo(HaveOccurred()) |
| 1292 | Expect(vlanLink.Attrs().Name).To(Equal(fmt.Sprintf("%s.%d", BRNAME, tc.vlan))) |
| 1293 | Expect(vlanLink).To(BeAssignableToTypeOf(&netlink.Veth{})) |
| 1294 | |
| 1295 | // Check the bridge dot vlan interface have the vlan tag |
| 1296 | peerLink, err := netlink.LinkByIndex(vlanLink.Attrs().Index - 1) |
| 1297 | Expect(err).NotTo(HaveOccurred()) |
| 1298 | interfaceMap, err := netlinksafe.BridgeVlanList() |
| 1299 | Expect(err).NotTo(HaveOccurred()) |
| 1300 | vlans, isExist := interfaceMap[int32(peerLink.Attrs().Index)] |
| 1301 | Expect(isExist).To(BeTrue()) |
| 1302 | Expect(checkVlan(tc.vlan, vlans)).To(BeTrue()) |
| 1303 | if tc.removeDefaultVlan { |
| 1304 | Expect(vlans).To(HaveLen(1)) |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | // Check the bridge vlan filtering equals true |
| 1309 | if tc.vlan != 0 || tc.vlanTrunk != nil { |
| 1310 | Expect(*link.(*netlink.Bridge).VlanFiltering).To(BeTrue()) |
| 1311 | } else { |
| 1312 | Expect(*link.(*netlink.Bridge).VlanFiltering).To(BeFalse()) |
| 1313 | } |
| 1314 | |
| 1315 | // Ensure bridge has expected gateway address(es) |
| 1316 | var addrs []netlink.Addr |
| 1317 | if tc.vlan == 0 { |
| 1318 | addrs, err = netlinksafe.AddrList(link, netlink.FAMILY_ALL) |
| 1319 | } else { |
| 1320 | addrs, err = netlinksafe.AddrList(vlanLink, netlink.FAMILY_ALL) |
| 1321 | } |
| 1322 | Expect(err).NotTo(HaveOccurred()) |
| 1323 | Expect(addrs).ToNot(BeEmpty()) |
| 1324 | for _, cidr := range tc.expGWCIDRs { |
| 1325 | ip, subnet, err := net.ParseCIDR(cidr) |
| 1326 | Expect(err).NotTo(HaveOccurred()) |
| 1327 | |
| 1328 | found := false |
| 1329 | subnetPrefix, subnetBits := subnet.Mask.Size() |
| 1330 | for _, a := range addrs { |
| 1331 | aPrefix, aBits := a.IPNet.Mask.Size() |
| 1332 | if a.IPNet.IP.Equal(ip) && aPrefix == subnetPrefix && aBits == subnetBits { |
| 1333 | found = true |
| 1334 | break |
| 1335 | } |
| 1336 | } |
| 1337 | Expect(found).To(BeTrue()) |
| 1338 | } |
| 1339 | |
| 1340 | // Check for the veth link in the main namespace |
| 1341 | links, err := netlinksafe.LinkList() |
| 1342 | Expect(err).NotTo(HaveOccurred()) |
| 1343 | if !tc.isLayer2 && tc.vlan != 0 { |
| 1344 | Expect(links).To(HaveLen(5)) // Bridge, Bridge vlan veth, veth, and loopback |
| 1345 | } else { |
| 1346 | Expect(links).To(HaveLen(3)) // Bridge, veth, and loopback |
| 1347 | } |
| 1348 | |
| 1349 | link, err = netlinksafe.LinkByName(result.Interfaces[1].Name) |
| 1350 | Expect(err).NotTo(HaveOccurred()) |
| 1351 | Expect(link).To(BeAssignableToTypeOf(&netlink.Veth{})) |
| 1352 | tester.vethName = result.Interfaces[1].Name |
| 1353 | |
| 1354 | // check vlan exist on the veth interface |
| 1355 | if tc.vlan != 0 { |
| 1356 | interfaceMap, err := netlinksafe.BridgeVlanList() |
| 1357 | Expect(err).NotTo(HaveOccurred()) |
| 1358 | vlans, isExist := interfaceMap[int32(link.Attrs().Index)] |
| 1359 | Expect(isExist).To(BeTrue()) |
| 1360 | Expect(checkVlan(tc.vlan, vlans)).To(BeTrue()) |
| 1361 | if tc.removeDefaultVlan { |
| 1362 | Expect(vlans).To(HaveLen(1)) |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | // check VlanTrunks exist on the veth interface |
| 1367 | if tc.vlanTrunk != nil { |
| 1368 | interfaceMap, err := netlinksafe.BridgeVlanList() |
| 1369 | Expect(err).NotTo(HaveOccurred()) |
| 1370 | vlans, isExist := interfaceMap[int32(link.Attrs().Index)] |
| 1371 | Expect(isExist).To(BeTrue()) |
| 1372 | |
| 1373 | for _, vlanEntry := range tc.vlanTrunk { |
| 1374 | if vlanEntry.ID != nil { |
| 1375 | Expect(checkVlan(*vlanEntry.ID, vlans)).To(BeTrue()) |
| 1376 | } |
| 1377 | if vlanEntry.MinID != nil && vlanEntry.MaxID != nil { |
| 1378 | for vid := *vlanEntry.MinID; vid <= *vlanEntry.MaxID; vid++ { |
| 1379 | Expect(checkVlan(vid, vlans)).To(BeTrue()) |
| 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | // Check native vlan |
| 1385 | nativeVlan := tc.vlan |
| 1386 | if tc.vlan == 0 { |
| 1387 | nativeVlan = 1 |
| 1388 | } |
| 1389 | Expect(checkVlan(nativeVlan, vlans)).To(BeTrue()) |
| 1390 | } |
| 1391 | |
| 1392 | // Check that the bridge has a different mac from the veth |
| 1393 | // If not, it means the bridge has an unstable mac and will change |
| 1394 | // as ifs are added and removed |
| 1395 | // this check is not relevant for a layer 2 bridge |
| 1396 | if !tc.isLayer2 && tc.vlan == 0 { |
| 1397 | Expect(link.Attrs().HardwareAddr.String()).NotTo(Equal(bridgeMAC)) |
| 1398 | } |
| 1399 | |
| 1400 | return nil |
| 1401 | }) |
| 1402 | Expect(err).NotTo(HaveOccurred()) |
| 1403 | |
| 1404 | // Find the veth peer in the container namespace and the default route |
| 1405 | err = tester.targetNS.Do(func(ns.NetNS) error { |
| 1406 | defer GinkgoRecover() |
| 1407 | |
| 1408 | link, err := netlinksafe.LinkByName(IFNAME) |
| 1409 | Expect(err).NotTo(HaveOccurred()) |
| 1410 | Expect(link.Attrs().Name).To(Equal(IFNAME)) |
| 1411 | Expect(link).To(BeAssignableToTypeOf(&netlink.Veth{})) |
| 1412 | |
| 1413 | expCIDRsV4, expCIDRsV6 := tc.expectedCIDRs() |
| 1414 | addrs, err := netlinksafe.AddrList(link, netlink.FAMILY_V4) |
| 1415 | Expect(err).NotTo(HaveOccurred()) |
| 1416 | Expect(addrs).To(HaveLen(len(expCIDRsV4))) |
| 1417 | addrs, err = netlinksafe.AddrList(link, netlink.FAMILY_V6) |
| 1418 | Expect(err).NotTo(HaveOccurred()) |
| 1419 | // Ignore link local address which may or may not be |
| 1420 | // ready when we read addresses. |
| 1421 | var foundAddrs int |
| 1422 | for _, addr := range addrs { |
| 1423 | if !addr.IP.IsLinkLocalUnicast() { |
| 1424 | foundAddrs++ |
| 1425 | } |
| 1426 | } |
| 1427 | Expect(foundAddrs).To(Equal(len(expCIDRsV6))) |
| 1428 | |
| 1429 | // Ensure the default route(s) |
| 1430 | routes, err := netlinksafe.RouteList(link, 0) |
| 1431 | Expect(err).NotTo(HaveOccurred()) |
| 1432 | |
| 1433 | var defaultRouteFound4, defaultRouteFound6 bool |
| 1434 | for _, cidr := range tc.expGWCIDRs { |
| 1435 | gwIP, _, err := net.ParseCIDR(cidr) |
| 1436 | Expect(err).NotTo(HaveOccurred()) |
| 1437 | var found *bool |
| 1438 | if ipVersion(gwIP) == "4" { |
| 1439 | found = &defaultRouteFound4 |
| 1440 | } else { |
| 1441 | found = &defaultRouteFound6 |
| 1442 | } |
| 1443 | if *found == true { |
| 1444 | continue |
| 1445 | } |
| 1446 | for _, route := range routes { |
| 1447 | *found = (ip.IsIPNetZero(route.Dst) && route.Src == nil && route.Gw.Equal(gwIP)) |
| 1448 | if *found { |
| 1449 | break |
| 1450 | } |
| 1451 | } |
| 1452 | Expect(*found).To(BeTrue()) |
| 1453 | } |
| 1454 | |
| 1455 | return nil |
| 1456 | }) |
| 1457 | Expect(err).NotTo(HaveOccurred()) |
| 1458 | return result, nil |
| 1459 | } |
| 1460 | |
| 1461 | func (tester *testerV03x) cmdCheckTest(_ testCase, _ *Net, _ string) { |
| 1462 | } |
| 1463 | |
| 1464 | func (tester *testerV03x) cmdDelTest(_ testCase, _ string) { |
| 1465 | err := tester.testNS.Do(func(ns.NetNS) error { |
| 1466 | defer GinkgoRecover() |
| 1467 | |
| 1468 | err := testutils.CmdDelWithArgs(tester.args, func() error { |
| 1469 | return cmdDel(tester.args) |
| 1470 | }) |
| 1471 | Expect(err).NotTo(HaveOccurred()) |
| 1472 | return nil |
| 1473 | }) |
| 1474 | Expect(err).NotTo(HaveOccurred()) |
| 1475 | |
| 1476 | // Make sure the host veth has been deleted |
| 1477 | err = tester.targetNS.Do(func(ns.NetNS) error { |
| 1478 | defer GinkgoRecover() |
| 1479 | |
| 1480 | link, err := netlinksafe.LinkByName(IFNAME) |
| 1481 | Expect(err).To(HaveOccurred()) |
| 1482 | Expect(link).To(BeNil()) |
| 1483 | return nil |
| 1484 | }) |
| 1485 | Expect(err).NotTo(HaveOccurred()) |
| 1486 | |
| 1487 | // Make sure the container veth has been deleted |
| 1488 | err = tester.testNS.Do(func(ns.NetNS) error { |
| 1489 | defer GinkgoRecover() |
| 1490 | |
| 1491 | link, err := netlinksafe.LinkByName(tester.vethName) |
| 1492 | Expect(err).To(HaveOccurred()) |
| 1493 | Expect(link).To(BeNil()) |
| 1494 | return nil |
| 1495 | }) |
| 1496 | Expect(err).NotTo(HaveOccurred()) |
| 1497 | } |
| 1498 | |
| 1499 | func expect020AddError(tc testCase) bool { |
| 1500 | return tc.AddErr020 != "" && tc.cniVersion == "0.2.0" |
| 1501 | } |
| 1502 | |
| 1503 | func expect020DelError(tc testCase) bool { |
| 1504 | return tc.DelErr020 != "" && tc.cniVersion == "0.2.0" |
| 1505 | } |
| 1506 | |
| 1507 | func expect010AddError(tc testCase) bool { |
| 1508 | return tc.AddErr010 != "" && tc.cniVersion == "0.1.0" |
| 1509 | } |
| 1510 | |
| 1511 | func expect010DelError(tc testCase) bool { |
| 1512 | return tc.DelErr010 != "" && tc.cniVersion == "0.1.0" |
| 1513 | } |
| 1514 | |
| 1515 | func (tester *testerV01xOr02x) cmdAddTest(tc testCase, dataDir string) (types.Result, error) { |
| 1516 | // Generate network config and command arguments |
| 1517 | tester.args = tc.createCmdArgs(tester.targetNS, dataDir) |
| 1518 | |
| 1519 | var hostNSVlanMap map[int32][]*nl.BridgeVlanInfo |
| 1520 | |
| 1521 | // Execute cmdADD on the plugin |
| 1522 | err := tester.testNS.Do(func(ns.NetNS) error { |
| 1523 | defer GinkgoRecover() |
| 1524 | |
| 1525 | // check that STATUS is |
| 1526 | if testutils.SpecVersionHasSTATUS(tc.cniVersion) { |
| 1527 | err := testutils.CmdStatus(func() error { |
| 1528 | return cmdStatus(&skel.CmdArgs{StdinData: []byte(tc.netConfJSON(dataDir))}) |
| 1529 | }) |
| 1530 | Expect(err).NotTo(HaveOccurred()) |
| 1531 | } |
| 1532 | |
| 1533 | r, raw, err := testutils.CmdAddWithArgs(tester.args, func() error { |
| 1534 | return cmdAdd(tester.args) |
| 1535 | }) |
| 1536 | |
| 1537 | if expect020AddError(tc) || expect010AddError(tc) { |
| 1538 | return err |
| 1539 | } |
| 1540 | Expect(err).NotTo(HaveOccurred()) |
| 1541 | Expect(strings.Index(string(raw), "\"ip\":")).Should(BeNumerically(">", 0)) |
| 1542 | |
| 1543 | // We expect a version 0.1.0 or 0.2.0 result |
| 1544 | _, err = r.GetAsVersion(tc.cniVersion) |
| 1545 | Expect(err).NotTo(HaveOccurred()) |
| 1546 | |
| 1547 | // Make sure bridge link exists |
| 1548 | link, err := netlinksafe.LinkByName(BRNAME) |
| 1549 | Expect(err).NotTo(HaveOccurred()) |
| 1550 | Expect(link.Attrs().Name).To(Equal(BRNAME)) |
| 1551 | Expect(link).To(BeAssignableToTypeOf(&netlink.Bridge{})) |
| 1552 | |
| 1553 | var vlanLink netlink.Link |
| 1554 | if !tc.isLayer2 && tc.vlan != 0 { |
| 1555 | // Make sure vlan link exists |
| 1556 | vlanLink, err = netlinksafe.LinkByName(fmt.Sprintf("%s.%d", BRNAME, tc.vlan)) |
| 1557 | Expect(err).NotTo(HaveOccurred()) |
| 1558 | Expect(vlanLink.Attrs().Name).To(Equal(fmt.Sprintf("%s.%d", BRNAME, tc.vlan))) |
| 1559 | Expect(vlanLink).To(BeAssignableToTypeOf(&netlink.Veth{})) |
| 1560 | |
| 1561 | // Check the bridge dot vlan interface have the vlan tag |
| 1562 | peerLink, err := netlink.LinkByIndex(vlanLink.Attrs().Index - 1) |
| 1563 | Expect(err).NotTo(HaveOccurred()) |
| 1564 | interfaceMap, err := netlinksafe.BridgeVlanList() |
| 1565 | Expect(err).NotTo(HaveOccurred()) |
| 1566 | vlans, isExist := interfaceMap[int32(peerLink.Attrs().Index)] |
| 1567 | Expect(isExist).To(BeTrue()) |
| 1568 | Expect(checkVlan(tc.vlan, vlans)).To(BeTrue()) |
| 1569 | if tc.removeDefaultVlan { |
| 1570 | Expect(vlans).To(HaveLen(1)) |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | // Check the bridge vlan filtering equals true |
| 1575 | if tc.vlan != 0 { |
| 1576 | Expect(*link.(*netlink.Bridge).VlanFiltering).To(BeTrue()) |
| 1577 | } else { |
| 1578 | Expect(*link.(*netlink.Bridge).VlanFiltering).To(BeFalse()) |
| 1579 | } |
| 1580 | |
| 1581 | // Ensure bridge has expected gateway address(es) |
| 1582 | var addrs []netlink.Addr |
| 1583 | if tc.vlan == 0 { |
| 1584 | addrs, err = netlinksafe.AddrList(link, netlink.FAMILY_ALL) |
| 1585 | } else { |
| 1586 | addrs, err = netlinksafe.AddrList(vlanLink, netlink.FAMILY_ALL) |
| 1587 | } |
| 1588 | Expect(err).NotTo(HaveOccurred()) |
| 1589 | Expect(addrs).ToNot(BeEmpty()) |
| 1590 | for _, cidr := range tc.expGWCIDRs { |
| 1591 | ip, subnet, err := net.ParseCIDR(cidr) |
| 1592 | Expect(err).NotTo(HaveOccurred()) |
| 1593 | |
| 1594 | found := false |
| 1595 | subnetPrefix, subnetBits := subnet.Mask.Size() |
| 1596 | for _, a := range addrs { |
| 1597 | aPrefix, aBits := a.IPNet.Mask.Size() |
| 1598 | if a.IPNet.IP.Equal(ip) && aPrefix == subnetPrefix && aBits == subnetBits { |
| 1599 | found = true |
| 1600 | break |
| 1601 | } |
| 1602 | } |
| 1603 | Expect(found).To(BeTrue()) |
| 1604 | } |
| 1605 | |
| 1606 | // Check for the veth link in the main namespace; can't |
| 1607 | // check the for the specific link since version 0.1.0 |
| 1608 | // doesn't report interfaces |
| 1609 | links, err := netlinksafe.LinkList() |
| 1610 | Expect(err).NotTo(HaveOccurred()) |
| 1611 | if !tc.isLayer2 && tc.vlan != 0 { |
| 1612 | Expect(links).To(HaveLen(5)) // Bridge, Bridge vlan veth, veth, and loopback |
| 1613 | } else { |
| 1614 | Expect(links).To(HaveLen(3)) // Bridge, veth, and loopback |
| 1615 | } |
| 1616 | |
| 1617 | // Grab the vlan map in the host NS for checking later |
| 1618 | if tc.vlan != 0 { |
| 1619 | hostNSVlanMap, err = netlinksafe.BridgeVlanList() |
| 1620 | Expect(err).NotTo(HaveOccurred()) |
| 1621 | } |
| 1622 | return nil |
| 1623 | }) |
| 1624 | if expect020AddError(tc) { |
| 1625 | Expect(err).To(MatchError(tc.AddErr020)) |
| 1626 | return nil, nil |
| 1627 | } else if expect010AddError(tc) { |
| 1628 | Expect(err).To(MatchError(tc.AddErr010)) |
| 1629 | return nil, nil |
| 1630 | } |
| 1631 | Expect(err).NotTo(HaveOccurred()) |
| 1632 | |
| 1633 | // Find the veth peer in the container namespace and the default route |
| 1634 | err = tester.targetNS.Do(func(ns.NetNS) error { |
| 1635 | defer GinkgoRecover() |
| 1636 | |
| 1637 | link, err := netlinksafe.LinkByName(IFNAME) |
| 1638 | Expect(err).NotTo(HaveOccurred()) |
| 1639 | Expect(link.Attrs().Name).To(Equal(IFNAME)) |
| 1640 | Expect(link).To(BeAssignableToTypeOf(&netlink.Veth{})) |
| 1641 | |
| 1642 | expCIDRsV4, expCIDRsV6 := tc.expectedCIDRs() |
| 1643 | addrs, err := netlinksafe.AddrList(link, netlink.FAMILY_V4) |
| 1644 | Expect(err).NotTo(HaveOccurred()) |
| 1645 | Expect(addrs).To(HaveLen(len(expCIDRsV4))) |
| 1646 | addrs, err = netlinksafe.AddrList(link, netlink.FAMILY_V6) |
| 1647 | Expect(err).NotTo(HaveOccurred()) |
| 1648 | // Ignore link local address which may or may not be |
| 1649 | // ready when we read addresses. |
| 1650 | var foundAddrs int |
| 1651 | for _, addr := range addrs { |
| 1652 | if !addr.IP.IsLinkLocalUnicast() { |
| 1653 | foundAddrs++ |
| 1654 | } |
| 1655 | } |
| 1656 | Expect(foundAddrs).To(Equal(len(expCIDRsV6))) |
| 1657 | |
| 1658 | // Ensure the default route(s) |
| 1659 | routes, err := netlinksafe.RouteList(link, 0) |
| 1660 | Expect(err).NotTo(HaveOccurred()) |
| 1661 | |
| 1662 | var defaultRouteFound4, defaultRouteFound6 bool |
| 1663 | for _, cidr := range tc.expGWCIDRs { |
| 1664 | gwIP, _, err := net.ParseCIDR(cidr) |
| 1665 | Expect(err).NotTo(HaveOccurred()) |
| 1666 | var found *bool |
| 1667 | if ipVersion(gwIP) == "4" { |
| 1668 | found = &defaultRouteFound4 |
| 1669 | } else { |
| 1670 | found = &defaultRouteFound6 |
| 1671 | } |
| 1672 | if *found == true { |
| 1673 | continue |
| 1674 | } |
| 1675 | for _, route := range routes { |
| 1676 | *found = (ip.IsIPNetZero(route.Dst) && route.Src == nil && route.Gw.Equal(gwIP)) |
| 1677 | if *found { |
| 1678 | break |
| 1679 | } |
| 1680 | } |
| 1681 | Expect(*found).To(BeTrue()) |
| 1682 | } |
| 1683 | |
| 1684 | // Validate VLAN in the host NS. Since 0.1.0/0.2.0 don't return |
| 1685 | // any host interface information, we have to look up the container |
| 1686 | // namespace veth's peer index instead |
| 1687 | if tc.vlan != 0 { |
| 1688 | _, peerIndex, err := ip.GetVethPeerIfindex(IFNAME) |
| 1689 | Expect(err).NotTo(HaveOccurred()) |
| 1690 | Expect(peerIndex).To(BeNumerically(">", 0)) |
| 1691 | vlans, isExist := hostNSVlanMap[int32(peerIndex)] |
| 1692 | Expect(isExist).To(BeTrue()) |
| 1693 | Expect(checkVlan(tc.vlan, vlans)).To(BeTrue()) |
| 1694 | if tc.removeDefaultVlan { |
| 1695 | Expect(vlans).To(HaveLen(1)) |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | return nil |
| 1700 | }) |
| 1701 | Expect(err).NotTo(HaveOccurred()) |
| 1702 | return nil, nil |
| 1703 | } |
| 1704 | |
| 1705 | func (tester *testerV01xOr02x) cmdCheckTest(_ testCase, _ *Net, _ string) { |
| 1706 | } |
| 1707 | |
| 1708 | func (tester *testerV01xOr02x) cmdDelTest(tc testCase, _ string) { |
| 1709 | err := tester.testNS.Do(func(ns.NetNS) error { |
| 1710 | defer GinkgoRecover() |
| 1711 | |
| 1712 | err := testutils.CmdDelWithArgs(tester.args, func() error { |
| 1713 | return cmdDel(tester.args) |
| 1714 | }) |
| 1715 | switch { |
| 1716 | case expect020DelError(tc): |
| 1717 | Expect(err).To(MatchError(tc.DelErr020)) |
| 1718 | case expect010DelError(tc): |
| 1719 | Expect(err).To(MatchError(tc.DelErr010)) |
| 1720 | default: |
| 1721 | Expect(err).NotTo(HaveOccurred()) |
| 1722 | } |
| 1723 | return nil |
| 1724 | }) |
| 1725 | Expect(err).NotTo(HaveOccurred()) |
| 1726 | |
| 1727 | // Make sure the container veth has been deleted; cannot check |
| 1728 | // host veth as version 0.1.0 can't report its name |
| 1729 | err = tester.testNS.Do(func(ns.NetNS) error { |
| 1730 | defer GinkgoRecover() |
| 1731 | |
| 1732 | link, err := netlinksafe.LinkByName(IFNAME) |
| 1733 | Expect(err).To(HaveOccurred()) |
| 1734 | Expect(link).To(BeNil()) |
| 1735 | return nil |
| 1736 | }) |
| 1737 | Expect(err).NotTo(HaveOccurred()) |
| 1738 | } |
| 1739 | |
| 1740 | func cmdAddDelTest(origNS, targetNS ns.NetNS, tc testCase, dataDir string) { |
| 1741 | tester := newTesterByVersion(tc.cniVersion, origNS, targetNS) |
| 1742 | |
| 1743 | // Test IP allocation |
| 1744 | _, err := tester.cmdAddTest(tc, dataDir) |
| 1745 | Expect(err).NotTo(HaveOccurred()) |
| 1746 | |
| 1747 | // Test IP Release |
| 1748 | tester.cmdDelTest(tc, dataDir) |
| 1749 | |
| 1750 | // Clean up bridge addresses for next test case |
| 1751 | delBridgeAddrs(origNS) |
| 1752 | } |
| 1753 | |
| 1754 | func buildOneConfig(name, cniVersion string, orig *Net, prevResult types.Result) (*Net, error) { |
| 1755 | var err error |
| 1756 | |
| 1757 | inject := map[string]interface{}{ |
| 1758 | "name": name, |
| 1759 | "cniVersion": cniVersion, |
| 1760 | } |
| 1761 | // Add previous plugin result |
| 1762 | if prevResult != nil { |
| 1763 | inject["prevResult"] = prevResult |
| 1764 | } |
| 1765 | |
| 1766 | // Ensure every config uses the same name and version |
| 1767 | config := make(map[string]interface{}) |
| 1768 | confBytes, err := json.Marshal(orig) |
| 1769 | if err != nil { |
| 1770 | return nil, err |
| 1771 | } |
| 1772 | |
| 1773 | err = json.Unmarshal(confBytes, &config) |
| 1774 | if err != nil { |
| 1775 | return nil, fmt.Errorf("unmarshal existing network bytes: %s", err) |
| 1776 | } |
| 1777 | |
| 1778 | for key, value := range inject { |
| 1779 | config[key] = value |
| 1780 | } |
| 1781 | |
| 1782 | newBytes, err := json.Marshal(config) |
| 1783 | if err != nil { |
| 1784 | return nil, err |
| 1785 | } |
| 1786 | |
| 1787 | conf := &Net{} |
| 1788 | if err := json.Unmarshal(newBytes, &conf); err != nil { |
| 1789 | return nil, fmt.Errorf("error parsing configuration: %s", err) |
| 1790 | } |
| 1791 | |
| 1792 | return conf, nil |
| 1793 | } |
| 1794 | |
| 1795 | func cmdAddDelCheckTest(origNS, targetNS ns.NetNS, tc testCase, dataDir string) { |
| 1796 | tester := newTesterByVersion(tc.cniVersion, origNS, targetNS) |
| 1797 | |
| 1798 | // Test IP allocation |
| 1799 | prevResult, err := tester.cmdAddTest(tc, dataDir) |
| 1800 | Expect(err).NotTo(HaveOccurred()) |
| 1801 | |
| 1802 | Expect(prevResult).NotTo(BeNil()) |
| 1803 | |
| 1804 | confString := tc.netConfJSON(dataDir) |
| 1805 | |
| 1806 | conf := &Net{} |
| 1807 | err = json.Unmarshal([]byte(confString), &conf) |
| 1808 | Expect(err).NotTo(HaveOccurred()) |
| 1809 | |
| 1810 | conf.IPAM, _, err = allocator.LoadIPAMConfig([]byte(confString), "") |
| 1811 | Expect(err).NotTo(HaveOccurred()) |
| 1812 | |
| 1813 | newConf, err := buildOneConfig("testConfig", tc.cniVersion, conf, prevResult) |
| 1814 | Expect(err).NotTo(HaveOccurred()) |
| 1815 | |
| 1816 | // Test CHECK |
| 1817 | tester.cmdCheckTest(tc, newConf, dataDir) |
| 1818 | |
| 1819 | // Test IP Release |
| 1820 | tester.cmdDelTest(tc, dataDir) |
| 1821 | |
| 1822 | // Clean up bridge addresses for next test case |
| 1823 | delBridgeAddrs(origNS) |
| 1824 | |
| 1825 | if tc.vlan != 0 && !tc.isLayer2 { |
| 1826 | delVlanAddrs(origNS, tc.vlan) |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | var _ = Describe("bridge Operations", func() { |
| 1831 | var originalNS, targetNS ns.NetNS |
| 1832 | var dataDir string |
| 1833 | |
| 1834 | BeforeEach(func() { |
| 1835 | // Create a new NetNS so we don't modify the host |
| 1836 | var err error |
| 1837 | originalNS, err = testutils.NewNS() |
| 1838 | Expect(err).NotTo(HaveOccurred()) |
| 1839 | targetNS, err = testutils.NewNS() |
| 1840 | Expect(err).NotTo(HaveOccurred()) |
| 1841 | |
| 1842 | dataDir, err = os.MkdirTemp("", "bridge_test") |
| 1843 | Expect(err).NotTo(HaveOccurred()) |
| 1844 | |
| 1845 | // Do not emulate an error, each test will set this if needed |
| 1846 | debugPostIPAMError = nil |
| 1847 | }) |
| 1848 | |
| 1849 | AfterEach(func() { |
| 1850 | Expect(os.RemoveAll(dataDir)).To(Succeed()) |
| 1851 | Expect(originalNS.Close()).To(Succeed()) |
| 1852 | Expect(testutils.UnmountNS(originalNS)).To(Succeed()) |
| 1853 | Expect(targetNS.Close()).To(Succeed()) |
| 1854 | Expect(testutils.UnmountNS(targetNS)).To(Succeed()) |
| 1855 | }) |
| 1856 | |
| 1857 | var ( |
| 1858 | correctID = 10 |
| 1859 | correctMinID = 100 |
| 1860 | correctMaxID = 105 |
| 1861 | incorrectMinID = 1000 |
| 1862 | incorrectMaxID = 100 |
| 1863 | overID = 5000 |
| 1864 | negativeID = -1 |
| 1865 | ) |
| 1866 | |
| 1867 | DescribeTable( |
| 1868 | "collectVlanTrunk succeeds", |
| 1869 | func(vlanTrunks []*VlanTrunk, expectedVIDs []int) { |
| 1870 | Expect(collectVlanTrunk(vlanTrunks)).To(ConsistOf(expectedVIDs)) |
| 1871 | }, |
| 1872 | Entry("when provided an empty VLAN trunk configuration", []*VlanTrunk{}, nil), |
| 1873 | Entry("when provided a VLAN trunk configuration with both min / max range", []*VlanTrunk{ |
| 1874 | { |
| 1875 | MinID: &correctMinID, |
| 1876 | MaxID: &correctMaxID, |
| 1877 | }, |
| 1878 | }, []int{100, 101, 102, 103, 104, 105}), |
| 1879 | Entry("when provided a VLAN trunk configuration with id only", []*VlanTrunk{ |
| 1880 | { |
| 1881 | ID: &correctID, |
| 1882 | }, |
| 1883 | }, []int{10}), |
| 1884 | Entry("when provided a VLAN trunk configuration with id and range", []*VlanTrunk{ |
| 1885 | { |
| 1886 | ID: &correctID, |
| 1887 | }, |
| 1888 | { |
| 1889 | MinID: &correctMinID, |
| 1890 | MaxID: &correctMaxID, |
| 1891 | }, |
| 1892 | }, []int{10, 100, 101, 102, 103, 104, 105}), |
| 1893 | ) |
| 1894 | |
| 1895 | DescribeTable( |
| 1896 | "collectVlanTrunk failed", |
| 1897 | func(vlanTrunks []*VlanTrunk, expectedError error) { |
| 1898 | _, err := collectVlanTrunk(vlanTrunks) |
| 1899 | Expect(err).To(MatchError(expectedError)) |
| 1900 | }, |
| 1901 | Entry("when not passed the maxID", []*VlanTrunk{{MinID: &correctMinID}}, fmt.Errorf("minID and maxID should be configured simultaneously, maxID is missing")), |
| 1902 | Entry("when not passed the minID", []*VlanTrunk{{MaxID: &correctMaxID}}, fmt.Errorf("minID and maxID should be configured simultaneously, minID is missing")), |
| 1903 | Entry("when the minID is negative", []*VlanTrunk{{MinID: &negativeID, MaxID: &correctMaxID}}, fmt.Errorf("incorrect trunk minID parameter")), |
| 1904 | Entry("when the minID is larger than 4094", []*VlanTrunk{{MinID: &overID, MaxID: &correctMaxID}}, fmt.Errorf("incorrect trunk minID parameter")), |
| 1905 | Entry("when the maxID is larger than 4094", []*VlanTrunk{{MinID: &correctMinID, MaxID: &overID}}, fmt.Errorf("incorrect trunk maxID parameter")), |
| 1906 | Entry("when the maxID is negative", []*VlanTrunk{{MinID: &correctMinID, MaxID: &overID}}, fmt.Errorf("incorrect trunk maxID parameter")), |
| 1907 | Entry("when the ID is larger than 4094", []*VlanTrunk{{ID: &overID}}, fmt.Errorf("incorrect trunk id parameter")), |
| 1908 | Entry("when the ID is negative", []*VlanTrunk{{ID: &negativeID}}, fmt.Errorf("incorrect trunk id parameter")), |
| 1909 | Entry("when the maxID is smaller than minID", []*VlanTrunk{{MinID: &incorrectMinID, MaxID: &incorrectMaxID}}, fmt.Errorf("minID is greater than maxID in trunk parameter")), |
| 1910 | ) |
| 1911 | |
| 1912 | for _, ver := range testutils.AllSpecVersions { |
| 1913 | // Redefine ver inside for scope so real value is picked up by each dynamically defined It() |
| 1914 | // See Gingkgo's "Patterns for dynamically generating tests" documentation. |
| 1915 | ver := ver |
| 1916 | |
| 1917 | It(fmt.Sprintf("[%s] creates a bridge", ver), func() { |
| 1918 | conf := testCase{cniVersion: ver}.netConf() |
| 1919 | err := originalNS.Do(func(ns.NetNS) error { |
| 1920 | defer GinkgoRecover() |
| 1921 | |
| 1922 | bridge, _, err := setupBridge(conf) |
| 1923 | Expect(err).NotTo(HaveOccurred()) |
| 1924 | Expect(bridge.Attrs().Name).To(Equal(BRNAME)) |
| 1925 | |
| 1926 | // Double check that the link was added |
| 1927 | link, err := netlinksafe.LinkByName(BRNAME) |
| 1928 | Expect(err).NotTo(HaveOccurred()) |
| 1929 | Expect(link.Attrs().Name).To(Equal(BRNAME)) |
| 1930 | Expect(link.Attrs().Promisc).To(Equal(0)) |
| 1931 | return nil |
| 1932 | }) |
| 1933 | Expect(err).NotTo(HaveOccurred()) |
| 1934 | }) |
| 1935 | |
| 1936 | It(fmt.Sprintf("[%s] handles an existing bridge", ver), func() { |
| 1937 | err := originalNS.Do(func(ns.NetNS) error { |
| 1938 | defer GinkgoRecover() |
| 1939 | |
| 1940 | linkAttrs := netlink.NewLinkAttrs() |
| 1941 | linkAttrs.Name = BRNAME |
| 1942 | err := netlink.LinkAdd(&netlink.Bridge{ |
| 1943 | LinkAttrs: linkAttrs, |
| 1944 | }) |
| 1945 | Expect(err).NotTo(HaveOccurred()) |
| 1946 | link, err := netlinksafe.LinkByName(BRNAME) |
| 1947 | Expect(err).NotTo(HaveOccurred()) |
| 1948 | Expect(link.Attrs().Name).To(Equal(BRNAME)) |
| 1949 | ifindex := link.Attrs().Index |
| 1950 | |
| 1951 | tc := testCase{cniVersion: ver, isGW: false} |
| 1952 | conf := tc.netConf() |
| 1953 | |
| 1954 | bridge, _, err := setupBridge(conf) |
| 1955 | Expect(err).NotTo(HaveOccurred()) |
| 1956 | Expect(bridge.Attrs().Name).To(Equal(BRNAME)) |
| 1957 | Expect(bridge.Attrs().Index).To(Equal(ifindex)) |
| 1958 | |
| 1959 | // Double check that the link has the same ifindex |
| 1960 | link, err = netlinksafe.LinkByName(BRNAME) |
| 1961 | Expect(err).NotTo(HaveOccurred()) |
| 1962 | Expect(link.Attrs().Name).To(Equal(BRNAME)) |
| 1963 | Expect(link.Attrs().Index).To(Equal(ifindex)) |
| 1964 | return nil |
| 1965 | }) |
| 1966 | Expect(err).NotTo(HaveOccurred()) |
| 1967 | }) |
| 1968 | |
| 1969 | for i, tc := range []testCase{ |
| 1970 | { |
| 1971 | // IPv4 only |
| 1972 | subnet: "10.1.2.0/24", |
| 1973 | expGWCIDRs: []string{"10.1.2.1/24"}, |
| 1974 | }, |
| 1975 | { |
| 1976 | // IPv6 only |
| 1977 | subnet: "2001:db8::0/64", |
| 1978 | expGWCIDRs: []string{"2001:db8::1/64"}, |
| 1979 | }, |
| 1980 | { |
| 1981 | // Dual-Stack |
| 1982 | ranges: []rangeInfo{ |
| 1983 | {subnet: "192.168.0.0/24"}, |
| 1984 | {subnet: "fd00::0/64"}, |
| 1985 | }, |
| 1986 | expGWCIDRs: []string{ |
| 1987 | "192.168.0.1/24", |
| 1988 | "fd00::1/64", |
| 1989 | }, |
| 1990 | }, |
| 1991 | { |
| 1992 | // 3 Subnets (1 IPv4 and 2 IPv6 subnets) |
| 1993 | ranges: []rangeInfo{ |
| 1994 | {subnet: "192.168.0.0/24"}, |
| 1995 | {subnet: "fd00::0/64"}, |
| 1996 | {subnet: "2001:db8::0/64"}, |
| 1997 | }, |
| 1998 | expGWCIDRs: []string{ |
| 1999 | "192.168.0.1/24", |
| 2000 | "fd00::1/64", |
| 2001 | "2001:db8::1/64", |
| 2002 | }, |
| 2003 | AddErr020: "CNI version 0.2.0 does not support more than 1 address per family", |
| 2004 | DelErr020: "CNI version 0.2.0 does not support more than 1 address per family", |
| 2005 | AddErr010: "CNI version 0.1.0 does not support more than 1 address per family", |
| 2006 | DelErr010: "CNI version 0.1.0 does not support more than 1 address per family", |
| 2007 | }, |
| 2008 | { |
| 2009 | // with resolvConf DNS settings |
| 2010 | subnet: "10.1.2.0/24", |
| 2011 | expGWCIDRs: []string{"10.1.2.1/24"}, |
| 2012 | resolvConf: resolvConf, |
| 2013 | }, |
| 2014 | } { |
| 2015 | tc := tc |
| 2016 | i := i |
| 2017 | It(fmt.Sprintf("[%s] (%d) configures and deconfigures a bridge and veth with default route with ADD/DEL", ver, i), func() { |
| 2018 | tc.cniVersion = ver |
| 2019 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2020 | }) |
| 2021 | } |
| 2022 | |
| 2023 | It(fmt.Sprintf("[%s] configures and deconfigures a l2 bridge and veth with ADD/DEL", ver), func() { |
| 2024 | tc := testCase{ |
| 2025 | cniVersion: ver, |
| 2026 | isLayer2: true, |
| 2027 | AddErr020: "cannot convert: no valid IP addresses", |
| 2028 | AddErr010: "cannot convert: no valid IP addresses", |
| 2029 | } |
| 2030 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2031 | }) |
| 2032 | |
| 2033 | It(fmt.Sprintf("[%s] configures and deconfigures a l2 bridge with vlan id 100 using ADD/DEL", ver), func() { |
| 2034 | tc := testCase{ |
| 2035 | cniVersion: ver, |
| 2036 | isLayer2: true, |
| 2037 | vlan: 100, |
| 2038 | AddErr020: "cannot convert: no valid IP addresses", |
| 2039 | AddErr010: "cannot convert: no valid IP addresses", |
| 2040 | } |
| 2041 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2042 | }) |
| 2043 | |
| 2044 | // TODO find some way to put pointer |
| 2045 | It(fmt.Sprintf("[%s] configures and deconfigures a l2 bridge with vlanTrunk 101,200~210 using ADD/DEL", ver), func() { |
| 2046 | id, minID, maxID := 101, 200, 210 |
| 2047 | tc := testCase{ |
| 2048 | cniVersion: ver, |
| 2049 | isLayer2: true, |
| 2050 | vlanTrunk: []*VlanTrunk{ |
| 2051 | {ID: &id}, |
| 2052 | { |
| 2053 | MinID: &minID, |
| 2054 | MaxID: &maxID, |
| 2055 | }, |
| 2056 | }, |
| 2057 | AddErr020: "cannot convert: no valid IP addresses", |
| 2058 | AddErr010: "cannot convert: no valid IP addresses", |
| 2059 | } |
| 2060 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2061 | }) |
| 2062 | |
| 2063 | It(fmt.Sprintf("[%s] configures and deconfigures a l2 bridge with vlan 100, and vlanTrunk 101,200~210 using ADD/DEL", ver), func() { |
| 2064 | nativeVlan := 100 |
| 2065 | id, minID, maxID := 101, 200, 210 |
| 2066 | tc := testCase{ |
| 2067 | cniVersion: ver, |
| 2068 | isLayer2: true, |
| 2069 | vlan: nativeVlan, |
| 2070 | vlanTrunk: []*VlanTrunk{ |
| 2071 | {ID: &id}, |
| 2072 | { |
| 2073 | MinID: &minID, |
| 2074 | MaxID: &maxID, |
| 2075 | }, |
| 2076 | }, |
| 2077 | AddErr020: "cannot convert: no valid IP addresses", |
| 2078 | AddErr010: "cannot convert: no valid IP addresses", |
| 2079 | } |
| 2080 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2081 | }) |
| 2082 | |
| 2083 | It(fmt.Sprintf("[%s] configures and deconfigures a l2 bridge with vlan id 100 and no default vlan using ADD/DEL", ver), func() { |
| 2084 | tc := testCase{ |
| 2085 | cniVersion: ver, |
| 2086 | isLayer2: true, |
| 2087 | vlan: 100, |
| 2088 | removeDefaultVlan: true, |
| 2089 | AddErr020: "cannot convert: no valid IP addresses", |
| 2090 | AddErr010: "cannot convert: no valid IP addresses", |
| 2091 | } |
| 2092 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2093 | }) |
| 2094 | |
| 2095 | for i, tc := range []testCase{ |
| 2096 | { |
| 2097 | // IPv4 only |
| 2098 | subnet: "10.1.2.0/24", |
| 2099 | expGWCIDRs: []string{"10.1.2.1/24"}, |
| 2100 | vlan: 100, |
| 2101 | }, |
| 2102 | { |
| 2103 | // IPv6 only |
| 2104 | subnet: "2001:db8::0/64", |
| 2105 | expGWCIDRs: []string{"2001:db8::1/64"}, |
| 2106 | vlan: 100, |
| 2107 | }, |
| 2108 | { |
| 2109 | // Dual-Stack |
| 2110 | ranges: []rangeInfo{ |
| 2111 | {subnet: "192.168.0.0/24"}, |
| 2112 | {subnet: "fd00::0/64"}, |
| 2113 | }, |
| 2114 | expGWCIDRs: []string{ |
| 2115 | "192.168.0.1/24", |
| 2116 | "fd00::1/64", |
| 2117 | }, |
| 2118 | vlan: 100, |
| 2119 | }, |
| 2120 | { |
| 2121 | // 3 Subnets (1 IPv4 and 2 IPv6 subnets) |
| 2122 | ranges: []rangeInfo{ |
| 2123 | {subnet: "192.168.0.0/24"}, |
| 2124 | {subnet: "fd00::0/64"}, |
| 2125 | {subnet: "2001:db8::0/64"}, |
| 2126 | }, |
| 2127 | expGWCIDRs: []string{ |
| 2128 | "192.168.0.1/24", |
| 2129 | "fd00::1/64", |
| 2130 | "2001:db8::1/64", |
| 2131 | }, |
| 2132 | vlan: 100, |
| 2133 | AddErr020: "CNI version 0.2.0 does not support more than 1 address per family", |
| 2134 | DelErr020: "CNI version 0.2.0 does not support more than 1 address per family", |
| 2135 | AddErr010: "CNI version 0.1.0 does not support more than 1 address per family", |
| 2136 | DelErr010: "CNI version 0.1.0 does not support more than 1 address per family", |
| 2137 | }, |
| 2138 | } { |
| 2139 | tc := tc |
| 2140 | i := i |
| 2141 | It(fmt.Sprintf("[%s] (%d) configures and deconfigures a bridge, veth with default route and vlanID 100 with ADD/DEL", ver, i), func() { |
| 2142 | tc.cniVersion = ver |
| 2143 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2144 | }) |
| 2145 | It(fmt.Sprintf("[%s] (%d) configures and deconfigures a bridge, veth with default route and vlanID 100 and no default vlan with ADD/DEL", ver, i), func() { |
| 2146 | tc.cniVersion = ver |
| 2147 | tc.removeDefaultVlan = true |
| 2148 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2149 | }) |
| 2150 | } |
| 2151 | |
| 2152 | for i, tc := range []testCase{ |
| 2153 | { |
| 2154 | // IPv4 only |
| 2155 | subnet: "10.1.2.0/24", |
| 2156 | expGWCIDRs: []string{"10.1.2.1/24"}, |
| 2157 | }, |
| 2158 | { |
| 2159 | // IPv6 only |
| 2160 | subnet: "2001:db8::0/64", |
| 2161 | expGWCIDRs: []string{"2001:db8::1/64"}, |
| 2162 | }, |
| 2163 | { |
| 2164 | // Dual-Stack |
| 2165 | ranges: []rangeInfo{ |
| 2166 | {subnet: "192.168.0.0/24"}, |
| 2167 | {subnet: "fd00::0/64"}, |
| 2168 | }, |
| 2169 | expGWCIDRs: []string{ |
| 2170 | "192.168.0.1/24", |
| 2171 | "fd00::1/64", |
| 2172 | }, |
| 2173 | }, |
| 2174 | } { |
| 2175 | tc := tc |
| 2176 | i := i |
| 2177 | It(fmt.Sprintf("[%s] (%d) configures and deconfigures a bridge and veth with default route with ADD/DEL", ver, i), func() { |
| 2178 | tc.cniVersion = ver |
| 2179 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2180 | }) |
| 2181 | } |
| 2182 | |
| 2183 | It("deconfigures an unconfigured bridge with DEL", func() { |
| 2184 | tc := testCase{ |
| 2185 | cniVersion: ver, |
| 2186 | subnet: "10.1.2.0/24", |
| 2187 | expGWCIDRs: []string{"10.1.2.1/24"}, |
| 2188 | } |
| 2189 | |
| 2190 | tester := testerV03x{ |
| 2191 | testNS: originalNS, |
| 2192 | targetNS: targetNS, |
| 2193 | args: tc.createCmdArgs(targetNS, dataDir), |
| 2194 | } |
| 2195 | |
| 2196 | // Execute cmdDEL on the plugin, expect no errors |
| 2197 | tester.cmdDelTest(tc, dataDir) |
| 2198 | }) |
| 2199 | |
| 2200 | for i, tc := range []struct { |
| 2201 | gwCIDRFirst string |
| 2202 | gwCIDRSecond string |
| 2203 | }{ |
| 2204 | { |
| 2205 | // IPv4 |
| 2206 | gwCIDRFirst: "10.0.0.1/8", |
| 2207 | gwCIDRSecond: "10.1.2.3/16", |
| 2208 | }, |
| 2209 | { |
| 2210 | // IPv6, overlapping subnets |
| 2211 | gwCIDRFirst: "2001:db8:1::1/48", |
| 2212 | gwCIDRSecond: "2001:db8:1:2::1/64", |
| 2213 | }, |
| 2214 | { |
| 2215 | // IPv6, non-overlapping subnets |
| 2216 | gwCIDRFirst: "2001:db8:1:2::1/64", |
| 2217 | gwCIDRSecond: "fd00:1234::1/64", |
| 2218 | }, |
| 2219 | } { |
| 2220 | tc := tc |
| 2221 | i := i |
| 2222 | It(fmt.Sprintf("[%s] (%d) ensure bridge address", ver, i), func() { |
| 2223 | conf := testCase{cniVersion: ver, isGW: true}.netConf() |
| 2224 | |
| 2225 | gwIP, gwSubnet, err := net.ParseCIDR(tc.gwCIDRFirst) |
| 2226 | Expect(err).NotTo(HaveOccurred()) |
| 2227 | gwnFirst := net.IPNet{IP: gwIP, Mask: gwSubnet.Mask} |
| 2228 | gwIP, gwSubnet, err = net.ParseCIDR(tc.gwCIDRSecond) |
| 2229 | Expect(err).NotTo(HaveOccurred()) |
| 2230 | gwnSecond := net.IPNet{IP: gwIP, Mask: gwSubnet.Mask} |
| 2231 | |
| 2232 | var family, expNumAddrs int |
| 2233 | switch { |
| 2234 | case gwIP.To4() != nil: |
| 2235 | family = netlink.FAMILY_V4 |
| 2236 | expNumAddrs = 1 |
| 2237 | default: |
| 2238 | family = netlink.FAMILY_V6 |
| 2239 | // Expect configured gw address plus link local |
| 2240 | expNumAddrs = 2 |
| 2241 | } |
| 2242 | |
| 2243 | subnetsOverlap := gwnFirst.Contains(gwnSecond.IP) || |
| 2244 | gwnSecond.Contains(gwnFirst.IP) |
| 2245 | |
| 2246 | err = originalNS.Do(func(ns.NetNS) error { |
| 2247 | defer GinkgoRecover() |
| 2248 | |
| 2249 | // Create the bridge |
| 2250 | bridge, _, err := setupBridge(conf) |
| 2251 | Expect(err).NotTo(HaveOccurred()) |
| 2252 | |
| 2253 | // Function to check IP address(es) on bridge |
| 2254 | checkBridgeIPs := func(cidr0, cidr1 string) { |
| 2255 | addrs, err := netlinksafe.AddrList(bridge, family) |
| 2256 | Expect(err).NotTo(HaveOccurred()) |
| 2257 | Expect(addrs).To(HaveLen(expNumAddrs)) |
| 2258 | addr := addrs[0].IPNet.String() |
| 2259 | Expect(addr).To(Equal(cidr0)) |
| 2260 | if cidr1 != "" { |
| 2261 | addr = addrs[1].IPNet.String() |
| 2262 | Expect(addr).To(Equal(cidr1)) |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | // Check if ForceAddress has default value |
| 2267 | Expect(conf.ForceAddress).To(BeFalse()) |
| 2268 | |
| 2269 | // Set first address on bridge |
| 2270 | err = ensureAddr(bridge, family, &gwnFirst, conf.ForceAddress) |
| 2271 | Expect(err).NotTo(HaveOccurred()) |
| 2272 | checkBridgeIPs(tc.gwCIDRFirst, "") |
| 2273 | |
| 2274 | // Attempt to set the second address on the bridge |
| 2275 | // with ForceAddress set to false. |
| 2276 | err = ensureAddr(bridge, family, &gwnSecond, false) |
| 2277 | if family == netlink.FAMILY_V4 || subnetsOverlap { |
| 2278 | // IPv4 or overlapping IPv6 subnets: |
| 2279 | // Expect an error, and address should remain the same |
| 2280 | Expect(err).To(HaveOccurred()) |
| 2281 | checkBridgeIPs(tc.gwCIDRFirst, "") |
| 2282 | } else { |
| 2283 | // Non-overlapping IPv6 subnets: |
| 2284 | // There should be 2 addresses (in addition to link local) |
| 2285 | Expect(err).NotTo(HaveOccurred()) |
| 2286 | expNumAddrs++ |
| 2287 | checkBridgeIPs(tc.gwCIDRSecond, tc.gwCIDRFirst) |
| 2288 | } |
| 2289 | |
| 2290 | // Set the second address on the bridge |
| 2291 | // with ForceAddress set to true. |
| 2292 | err = ensureAddr(bridge, family, &gwnSecond, true) |
| 2293 | Expect(err).NotTo(HaveOccurred()) |
| 2294 | if family == netlink.FAMILY_V4 || subnetsOverlap { |
| 2295 | // IPv4 or overlapping IPv6 subnets: |
| 2296 | // IP address should be reconfigured. |
| 2297 | checkBridgeIPs(tc.gwCIDRSecond, "") |
| 2298 | } else { |
| 2299 | // Non-overlapping IPv6 subnets: |
| 2300 | // There should be 2 addresses (in addition to link local) |
| 2301 | checkBridgeIPs(tc.gwCIDRSecond, tc.gwCIDRFirst) |
| 2302 | } |
| 2303 | |
| 2304 | return nil |
| 2305 | }) |
| 2306 | Expect(err).NotTo(HaveOccurred()) |
| 2307 | |
| 2308 | // Clean up bridge addresses for next test case |
| 2309 | delBridgeAddrs(originalNS) |
| 2310 | }) |
| 2311 | } |
| 2312 | |
| 2313 | It(fmt.Sprintf("[%s] ensure promiscuous mode on bridge", ver), func() { |
| 2314 | const IFNAME = "bridge0" |
| 2315 | |
| 2316 | conf := &NetConf{ |
| 2317 | NetConf: types.NetConf{ |
| 2318 | Name: "testConfig", |
| 2319 | Type: "bridge", |
| 2320 | }, |
| 2321 | BrName: IFNAME, |
| 2322 | IsGW: true, |
| 2323 | IPMasq: false, |
| 2324 | HairpinMode: false, |
| 2325 | PromiscMode: true, |
| 2326 | MTU: 5000, |
| 2327 | } |
| 2328 | |
| 2329 | err := originalNS.Do(func(ns.NetNS) error { |
| 2330 | defer GinkgoRecover() |
| 2331 | |
| 2332 | conf.NetConf.CNIVersion = ver |
| 2333 | _, _, err := setupBridge(conf) |
| 2334 | Expect(err).NotTo(HaveOccurred()) |
| 2335 | // Check if ForceAddress has default value |
| 2336 | Expect(conf.ForceAddress).To(BeFalse()) |
| 2337 | |
| 2338 | // Check if promiscuous mode is set correctly |
| 2339 | link, err := netlinksafe.LinkByName("bridge0") |
| 2340 | Expect(err).NotTo(HaveOccurred()) |
| 2341 | |
| 2342 | Expect(link.Attrs().Promisc).To(Equal(1)) |
| 2343 | return nil |
| 2344 | }) |
| 2345 | Expect(err).NotTo(HaveOccurred()) |
| 2346 | }) |
| 2347 | |
| 2348 | for i, tc := range []testCase{ |
| 2349 | { |
| 2350 | subnet: "10.1.2.0/24", |
| 2351 | }, |
| 2352 | { |
| 2353 | subnet: "2001:db8:42::/64", |
| 2354 | }, |
| 2355 | } { |
| 2356 | tc := tc |
| 2357 | i := i |
| 2358 | It(fmt.Sprintf("[%s] (%d) creates a bridge with a stable MAC addresses", ver, i), func() { |
| 2359 | err := originalNS.Do(func(ns.NetNS) error { |
| 2360 | defer GinkgoRecover() |
| 2361 | |
| 2362 | tc.cniVersion = ver |
| 2363 | _, _, err := setupBridge(tc.netConf()) |
| 2364 | Expect(err).NotTo(HaveOccurred()) |
| 2365 | link, err := netlinksafe.LinkByName(BRNAME) |
| 2366 | Expect(err).NotTo(HaveOccurred()) |
| 2367 | origMac := link.Attrs().HardwareAddr |
| 2368 | |
| 2369 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2370 | |
| 2371 | link, err = netlinksafe.LinkByName(BRNAME) |
| 2372 | Expect(err).NotTo(HaveOccurred()) |
| 2373 | Expect(link.Attrs().HardwareAddr).To(Equal(origMac)) |
| 2374 | return nil |
| 2375 | }) |
| 2376 | Expect(err).NotTo(HaveOccurred()) |
| 2377 | }) |
| 2378 | } |
| 2379 | |
| 2380 | It(fmt.Sprintf("[%s] uses an explicit MAC addresses for the container iface (from CNI_ARGS)", ver), func() { |
| 2381 | err := originalNS.Do(func(ns.NetNS) error { |
| 2382 | defer GinkgoRecover() |
| 2383 | |
| 2384 | const expectedMac = "02:00:00:00:00:00" |
| 2385 | tc := testCase{ |
| 2386 | cniVersion: ver, |
| 2387 | subnet: "10.1.2.0/24", |
| 2388 | envArgs: "MAC=" + expectedMac, |
| 2389 | |
| 2390 | expectedMac: expectedMac, |
| 2391 | } |
| 2392 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2393 | |
| 2394 | return nil |
| 2395 | }) |
| 2396 | Expect(err).NotTo(HaveOccurred()) |
| 2397 | }) |
| 2398 | |
| 2399 | It(fmt.Sprintf("[%s] uses an explicit MAC addresses for the container iface (from Args)", ver), func() { |
| 2400 | err := originalNS.Do(func(ns.NetNS) error { |
| 2401 | defer GinkgoRecover() |
| 2402 | |
| 2403 | const expectedMac = "02:00:00:00:00:00" |
| 2404 | tc := testCase{ |
| 2405 | cniVersion: ver, |
| 2406 | subnet: "10.1.2.0/24", |
| 2407 | envArgs: "MAC=" + "02:00:00:00:04:56", |
| 2408 | |
| 2409 | expectedMac: expectedMac, |
| 2410 | } |
| 2411 | tc.args.cni.mac = expectedMac |
| 2412 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2413 | |
| 2414 | return nil |
| 2415 | }) |
| 2416 | Expect(err).NotTo(HaveOccurred()) |
| 2417 | }) |
| 2418 | |
| 2419 | It(fmt.Sprintf("[%s] uses an explicit MAC addresses for the container iface (from RuntimeConfig)", ver), func() { |
| 2420 | err := originalNS.Do(func(ns.NetNS) error { |
| 2421 | defer GinkgoRecover() |
| 2422 | |
| 2423 | const expectedMac = "02:00:00:00:00:00" |
| 2424 | tc := testCase{ |
| 2425 | cniVersion: ver, |
| 2426 | subnet: "10.1.2.0/24", |
| 2427 | envArgs: "MAC=" + "02:00:00:00:04:56", |
| 2428 | |
| 2429 | expectedMac: expectedMac, |
| 2430 | } |
| 2431 | tc.args.cni.mac = "02:00:00:00:07:89" |
| 2432 | tc.runtimeConfig.mac = expectedMac |
| 2433 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2434 | |
| 2435 | return nil |
| 2436 | }) |
| 2437 | Expect(err).NotTo(HaveOccurred()) |
| 2438 | }) |
| 2439 | |
| 2440 | It(fmt.Sprintf("[%s] checks ip release in case of error", ver), func() { |
| 2441 | err := originalNS.Do(func(ns.NetNS) error { |
| 2442 | defer GinkgoRecover() |
| 2443 | tc := testCase{ |
| 2444 | cniVersion: ver, |
| 2445 | subnet: "10.1.2.0/24", |
| 2446 | } |
| 2447 | |
| 2448 | _, _, err := setupBridge(tc.netConf()) |
| 2449 | Expect(err).NotTo(HaveOccurred()) |
| 2450 | |
| 2451 | args := tc.createCmdArgs(originalNS, dataDir) |
| 2452 | |
| 2453 | // get number of allocated IPs before asking for a new one |
| 2454 | before, err := countIPAMIPs(dataDir) |
| 2455 | Expect(err).NotTo(HaveOccurred()) |
| 2456 | |
| 2457 | debugPostIPAMError = fmt.Errorf("debugPostIPAMError") |
| 2458 | _, _, err = testutils.CmdAddWithArgs(args, func() error { |
| 2459 | return cmdAdd(args) |
| 2460 | }) |
| 2461 | Expect(err).To(MatchError("debugPostIPAMError")) |
| 2462 | |
| 2463 | // get number of allocated IPs after failure |
| 2464 | after, err := countIPAMIPs(dataDir) |
| 2465 | Expect(err).NotTo(HaveOccurred()) |
| 2466 | |
| 2467 | Expect(before).To(Equal(after)) |
| 2468 | return nil |
| 2469 | }) |
| 2470 | Expect(err).NotTo(HaveOccurred()) |
| 2471 | }) |
| 2472 | |
| 2473 | if testutils.SpecVersionHasChaining(ver) { |
| 2474 | for _, tc := range []testCase{ |
| 2475 | { |
| 2476 | ranges: []rangeInfo{{ |
| 2477 | subnet: "10.1.2.0/24", |
| 2478 | }}, |
| 2479 | ipMasq: true, |
| 2480 | cniVersion: ver, |
| 2481 | }, |
| 2482 | { |
| 2483 | ranges: []rangeInfo{{ |
| 2484 | subnet: "10.1.2.0/24", |
| 2485 | }}, |
| 2486 | ipMasq: true, |
| 2487 | ipMasqBackend: "iptables", |
| 2488 | cniVersion: ver, |
| 2489 | }, |
| 2490 | { |
| 2491 | ranges: []rangeInfo{{ |
| 2492 | subnet: "10.1.2.0/24", |
| 2493 | }}, |
| 2494 | ipMasq: true, |
| 2495 | ipMasqBackend: "nftables", |
| 2496 | cniVersion: ver, |
| 2497 | }, |
| 2498 | } { |
| 2499 | tc := tc |
| 2500 | It(fmt.Sprintf("[%s] configures a bridge and ipMasq rules with ipMasqBackend %q", ver, tc.ipMasqBackend), func() { |
| 2501 | err := originalNS.Do(func(ns.NetNS) error { |
| 2502 | defer GinkgoRecover() |
| 2503 | |
| 2504 | args := tc.createCmdArgs(originalNS, dataDir) |
| 2505 | r, _, err := testutils.CmdAddWithArgs(args, func() error { |
| 2506 | return cmdAdd(args) |
| 2507 | }) |
| 2508 | Expect(err).NotTo(HaveOccurred()) |
| 2509 | result, err := types100.GetResult(r) |
| 2510 | Expect(err).NotTo(HaveOccurred()) |
| 2511 | Expect(result.IPs).Should(HaveLen(1)) |
| 2512 | |
| 2513 | ip := result.IPs[0].Address.IP.String() |
| 2514 | |
| 2515 | // Update this if the default ipmasq backend changes |
| 2516 | switch tc.ipMasqBackend { |
| 2517 | case "iptables", "": |
| 2518 | ipt, err := iptables.NewWithProtocol(iptables.ProtocolIPv4) |
| 2519 | Expect(err).NotTo(HaveOccurred()) |
| 2520 | |
| 2521 | rules, err := ipt.List("nat", "POSTROUTING") |
| 2522 | Expect(err).NotTo(HaveOccurred()) |
| 2523 | Expect(rules).Should(ContainElement(ContainSubstring(ip))) |
| 2524 | case "nftables": |
| 2525 | nft, err := knftables.New(knftables.InetFamily, "cni_plugins_masquerade") |
| 2526 | Expect(err).NotTo(HaveOccurred()) |
| 2527 | rules, err := nft.ListRules(context.TODO(), "masq_checks") |
| 2528 | Expect(err).NotTo(HaveOccurred()) |
| 2529 | // FIXME: ListRules() doesn't return the actual rule strings, |
| 2530 | // and we can't easily compute the ipmasq plugin's comment. |
| 2531 | comments := 0 |
| 2532 | for _, r := range rules { |
| 2533 | if r.Comment != nil { |
| 2534 | comments++ |
| 2535 | break |
| 2536 | } |
| 2537 | } |
| 2538 | Expect(comments).To(Equal(1), "expected to find exactly one Rule with a comment") |
| 2539 | } |
| 2540 | |
| 2541 | err = testutils.CmdDelWithArgs(args, func() error { |
| 2542 | return cmdDel(args) |
| 2543 | }) |
| 2544 | Expect(err).NotTo(HaveOccurred()) |
| 2545 | return nil |
| 2546 | }) |
| 2547 | Expect(err).NotTo(HaveOccurred()) |
| 2548 | }) |
| 2549 | } |
| 2550 | |
| 2551 | for i, tc := range []testCase{ |
| 2552 | { |
| 2553 | // IPv4 only |
| 2554 | ranges: []rangeInfo{{ |
| 2555 | subnet: "10.1.2.0/24", |
| 2556 | }}, |
| 2557 | expGWCIDRs: []string{"10.1.2.1/24"}, |
| 2558 | }, |
| 2559 | { |
| 2560 | // IPv6 only |
| 2561 | ranges: []rangeInfo{{ |
| 2562 | subnet: "2001:db8::0/64", |
| 2563 | }}, |
| 2564 | expGWCIDRs: []string{"2001:db8::1/64"}, |
| 2565 | }, |
| 2566 | { |
| 2567 | // Dual-Stack |
| 2568 | ranges: []rangeInfo{ |
| 2569 | {subnet: "192.168.0.0/24"}, |
| 2570 | {subnet: "fd00::0/64"}, |
| 2571 | }, |
| 2572 | expGWCIDRs: []string{ |
| 2573 | "192.168.0.1/24", |
| 2574 | "fd00::1/64", |
| 2575 | }, |
| 2576 | }, |
| 2577 | } { |
| 2578 | tc := tc |
| 2579 | i := i |
| 2580 | It(fmt.Sprintf("[%s] (%d) configures and deconfigures a bridge and veth with default route with ADD/DEL/CHECK", ver, i), func() { |
| 2581 | tc.cniVersion = ver |
| 2582 | cmdAddDelCheckTest(originalNS, targetNS, tc, dataDir) |
| 2583 | }) |
| 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | It(fmt.Sprintf("[%s] configures mac spoof-check (no mac spoofing)", ver), func() { |
| 2588 | Expect(originalNS.Do(func(ns.NetNS) error { |
| 2589 | defer GinkgoRecover() |
| 2590 | |
| 2591 | tc := testCase{ |
| 2592 | cniVersion: ver, |
| 2593 | subnet: "10.1.2.0/24", |
| 2594 | macspoofchk: true, |
| 2595 | } |
| 2596 | args := tc.createCmdArgs(originalNS, dataDir) |
| 2597 | _, _, err := testutils.CmdAddWithArgs(args, func() error { |
| 2598 | return cmdAdd(args) |
| 2599 | }) |
| 2600 | Expect(err).NotTo(HaveOccurred()) |
| 2601 | |
| 2602 | assertMacSpoofCheckRulesExist() |
| 2603 | |
| 2604 | Expect(testutils.CmdDelWithArgs(args, func() error { |
| 2605 | if err := cmdDel(args); err != nil { |
| 2606 | return err |
| 2607 | } |
| 2608 | assertMacSpoofCheckRulesMissing() |
| 2609 | return nil |
| 2610 | })).To(Succeed()) |
| 2611 | |
| 2612 | return nil |
| 2613 | })).To(Succeed()) |
| 2614 | }) |
| 2615 | |
| 2616 | It(fmt.Sprintf("[%s] should fail when both IPAM and DisableContainerInterface are set", ver), func() { |
| 2617 | Expect(originalNS.Do(func(ns.NetNS) error { |
| 2618 | defer GinkgoRecover() |
| 2619 | tc := testCase{ |
| 2620 | cniVersion: ver, |
| 2621 | subnet: "10.1.2.0/24", |
| 2622 | disableContIface: true, |
| 2623 | } |
| 2624 | args := tc.createCmdArgs(targetNS, dataDir) |
| 2625 | Expect(cmdAdd(args)).To(HaveOccurred()) |
| 2626 | |
| 2627 | return nil |
| 2628 | })).To(Succeed()) |
| 2629 | }) |
| 2630 | |
| 2631 | It(fmt.Sprintf("[%s] should set the container veth peer state down", ver), func() { |
| 2632 | Expect(originalNS.Do(func(ns.NetNS) error { |
| 2633 | defer GinkgoRecover() |
| 2634 | tc := testCase{ |
| 2635 | cniVersion: ver, |
| 2636 | disableContIface: true, |
| 2637 | isLayer2: true, |
| 2638 | AddErr020: "cannot convert: no valid IP addresses", |
| 2639 | AddErr010: "cannot convert: no valid IP addresses", |
| 2640 | } |
| 2641 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2642 | return nil |
| 2643 | })).To(Succeed()) |
| 2644 | }) |
| 2645 | |
| 2646 | It(fmt.Sprintf("[%s] when port-isolation is off, should set the veth peer on node with isolation off", ver), func() { |
| 2647 | Expect(originalNS.Do(func(ns.NetNS) error { |
| 2648 | defer GinkgoRecover() |
| 2649 | tc := testCase{ |
| 2650 | cniVersion: ver, |
| 2651 | portIsolation: false, |
| 2652 | isLayer2: true, |
| 2653 | AddErr020: "cannot convert: no valid IP addresses", |
| 2654 | AddErr010: "cannot convert: no valid IP addresses", |
| 2655 | } |
| 2656 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2657 | return nil |
| 2658 | })).To(Succeed()) |
| 2659 | }) |
| 2660 | |
| 2661 | It(fmt.Sprintf("[%s] when port-isolation is on, should set the veth peer on node with isolation on", ver), func() { |
| 2662 | Expect(originalNS.Do(func(ns.NetNS) error { |
| 2663 | defer GinkgoRecover() |
| 2664 | tc := testCase{ |
| 2665 | cniVersion: ver, |
| 2666 | portIsolation: true, |
| 2667 | isLayer2: true, |
| 2668 | AddErr020: "cannot convert: no valid IP addresses", |
| 2669 | AddErr010: "cannot convert: no valid IP addresses", |
| 2670 | } |
| 2671 | cmdAddDelTest(originalNS, targetNS, tc, dataDir) |
| 2672 | return nil |
| 2673 | })).To(Succeed()) |
| 2674 | }) |
| 2675 | } |
| 2676 | |
| 2677 | It("check vlan id when loading net conf", func() { |
| 2678 | type vlanTC struct { |
| 2679 | testCase |
| 2680 | err error |
| 2681 | } |
| 2682 | |
| 2683 | createCaseFn := func(ver string, vlan int, err error) vlanTC { |
| 2684 | return vlanTC{ |
| 2685 | testCase: testCase{ |
| 2686 | cniVersion: ver, |
| 2687 | vlan: vlan, |
| 2688 | }, |
| 2689 | err: err, |
| 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | tests := []vlanTC{} |
| 2694 | tests = append(tests, createCaseFn("1.0.0", 0, nil)) |
| 2695 | tests = append(tests, createCaseFn("0.4.0", 0, nil)) |
| 2696 | tests = append(tests, createCaseFn("1.0.0", -100, fmt.Errorf("invalid VLAN ID -100 (must be between 0 and 4094)"))) |
| 2697 | tests = append(tests, createCaseFn("0.4.0", -100, fmt.Errorf("invalid VLAN ID -100 (must be between 0 and 4094)"))) |
| 2698 | tests = append(tests, createCaseFn("1.0.0", 5000, fmt.Errorf("invalid VLAN ID 5000 (must be between 0 and 4094)"))) |
| 2699 | tests = append(tests, createCaseFn("0.4.0", 5000, fmt.Errorf("invalid VLAN ID 5000 (must be between 0 and 4094)"))) |
| 2700 | |
| 2701 | for _, test := range tests { |
| 2702 | _, _, err := loadNetConf([]byte(test.netConfJSON("")), "") |
| 2703 | if test.err == nil { |
| 2704 | Expect(err).ToNot(HaveOccurred()) |
| 2705 | } else { |
| 2706 | Expect(err).To(Equal(test.err)) |
| 2707 | } |
| 2708 | } |
| 2709 | }) |
| 2710 | }) |
| 2711 | |
| 2712 | func assertMacSpoofCheckRulesExist() { |
| 2713 | assertMacSpoofCheckRules( |
| 2714 | func(actual interface{}, expectedLen int) { |
| 2715 | ExpectWithOffset(3, actual).To(HaveLen(expectedLen)) |
| 2716 | }) |
| 2717 | } |
| 2718 | |
| 2719 | func assertMacSpoofCheckRulesMissing() { |
| 2720 | assertMacSpoofCheckRules( |
| 2721 | func(actual interface{}, _ int) { |
| 2722 | ExpectWithOffset(3, actual).To(BeEmpty()) |
| 2723 | }) |
| 2724 | } |
| 2725 | |
| 2726 | func assertMacSpoofCheckRules(assert func(actual interface{}, expectedLen int)) { |
| 2727 | c, err := nft.ReadConfig() |
| 2728 | ExpectWithOffset(2, err).NotTo(HaveOccurred()) |
| 2729 | |
| 2730 | expectedTable := nft.NewTable("nat", "bridge") |
| 2731 | filter := nft.TypeFilter |
| 2732 | hook := nft.HookPreRouting |
| 2733 | prio := -300 |
| 2734 | policy := nft.PolicyAccept |
| 2735 | expectedBaseChain := nft.NewChain(expectedTable, "PREROUTING", &filter, &hook, &prio, &policy) |
| 2736 | |
| 2737 | assert(c.LookupRule(nft.NewRule( |
| 2738 | expectedTable, |
| 2739 | expectedBaseChain, |
| 2740 | nil, nil, nil, |
| 2741 | "macspoofchk-dummy-0-eth0", |
| 2742 | )), 1) |
| 2743 | |
| 2744 | assert(c.LookupRule(nft.NewRule( |
| 2745 | expectedTable, |
| 2746 | nft.NewRegularChain(expectedTable, "cni-br-iface-dummy-0-eth0"), |
| 2747 | nil, nil, nil, |
| 2748 | "macspoofchk-dummy-0-eth0", |
| 2749 | )), 1) |
| 2750 | |
| 2751 | assert(c.LookupRule(nft.NewRule( |
| 2752 | expectedTable, |
| 2753 | nft.NewRegularChain(expectedTable, "cni-br-iface-dummy-0-eth0-mac"), |
| 2754 | nil, nil, nil, |
| 2755 | "macspoofchk-dummy-0-eth0", |
| 2756 | )), 2) |
| 2757 | } |