2015-02-24 14:19:00 -05:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
"github.com/docker/libnetwork/netutils"
|
2015-02-24 14:19:00 -05:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetupNewBridge(t *testing.T) {
|
2015-04-13 14:40:42 -04:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
2015-02-24 14:19:00 -05:00
|
|
|
|
2015-05-06 01:51:26 -04:00
|
|
|
config := &NetworkConfiguration{BridgeName: DefaultBridgeName}
|
2015-04-15 01:25:42 -04:00
|
|
|
br := &bridgeInterface{}
|
|
|
|
|
|
|
|
if err := setupDevice(config, br); err != nil {
|
2015-02-24 14:19:00 -05:00
|
|
|
t.Fatalf("Bridge creation failed: %v", err)
|
|
|
|
}
|
|
|
|
if br.Link == nil {
|
2015-03-04 16:25:43 -05:00
|
|
|
t.Fatal("bridgeInterface link is nil (expected valid link)")
|
2015-02-24 14:19:00 -05:00
|
|
|
}
|
|
|
|
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 {
|
2015-03-04 16:25:43 -05:00
|
|
|
t.Fatalf("bridgeInterface should be created down")
|
2015-02-24 14:19:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupNewNonDefaultBridge(t *testing.T) {
|
2015-04-13 14:40:42 -04:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
2015-02-24 14:19:00 -05:00
|
|
|
|
2015-05-06 01:51:26 -04:00
|
|
|
config := &NetworkConfiguration{BridgeName: "test0"}
|
2015-04-15 01:25:42 -04:00
|
|
|
br := &bridgeInterface{}
|
|
|
|
|
2015-04-16 22:47:12 -04:00
|
|
|
err := setupDevice(config, br)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected bridge creation failure with \"non default name\", succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := err.(NonDefaultBridgeExistError); !ok {
|
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
2015-02-24 14:19:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupDeviceUp(t *testing.T) {
|
2015-04-13 14:40:42 -04:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
2015-02-24 14:19:00 -05:00
|
|
|
|
2015-05-06 01:51:26 -04:00
|
|
|
config := &NetworkConfiguration{BridgeName: DefaultBridgeName}
|
2015-04-15 01:25:42 -04:00
|
|
|
br := &bridgeInterface{}
|
|
|
|
|
|
|
|
if err := setupDevice(config, br); err != nil {
|
2015-02-24 14:19:00 -05:00
|
|
|
t.Fatalf("Bridge creation failed: %v", err)
|
|
|
|
}
|
2015-04-15 01:25:42 -04:00
|
|
|
if err := setupDeviceUp(config, br); err != nil {
|
2015-02-24 14:19:00 -05:00
|
|
|
t.Fatalf("Failed to up bridge device: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lnk, _ := netlink.LinkByName(DefaultBridgeName)
|
|
|
|
if lnk.Attrs().Flags&net.FlagUp != net.FlagUp {
|
2015-03-04 16:25:43 -05:00
|
|
|
t.Fatalf("bridgeInterface should be up")
|
2015-02-24 14:19:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateRandomMAC(t *testing.T) {
|
2015-04-13 14:40:42 -04:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
2015-02-24 14:19:00 -05:00
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
mac1 := netutils.GenerateRandomMAC()
|
|
|
|
mac2 := netutils.GenerateRandomMAC()
|
2015-02-24 14:19:00 -05:00
|
|
|
if bytes.Compare(mac1, mac2) == 0 {
|
|
|
|
t.Fatalf("Generated twice the same MAC address %v", mac1)
|
|
|
|
}
|
|
|
|
}
|