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"
|
2015-05-16 16:02:51 -07:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2015-09-07 13:33:28 -04:00
|
|
|
"github.com/docker/libnetwork/testutils"
|
2015-04-10 16:02:25 +00:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLinkCreate(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(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-09-18 14:00:36 -07:00
|
|
|
if err := d.configure(nil); err != nil {
|
2015-06-11 18:12:00 -07:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-23 13:39:41 -07:00
|
|
|
mtu := 1490
|
2015-05-22 10:56:36 -07: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
|
|
|
|
2016-06-28 11:42:50 +08:00
|
|
|
ipdList := getIPv4Data(t, "")
|
2016-04-18 19:55:39 -07:00
|
|
|
err := d.CreateNetwork("dummy", genericOption, nil, ipdList, nil)
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:53:25 -07:00
|
|
|
te := newTestEndpoint(ipdList[0].Pool, 10)
|
2015-10-03 16:11:50 -07:00
|
|
|
err = d.CreateEndpoint("dummy", "", te.Interface(), 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 {
|
2016-11-22 09:29:53 +08:00
|
|
|
t.Fatal("Failed to detect invalid config")
|
2015-04-20 04:23:04 -07:00
|
|
|
}
|
|
|
|
|
2015-04-23 13:39:41 -07:00
|
|
|
// Good endpoint creation
|
2015-10-03 16:11:50 -07:00
|
|
|
err = d.CreateEndpoint("dummy", "ep", te.Interface(), nil)
|
2015-05-14 06:23:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a link: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.Join("dummy", "ep", "sbox", te, nil)
|
2015-04-20 04:23:04 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a link: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2017-05-22 02:25:52 +00:00
|
|
|
// Verify sbox endpoint interface inherited MTU value from bridge config
|
2015-09-09 16:06:35 -07:00
|
|
|
sboxLnk, err := netlink.LinkByName(te.iface.srcName)
|
2015-04-23 13:39:41 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if mtu != sboxLnk.Attrs().MTU {
|
2016-11-22 09:29:53 +08:00
|
|
|
t.Fatal("Sandbox endpoint interface did not inherit bridge interface MTU config")
|
2015-04-23 13:39:41 -07:00
|
|
|
}
|
|
|
|
// TODO: if we could get peer name from (sboxLnk.(*netlink.Veth)).PeerName
|
|
|
|
// then we could check the MTU on hostLnk as well.
|
|
|
|
|
2015-10-05 14:53:25 -07:00
|
|
|
te1 := newTestEndpoint(ipdList[0].Pool, 11)
|
2015-10-03 16:11:50 -07:00
|
|
|
err = d.CreateEndpoint("dummy", "ep", te1.Interface(), nil)
|
2015-04-20 04:23:04 -07:00
|
|
|
if err == nil {
|
2016-11-22 09:29:53 +08:00
|
|
|
t.Fatal("Failed to detect duplicate endpoint id on same network")
|
2015-04-20 04:23:04 -07:00
|
|
|
}
|
|
|
|
|
2015-09-09 16:06:35 -07:00
|
|
|
if te.iface.dstName == "" {
|
2015-04-10 16:02:25 +00:00
|
|
|
t.Fatal("Invalid Dstname returned")
|
|
|
|
}
|
|
|
|
|
2015-09-09 16:06:35 -07:00
|
|
|
_, err = netlink.LinkByName(te.iface.srcName)
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
2015-09-09 16:06:35 -07:00
|
|
|
t.Fatalf("Could not find source link %s: %v", te.iface.srcName, err)
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 14:00:36 -07:00
|
|
|
n, ok := d.networks["dummy"]
|
2015-05-21 13:13:19 -07:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Cannot find network %s inside driver", "dummy")
|
|
|
|
}
|
2015-09-09 16:06:35 -07:00
|
|
|
ip := te.iface.addr.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-09-09 16:06:35 -07:00
|
|
|
ip6 := te.iface.addrv6.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-05-14 06:23:45 +00:00
|
|
|
if !te.gw.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-05-14 06:23:45 +00:00
|
|
|
te.gw.String())
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
if !te.gw6.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-05-14 06:23:45 +00:00
|
|
|
te.gw6.String())
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkCreateTwo(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(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-09-18 14:00:36 -07:00
|
|
|
if err := d.configure(nil); err != nil {
|
2015-06-11 18:12:00 -07:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-22 10:56:36 -07: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
|
|
|
|
2016-06-28 11:42:50 +08:00
|
|
|
ipdList := getIPv4Data(t, "")
|
2016-04-18 19:55:39 -07:00
|
|
|
err := d.CreateNetwork("dummy", genericOption, nil, ipdList, nil)
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:53:25 -07:00
|
|
|
te1 := newTestEndpoint(ipdList[0].Pool, 11)
|
2015-10-03 16:11:50 -07:00
|
|
|
err = d.CreateEndpoint("dummy", "ep", te1.Interface(), 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-10-05 14:53:25 -07:00
|
|
|
te2 := newTestEndpoint(ipdList[0].Pool, 12)
|
2015-10-03 16:11:50 -07:00
|
|
|
err = d.CreateEndpoint("dummy", "ep", te2.Interface(), nil)
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
2015-05-14 14:56:15 -07:00
|
|
|
if _, ok := err.(driverapi.ErrEndpointExists); !ok {
|
|
|
|
t.Fatalf("Failed with a wrong error: %s", err.Error())
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-22 09:29:53 +08:00
|
|
|
t.Fatal("Expected to fail while trying to add same endpoint twice")
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkCreateNoEnableIPv6(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(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-09-18 14:00:36 -07:00
|
|
|
if err := d.configure(nil); err != nil {
|
2015-06-11 18:12:00 -07:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-22 10:56:36 -07: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
|
|
|
|
2016-06-28 11:42:50 +08:00
|
|
|
ipdList := getIPv4Data(t, "")
|
2016-04-18 19:55:39 -07:00
|
|
|
err := d.CreateNetwork("dummy", genericOption, nil, ipdList, nil)
|
2015-04-10 16:02:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
2015-10-05 14:53:25 -07:00
|
|
|
te := newTestEndpoint(ipdList[0].Pool, 30)
|
2015-10-03 16:11:50 -07:00
|
|
|
err = d.CreateEndpoint("dummy", "ep", te.Interface(), 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-09-09 16:06:35 -07:00
|
|
|
iface := te.iface
|
2015-10-03 16:11:50 -07:00
|
|
|
if iface.addrv6 != nil && iface.addrv6.IP.To16() != nil {
|
2017-05-22 02:25:52 +00:00
|
|
|
t.Fatalf("Expected IPv6 address to be nil when IPv6 is not enabled. Got IPv6 = %s", iface.addrv6.String())
|
2015-04-14 01:36:58 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
if te.gw6.To16() != nil {
|
|
|
|
t.Fatalf("Expected GatewayIPv6 to be nil when IPv6 is not enabled. Got GatewayIPv6 = %s", te.gw6.String())
|
2015-04-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-20 04:23:04 -07:00
|
|
|
|
|
|
|
func TestLinkDelete(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(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-09-18 14:00:36 -07:00
|
|
|
if err := d.configure(nil); err != nil {
|
2015-06-11 18:12:00 -07:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-22 10:56:36 -07: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
|
|
|
|
2016-06-28 11:42:50 +08:00
|
|
|
ipdList := getIPv4Data(t, "")
|
2016-04-18 19:55:39 -07:00
|
|
|
err := d.CreateNetwork("dummy", genericOption, nil, ipdList, nil)
|
2015-04-20 04:23:04 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:53:25 -07:00
|
|
|
te := newTestEndpoint(ipdList[0].Pool, 30)
|
2015-10-03 16:11:50 -07:00
|
|
|
err = d.CreateEndpoint("dummy", "ep1", te.Interface(), 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 {
|
2016-11-22 09:29:53 +08:00
|
|
|
t.Fatal("Failed to detect invalid config")
|
2015-04-20 04:23:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
err = d.DeleteEndpoint("dummy", "ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|