mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
68d223a0e0
- Move ipallocator package into libnetwork - Also ported network utility functions and their tests in libnetwork: docker/daemon/networkdriver/utilg.go => libnetwork/utils.go docker/daemon/networkdriver/network_test.go => libnetwork/utils_test.go - Changed drivers/setup_device.go and setup_ipv4.go to reuse functions in utils.go, instead of redefining internally. - Modified utils to use vishvananda/netlink instead of libcontainer/netlink Signed-off-by: Alessandro Boch <aboch@socketplane.io>
77 lines
1.8 KiB
Go
77 lines
1.8 KiB
Go
package bridge
|
|
|
|
import (
|
|
"bytes"
|
|
"net"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/docker/libnetwork"
|
|
"github.com/vishvananda/netlink"
|
|
)
|
|
|
|
func TestSetupNewBridge(t *testing.T) {
|
|
defer libnetwork.SetupTestNetNS(t)()
|
|
|
|
br := &bridgeInterface{
|
|
Config: &Configuration{
|
|
BridgeName: DefaultBridgeName,
|
|
},
|
|
}
|
|
if err := setupDevice(br); err != nil {
|
|
t.Fatalf("Bridge creation failed: %v", err)
|
|
}
|
|
if br.Link == nil {
|
|
t.Fatal("bridgeInterface link is nil (expected valid link)")
|
|
}
|
|
if _, err := netlink.LinkByName(DefaultBridgeName); err != nil {
|
|
t.Fatalf("Failed to retrieve bridge device: %v", err)
|
|
}
|
|
if br.Link.Attrs().Flags&net.FlagUp == net.FlagUp {
|
|
t.Fatalf("bridgeInterface should be created down")
|
|
}
|
|
}
|
|
|
|
func TestSetupNewNonDefaultBridge(t *testing.T) {
|
|
defer libnetwork.SetupTestNetNS(t)()
|
|
|
|
br := &bridgeInterface{
|
|
Config: &Configuration{
|
|
BridgeName: "test0",
|
|
},
|
|
}
|
|
if err := setupDevice(br); err == nil || !strings.Contains(err.Error(), "non default name") {
|
|
t.Fatalf("Expected bridge creation failure with \"non default name\", got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSetupDeviceUp(t *testing.T) {
|
|
defer libnetwork.SetupTestNetNS(t)()
|
|
|
|
br := &bridgeInterface{
|
|
Config: &Configuration{
|
|
BridgeName: DefaultBridgeName,
|
|
},
|
|
}
|
|
if err := setupDevice(br); err != nil {
|
|
t.Fatalf("Bridge creation failed: %v", err)
|
|
}
|
|
if err := setupDeviceUp(br); err != nil {
|
|
t.Fatalf("Failed to up bridge device: %v", err)
|
|
}
|
|
|
|
lnk, _ := netlink.LinkByName(DefaultBridgeName)
|
|
if lnk.Attrs().Flags&net.FlagUp != net.FlagUp {
|
|
t.Fatalf("bridgeInterface should be up")
|
|
}
|
|
}
|
|
|
|
func TestGenerateRandomMAC(t *testing.T) {
|
|
defer libnetwork.SetupTestNetNS(t)()
|
|
|
|
mac1 := libnetwork.GenerateRandomMAC()
|
|
mac2 := libnetwork.GenerateRandomMAC()
|
|
if bytes.Compare(mac1, mac2) == 0 {
|
|
t.Fatalf("Generated twice the same MAC address %v", mac1)
|
|
}
|
|
}
|