func TestCreateFullOptions(t *testing.T) { defer netutils.SetupTestNetNS(t)() d := newDriver() config := &configuration{ EnableIPForwarding: true, } netConfig := &networkConfiguration{ BridgeName: DefaultBridgeName, EnableIPv6: true, FixedCIDR: bridgeNetworks[0], EnableIPTables: true, } _, netConfig.FixedCIDRv6, _ = net.ParseCIDR("2001:db8::/48") genericOption := make(map[string]interface{}) genericOption[netlabel.GenericData] = config if err := d.Config(genericOption); err != nil { t.Fatalf("Failed to setup driver config: %v", err) } netOption := make(map[string]interface{}) netOption[netlabel.GenericData] = netConfig err := d.CreateNetwork("dummy", netOption) if err != nil { t.Fatalf("Failed to create bridge: %v", err) } }
func TestUnknownNetwork(t *testing.T) { if !netutils.IsRunningInContainer() { defer netutils.SetupTestNetNS(t)() } netOption := options.Generic{ "BridgeName": "testnetwork", "AllowNonDefaultBridge": true} option := options.Generic{ netlabel.GenericData: netOption, } network, err := createTestNetwork(bridgeNetType, "testnetwork", option) if err != nil { t.Fatal(err) } err = network.Delete() if err != nil { t.Fatal(err) } err = network.Delete() if err == nil { t.Fatal("Expected to fail. But instead succeeded") } if _, ok := err.(*libnetwork.UnknownNetworkError); !ok { t.Fatalf("Did not fail with expected error. Actual error: %v", err) } }
func TestEndpointInvalidLeave(t *testing.T) { if !netutils.IsRunningInContainer() { defer netutils.SetupTestNetNS(t)() } n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{}) if err != nil { t.Fatal(err) } ep, err := n.CreateEndpoint("ep1") if err != nil { t.Fatal(err) } err = ep.Leave(containerID) if err == nil { t.Fatal("Expected to fail leave from an endpoint which has no active join") } if _, ok := err.(libnetwork.InvalidContainerIDError); !ok { if err != libnetwork.ErrNoContainer { t.Fatalf("Failed for unexpected reason: %v", err) } } _, err = ep.Join(containerID, libnetwork.JoinOptionHostname("test"), libnetwork.JoinOptionDomainname("docker.io"), libnetwork.JoinOptionExtraHost("web", "192.168.0.1")) if err != nil { t.Fatal(err) } defer func() { err = ep.Leave(containerID) if err != nil { t.Fatal(err) } }() err = ep.Leave("") if err == nil { t.Fatal("Expected to fail leave with empty container id") } if _, ok := err.(libnetwork.InvalidContainerIDError); !ok { t.Fatalf("Failed for unexpected reason: %v", err) } err = ep.Leave("container2") if err == nil { t.Fatal("Expected to fail leave with wrong container id") } if _, ok := err.(libnetwork.InvalidContainerIDError); !ok { t.Fatalf("Failed for unexpected reason: %v", err) } }
func TestSandboxCreateTwice(t *testing.T) { defer netutils.SetupTestNetNS(t)() key, err := newKey(t) if err != nil { t.Fatalf("Failed to obtain a key: %v", err) } _, err = NewSandbox(key, true) if err != nil { t.Fatalf("Failed to create a new sandbox: %v", err) } runtime.LockOSThread() // Create another sandbox with the same key to see if we handle it // gracefully. s, err := NewSandbox(key, true) if err != nil { t.Fatalf("Failed to create a new sandbox: %v", err) } runtime.LockOSThread() err = s.Destroy() if err != nil { t.Fatal(err) } GC() verifyCleanup(t, s, false) }
func TestDuplicateNetwork(t *testing.T) { if !netutils.IsRunningInContainer() { defer netutils.SetupTestNetNS(t)() } controller, err := libnetwork.New() if err != nil { t.Fatal(err) } genericOption := make(map[string]interface{}) genericOption[netlabel.GenericData] = options.Generic{} err = controller.ConfigureNetworkDriver(bridgeNetType, genericOption) if err != nil { t.Fatal(err) } _, err = controller.NewNetwork(bridgeNetType, "testnetwork", libnetwork.NetworkOptionGeneric(genericOption)) if err != nil { t.Fatal(err) } _, err = controller.NewNetwork(bridgeNetType, "testnetwork") if err == nil { t.Fatal("Expected to fail. But instead succeeded") } if _, ok := err.(libnetwork.NetworkNameError); !ok { t.Fatalf("Did not fail with expected error. Actual error: %v", err) } }
func TestSetupBadFixedCIDRv4(t *testing.T) { defer netutils.SetupTestNetNS(t)() config := &NetworkConfiguration{ BridgeName: DefaultBridgeName, AddressIPv4: &net.IPNet{IP: net.ParseIP("192.168.1.1"), Mask: net.CIDRMask(24, 32)}, FixedCIDR: &net.IPNet{IP: net.ParseIP("192.168.2.0"), Mask: net.CIDRMask(24, 32)}} br := &bridgeInterface{} if err := setupDevice(config, br); err != nil { t.Fatalf("Bridge creation failed: %v", err) } if err := setupBridgeIPv4(config, br); err != nil { t.Fatalf("Assign IPv4 to bridge failed: %v", err) } err := setupFixedCIDRv4(config, br) if err == nil { t.Fatal("Setup bridge FixedCIDRv4 should have failed") } if _, ok := err.(*FixedCIDRv4Error); !ok { t.Fatalf("Did not fail with expected error. Actual error: %v", err) } }
func TestSetupFixedCIDRv4(t *testing.T) { defer netutils.SetupTestNetNS(t)() config := &NetworkConfiguration{ BridgeName: DefaultBridgeName, AddressIPv4: &net.IPNet{IP: net.ParseIP("192.168.1.1"), Mask: net.CIDRMask(16, 32)}, FixedCIDR: &net.IPNet{IP: net.ParseIP("192.168.2.0"), Mask: net.CIDRMask(24, 32)}} br := &bridgeInterface{} if err := setupDevice(config, br); err != nil { t.Fatalf("Bridge creation failed: %v", err) } if err := setupBridgeIPv4(config, br); err != nil { t.Fatalf("Assign IPv4 to bridge failed: %v", err) } if err := setupFixedCIDRv4(config, br); err != nil { t.Fatalf("Failed to setup bridge FixedCIDRv4: %v", err) } if ip, err := ipAllocator.RequestIP(config.FixedCIDR, nil); err != nil { t.Fatalf("Failed to request IP to allocator: %v", err) } else if expected := "192.168.2.1"; ip.String() != expected { t.Fatalf("Expected allocated IP %s, got %s", expected, ip) } }
func TestCreate(t *testing.T) { defer netutils.SetupTestNetNS(t)() d := newDriver() if err := d.Config(nil); err != nil { t.Fatalf("Failed to setup driver config: %v", err) } netconfig := &networkConfiguration{BridgeName: DefaultBridgeName} genericOption := make(map[string]interface{}) genericOption[netlabel.GenericData] = netconfig if err := d.CreateNetwork("dummy", genericOption); err != nil { t.Fatalf("Failed to create bridge: %v", err) } err := d.CreateNetwork("dummy", genericOption) if err == nil { t.Fatalf("Expected bridge driver to refuse creation of second network with default name") } if _, ok := err.(types.ForbiddenError); !ok { t.Fatalf("Creation of second network with default name failed with unexpected error type") } err = d.DeleteNetwork("dummy") if err == nil { t.Fatalf("deletion of network with default name should fail on this driver") } if _, ok := err.(types.ForbiddenError); !ok { t.Fatalf("deletion of network with default name failed with unexpected error type") } }
func TestLinkCreateNoEnableIPv6(t *testing.T) { defer netutils.SetupTestNetNS(t)() d := newDriver() if err := d.Config(nil); err != nil { t.Fatalf("Failed to setup driver config: %v", err) } config := &networkConfiguration{ BridgeName: DefaultBridgeName} genericOption := make(map[string]interface{}) genericOption[netlabel.GenericData] = config err := d.CreateNetwork("dummy", genericOption) if err != nil { t.Fatalf("Failed to create bridge: %v", err) } te := &testEndpoint{ifaces: []*testInterface{}} err = d.CreateEndpoint("dummy", "ep", te, nil) if err != nil { t.Fatalf("Failed to create a link: %s", err.Error()) } interfaces := te.ifaces if interfaces[0].addrv6.IP.To16() != nil { t.Fatalf("Expectd IPv6 address to be nil when IPv6 is not enabled. Got IPv6 = %s", interfaces[0].addrv6.String()) } if te.gw6.To16() != nil { t.Fatalf("Expected GatewayIPv6 to be nil when IPv6 is not enabled. Got GatewayIPv6 = %s", te.gw6.String()) } }
func TestSetupFixedCIDRv6(t *testing.T) { defer netutils.SetupTestNetNS(t)() config := &NetworkConfiguration{} br := newInterface(config) _, config.FixedCIDRv6, _ = net.ParseCIDR("2002:db8::/48") if err := setupDevice(config, br); err != nil { t.Fatalf("Bridge creation failed: %v", err) } if err := setupBridgeIPv4(config, br); err != nil { t.Fatalf("Assign IPv4 to bridge failed: %v", err) } if err := setupBridgeIPv6(config, br); err != nil { t.Fatalf("Assign IPv4 to bridge failed: %v", err) } if err := setupFixedCIDRv6(config, br); err != nil { t.Fatalf("Failed to setup bridge FixedCIDRv6: %v", err) } if ip, err := ipAllocator.RequestIP(config.FixedCIDRv6, nil); err != nil { t.Fatalf("Failed to request IP to allocator: %v", err) } else if expected := "2002:db8::1"; ip.String() != expected { t.Fatalf("Expected allocated IP %s, got %s", expected, ip) } }
func TestNetworkType(t *testing.T) { if !netutils.IsRunningInContainer() { defer netutils.SetupTestNetNS(t)() } netOption := options.Generic{ netlabel.GenericData: options.Generic{ "BridgeName": "testnetwork", "AllowNonDefaultBridge": true, }, } n, err := createTestNetwork(bridgeNetType, "testnetwork", netOption) if err != nil { t.Fatal(err) } defer func() { if err := n.Delete(); err != nil { t.Fatal(err) } }() if n.Type() != bridgeNetType { t.Fatalf("Expected network type %s, got %s", bridgeNetType, n.Type()) } }
func TestNetworkID(t *testing.T) { if !netutils.IsRunningInContainer() { defer netutils.SetupTestNetNS(t)() } netOption := options.Generic{ netlabel.GenericData: options.Generic{ "BridgeName": "testnetwork", "AllowNonDefaultBridge": true, }, } n, err := createTestNetwork(bridgeNetType, "testnetwork", netOption) if err != nil { t.Fatal(err) } defer func() { if err := n.Delete(); err != nil { t.Fatal(err) } }() if n.ID() == "" { t.Fatal("Expected non-empty network id") } }
func TestLinkCreateTwo(t *testing.T) { defer netutils.SetupTestNetNS(t)() d := newDriver() if err := d.Config(nil); err != nil { t.Fatalf("Failed to setup driver config: %v", err) } config := &networkConfiguration{ BridgeName: DefaultBridgeName, EnableIPv6: true} genericOption := make(map[string]interface{}) genericOption[netlabel.GenericData] = config err := d.CreateNetwork("dummy", genericOption) if err != nil { t.Fatalf("Failed to create bridge: %v", err) } te1 := &testEndpoint{ifaces: []*testInterface{}} err = d.CreateEndpoint("dummy", "ep", te1, nil) if err != nil { t.Fatalf("Failed to create a link: %s", err.Error()) } te2 := &testEndpoint{ifaces: []*testInterface{}} err = d.CreateEndpoint("dummy", "ep", te2, nil) if err != nil { if _, ok := err.(driverapi.ErrEndpointExists); !ok { t.Fatalf("Failed with a wrong error: %s", err.Error()) } } else { t.Fatalf("Expected to fail while trying to add same endpoint twice") } }
func TestSetupIPChains(t *testing.T) { // Create a test bridge with a basic bridge configuration (name + IPv4). defer netutils.SetupTestNetNS(t)() driverconfig := &configuration{ EnableIPTables: true, } d := &driver{ config: driverconfig, } assertChainConfig(d, t) config := getBasicTestConfig() br := &bridgeInterface{} createTestBridge(config, br, t) assertBridgeConfig(config, br, d, t) config.EnableIPMasquerade = true assertBridgeConfig(config, br, d, t) config.EnableICC = true assertBridgeConfig(config, br, d, t) config.EnableIPMasquerade = false assertBridgeConfig(config, br, d, t) }
func TestHttpHandlerBadBody(t *testing.T) { defer netutils.SetupTestNetNS(t)() rsp := newWriter() c, err := libnetwork.New() if err != nil { t.Fatal(err) } handleRequest := NewHTTPHandler(c) req, err := http.NewRequest("POST", "/v1.19/networks", &localReader{beBad: true}) if err != nil { t.Fatal(err) } handleRequest(rsp, req) if rsp.statusCode != http.StatusBadRequest { t.Fatalf("Unexpected status code. Expected (%d). Got (%d): %s.", http.StatusBadRequest, rsp.statusCode, string(rsp.body)) } body := []byte{} lr := newLocalReader(body) req, err = http.NewRequest("POST", "/v1.19/networks", lr) if err != nil { t.Fatal(err) } handleRequest(rsp, req) if rsp.statusCode != http.StatusBadRequest { t.Fatalf("Unexpected status code. Expected (%d). Got (%d): %s.", http.StatusBadRequest, rsp.statusCode, string(rsp.body)) } }
func TestSetupBridgeIPv4Fixed(t *testing.T) { defer netutils.SetupTestNetNS(t)() ip, netw, err := net.ParseCIDR("192.168.1.1/24") if err != nil { t.Fatalf("Failed to parse bridge IPv4: %v", err) } config, br := setupTestInterface(t) config.AddressIPv4 = &net.IPNet{IP: ip, Mask: netw.Mask} if err := setupBridgeIPv4(config, br); err != nil { t.Fatalf("Failed to setup bridge IPv4: %v", err) } addrsv4, err := netlink.AddrList(br.Link, netlink.FAMILY_V4) if err != nil { t.Fatalf("Failed to list device IPv4 addresses: %v", err) } var found bool for _, addr := range addrsv4 { if config.AddressIPv4.String() == addr.IPNet.String() { found = true break } } if !found { t.Fatalf("Bridge device does not have requested IPv4 address %v", config.AddressIPv4) } }
func TestLinkDelete(t *testing.T) { defer netutils.SetupTestNetNS(t)() d := newDriver() config := &NetworkConfiguration{ BridgeName: DefaultBridgeName, EnableIPv6: true} genericOption := make(map[string]interface{}) genericOption[netlabel.GenericData] = config err := d.CreateNetwork("dummy", genericOption) if err != nil { t.Fatalf("Failed to create bridge: %v", err) } te := &testEndpoint{ifaces: []*testInterface{}} err = d.CreateEndpoint("dummy", "ep1", te, nil) if err != nil { t.Fatalf("Failed to create a link: %s", err.Error()) } err = d.DeleteEndpoint("dummy", "") if err != nil { if _, ok := err.(InvalidEndpointIDError); !ok { t.Fatalf("Failed with a wrong error :%s", err.Error()) } } else { t.Fatalf("Failed to detect invalid config") } err = d.DeleteEndpoint("dummy", "ep1") if err != nil { t.Fatal(err) } }
func TestSetupIPv6(t *testing.T) { defer netutils.SetupTestNetNS(t)() config, br := setupTestInterface(t) if err := setupBridgeIPv6(config, br); err != nil { t.Fatalf("Failed to setup bridge IPv6: %v", err) } procSetting, err := ioutil.ReadFile(fmt.Sprintf("/proc/sys/net/ipv6/conf/%s/disable_ipv6", config.BridgeName)) if err != nil { t.Fatalf("Failed to read disable_ipv6 kernel setting: %v", err) } if expected := []byte("0\n"); bytes.Compare(expected, procSetting) != 0 { t.Fatalf("Invalid kernel setting disable_ipv6: expected %q, got %q", string(expected), string(procSetting)) } addrsv6, err := netlink.AddrList(br.Link, netlink.FAMILY_V6) if err != nil { t.Fatalf("Failed to list device IPv6 addresses: %v", err) } var found bool for _, addr := range addrsv6 { if bridgeIPv6Str == addr.IPNet.String() { found = true break } } if !found { t.Fatalf("Bridge device does not have requested IPv6 address %v", bridgeIPv6Str) } }
func TestSetupBridgeIPv4Auto(t *testing.T) { defer netutils.SetupTestNetNS(t)() config, br := setupTestInterface(t) if err := setupBridgeIPv4(config, br); err != nil { t.Fatalf("Failed to setup bridge IPv4: %v", err) } addrsv4, err := netlink.AddrList(br.Link, netlink.FAMILY_V4) if err != nil { t.Fatalf("Failed to list device IPv4 addresses: %v", err) } var found bool for _, addr := range addrsv4 { if bridgeNetworks[0].String() == addr.IPNet.String() { found = true break } } if !found { t.Fatalf("Bridge device does not have the automatic IPv4 address %v", bridgeNetworks[0].String()) } }
func TestEndpointJoin(t *testing.T) { if !netutils.IsRunningInContainer() { defer netutils.SetupTestNetNS(t)() } n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{}) if err != nil { t.Fatal(err) } ep, err := n.CreateEndpoint("ep1") if err != nil { t.Fatal(err) } // Validate if ep.Info() only gives me IP address info and not names and gateway during CreateEndpoint() info := ep.Info() for _, iface := range info.InterfaceList() { if iface.Address().IP.To4() == nil { t.Fatalf("Invalid IP address returned: %v", iface.Address()) } } if info.Gateway().To4() != nil { t.Fatalf("Expected empty gateway for an empty endpoint. Instead found a gateway: %v", info.Gateway()) } if info.SandboxKey() != "" { t.Fatalf("Expected an empty sandbox key for an empty endpoint. Instead found a non-empty sandbox key: %s", info.SandboxKey()) } _, err = ep.Join(containerID, libnetwork.JoinOptionHostname("test"), libnetwork.JoinOptionDomainname("docker.io"), libnetwork.JoinOptionExtraHost("web", "192.168.0.1")) if err != nil { t.Fatal(err) } defer func() { err = ep.Leave(containerID) if err != nil { t.Fatal(err) } }() // Validate if ep.Info() only gives valid gateway and sandbox key after has container has joined. info = ep.Info() if info.Gateway().To4() == nil { t.Fatalf("Expected a valid gateway for a joined endpoint. Instead found an invalid gateway: %v", info.Gateway()) } if info.SandboxKey() == "" { t.Fatalf("Expected an non-empty sandbox key for a joined endpoint. Instead found a empty sandbox key") } checkSandbox(t, info) }
func TestInterfaceDefaultName(t *testing.T) { defer netutils.SetupTestNetNS(t)() config := &NetworkConfiguration{} if _ = newInterface(config); config.BridgeName != DefaultBridgeName { t.Fatalf("Expected default interface name %q, got %q", DefaultBridgeName, config.BridgeName) } }
func TestEndpointDeleteWithActiveContainer(t *testing.T) { if !netutils.IsRunningInContainer() { defer netutils.SetupTestNetNS(t)() } n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{ netlabel.GenericData: options.Generic{ "BridgeName": "testnetwork", "AllowNonDefaultBridge": true, }, }) if err != nil { t.Fatal(err) } defer func() { if err := n.Delete(); err != nil { t.Fatal(err) } }() ep, err := n.CreateEndpoint("ep1") if err != nil { t.Fatal(err) } defer func() { err = ep.Delete() if err != nil { t.Fatal(err) } }() defer controller.LeaveAll(containerID) err = ep.Join(containerID, libnetwork.JoinOptionHostname("test"), libnetwork.JoinOptionDomainname("docker.io"), libnetwork.JoinOptionExtraHost("web", "192.168.0.1")) runtime.LockOSThread() if err != nil { t.Fatal(err) } defer func() { err = ep.Leave(containerID) runtime.LockOSThread() if err != nil { t.Fatal(err) } }() err = ep.Delete() if err == nil { t.Fatal("Expected to fail. But instead succeeded") } if _, ok := err.(*libnetwork.ActiveContainerError); !ok { t.Fatalf("Did not fail with expected error. Actual error: %v", err) } }
func TestCreateMultipleNetworks(t *testing.T) { defer netutils.SetupTestNetNS(t)() d := newDriver() dd, _ := d.(*driver) config := &configuration{ EnableIPTables: true, } genericOption := make(map[string]interface{}) genericOption[netlabel.GenericData] = config if err := d.Config(genericOption); err != nil { t.Fatalf("Failed to setup driver config: %v", err) } config1 := &networkConfiguration{BridgeName: "net_test_1", AllowNonDefaultBridge: true} genericOption = make(map[string]interface{}) genericOption[netlabel.GenericData] = config1 if err := d.CreateNetwork("1", genericOption); err != nil { t.Fatalf("Failed to create bridge: %v", err) } config2 := &networkConfiguration{BridgeName: "net_test_2", AllowNonDefaultBridge: true} genericOption[netlabel.GenericData] = config2 if err := d.CreateNetwork("2", genericOption); err != nil { t.Fatalf("Failed to create bridge: %v", err) } config3 := &networkConfiguration{BridgeName: "net_test_3", AllowNonDefaultBridge: true} genericOption[netlabel.GenericData] = config3 if err := d.CreateNetwork("3", genericOption); err != nil { t.Fatalf("Failed to create bridge: %v", err) } // Verify the network isolation rules are installed, each network subnet should appear 4 times verifyV4INCEntries(dd.networks, 4, t) config4 := &networkConfiguration{BridgeName: "net_test_4", AllowNonDefaultBridge: true} genericOption[netlabel.GenericData] = config4 if err := d.CreateNetwork("4", genericOption); err != nil { t.Fatalf("Failed to create bridge: %v", err) } // Now 6 times verifyV4INCEntries(dd.networks, 6, t) d.DeleteNetwork("1") verifyV4INCEntries(dd.networks, 4, t) d.DeleteNetwork("2") verifyV4INCEntries(dd.networks, 2, t) d.DeleteNetwork("3") verifyV4INCEntries(dd.networks, 0, t) d.DeleteNetwork("4") verifyV4INCEntries(dd.networks, 0, t) }
func TestEndpointMultipleJoins(t *testing.T) { if !netutils.IsRunningInContainer() { defer netutils.SetupTestNetNS(t)() } n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{ netlabel.GenericData: options.Generic{ "BridgeName": "testnetwork", "AllowNonDefaultBridge": true, }, }) if err != nil { t.Fatal(err) } defer func() { if err := n.Delete(); err != nil { t.Fatal(err) } }() ep, err := n.CreateEndpoint("ep1") if err != nil { t.Fatal(err) } defer func() { if err := ep.Delete(); err != nil { t.Fatal(err) } }() defer controller.LeaveAll(containerID) err = ep.Join(containerID, libnetwork.JoinOptionHostname("test"), libnetwork.JoinOptionDomainname("docker.io"), libnetwork.JoinOptionExtraHost("web", "192.168.0.1")) runtime.LockOSThread() if err != nil { t.Fatal(err) } defer func() { err = ep.Leave(containerID) runtime.LockOSThread() if err != nil { t.Fatal(err) } }() err = ep.Join("container2") if err == nil { t.Fatal("Expected to fail multiple joins for the same endpoint") } if _, ok := err.(libnetwork.ErrInvalidJoin); !ok { t.Fatalf("Failed for unexpected reason: %v", err) } }
func TestGenerateRandomMAC(t *testing.T) { defer netutils.SetupTestNetNS(t)() mac1 := netutils.GenerateRandomMAC() mac2 := netutils.GenerateRandomMAC() if bytes.Compare(mac1, mac2) == 0 { t.Fatalf("Generated twice the same MAC address %v", mac1) } }
func testQueryEndpointInfo(t *testing.T, ulPxyEnabled bool) { defer netutils.SetupTestNetNS(t)() d := newDriver() dd, _ := d.(*driver) config := &NetworkConfiguration{ BridgeName: DefaultBridgeName, EnableIPTables: true, EnableICC: false, EnableUserlandProxy: ulPxyEnabled, } genericOption := make(map[string]interface{}) genericOption[netlabel.GenericData] = config err := d.CreateNetwork("net1", genericOption) if err != nil { t.Fatalf("Failed to create bridge: %v", err) } portMappings := getPortMapping() epOptions := make(map[string]interface{}) epOptions[netlabel.PortMap] = portMappings te := &testEndpoint{ifaces: []*testInterface{}} err = d.CreateEndpoint("net1", "ep1", te, epOptions) if err != nil { t.Fatalf("Failed to create an endpoint : %s", err.Error()) } ep, _ := dd.network.endpoints["ep1"] data, err := d.EndpointOperInfo(dd.network.id, ep.id) if err != nil { t.Fatalf("Failed to ask for endpoint operational data: %v", err) } pmd, ok := data[netlabel.PortMap] if !ok { t.Fatalf("Endpoint operational data does not contain port mapping data") } pm, ok := pmd.([]netutils.PortBinding) if !ok { t.Fatalf("Unexpected format for port mapping in endpoint operational data") } if len(ep.portMapping) != len(pm) { t.Fatalf("Incomplete data for port mapping in endpoint operational data") } for i, pb := range ep.portMapping { if !pb.Equal(&pm[i]) { t.Fatalf("Unexpected data for port mapping in endpoint operational data") } } // Cleanup as host ports are there err = releasePorts(ep) if err != nil { t.Fatalf("Failed to release mapped ports: %v", err) } }
func TestSandboxCreate(t *testing.T) { defer netutils.SetupTestNetNS(t)() key, err := newKey(t) if err != nil { t.Fatalf("Failed to obtain a key: %v", err) } s, err := NewSandbox(key, true) if err != nil { t.Fatalf("Failed to create a new sandbox: %v", err) } runtime.LockOSThread() if s.Key() != key { t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key) } tbox, err := newInfo(t) if err != nil { t.Fatalf("Failed to generate new sandbox info: %v", err) } for _, i := range tbox.Info().Interfaces() { err = s.AddInterface(i.SrcName(), i.DstName(), tbox.InterfaceOptions().Bridge(i.Bridge()), tbox.InterfaceOptions().Address(i.Address()), tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6())) if err != nil { t.Fatalf("Failed to add interfaces to sandbox: %v", err) } runtime.LockOSThread() } err = s.SetGateway(tbox.Info().Gateway()) if err != nil { t.Fatalf("Failed to set gateway to sandbox: %v", err) } runtime.LockOSThread() err = s.SetGatewayIPv6(tbox.Info().GatewayIPv6()) if err != nil { t.Fatalf("Failed to set ipv6 gateway to sandbox: %v", err) } runtime.LockOSThread() verifySandbox(t, s, []string{"0", "1", "2"}) runtime.LockOSThread() err = s.Destroy() if err != nil { t.Fatal(err) } verifyCleanup(t, s, true) }
func TestDetectGetEndpointsInvalidQueryComposition(t *testing.T) { defer netutils.SetupTestNetNS(t)() c, _ := createTestNetwork(t, "network") vars := map[string]string{urlNwName: "network", urlEpName: "x", urlEpPID: "y"} _, errRsp := procGetEndpoints(c, vars, nil) if errRsp.StatusCode != http.StatusBadRequest { t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp) } }
func TestHttpHandlerUninit(t *testing.T) { defer netutils.SetupTestNetNS(t)() c, err := libnetwork.New() if err != nil { t.Fatal(err) } h := &httpHandler{c: c} h.initRouter() if h.r == nil { t.Fatalf("initRouter() did not initialize the router") } rsp := newWriter() req, err := http.NewRequest("GET", "/networks", nil) if err != nil { t.Fatal(err) } handleRequest := NewHTTPHandler(nil) handleRequest(rsp, req) if rsp.statusCode != http.StatusServiceUnavailable { t.Fatalf("Expected (%d). Got (%d): %s", http.StatusServiceUnavailable, rsp.statusCode, rsp.body) } handleRequest = NewHTTPHandler(c) handleRequest(rsp, req) if rsp.statusCode != http.StatusOK { t.Fatalf("Unexpectded failure: (%d): %s", rsp.statusCode, rsp.body) } n, err := c.NewNetwork(bridgeNetType, "onenet", nil) if err != nil { t.Fatal(err) } nwr := buildNetworkResource(n) expected, err := json.Marshal([]networkResource{*nwr}) if err != nil { t.Fatal(err) } handleRequest(rsp, req) if rsp.statusCode != http.StatusOK { t.Fatalf("Unexpectded failure: (%d): %s", rsp.statusCode, rsp.body) } if len(rsp.body) == 0 { t.Fatalf("Empty list of networks") } if bytes.Equal(rsp.body, expected) { t.Fatalf("Incorrect list of networks in response's body") } }
func TestHttpHandlerGood(t *testing.T) { defer netutils.SetupTestNetNS(t)() rsp := newWriter() c, err := libnetwork.New() if err != nil { t.Fatal(err) } handleRequest := NewHTTPHandler(c) nc := networkCreate{Name: "zero-network", NetworkType: bridgeNetType} body, err := json.Marshal(nc) if err != nil { t.Fatal(err) } lr := newLocalReader(body) req, err := http.NewRequest("POST", "/networks/name/zero-network", lr) if err != nil { t.Fatal(err) } handleRequest(rsp, req) if rsp.statusCode != http.StatusCreated { t.Fatalf("Unexpectded status code. Expected (%d). Got (%d): %s.", http.StatusCreated, rsp.statusCode, string(rsp.body)) } if len(rsp.body) == 0 { t.Fatalf("Empty response body") } var id string err = json.Unmarshal(rsp.body, &id) if err != nil { t.Fatal(err) } req, err = http.NewRequest("GET", "/networks/id/"+id, nil) if err != nil { t.Fatal(err) } handleRequest(rsp, req) if rsp.statusCode != http.StatusOK { t.Fatalf("Unexpectded failure: (%d): %s", rsp.statusCode, rsp.body) } var nwr networkResource err = json.Unmarshal(rsp.body, &nwr) if err != nil { t.Fatal(err) } if nwr.Name != "zero-network" || id != nwr.ID { t.Fatalf("Incongruent resource found") } }