2015-04-10 16:02:25 +00:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/libnetwork/netutils"
|
2015-04-10 16:02:25 +00:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLinkCreate(t *testing.T) {
|
2015-04-13 18:40:42 +00:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
_, d := New()
|
|
|
|
dr := d.(*driver)
|
2015-04-10 16:02:25 +00:00
|
|
|
|
|
|
|
config := &Configuration{
|
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
EnableIPv6: true}
|
2015-04-15 05:25:42 +00:00
|
|
|
if err := d.Config(config); err != nil {
|
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.CreateNetwork("dummy", "")
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-20 11:23:04 +00:00
|
|
|
sinfo, err := d.CreateEndpoint("dummy", "", "sb1", "")
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
2015-04-20 11:23:04 +00:00
|
|
|
if _, ok := err.(InvalidEndpointIDError); !ok {
|
|
|
|
t.Fatalf("Failed with a wrong error :%s", err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Failed to detect invalid config")
|
|
|
|
}
|
|
|
|
|
|
|
|
sinfo, err = d.CreateEndpoint("dummy", "ep", "", "")
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(InvalidSandboxIDError); !ok {
|
|
|
|
t.Fatalf("Failed with a wrong error :%s", err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Failed to detect invalid config")
|
|
|
|
}
|
|
|
|
|
|
|
|
sinfo, err = d.CreateEndpoint("dummy", "ep", "cc", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a link: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.CreateEndpoint("dummy", "ep", "cc2", "")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Failed to detect duplicate endpoint id on same network")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.CreateEndpoint("dummy", "ep2", "cc", "")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Failed to detect addition of more than one endpoint to same sandbox")
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
interfaces := sinfo.Interfaces
|
2015-04-10 16:02:25 +00: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 18:40:42 +00:00
|
|
|
n := dr.network
|
2015-04-14 01:36:58 +00:00
|
|
|
ip := interfaces[0].Address.IP
|
2015-04-13 18:40:42 +00: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 16:02:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 01:36:58 +00:00
|
|
|
ip6 := interfaces[0].AddressIPv6.IP
|
2015-04-13 18:40:42 +00:00
|
|
|
if !n.bridge.bridgeIPv6.Contains(ip6) {
|
2015-04-10 16:02:25 +00:00
|
|
|
t.Fatalf("IP %s is not a valid ip in the subnet %s", ip6.String(), bridgeIPv6.String())
|
|
|
|
}
|
|
|
|
|
2015-04-14 01:36:58 +00:00
|
|
|
if sinfo.Gateway.String() != n.bridge.bridgeIPv4.IP.String() {
|
2015-04-13 18:40:42 +00:00
|
|
|
t.Fatalf("Invalid default gateway. Expected %s. Got %s", n.bridge.bridgeIPv4.IP.String(),
|
2015-04-14 01:36:58 +00:00
|
|
|
sinfo.Gateway.String())
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 01:36:58 +00:00
|
|
|
if sinfo.GatewayIPv6.String() != n.bridge.bridgeIPv6.IP.String() {
|
2015-04-13 18:40:42 +00:00
|
|
|
t.Fatalf("Invalid default gateway for IPv6. Expected %s. Got %s", n.bridge.bridgeIPv6.IP.String(),
|
2015-04-14 01:36:58 +00:00
|
|
|
sinfo.GatewayIPv6.String())
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkCreateTwo(t *testing.T) {
|
2015-04-13 18:40:42 +00:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
_, d := New()
|
2015-04-10 16:02:25 +00:00
|
|
|
|
|
|
|
config := &Configuration{
|
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
EnableIPv6: true}
|
2015-04-15 05:25:42 +00:00
|
|
|
if err := d.Config(config); err != nil {
|
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.CreateNetwork("dummy", "")
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-20 11:23:04 +00:00
|
|
|
_, err = d.CreateEndpoint("dummy", "ep", "s1", "")
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
2015-04-20 11:23:04 +00:00
|
|
|
t.Fatalf("Failed to create a link: %s", err.Error())
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 11:23:04 +00:00
|
|
|
_, err = d.CreateEndpoint("dummy", "ep", "s1", "")
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
2015-04-13 18:40:42 +00:00
|
|
|
if err != driverapi.ErrEndpointExists {
|
2015-04-20 11:23:04 +00:00
|
|
|
t.Fatalf("Failed with a wrong error :%s", err.Error())
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-20 11:23:04 +00:00
|
|
|
t.Fatalf("Expected to fail while trying to add same endpoint twice")
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkCreateNoEnableIPv6(t *testing.T) {
|
2015-04-13 18:40:42 +00:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
_, d := New()
|
2015-04-10 16:02:25 +00:00
|
|
|
|
|
|
|
config := &Configuration{
|
|
|
|
BridgeName: DefaultBridgeName}
|
2015-04-15 05:25:42 +00:00
|
|
|
if err := d.Config(config); err != nil {
|
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.CreateNetwork("dummy", "")
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-20 11:23:04 +00:00
|
|
|
sinfo, err := d.CreateEndpoint("dummy", "ep", "sb2", "")
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
2015-04-20 11:23:04 +00:00
|
|
|
t.Fatalf("Failed to create a link: %s", err.Error())
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
interfaces := sinfo.Interfaces
|
2015-04-17 22:42:23 +00:00
|
|
|
if interfaces[0].AddressIPv6 != nil {
|
2015-04-14 01:36:58 +00: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 16:02:25 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-20 11:23:04 +00:00
|
|
|
|
|
|
|
func TestLinkDelete(t *testing.T) {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
_, d := New()
|
|
|
|
|
|
|
|
config := &Configuration{
|
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
EnableIPv6: true}
|
|
|
|
if err := d.Config(config); err != nil {
|
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.CreateNetwork("dummy", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.CreateEndpoint("dummy", "ep1", "s1", "")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|