2015-04-10 12:02:25 -04:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/libnetwork/netutils"
|
2015-04-30 20:57:06 -04:00
|
|
|
"github.com/docker/libnetwork/pkg/options"
|
2015-04-10 12:02:25 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLinkCreate(t *testing.T) {
|
2015-04-13 14:40:42 -04:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
_, d := New()
|
|
|
|
dr := d.(*driver)
|
2015-04-10 12:02:25 -04:00
|
|
|
|
2015-04-23 16:39:41 -04:00
|
|
|
mtu := 1490
|
2015-04-10 12:02:25 -04:00
|
|
|
config := &Configuration{
|
|
|
|
BridgeName: DefaultBridgeName,
|
2015-04-23 16:39:41 -04:00
|
|
|
Mtu: mtu,
|
2015-04-24 18:13:44 -04:00
|
|
|
EnableIPv6: true,
|
|
|
|
}
|
2015-04-30 20:57:06 -04:00
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[options.GenericData] = config
|
|
|
|
if err := d.Config(genericOption); err != nil {
|
2015-04-15 01:25:42 -04:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
err := d.CreateNetwork("dummy", nil)
|
2015-04-10 12:02:25 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-28 01:57:36 -04:00
|
|
|
sinfo, err := d.CreateEndpoint("dummy", "", nil)
|
2015-04-10 12:02:25 -04:00
|
|
|
if err != nil {
|
2015-04-20 07:23:04 -04:00
|
|
|
if _, ok := err.(InvalidEndpointIDError); !ok {
|
|
|
|
t.Fatalf("Failed with a wrong error :%s", err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Failed to detect invalid config")
|
|
|
|
}
|
|
|
|
|
2015-04-23 16:39:41 -04:00
|
|
|
// Good endpoint creation
|
2015-04-28 01:57:36 -04:00
|
|
|
sinfo, err = d.CreateEndpoint("dummy", "ep", nil)
|
2015-04-20 07:23:04 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a link: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2015-04-23 16:39:41 -04:00
|
|
|
// Verify sbox endoint interface inherited MTU value from bridge config
|
|
|
|
sboxLnk, err := netlink.LinkByName(sinfo.Interfaces[0].SrcName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if mtu != sboxLnk.Attrs().MTU {
|
|
|
|
t.Fatalf("Sandbox endpoint interface did not inherit bridge interface MTU config")
|
|
|
|
}
|
|
|
|
// TODO: if we could get peer name from (sboxLnk.(*netlink.Veth)).PeerName
|
|
|
|
// then we could check the MTU on hostLnk as well.
|
|
|
|
|
2015-04-28 01:57:36 -04:00
|
|
|
_, err = d.CreateEndpoint("dummy", "ep", nil)
|
2015-04-20 07:23:04 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Failed to detect duplicate endpoint id on same network")
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
interfaces := sinfo.Interfaces
|
2015-04-10 12:02:25 -04:00
|
|
|
if len(interfaces) != 1 {
|
|
|
|
t.Fatalf("Expected exactly one interface. Instead got %d interface(s)", len(interfaces))
|
|
|
|
}
|
|
|
|
|
|
|
|
if interfaces[0].DstName == "" {
|
|
|
|
t.Fatal("Invalid Dstname returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = netlink.LinkByName(interfaces[0].SrcName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not find source link %s: %v", interfaces[0].SrcName, err)
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
n := dr.network
|
2015-04-13 21:36:58 -04:00
|
|
|
ip := interfaces[0].Address.IP
|
2015-04-13 14:40:42 -04:00
|
|
|
if !n.bridge.bridgeIPv4.Contains(ip) {
|
|
|
|
t.Fatalf("IP %s is not a valid ip in the subnet %s", ip.String(), n.bridge.bridgeIPv4.String())
|
2015-04-10 12:02:25 -04:00
|
|
|
}
|
|
|
|
|
2015-04-13 21:36:58 -04:00
|
|
|
ip6 := interfaces[0].AddressIPv6.IP
|
2015-04-13 14:40:42 -04:00
|
|
|
if !n.bridge.bridgeIPv6.Contains(ip6) {
|
2015-04-10 12:02:25 -04:00
|
|
|
t.Fatalf("IP %s is not a valid ip in the subnet %s", ip6.String(), bridgeIPv6.String())
|
|
|
|
}
|
|
|
|
|
2015-04-24 18:13:44 -04:00
|
|
|
if !sinfo.Gateway.Equal(n.bridge.bridgeIPv4.IP) {
|
2015-04-13 14:40:42 -04:00
|
|
|
t.Fatalf("Invalid default gateway. Expected %s. Got %s", n.bridge.bridgeIPv4.IP.String(),
|
2015-04-13 21:36:58 -04:00
|
|
|
sinfo.Gateway.String())
|
2015-04-10 12:02:25 -04:00
|
|
|
}
|
|
|
|
|
2015-04-24 18:13:44 -04:00
|
|
|
if !sinfo.GatewayIPv6.Equal(n.bridge.bridgeIPv6.IP) {
|
2015-04-13 14:40:42 -04:00
|
|
|
t.Fatalf("Invalid default gateway for IPv6. Expected %s. Got %s", n.bridge.bridgeIPv6.IP.String(),
|
2015-04-13 21:36:58 -04:00
|
|
|
sinfo.GatewayIPv6.String())
|
2015-04-10 12:02:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkCreateTwo(t *testing.T) {
|
2015-04-13 14:40:42 -04:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
_, d := New()
|
2015-04-10 12:02:25 -04:00
|
|
|
|
|
|
|
config := &Configuration{
|
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
EnableIPv6: true}
|
2015-04-30 20:57:06 -04:00
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[options.GenericData] = config
|
|
|
|
if err := d.Config(genericOption); err != nil {
|
2015-04-15 01:25:42 -04:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
err := d.CreateNetwork("dummy", nil)
|
2015-04-10 12:02:25 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-28 01:57:36 -04:00
|
|
|
_, err = d.CreateEndpoint("dummy", "ep", nil)
|
2015-04-10 12:02:25 -04:00
|
|
|
if err != nil {
|
2015-04-20 07:23:04 -04:00
|
|
|
t.Fatalf("Failed to create a link: %s", err.Error())
|
2015-04-10 12:02:25 -04:00
|
|
|
}
|
|
|
|
|
2015-04-28 01:57:36 -04:00
|
|
|
_, err = d.CreateEndpoint("dummy", "ep", nil)
|
2015-04-10 12:02:25 -04:00
|
|
|
if err != nil {
|
2015-04-13 14:40:42 -04:00
|
|
|
if err != driverapi.ErrEndpointExists {
|
2015-04-20 07:23:04 -04:00
|
|
|
t.Fatalf("Failed with a wrong error :%s", err.Error())
|
2015-04-10 12:02:25 -04:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-20 07:23:04 -04:00
|
|
|
t.Fatalf("Expected to fail while trying to add same endpoint twice")
|
2015-04-10 12:02:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkCreateNoEnableIPv6(t *testing.T) {
|
2015-04-13 14:40:42 -04:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
_, d := New()
|
2015-04-10 12:02:25 -04:00
|
|
|
|
|
|
|
config := &Configuration{
|
|
|
|
BridgeName: DefaultBridgeName}
|
2015-04-30 20:57:06 -04:00
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[options.GenericData] = config
|
|
|
|
|
|
|
|
if err := d.Config(genericOption); err != nil {
|
2015-04-15 01:25:42 -04:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
err := d.CreateNetwork("dummy", nil)
|
2015-04-10 12:02:25 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-28 01:57:36 -04:00
|
|
|
sinfo, err := d.CreateEndpoint("dummy", "ep", nil)
|
2015-04-10 12:02:25 -04:00
|
|
|
if err != nil {
|
2015-04-20 07:23:04 -04:00
|
|
|
t.Fatalf("Failed to create a link: %s", err.Error())
|
2015-04-10 12:02:25 -04:00
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
interfaces := sinfo.Interfaces
|
2015-04-17 18:42:23 -04:00
|
|
|
if interfaces[0].AddressIPv6 != nil {
|
2015-04-13 21:36:58 -04:00
|
|
|
t.Fatalf("Expectd IPv6 address to be nil when IPv6 is not enabled. Got IPv6 = %s", interfaces[0].AddressIPv6.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if sinfo.GatewayIPv6 != nil {
|
|
|
|
t.Fatalf("Expected GatewayIPv6 to be nil when IPv6 is not enabled. Got GatewayIPv6 = %s", sinfo.GatewayIPv6.String())
|
2015-04-10 12:02:25 -04:00
|
|
|
}
|
|
|
|
}
|
2015-04-20 07:23:04 -04:00
|
|
|
|
|
|
|
func TestLinkDelete(t *testing.T) {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
_, d := New()
|
|
|
|
|
|
|
|
config := &Configuration{
|
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
EnableIPv6: true}
|
2015-04-30 20:57:06 -04:00
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[options.GenericData] = config
|
|
|
|
|
|
|
|
if err := d.Config(genericOption); err != nil {
|
2015-04-20 07:23:04 -04:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
err := d.CreateNetwork("dummy", nil)
|
2015-04-20 07:23:04 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-28 01:57:36 -04:00
|
|
|
_, err = d.CreateEndpoint("dummy", "ep1", nil)
|
2015-04-20 07:23:04 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.DeleteEndpoint("dummy", "ep1")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|