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-05-06 04:19:57 +00:00
|
|
|
"github.com/docker/libnetwork/pkg/netlabel"
|
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)()
|
Make driver packages register themselves via DriverCallback
In the present code, each driver package provides a `New()` method
which constructs a driver of its type, which is then registered with
the controller.
However, this is not suitable for the `drivers/remote` package, since
it does not provide a (singleton) driver, but a mechanism for drivers
to be added dynamically. As a result, the implementation is oddly
dual-purpose, and a spurious `"remote"` driver is added to the
controller's list of available drivers.
Instead, it is better to provide the registration callback to each
package and let it register its own driver or drivers. That way, the
singleton driver packages can construct one and register it, and the
remote package can hook the callback up with whatever the dynamic
driver mechanism turns out to be.
NB there are some method signature changes; in particular to
controller.New, which can return an error if the built-in driver
packages fail to initialise.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-11 13:46:29 +01:00
|
|
|
d := newDriver()
|
2015-04-13 18:40:42 +00:00
|
|
|
dr := d.(*driver)
|
2015-04-10 16:02:25 +00:00
|
|
|
|
2015-04-23 13:39:41 -07:00
|
|
|
mtu := 1490
|
2015-05-06 05:51:26 +00:00
|
|
|
config := &NetworkConfiguration{
|
2015-04-10 16:02:25 +00:00
|
|
|
BridgeName: DefaultBridgeName,
|
2015-04-23 13:39:41 -07:00
|
|
|
Mtu: mtu,
|
2015-04-24 15:13:44 -07:00
|
|
|
EnableIPv6: true,
|
|
|
|
}
|
2015-04-30 17:57:06 -07:00
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 04:19:57 +00:00
|
|
|
genericOption[netlabel.GenericData] = config
|
2015-04-15 05:25:42 +00:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
err := d.CreateNetwork("dummy", genericOption)
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-28 05:57:36 +00:00
|
|
|
sinfo, err := d.CreateEndpoint("dummy", "", nil)
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
2015-04-20 04:23:04 -07: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 13:39:41 -07:00
|
|
|
// Good endpoint creation
|
2015-04-28 05:57:36 +00:00
|
|
|
sinfo, err = d.CreateEndpoint("dummy", "ep", nil)
|
2015-04-20 04:23:04 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a link: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2015-04-23 13:39:41 -07: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 05:57:36 +00:00
|
|
|
_, err = d.CreateEndpoint("dummy", "ep", nil)
|
2015-04-20 04:23:04 -07:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Failed to detect duplicate endpoint id on same network")
|
|
|
|
}
|
|
|
|
|
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-24 15:13:44 -07:00
|
|
|
if !sinfo.Gateway.Equal(n.bridge.bridgeIPv4.IP) {
|
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-24 15:13:44 -07:00
|
|
|
if !sinfo.GatewayIPv6.Equal(n.bridge.bridgeIPv6.IP) {
|
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)()
|
Make driver packages register themselves via DriverCallback
In the present code, each driver package provides a `New()` method
which constructs a driver of its type, which is then registered with
the controller.
However, this is not suitable for the `drivers/remote` package, since
it does not provide a (singleton) driver, but a mechanism for drivers
to be added dynamically. As a result, the implementation is oddly
dual-purpose, and a spurious `"remote"` driver is added to the
controller's list of available drivers.
Instead, it is better to provide the registration callback to each
package and let it register its own driver or drivers. That way, the
singleton driver packages can construct one and register it, and the
remote package can hook the callback up with whatever the dynamic
driver mechanism turns out to be.
NB there are some method signature changes; in particular to
controller.New, which can return an error if the built-in driver
packages fail to initialise.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-11 13:46:29 +01:00
|
|
|
d := newDriver()
|
2015-04-10 16:02:25 +00:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
config := &NetworkConfiguration{
|
2015-04-10 16:02:25 +00:00
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
EnableIPv6: true}
|
2015-04-30 17:57:06 -07:00
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 04:19:57 +00:00
|
|
|
genericOption[netlabel.GenericData] = config
|
2015-04-15 05:25:42 +00:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
err := d.CreateNetwork("dummy", genericOption)
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-28 05:57:36 +00:00
|
|
|
_, err = d.CreateEndpoint("dummy", "ep", nil)
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
2015-04-20 04:23:04 -07:00
|
|
|
t.Fatalf("Failed to create a link: %s", err.Error())
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 05:57:36 +00:00
|
|
|
_, err = d.CreateEndpoint("dummy", "ep", nil)
|
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 04:23:04 -07:00
|
|
|
t.Fatalf("Failed with a wrong error :%s", err.Error())
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-20 04:23:04 -07: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)()
|
Make driver packages register themselves via DriverCallback
In the present code, each driver package provides a `New()` method
which constructs a driver of its type, which is then registered with
the controller.
However, this is not suitable for the `drivers/remote` package, since
it does not provide a (singleton) driver, but a mechanism for drivers
to be added dynamically. As a result, the implementation is oddly
dual-purpose, and a spurious `"remote"` driver is added to the
controller's list of available drivers.
Instead, it is better to provide the registration callback to each
package and let it register its own driver or drivers. That way, the
singleton driver packages can construct one and register it, and the
remote package can hook the callback up with whatever the dynamic
driver mechanism turns out to be.
NB there are some method signature changes; in particular to
controller.New, which can return an error if the built-in driver
packages fail to initialise.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-11 13:46:29 +01:00
|
|
|
d := newDriver()
|
2015-04-10 16:02:25 +00:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
config := &NetworkConfiguration{
|
2015-04-10 16:02:25 +00:00
|
|
|
BridgeName: DefaultBridgeName}
|
2015-04-30 17:57:06 -07:00
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 04:19:57 +00:00
|
|
|
genericOption[netlabel.GenericData] = config
|
2015-04-30 17:57:06 -07:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
err := d.CreateNetwork("dummy", genericOption)
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-28 05:57:36 +00:00
|
|
|
sinfo, err := d.CreateEndpoint("dummy", "ep", nil)
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
2015-04-20 04:23:04 -07: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 15:42:23 -07: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 04:23:04 -07:00
|
|
|
|
|
|
|
func TestLinkDelete(t *testing.T) {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
Make driver packages register themselves via DriverCallback
In the present code, each driver package provides a `New()` method
which constructs a driver of its type, which is then registered with
the controller.
However, this is not suitable for the `drivers/remote` package, since
it does not provide a (singleton) driver, but a mechanism for drivers
to be added dynamically. As a result, the implementation is oddly
dual-purpose, and a spurious `"remote"` driver is added to the
controller's list of available drivers.
Instead, it is better to provide the registration callback to each
package and let it register its own driver or drivers. That way, the
singleton driver packages can construct one and register it, and the
remote package can hook the callback up with whatever the dynamic
driver mechanism turns out to be.
NB there are some method signature changes; in particular to
controller.New, which can return an error if the built-in driver
packages fail to initialise.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-11 13:46:29 +01:00
|
|
|
d := newDriver()
|
2015-04-20 04:23:04 -07:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
config := &NetworkConfiguration{
|
2015-04-20 04:23:04 -07:00
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
EnableIPv6: true}
|
2015-04-30 17:57:06 -07:00
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 04:19:57 +00:00
|
|
|
genericOption[netlabel.GenericData] = config
|
2015-04-30 17:57:06 -07:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
err := d.CreateNetwork("dummy", genericOption)
|
2015-04-20 04:23:04 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-28 05:57:36 +00:00
|
|
|
_, err = d.CreateEndpoint("dummy", "ep1", nil)
|
2015-04-20 04:23:04 -07: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)
|
|
|
|
}
|
|
|
|
}
|