2015-02-24 19:19:00 +00:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
"github.com/docker/libnetwork/netutils"
|
2015-09-07 17:33:28 +00:00
|
|
|
"github.com/docker/libnetwork/testutils"
|
2015-02-24 19:19:00 +00:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetupNewBridge(t *testing.T) {
|
2015-09-07 17:33:28 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-02-24 19:19:00 +00:00
|
|
|
|
2016-05-16 18:51:40 +00:00
|
|
|
nh, err := netlink.NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nh.Delete()
|
|
|
|
|
2015-05-22 17:56:36 +00:00
|
|
|
config := &networkConfiguration{BridgeName: DefaultBridgeName}
|
2016-05-16 18:51:40 +00:00
|
|
|
br := &bridgeInterface{nlh: nh}
|
2015-04-15 05:25:42 +00:00
|
|
|
|
|
|
|
if err := setupDevice(config, br); err != nil {
|
2015-02-24 19:19:00 +00:00
|
|
|
t.Fatalf("Bridge creation failed: %v", err)
|
|
|
|
}
|
|
|
|
if br.Link == nil {
|
2015-03-04 21:25:43 +00:00
|
|
|
t.Fatal("bridgeInterface link is nil (expected valid link)")
|
2015-02-24 19:19:00 +00:00
|
|
|
}
|
2016-05-16 18:51:40 +00:00
|
|
|
if _, err := nh.LinkByName(DefaultBridgeName); err != nil {
|
2015-02-24 19:19:00 +00:00
|
|
|
t.Fatalf("Failed to retrieve bridge device: %v", err)
|
|
|
|
}
|
|
|
|
if br.Link.Attrs().Flags&net.FlagUp == net.FlagUp {
|
2016-11-22 01:29:53 +00:00
|
|
|
t.Fatal("bridgeInterface should be created down")
|
2015-02-24 19:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupNewNonDefaultBridge(t *testing.T) {
|
2015-09-07 17:33:28 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-02-24 19:19:00 +00:00
|
|
|
|
2016-05-16 18:51:40 +00:00
|
|
|
nh, err := netlink.NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nh.Delete()
|
|
|
|
|
2015-09-25 16:02:18 +00:00
|
|
|
config := &networkConfiguration{BridgeName: "test0", DefaultBridge: true}
|
2016-05-16 18:51:40 +00:00
|
|
|
br := &bridgeInterface{nlh: nh}
|
2015-04-15 05:25:42 +00:00
|
|
|
|
2016-05-16 18:51:40 +00:00
|
|
|
err = setupDevice(config, br)
|
2015-04-17 02:47:12 +00:00
|
|
|
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 19:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupDeviceUp(t *testing.T) {
|
2015-09-07 17:33:28 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-02-24 19:19:00 +00:00
|
|
|
|
2016-05-16 18:51:40 +00:00
|
|
|
nh, err := netlink.NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nh.Delete()
|
|
|
|
|
2015-05-22 17:56:36 +00:00
|
|
|
config := &networkConfiguration{BridgeName: DefaultBridgeName}
|
2016-05-16 18:51:40 +00:00
|
|
|
br := &bridgeInterface{nlh: nh}
|
2015-04-15 05:25:42 +00:00
|
|
|
|
|
|
|
if err := setupDevice(config, br); err != nil {
|
2015-02-24 19:19:00 +00:00
|
|
|
t.Fatalf("Bridge creation failed: %v", err)
|
|
|
|
}
|
2015-04-15 05:25:42 +00:00
|
|
|
if err := setupDeviceUp(config, br); err != nil {
|
2015-02-24 19:19:00 +00:00
|
|
|
t.Fatalf("Failed to up bridge device: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-05-16 18:51:40 +00:00
|
|
|
lnk, _ := nh.LinkByName(DefaultBridgeName)
|
2015-02-24 19:19:00 +00:00
|
|
|
if lnk.Attrs().Flags&net.FlagUp != net.FlagUp {
|
2016-11-22 01:29:53 +00:00
|
|
|
t.Fatal("bridgeInterface should be up")
|
2015-02-24 19:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateRandomMAC(t *testing.T) {
|
2015-09-07 17:33:28 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-02-24 19:19:00 +00:00
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
mac1 := netutils.GenerateRandomMAC()
|
|
|
|
mac2 := netutils.GenerateRandomMAC()
|
2015-02-24 19:19:00 +00:00
|
|
|
if bytes.Compare(mac1, mac2) == 0 {
|
|
|
|
t.Fatalf("Generated twice the same MAC address %v", mac1)
|
|
|
|
}
|
|
|
|
}
|