2021-05-25 19:48:54 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-02-24 14:19:00 -05:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/netutils"
|
|
|
|
"github.com/docker/docker/libnetwork/testutils"
|
2015-02-24 14:19:00 -05:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetupNewBridge(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-02-24 14:19:00 -05:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
nh, err := netlink.NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nh.Delete()
|
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
config := &networkConfiguration{BridgeName: DefaultBridgeName}
|
2016-05-16 14:51:40 -04:00
|
|
|
br := &bridgeInterface{nlh: nh}
|
2015-04-15 01:25:42 -04:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2016-05-16 14:51:40 -04:00
|
|
|
if _, err := nh.LinkByName(DefaultBridgeName); err != nil {
|
2015-02-24 14:19:00 -05:00
|
|
|
t.Fatalf("Failed to retrieve bridge device: %v", err)
|
|
|
|
}
|
|
|
|
if br.Link.Attrs().Flags&net.FlagUp == net.FlagUp {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("bridgeInterface should be created down")
|
2015-02-24 14:19:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupNewNonDefaultBridge(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-02-24 14:19:00 -05:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
nh, err := netlink.NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nh.Delete()
|
|
|
|
|
2015-09-25 12:02:18 -04:00
|
|
|
config := &networkConfiguration{BridgeName: "test0", DefaultBridge: true}
|
2016-05-16 14:51:40 -04:00
|
|
|
br := &bridgeInterface{nlh: nh}
|
2015-04-15 01:25:42 -04:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
err = setupDevice(config, br)
|
2015-04-16 22:47:12 -04: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 14:19:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupDeviceUp(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-02-24 14:19:00 -05:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
nh, err := netlink.NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nh.Delete()
|
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
config := &networkConfiguration{BridgeName: DefaultBridgeName}
|
2016-05-16 14:51:40 -04:00
|
|
|
br := &bridgeInterface{nlh: nh}
|
2015-04-15 01:25:42 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
lnk, _ := nh.LinkByName(DefaultBridgeName)
|
2015-02-24 14:19:00 -05:00
|
|
|
if lnk.Attrs().Flags&net.FlagUp != net.FlagUp {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("bridgeInterface should be up")
|
2015-02-24 14:19:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateRandomMAC(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(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)
|
|
|
|
}
|
|
|
|
}
|