2015-04-10 12:02:25 -04:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/libnetwork/netutils"
|
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
|
|
|
|
|
|
|
config := &Configuration{
|
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
EnableIPv6: true}
|
2015-04-13 14:40:42 -04:00
|
|
|
err := d.CreateNetwork("dummy", config)
|
2015-04-10 12:02:25 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
sinfo, err := d.CreateEndpoint("dummy", "ep", "", "")
|
2015-04-10 12:02:25 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a link: %v", err)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, _, err := net.ParseCIDR(interfaces[0].Address)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Invalid IPv4 address returned, ip = %s: %v", interfaces[0].Address, err)
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
n := dr.network
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
ip6, _, err := net.ParseCIDR(interfaces[0].AddressIPv6)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Invalid IPv6 address returned, ip = %s: %v", interfaces[0].AddressIPv6, err)
|
|
|
|
}
|
|
|
|
|
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-13 14:40:42 -04:00
|
|
|
if sinfo.Gateway != n.bridge.bridgeIPv4.IP.String() {
|
|
|
|
t.Fatalf("Invalid default gateway. Expected %s. Got %s", n.bridge.bridgeIPv4.IP.String(),
|
|
|
|
sinfo.Gateway)
|
2015-04-10 12:02:25 -04:00
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
if sinfo.GatewayIPv6 != n.bridge.bridgeIPv6.IP.String() {
|
|
|
|
t.Fatalf("Invalid default gateway for IPv6. Expected %s. Got %s", n.bridge.bridgeIPv6.IP.String(),
|
|
|
|
sinfo.GatewayIPv6)
|
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-13 14:40:42 -04:00
|
|
|
err := d.CreateNetwork("dummy", config)
|
2015-04-10 12:02:25 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
_, err = d.CreateEndpoint("dummy", "ep", "", "")
|
2015-04-10 12:02:25 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a link: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
_, err = d.CreateEndpoint("dummy", "ep1", "", "")
|
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-10 12:02:25 -04:00
|
|
|
t.Fatalf("Failed with a wrong error :%v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Expected to fail while trying to add more than one endpoint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-13 14:40:42 -04:00
|
|
|
err := d.CreateNetwork("dummy", config)
|
2015-04-10 12:02:25 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
sinfo, err := d.CreateEndpoint("dummy", "ep", "", "")
|
2015-04-10 12:02:25 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a link: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
interfaces := sinfo.Interfaces
|
2015-04-10 12:02:25 -04:00
|
|
|
if interfaces[0].AddressIPv6 != "" ||
|
2015-04-13 14:40:42 -04:00
|
|
|
sinfo.GatewayIPv6 != "" {
|
2015-04-10 12:02:25 -04:00
|
|
|
t.Fatalf("Expected IPv6 address and GatewayIPv6 to be empty when IPv6 enabled. Instead got IPv6 = %s and GatewayIPv6 = %s",
|
2015-04-13 14:40:42 -04:00
|
|
|
interfaces[0].AddressIPv6, sinfo.GatewayIPv6)
|
2015-04-10 12:02:25 -04:00
|
|
|
}
|
|
|
|
}
|