github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/libnetwork/drivers/solaris/bridge/bridge_test.go (about) 1 // +build solaris 2 3 package bridge 4 5 import ( 6 "bytes" 7 "encoding/json" 8 "net" 9 "testing" 10 11 "github.com/docker/libnetwork/driverapi" 12 "github.com/docker/libnetwork/ipamutils" 13 "github.com/docker/libnetwork/netlabel" 14 "github.com/docker/libnetwork/netutils" 15 "github.com/docker/libnetwork/testutils" 16 "github.com/docker/libnetwork/types" 17 ) 18 19 func init() { 20 ipamutils.InitNetworks() 21 } 22 23 func TestEndpointMarshalling(t *testing.T) { 24 ip1, _ := types.ParseCIDR("172.22.0.9/16") 25 ip2, _ := types.ParseCIDR("2001:db8::9") 26 mac, _ := net.ParseMAC("ac:bd:24:57:66:77") 27 e := &bridgeEndpoint{ 28 id: "d2c015a1fe5930650cbcd50493efba0500bcebd8ee1f4401a16319f8a567de33", 29 nid: "ee33fbb43c323f1920b6b35a0101552ac22ede960d0e5245e9738bccc68b2415", 30 addr: ip1, 31 addrv6: ip2, 32 macAddress: mac, 33 srcName: "veth123456", 34 config: &endpointConfiguration{MacAddress: mac}, 35 containerConfig: &containerConfiguration{ 36 ParentEndpoints: []string{"one", "due", "three"}, 37 ChildEndpoints: []string{"four", "five", "six"}, 38 }, 39 extConnConfig: &connectivityConfiguration{ 40 ExposedPorts: []types.TransportPort{ 41 { 42 Proto: 6, 43 Port: uint16(18), 44 }, 45 }, 46 PortBindings: []types.PortBinding{ 47 { 48 Proto: 6, 49 IP: net.ParseIP("17210.33.9.56"), 50 Port: uint16(18), 51 HostPort: uint16(3000), 52 HostPortEnd: uint16(14000), 53 }, 54 }, 55 }, 56 portMapping: []types.PortBinding{ 57 { 58 Proto: 17, 59 IP: net.ParseIP("172.33.9.56"), 60 Port: uint16(99), 61 HostIP: net.ParseIP("10.10.100.2"), 62 HostPort: uint16(9900), 63 HostPortEnd: uint16(10000), 64 }, 65 { 66 Proto: 6, 67 IP: net.ParseIP("171.33.9.56"), 68 Port: uint16(55), 69 HostIP: net.ParseIP("10.11.100.2"), 70 HostPort: uint16(5500), 71 HostPortEnd: uint16(55000), 72 }, 73 }, 74 } 75 76 b, err := json.Marshal(e) 77 if err != nil { 78 t.Fatal(err) 79 } 80 81 ee := &bridgeEndpoint{} 82 err = json.Unmarshal(b, ee) 83 if err != nil { 84 t.Fatal(err) 85 } 86 87 if e.id != ee.id || e.nid != ee.nid || e.srcName != ee.srcName || !bytes.Equal(e.macAddress, ee.macAddress) || 88 !types.CompareIPNet(e.addr, ee.addr) || !types.CompareIPNet(e.addrv6, ee.addrv6) || 89 !compareEpConfig(e.config, ee.config) || 90 !compareContainerConfig(e.containerConfig, ee.containerConfig) || 91 !compareConnConfig(e.extConnConfig, ee.extConnConfig) || 92 !compareBindings(e.portMapping, ee.portMapping) { 93 t.Fatalf("JSON marsh/unmarsh failed.\nOriginal:\n%#v\nDecoded:\n%#v", e, ee) 94 } 95 } 96 97 func compareEpConfig(a, b *endpointConfiguration) bool { 98 if a == b { 99 return true 100 } 101 if a == nil || b == nil { 102 return false 103 } 104 return bytes.Equal(a.MacAddress, b.MacAddress) 105 } 106 107 func compareContainerConfig(a, b *containerConfiguration) bool { 108 if a == b { 109 return true 110 } 111 if a == nil || b == nil { 112 return false 113 } 114 if len(a.ParentEndpoints) != len(b.ParentEndpoints) || 115 len(a.ChildEndpoints) != len(b.ChildEndpoints) { 116 return false 117 } 118 for i := 0; i < len(a.ParentEndpoints); i++ { 119 if a.ParentEndpoints[i] != b.ParentEndpoints[i] { 120 return false 121 } 122 } 123 for i := 0; i < len(a.ChildEndpoints); i++ { 124 if a.ChildEndpoints[i] != b.ChildEndpoints[i] { 125 return false 126 } 127 } 128 return true 129 } 130 131 func compareConnConfig(a, b *connectivityConfiguration) bool { 132 if a == b { 133 return true 134 } 135 if a == nil || b == nil { 136 return false 137 } 138 if len(a.ExposedPorts) != len(b.ExposedPorts) || 139 len(a.PortBindings) != len(b.PortBindings) { 140 return false 141 } 142 for i := 0; i < len(a.ExposedPorts); i++ { 143 if !a.ExposedPorts[i].Equal(&b.ExposedPorts[i]) { 144 return false 145 } 146 } 147 for i := 0; i < len(a.PortBindings); i++ { 148 if !a.PortBindings[i].Equal(&b.PortBindings[i]) { 149 return false 150 } 151 } 152 return true 153 } 154 155 func compareBindings(a, b []types.PortBinding) bool { 156 if len(a) != len(b) { 157 return false 158 } 159 for i := 0; i < len(a); i++ { 160 if !a[i].Equal(&b[i]) { 161 return false 162 } 163 } 164 return true 165 } 166 167 func getIPv4Data(t *testing.T) []driverapi.IPAMData { 168 ipd := driverapi.IPAMData{AddressSpace: "full"} 169 nw, _, err := netutils.ElectInterfaceAddresses("") 170 if err != nil { 171 t.Fatal(err) 172 } 173 ipd.Pool = nw 174 // Set network gateway to X.X.X.1 175 ipd.Gateway = types.GetIPNetCopy(nw) 176 ipd.Gateway.IP[len(ipd.Gateway.IP)-1] = 1 177 return []driverapi.IPAMData{ipd} 178 } 179 180 func TestCreateFullOptions(t *testing.T) { 181 defer testutils.SetupTestOSContext(t)() 182 d := newDriver() 183 184 config := &configuration{ 185 EnableIPForwarding: true, 186 EnableIPTables: true, 187 } 188 189 // Test this scenario: Default gw address does not belong to 190 // container network and it's greater than bridge address 191 cnw, _ := types.ParseCIDR("172.16.122.0/24") 192 bnw, _ := types.ParseCIDR("172.16.0.0/24") 193 br, _ := types.ParseCIDR("172.16.0.1/16") 194 defgw, _ := types.ParseCIDR("172.16.0.100/16") 195 196 genericOption := make(map[string]interface{}) 197 genericOption[netlabel.GenericData] = config 198 199 if err := d.configure(genericOption); err != nil { 200 t.Fatalf("Failed to setup driver config: %v", err) 201 } 202 203 netOption := make(map[string]interface{}) 204 netOption[netlabel.EnableIPv6] = true 205 netOption[netlabel.GenericData] = &networkConfiguration{ 206 BridgeName: DefaultBridgeName, 207 } 208 209 ipdList := []driverapi.IPAMData{ 210 { 211 Pool: bnw, 212 Gateway: br, 213 AuxAddresses: map[string]*net.IPNet{DefaultGatewayV4AuxKey: defgw}, 214 }, 215 } 216 err := d.CreateNetwork("dummy", netOption, nil, ipdList, nil) 217 if err != nil { 218 t.Fatalf("Failed to create bridge: %v", err) 219 } 220 221 // Verify the IP address allocated for the endpoint belongs to the container network 222 epOptions := make(map[string]interface{}) 223 te := newTestEndpoint(cnw, 10) 224 err = d.CreateEndpoint("dummy", "ep1", te.Interface(), epOptions) 225 if err != nil { 226 t.Fatalf("Failed to create an endpoint : %s", err.Error()) 227 } 228 229 if !cnw.Contains(te.Interface().Address().IP) { 230 t.Fatalf("endpoint got assigned address outside of container network(%s): %s", cnw.String(), te.Interface().Address()) 231 } 232 } 233 234 func TestCreateNoConfig(t *testing.T) { 235 if !testutils.IsRunningInContainer() { 236 defer testutils.SetupTestOSContext(t)() 237 } 238 d := newDriver() 239 240 netconfig := &networkConfiguration{BridgeName: DefaultBridgeName} 241 genericOption := make(map[string]interface{}) 242 genericOption[netlabel.GenericData] = netconfig 243 244 if err := d.CreateNetwork("dummy", genericOption, nil, getIPv4Data(t), nil); err != nil { 245 t.Fatalf("Failed to create bridge: %v", err) 246 } 247 } 248 249 func TestCreateFullOptionsLabels(t *testing.T) { 250 if !testutils.IsRunningInContainer() { 251 defer testutils.SetupTestOSContext(t)() 252 } 253 d := newDriver() 254 255 config := &configuration{ 256 EnableIPForwarding: true, 257 } 258 genericOption := make(map[string]interface{}) 259 genericOption[netlabel.GenericData] = config 260 261 if err := d.configure(genericOption); err != nil { 262 t.Fatalf("Failed to setup driver config: %v", err) 263 } 264 265 bndIPs := "127.0.0.1" 266 nwV6s := "2001:db8:2600:2700:2800::/80" 267 gwV6s := "2001:db8:2600:2700:2800::25/80" 268 nwV6, _ := types.ParseCIDR(nwV6s) 269 gwV6, _ := types.ParseCIDR(gwV6s) 270 271 labels := map[string]string{ 272 BridgeName: DefaultBridgeName, 273 DefaultBridge: "true", 274 EnableICC: "true", 275 EnableIPMasquerade: "true", 276 DefaultBindingIP: bndIPs, 277 } 278 279 netOption := make(map[string]interface{}) 280 netOption[netlabel.EnableIPv6] = true 281 netOption[netlabel.GenericData] = labels 282 283 ipdList := getIPv4Data(t) 284 ipd6List := []driverapi.IPAMData{ 285 { 286 Pool: nwV6, 287 AuxAddresses: map[string]*net.IPNet{ 288 DefaultGatewayV6AuxKey: gwV6, 289 }, 290 }, 291 } 292 293 err := d.CreateNetwork("dummy", netOption, nil, ipdList, ipd6List) 294 if err != nil { 295 t.Fatalf("Failed to create bridge: %v", err) 296 } 297 298 nw, ok := d.networks["dummy"] 299 if !ok { 300 t.Fatalf("Cannot find dummy network in bridge driver") 301 } 302 303 if nw.config.BridgeName != DefaultBridgeName { 304 t.Fatalf("incongruent name in bridge network") 305 } 306 307 if !nw.config.EnableIPv6 { 308 t.Fatalf("incongruent EnableIPv6 in bridge network") 309 } 310 311 if !nw.config.EnableICC { 312 t.Fatalf("incongruent EnableICC in bridge network") 313 } 314 315 if !nw.config.EnableIPMasquerade { 316 t.Fatalf("incongruent EnableIPMasquerade in bridge network") 317 } 318 319 bndIP := net.ParseIP(bndIPs) 320 if !bndIP.Equal(nw.config.DefaultBindingIP) { 321 t.Fatalf("Unexpected: %v", nw.config.DefaultBindingIP) 322 } 323 324 if !types.CompareIPNet(nw.config.AddressIPv6, nwV6) { 325 t.Fatalf("Unexpected: %v", nw.config.AddressIPv6) 326 } 327 328 if !gwV6.IP.Equal(nw.config.DefaultGatewayIPv6) { 329 t.Fatalf("Unexpected: %v", nw.config.DefaultGatewayIPv6) 330 } 331 332 // In short here we are testing --fixed-cidr-v6 daemon option 333 // plus --mac-address run option 334 mac, _ := net.ParseMAC("aa:bb:cc:dd:ee:ff") 335 epOptions := map[string]interface{}{netlabel.MacAddress: mac} 336 te := newTestEndpoint(ipdList[0].Pool, 20) 337 err = d.CreateEndpoint("dummy", "ep1", te.Interface(), epOptions) 338 if err != nil { 339 t.Fatal(err) 340 } 341 } 342 343 func TestCreate(t *testing.T) { 344 if !testutils.IsRunningInContainer() { 345 defer testutils.SetupTestOSContext(t)() 346 } 347 348 d := newDriver() 349 350 if err := d.configure(nil); err != nil { 351 t.Fatalf("Failed to setup driver config: %v", err) 352 } 353 354 netconfig := &networkConfiguration{BridgeName: DefaultBridgeName} 355 genericOption := make(map[string]interface{}) 356 genericOption[netlabel.GenericData] = netconfig 357 358 if err := d.CreateNetwork("dummy", genericOption, nil, getIPv4Data(t), nil); err != nil { 359 t.Fatalf("Failed to create bridge: %v", err) 360 } 361 362 err := d.CreateNetwork("dummy", genericOption, nil, getIPv4Data(t), nil) 363 if err == nil { 364 t.Fatalf("Expected bridge driver to refuse creation of second network with default name") 365 } 366 if _, ok := err.(types.ForbiddenError); !ok { 367 t.Fatalf("Creation of second network with default name failed with unexpected error type") 368 } 369 } 370 371 func TestCreateFail(t *testing.T) { 372 if !testutils.IsRunningInContainer() { 373 defer testutils.SetupTestOSContext(t)() 374 } 375 376 d := newDriver() 377 378 if err := d.configure(nil); err != nil { 379 t.Fatalf("Failed to setup driver config: %v", err) 380 } 381 382 netconfig := &networkConfiguration{BridgeName: "dummy0", DefaultBridge: true} 383 genericOption := make(map[string]interface{}) 384 genericOption[netlabel.GenericData] = netconfig 385 386 if err := d.CreateNetwork("dummy", genericOption, nil, getIPv4Data(t), nil); err == nil { 387 t.Fatal("Bridge creation was expected to fail") 388 } 389 } 390 391 type testInterface struct { 392 mac net.HardwareAddr 393 addr *net.IPNet 394 addrv6 *net.IPNet 395 srcName string 396 dstName string 397 } 398 399 type testEndpoint struct { 400 iface *testInterface 401 gw net.IP 402 gw6 net.IP 403 hostsPath string 404 resolvConfPath string 405 routes []types.StaticRoute 406 } 407 408 func newTestEndpoint(nw *net.IPNet, ordinal byte) *testEndpoint { 409 addr := types.GetIPNetCopy(nw) 410 addr.IP[len(addr.IP)-1] = ordinal 411 return &testEndpoint{iface: &testInterface{addr: addr}} 412 } 413 414 func (te *testEndpoint) Interface() driverapi.InterfaceInfo { 415 if te.iface != nil { 416 return te.iface 417 } 418 419 return nil 420 } 421 422 func (i *testInterface) MacAddress() net.HardwareAddr { 423 return i.mac 424 } 425 426 func (i *testInterface) Address() *net.IPNet { 427 return i.addr 428 } 429 430 func (i *testInterface) AddressIPv6() *net.IPNet { 431 return i.addrv6 432 } 433 434 func (i *testInterface) SetMacAddress(mac net.HardwareAddr) error { 435 if i.mac != nil { 436 return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", i.mac, mac) 437 } 438 if mac == nil { 439 return types.BadRequestErrorf("tried to set nil MAC address to endpoint interface") 440 } 441 i.mac = types.GetMacCopy(mac) 442 return nil 443 } 444 445 func (i *testInterface) SetIPAddress(address *net.IPNet) error { 446 if address.IP == nil { 447 return types.BadRequestErrorf("tried to set nil IP address to endpoint interface") 448 } 449 if address.IP.To4() == nil { 450 return setAddress(&i.addrv6, address) 451 } 452 return setAddress(&i.addr, address) 453 } 454 455 func setAddress(ifaceAddr **net.IPNet, address *net.IPNet) error { 456 if *ifaceAddr != nil { 457 return types.ForbiddenErrorf("endpoint interface IP present (%s). Cannot be modified with (%s).", *ifaceAddr, address) 458 } 459 *ifaceAddr = types.GetIPNetCopy(address) 460 return nil 461 } 462 463 func (i *testInterface) SetNames(srcName string, dstName string) error { 464 i.srcName = srcName 465 i.dstName = dstName 466 return nil 467 } 468 469 func (te *testEndpoint) InterfaceName() driverapi.InterfaceNameInfo { 470 if te.iface != nil { 471 return te.iface 472 } 473 474 return nil 475 } 476 477 func (te *testEndpoint) SetGateway(gw net.IP) error { 478 te.gw = gw 479 return nil 480 } 481 482 func (te *testEndpoint) SetGatewayIPv6(gw6 net.IP) error { 483 te.gw6 = gw6 484 return nil 485 } 486 487 func (te *testEndpoint) AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error { 488 te.routes = append(te.routes, types.StaticRoute{Destination: destination, RouteType: routeType, NextHop: nextHop}) 489 return nil 490 } 491 492 func (te *testEndpoint) AddTableEntry(tableName string, key string, value []byte) error { 493 return nil 494 } 495 496 func (te *testEndpoint) DisableGatewayService() {} 497 498 func TestQueryEndpointInfo(t *testing.T) { 499 testQueryEndpointInfo(t, true) 500 } 501 502 func testQueryEndpointInfo(t *testing.T, ulPxyEnabled bool) { 503 defer testutils.SetupTestOSContext(t)() 504 505 d := newDriver() 506 507 config := &configuration{ 508 EnableIPTables: true, 509 EnableUserlandProxy: ulPxyEnabled, 510 } 511 genericOption := make(map[string]interface{}) 512 genericOption[netlabel.GenericData] = config 513 514 if err := d.configure(genericOption); err != nil { 515 t.Fatalf("Failed to setup driver config: %v", err) 516 } 517 518 netconfig := &networkConfiguration{ 519 BridgeName: DefaultBridgeName, 520 EnableICC: false, 521 } 522 genericOption = make(map[string]interface{}) 523 genericOption[netlabel.GenericData] = netconfig 524 525 ipdList := getIPv4Data(t) 526 err := d.CreateNetwork("net1", genericOption, nil, ipdList, nil) 527 if err != nil { 528 t.Fatalf("Failed to create bridge: %v", err) 529 } 530 531 sbOptions := make(map[string]interface{}) 532 sbOptions[netlabel.PortMap] = getPortMapping() 533 534 te := newTestEndpoint(ipdList[0].Pool, 11) 535 err = d.CreateEndpoint("net1", "ep1", te.Interface(), nil) 536 if err != nil { 537 t.Fatalf("Failed to create an endpoint : %s", err.Error()) 538 } 539 540 err = d.Join("net1", "ep1", "sbox", te, sbOptions) 541 if err != nil { 542 t.Fatalf("Failed to join the endpoint: %v", err) 543 } 544 545 err = d.ProgramExternalConnectivity("net1", "ep1", sbOptions) 546 if err != nil { 547 t.Fatalf("Failed to program external connectivity: %v", err) 548 } 549 550 network, ok := d.networks["net1"] 551 if !ok { 552 t.Fatalf("Cannot find network %s inside driver", "net1") 553 } 554 ep, _ := network.endpoints["ep1"] 555 data, err := d.EndpointOperInfo(network.id, ep.id) 556 if err != nil { 557 t.Fatalf("Failed to ask for endpoint operational data: %v", err) 558 } 559 pmd, ok := data[netlabel.PortMap] 560 if !ok { 561 t.Fatalf("Endpoint operational data does not contain port mapping data") 562 } 563 pm, ok := pmd.([]types.PortBinding) 564 if !ok { 565 t.Fatalf("Unexpected format for port mapping in endpoint operational data") 566 } 567 if len(ep.portMapping) != len(pm) { 568 t.Fatalf("Incomplete data for port mapping in endpoint operational data") 569 } 570 for i, pb := range ep.portMapping { 571 if !pb.Equal(&pm[i]) { 572 t.Fatalf("Unexpected data for port mapping in endpoint operational data") 573 } 574 } 575 576 err = d.RevokeExternalConnectivity("net1", "ep1") 577 if err != nil { 578 t.Fatal(err) 579 } 580 581 // release host mapped ports 582 err = d.Leave("net1", "ep1") 583 if err != nil { 584 t.Fatal(err) 585 } 586 } 587 588 func getExposedPorts() []types.TransportPort { 589 return []types.TransportPort{ 590 {Proto: types.TCP, Port: uint16(5000)}, 591 {Proto: types.UDP, Port: uint16(400)}, 592 {Proto: types.TCP, Port: uint16(600)}, 593 } 594 } 595 596 func getPortMapping() []types.PortBinding { 597 return []types.PortBinding{ 598 {Proto: types.TCP, Port: uint16(230), HostPort: uint16(23000)}, 599 {Proto: types.UDP, Port: uint16(200), HostPort: uint16(22000)}, 600 {Proto: types.TCP, Port: uint16(120), HostPort: uint16(12000)}, 601 } 602 } 603 604 func TestValidateConfig(t *testing.T) { 605 if !testutils.IsRunningInContainer() { 606 defer testutils.SetupTestOSContext(t)() 607 } 608 609 // Test mtu 610 c := networkConfiguration{Mtu: -2} 611 err := c.Validate() 612 if err == nil { 613 t.Fatalf("Failed to detect invalid MTU number") 614 } 615 616 c.Mtu = 9000 617 err = c.Validate() 618 if err != nil { 619 t.Fatalf("unexpected validation error on MTU number") 620 } 621 622 // Bridge network 623 _, network, _ := net.ParseCIDR("172.28.0.0/16") 624 c = networkConfiguration{ 625 AddressIPv4: network, 626 } 627 628 err = c.Validate() 629 if err != nil { 630 t.Fatal(err) 631 } 632 633 // Test v4 gw 634 c.DefaultGatewayIPv4 = net.ParseIP("172.27.30.234") 635 err = c.Validate() 636 if err == nil { 637 t.Fatalf("Failed to detect invalid default gateway") 638 } 639 640 c.DefaultGatewayIPv4 = net.ParseIP("172.28.30.234") 641 err = c.Validate() 642 if err != nil { 643 t.Fatalf("Unexpected validation error on default gateway") 644 } 645 646 // Test v6 gw 647 _, v6nw, _ := net.ParseCIDR("2001:db8:ae:b004::/64") 648 c = networkConfiguration{ 649 EnableIPv6: true, 650 AddressIPv6: v6nw, 651 DefaultGatewayIPv6: net.ParseIP("2001:db8:ac:b004::bad:a55"), 652 } 653 err = c.Validate() 654 if err == nil { 655 t.Fatalf("Failed to detect invalid v6 default gateway") 656 } 657 658 c.DefaultGatewayIPv6 = net.ParseIP("2001:db8:ae:b004::bad:a55") 659 err = c.Validate() 660 if err != nil { 661 t.Fatalf("Unexpected validation error on v6 default gateway") 662 } 663 664 c.AddressIPv6 = nil 665 err = c.Validate() 666 if err == nil { 667 t.Fatalf("Failed to detect invalid v6 default gateway") 668 } 669 670 c.AddressIPv6 = nil 671 err = c.Validate() 672 if err == nil { 673 t.Fatalf("Failed to detect invalid v6 default gateway") 674 } 675 }