2021-05-25 19:48:54 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-02-24 21:41:17 -05:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
2016-06-10 11:30:56 -04:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-05-01 20:14:04 -04:00
|
|
|
"fmt"
|
2015-02-24 21:41:17 -05:00
|
|
|
"net"
|
2015-05-01 20:14:04 -04:00
|
|
|
"regexp"
|
2017-09-08 17:48:03 -04:00
|
|
|
"strconv"
|
2015-02-24 21:41:17 -05:00
|
|
|
"testing"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/iptables"
|
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/docker/libnetwork/netutils"
|
|
|
|
"github.com/docker/docker/libnetwork/options"
|
|
|
|
"github.com/docker/docker/libnetwork/testutils"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2016-06-27 23:42:50 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
2015-02-24 21:41:17 -05:00
|
|
|
)
|
|
|
|
|
2016-06-10 11:30:56 -04:00
|
|
|
func TestEndpointMarshalling(t *testing.T) {
|
|
|
|
ip1, _ := types.ParseCIDR("172.22.0.9/16")
|
|
|
|
ip2, _ := types.ParseCIDR("2001:db8::9")
|
|
|
|
mac, _ := net.ParseMAC("ac:bd:24:57:66:77")
|
|
|
|
e := &bridgeEndpoint{
|
|
|
|
id: "d2c015a1fe5930650cbcd50493efba0500bcebd8ee1f4401a16319f8a567de33",
|
|
|
|
nid: "ee33fbb43c323f1920b6b35a0101552ac22ede960d0e5245e9738bccc68b2415",
|
|
|
|
addr: ip1,
|
|
|
|
addrv6: ip2,
|
|
|
|
macAddress: mac,
|
|
|
|
srcName: "veth123456",
|
|
|
|
config: &endpointConfiguration{MacAddress: mac},
|
|
|
|
containerConfig: &containerConfiguration{
|
|
|
|
ParentEndpoints: []string{"one", "due", "three"},
|
|
|
|
ChildEndpoints: []string{"four", "five", "six"},
|
|
|
|
},
|
|
|
|
extConnConfig: &connectivityConfiguration{
|
|
|
|
ExposedPorts: []types.TransportPort{
|
|
|
|
{
|
|
|
|
Proto: 6,
|
|
|
|
Port: uint16(18),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PortBindings: []types.PortBinding{
|
|
|
|
{
|
|
|
|
Proto: 6,
|
|
|
|
IP: net.ParseIP("17210.33.9.56"),
|
|
|
|
Port: uint16(18),
|
|
|
|
HostPort: uint16(3000),
|
|
|
|
HostPortEnd: uint16(14000),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
portMapping: []types.PortBinding{
|
|
|
|
{
|
|
|
|
Proto: 17,
|
|
|
|
IP: net.ParseIP("172.33.9.56"),
|
|
|
|
Port: uint16(99),
|
|
|
|
HostIP: net.ParseIP("10.10.100.2"),
|
|
|
|
HostPort: uint16(9900),
|
|
|
|
HostPortEnd: uint16(10000),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Proto: 6,
|
|
|
|
IP: net.ParseIP("171.33.9.56"),
|
|
|
|
Port: uint16(55),
|
|
|
|
HostIP: net.ParseIP("10.11.100.2"),
|
|
|
|
HostPort: uint16(5500),
|
|
|
|
HostPortEnd: uint16(55000),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(e)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ee := &bridgeEndpoint{}
|
|
|
|
err = json.Unmarshal(b, ee)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.id != ee.id || e.nid != ee.nid || e.srcName != ee.srcName || !bytes.Equal(e.macAddress, ee.macAddress) ||
|
|
|
|
!types.CompareIPNet(e.addr, ee.addr) || !types.CompareIPNet(e.addrv6, ee.addrv6) ||
|
|
|
|
!compareEpConfig(e.config, ee.config) ||
|
|
|
|
!compareContainerConfig(e.containerConfig, ee.containerConfig) ||
|
|
|
|
!compareConnConfig(e.extConnConfig, ee.extConnConfig) ||
|
|
|
|
!compareBindings(e.portMapping, ee.portMapping) {
|
|
|
|
t.Fatalf("JSON marsh/unmarsh failed.\nOriginal:\n%#v\nDecoded:\n%#v", e, ee)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareEpConfig(a, b *endpointConfiguration) bool {
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return bytes.Equal(a.MacAddress, b.MacAddress)
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareContainerConfig(a, b *containerConfiguration) bool {
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(a.ParentEndpoints) != len(b.ParentEndpoints) ||
|
|
|
|
len(a.ChildEndpoints) != len(b.ChildEndpoints) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := 0; i < len(a.ParentEndpoints); i++ {
|
|
|
|
if a.ParentEndpoints[i] != b.ParentEndpoints[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < len(a.ChildEndpoints); i++ {
|
|
|
|
if a.ChildEndpoints[i] != b.ChildEndpoints[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareConnConfig(a, b *connectivityConfiguration) bool {
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(a.ExposedPorts) != len(b.ExposedPorts) ||
|
|
|
|
len(a.PortBindings) != len(b.PortBindings) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := 0; i < len(a.ExposedPorts); i++ {
|
|
|
|
if !a.ExposedPorts[i].Equal(&b.ExposedPorts[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < len(a.PortBindings); i++ {
|
|
|
|
if !a.PortBindings[i].Equal(&b.PortBindings[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareBindings(a, b []types.PortBinding) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
if !a[i].Equal(&b[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-06-27 23:42:50 -04:00
|
|
|
func getIPv4Data(t *testing.T, iface string) []driverapi.IPAMData {
|
2015-10-05 17:53:25 -04:00
|
|
|
ipd := driverapi.IPAMData{AddressSpace: "full"}
|
2016-09-17 01:40:44 -04:00
|
|
|
nws, _, err := netutils.ElectInterfaceAddresses(iface)
|
2015-10-05 17:53:25 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-17 01:40:44 -04:00
|
|
|
ipd.Pool = nws[0]
|
2015-10-05 17:53:25 -04:00
|
|
|
// Set network gateway to X.X.X.1
|
2016-09-17 01:40:44 -04:00
|
|
|
ipd.Gateway = types.GetIPNetCopy(nws[0])
|
2015-10-05 17:53:25 -04:00
|
|
|
ipd.Gateway.IP[len(ipd.Gateway.IP)-1] = 1
|
|
|
|
return []driverapi.IPAMData{ipd}
|
|
|
|
}
|
|
|
|
|
2015-05-03 16:23:52 -04:00
|
|
|
func TestCreateFullOptions(t *testing.T) {
|
2017-07-21 13:44:41 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
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 08:46:29 -04:00
|
|
|
d := newDriver()
|
2015-02-24 21:41:17 -05:00
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
config := &configuration{
|
2015-05-03 16:23:52 -04:00
|
|
|
EnableIPForwarding: true,
|
2015-06-11 21:12:00 -04:00
|
|
|
EnableIPTables: true,
|
2015-05-03 16:23:52 -04:00
|
|
|
}
|
2015-05-06 01:51:26 -04:00
|
|
|
|
2015-06-12 02:21:50 -04:00
|
|
|
// Test this scenario: Default gw address does not belong to
|
|
|
|
// container network and it's greater than bridge address
|
2015-10-05 17:53:25 -04:00
|
|
|
cnw, _ := types.ParseCIDR("172.16.122.0/24")
|
|
|
|
bnw, _ := types.ParseCIDR("172.16.0.0/24")
|
|
|
|
br, _ := types.ParseCIDR("172.16.0.1/16")
|
|
|
|
defgw, _ := types.ParseCIDR("172.16.0.100/16")
|
2015-06-12 02:21:50 -04:00
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 00:19:57 -04:00
|
|
|
genericOption[netlabel.GenericData] = config
|
2015-04-30 20:57:06 -04:00
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
if err := d.configure(genericOption); err != nil {
|
2015-04-15 01:25:42 -04:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-06 01:51:26 -04:00
|
|
|
netOption := make(map[string]interface{})
|
2016-01-29 19:54:57 -05:00
|
|
|
netOption[netlabel.EnableIPv6] = true
|
|
|
|
netOption[netlabel.GenericData] = &networkConfiguration{
|
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
}
|
2015-05-06 01:51:26 -04:00
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
ipdList := []driverapi.IPAMData{
|
2016-02-12 08:07:00 -05:00
|
|
|
{
|
2015-10-05 17:53:25 -04:00
|
|
|
Pool: bnw,
|
|
|
|
Gateway: br,
|
|
|
|
AuxAddresses: map[string]*net.IPNet{DefaultGatewayV4AuxKey: defgw},
|
|
|
|
},
|
|
|
|
}
|
2016-04-18 22:55:39 -04:00
|
|
|
err := d.CreateNetwork("dummy", netOption, nil, ipdList, nil)
|
2015-05-03 16:23:52 -04:00
|
|
|
if err != nil {
|
2015-02-24 21:41:17 -05:00
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
2015-07-27 19:48:30 -04:00
|
|
|
|
|
|
|
// Verify the IP address allocated for the endpoint belongs to the container network
|
|
|
|
epOptions := make(map[string]interface{})
|
2015-10-05 17:53:25 -04:00
|
|
|
te := newTestEndpoint(cnw, 10)
|
2015-10-03 19:11:50 -04:00
|
|
|
err = d.CreateEndpoint("dummy", "ep1", te.Interface(), epOptions)
|
2015-07-27 19:48:30 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create an endpoint : %s", err.Error())
|
|
|
|
}
|
2015-09-09 19:06:35 -04:00
|
|
|
|
|
|
|
if !cnw.Contains(te.Interface().Address().IP) {
|
|
|
|
t.Fatalf("endpoint got assigned address outside of container network(%s): %s", cnw.String(), te.Interface().Address())
|
2015-07-27 19:48:30 -04:00
|
|
|
}
|
2015-02-24 21:41:17 -05:00
|
|
|
}
|
|
|
|
|
2015-09-04 01:11:45 -04:00
|
|
|
func TestCreateNoConfig(t *testing.T) {
|
2016-05-16 14:51:40 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
}
|
2015-09-04 01:11:45 -04:00
|
|
|
d := newDriver()
|
|
|
|
|
|
|
|
netconfig := &networkConfiguration{BridgeName: DefaultBridgeName}
|
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = netconfig
|
|
|
|
|
2016-06-27 23:42:50 -04:00
|
|
|
if err := d.CreateNetwork("dummy", genericOption, nil, getIPv4Data(t, ""), nil); err != nil {
|
2015-09-04 01:11:45 -04:00
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 15:08:54 -04:00
|
|
|
func TestCreateFullOptionsLabels(t *testing.T) {
|
2016-05-16 14:51:40 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
}
|
2015-10-06 15:08:54 -04:00
|
|
|
d := newDriver()
|
|
|
|
|
|
|
|
config := &configuration{
|
|
|
|
EnableIPForwarding: true,
|
|
|
|
}
|
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = config
|
|
|
|
|
|
|
|
if err := d.configure(genericOption); err != nil {
|
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-11-06 02:50:10 -05:00
|
|
|
bndIPs := "127.0.0.1"
|
2019-09-25 01:08:25 -04:00
|
|
|
testHostIP := "1.2.3.4"
|
2015-11-15 12:28:53 -05:00
|
|
|
nwV6s := "2001:db8:2600:2700:2800::/80"
|
|
|
|
gwV6s := "2001:db8:2600:2700:2800::25/80"
|
2015-11-06 02:50:10 -05:00
|
|
|
nwV6, _ := types.ParseCIDR(nwV6s)
|
|
|
|
gwV6, _ := types.ParseCIDR(gwV6s)
|
|
|
|
|
2015-10-06 15:08:54 -04:00
|
|
|
labels := map[string]string{
|
2016-01-29 19:54:57 -05:00
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
DefaultBridge: "true",
|
|
|
|
EnableICC: "true",
|
|
|
|
EnableIPMasquerade: "true",
|
|
|
|
DefaultBindingIP: bndIPs,
|
2019-09-25 01:08:25 -04:00
|
|
|
netlabel.HostIP: testHostIP,
|
2015-10-06 15:08:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
netOption := make(map[string]interface{})
|
2016-01-29 19:54:57 -05:00
|
|
|
netOption[netlabel.EnableIPv6] = true
|
2015-10-06 15:08:54 -04:00
|
|
|
netOption[netlabel.GenericData] = labels
|
|
|
|
|
2016-06-27 23:42:50 -04:00
|
|
|
ipdList := getIPv4Data(t, "")
|
2015-11-06 02:50:10 -05:00
|
|
|
ipd6List := []driverapi.IPAMData{
|
2016-02-12 08:07:00 -05:00
|
|
|
{
|
2015-11-06 02:50:10 -05:00
|
|
|
Pool: nwV6,
|
|
|
|
AuxAddresses: map[string]*net.IPNet{
|
|
|
|
DefaultGatewayV6AuxKey: gwV6,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:55:39 -04:00
|
|
|
err := d.CreateNetwork("dummy", netOption, nil, ipdList, ipd6List)
|
2015-10-06 15:08:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nw, ok := d.networks["dummy"]
|
|
|
|
if !ok {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Cannot find dummy network in bridge driver")
|
2015-10-06 15:08:54 -04:00
|
|
|
}
|
|
|
|
|
2015-11-06 02:50:10 -05:00
|
|
|
if nw.config.BridgeName != DefaultBridgeName {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("incongruent name in bridge network")
|
2015-10-06 15:08:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !nw.config.EnableIPv6 {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("incongruent EnableIPv6 in bridge network")
|
2015-10-06 15:08:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !nw.config.EnableICC {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("incongruent EnableICC in bridge network")
|
2015-10-06 15:08:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !nw.config.EnableIPMasquerade {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("incongruent EnableIPMasquerade in bridge network")
|
2015-10-06 15:08:54 -04:00
|
|
|
}
|
2015-11-06 02:50:10 -05:00
|
|
|
|
|
|
|
bndIP := net.ParseIP(bndIPs)
|
|
|
|
if !bndIP.Equal(nw.config.DefaultBindingIP) {
|
|
|
|
t.Fatalf("Unexpected: %v", nw.config.DefaultBindingIP)
|
|
|
|
}
|
|
|
|
|
2019-09-25 01:08:25 -04:00
|
|
|
hostIP := net.ParseIP(testHostIP)
|
|
|
|
if !hostIP.Equal(nw.config.HostIP) {
|
|
|
|
t.Fatalf("Unexpected: %v", nw.config.HostIP)
|
|
|
|
}
|
|
|
|
|
2015-11-06 02:50:10 -05:00
|
|
|
if !types.CompareIPNet(nw.config.AddressIPv6, nwV6) {
|
|
|
|
t.Fatalf("Unexpected: %v", nw.config.AddressIPv6)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !gwV6.IP.Equal(nw.config.DefaultGatewayIPv6) {
|
|
|
|
t.Fatalf("Unexpected: %v", nw.config.DefaultGatewayIPv6)
|
|
|
|
}
|
|
|
|
|
|
|
|
// In short here we are testing --fixed-cidr-v6 daemon option
|
|
|
|
// plus --mac-address run option
|
|
|
|
mac, _ := net.ParseMAC("aa:bb:cc:dd:ee:ff")
|
|
|
|
epOptions := map[string]interface{}{netlabel.MacAddress: mac}
|
|
|
|
te := newTestEndpoint(ipdList[0].Pool, 20)
|
|
|
|
err = d.CreateEndpoint("dummy", "ep1", te.Interface(), epOptions)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !nwV6.Contains(te.Interface().AddressIPv6().IP) {
|
|
|
|
t.Fatalf("endpoint got assigned address outside of container network(%s): %s", nwV6.String(), te.Interface().AddressIPv6())
|
|
|
|
}
|
2015-11-15 12:28:53 -05:00
|
|
|
if te.Interface().AddressIPv6().IP.String() != "2001:db8:2600:2700:2800:aabb:ccdd:eeff" {
|
2015-11-06 02:50:10 -05:00
|
|
|
t.Fatalf("Unexpected endpoint IPv6 address: %v", te.Interface().AddressIPv6().IP)
|
|
|
|
}
|
2015-10-06 15:08:54 -04:00
|
|
|
}
|
|
|
|
|
2015-05-03 16:23:52 -04:00
|
|
|
func TestCreate(t *testing.T) {
|
2016-05-16 14:51:40 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
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 08:46:29 -04:00
|
|
|
d := newDriver()
|
2015-02-24 21:41:17 -05:00
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
if err := d.configure(nil); err != nil {
|
2015-06-11 21:12:00 -04:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
netconfig := &networkConfiguration{BridgeName: DefaultBridgeName}
|
2015-04-30 20:57:06 -04:00
|
|
|
genericOption := make(map[string]interface{})
|
2015-06-11 21:12:00 -04:00
|
|
|
genericOption[netlabel.GenericData] = netconfig
|
2015-04-30 20:57:06 -04:00
|
|
|
|
2016-06-27 23:42:50 -04:00
|
|
|
if err := d.CreateNetwork("dummy", genericOption, nil, getIPv4Data(t, ""), nil); err != nil {
|
2015-05-03 16:23:52 -04:00
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
2015-02-24 21:41:17 -05:00
|
|
|
}
|
2015-06-08 11:24:43 -04:00
|
|
|
|
2016-06-27 23:42:50 -04:00
|
|
|
err := d.CreateNetwork("dummy", genericOption, nil, getIPv4Data(t, ""), nil)
|
2015-06-08 11:24:43 -04:00
|
|
|
if err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Expected bridge driver to refuse creation of second network with default name")
|
2015-06-08 11:24:43 -04:00
|
|
|
}
|
|
|
|
if _, ok := err.(types.ForbiddenError); !ok {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Creation of second network with default name failed with unexpected error type")
|
2015-06-08 11:24:43 -04:00
|
|
|
}
|
2015-02-24 21:41:17 -05:00
|
|
|
}
|
|
|
|
|
2015-05-03 16:23:52 -04:00
|
|
|
func TestCreateFail(t *testing.T) {
|
2016-05-16 14:51:40 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
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 08:46:29 -04:00
|
|
|
d := newDriver()
|
2015-02-24 21:41:17 -05:00
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
if err := d.configure(nil); err != nil {
|
2015-06-11 21:12:00 -04:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-25 12:02:18 -04:00
|
|
|
netconfig := &networkConfiguration{BridgeName: "dummy0", DefaultBridge: true}
|
2015-04-30 20:57:06 -04:00
|
|
|
genericOption := make(map[string]interface{})
|
2015-06-11 21:12:00 -04:00
|
|
|
genericOption[netlabel.GenericData] = netconfig
|
2015-04-30 20:57:06 -04:00
|
|
|
|
2016-06-27 23:42:50 -04:00
|
|
|
if err := d.CreateNetwork("dummy", genericOption, nil, getIPv4Data(t, ""), nil); err == nil {
|
2015-05-03 16:23:52 -04:00
|
|
|
t.Fatal("Bridge creation was expected to fail")
|
2015-02-24 21:41:17 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-04 14:49:53 -04:00
|
|
|
|
2015-06-02 21:40:55 -04:00
|
|
|
func TestCreateMultipleNetworks(t *testing.T) {
|
2016-05-16 14:51:40 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
}
|
|
|
|
|
2015-06-02 21:40:55 -04:00
|
|
|
d := newDriver()
|
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
config := &configuration{
|
|
|
|
EnableIPTables: true,
|
|
|
|
}
|
2015-06-02 21:40:55 -04:00
|
|
|
genericOption := make(map[string]interface{})
|
2015-06-11 21:12:00 -04:00
|
|
|
genericOption[netlabel.GenericData] = config
|
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
if err := d.configure(genericOption); err != nil {
|
2015-06-11 21:12:00 -04:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-23 21:01:04 -04:00
|
|
|
config1 := &networkConfiguration{BridgeName: "net_test_1"}
|
2015-06-11 21:12:00 -04:00
|
|
|
genericOption = make(map[string]interface{})
|
2015-06-02 21:40:55 -04:00
|
|
|
genericOption[netlabel.GenericData] = config1
|
2016-06-27 23:42:50 -04:00
|
|
|
if err := d.CreateNetwork("1", genericOption, nil, getIPv4Data(t, ""), nil); err != nil {
|
2015-06-02 21:40:55 -04:00
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-11-02 13:14:09 -04:00
|
|
|
verifyV4INCEntries(d.networks, t)
|
|
|
|
|
2015-09-23 21:01:04 -04:00
|
|
|
config2 := &networkConfiguration{BridgeName: "net_test_2"}
|
2015-06-02 21:40:55 -04:00
|
|
|
genericOption[netlabel.GenericData] = config2
|
2016-06-27 23:42:50 -04:00
|
|
|
if err := d.CreateNetwork("2", genericOption, nil, getIPv4Data(t, ""), nil); err != nil {
|
2015-06-02 21:40:55 -04:00
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-11-02 13:14:09 -04:00
|
|
|
verifyV4INCEntries(d.networks, t)
|
2015-10-16 01:16:40 -04:00
|
|
|
|
2015-09-23 21:01:04 -04:00
|
|
|
config3 := &networkConfiguration{BridgeName: "net_test_3"}
|
2015-06-02 21:40:55 -04:00
|
|
|
genericOption[netlabel.GenericData] = config3
|
2016-06-27 23:42:50 -04:00
|
|
|
if err := d.CreateNetwork("3", genericOption, nil, getIPv4Data(t, ""), nil); err != nil {
|
2015-06-02 21:40:55 -04:00
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-11-02 13:14:09 -04:00
|
|
|
verifyV4INCEntries(d.networks, t)
|
2015-06-02 21:40:55 -04:00
|
|
|
|
2015-09-23 21:01:04 -04:00
|
|
|
config4 := &networkConfiguration{BridgeName: "net_test_4"}
|
2015-06-02 21:40:55 -04:00
|
|
|
genericOption[netlabel.GenericData] = config4
|
2016-06-27 23:42:50 -04:00
|
|
|
if err := d.CreateNetwork("4", genericOption, nil, getIPv4Data(t, ""), nil); err != nil {
|
2015-06-02 21:40:55 -04:00
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-11-02 13:14:09 -04:00
|
|
|
verifyV4INCEntries(d.networks, t)
|
2015-06-02 21:40:55 -04:00
|
|
|
|
|
|
|
d.DeleteNetwork("1")
|
2016-11-02 13:14:09 -04:00
|
|
|
verifyV4INCEntries(d.networks, t)
|
2015-06-02 21:40:55 -04:00
|
|
|
|
|
|
|
d.DeleteNetwork("2")
|
2016-11-02 13:14:09 -04:00
|
|
|
verifyV4INCEntries(d.networks, t)
|
2015-06-02 21:40:55 -04:00
|
|
|
|
|
|
|
d.DeleteNetwork("3")
|
2016-11-02 13:14:09 -04:00
|
|
|
verifyV4INCEntries(d.networks, t)
|
2015-06-02 21:40:55 -04:00
|
|
|
|
|
|
|
d.DeleteNetwork("4")
|
2016-11-02 13:14:09 -04:00
|
|
|
verifyV4INCEntries(d.networks, t)
|
2015-06-02 21:40:55 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 13:14:09 -04:00
|
|
|
// Verify the network isolation rules are installed for each network
|
|
|
|
func verifyV4INCEntries(networks map[string]*bridgeNetwork, t *testing.T) {
|
2017-11-28 16:15:55 -05:00
|
|
|
iptable := iptables.GetIptable(iptables.IPv4)
|
|
|
|
out1, err := iptable.Raw("-S", IsolationChain1)
|
2015-06-02 21:40:55 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-28 16:15:55 -05:00
|
|
|
out2, err := iptable.Raw("-S", IsolationChain2)
|
2016-11-02 13:14:09 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-06-02 21:40:55 -04:00
|
|
|
}
|
2015-12-15 00:57:56 -05:00
|
|
|
|
2016-11-02 13:14:09 -04:00
|
|
|
for _, n := range networks {
|
|
|
|
re := regexp.MustCompile(fmt.Sprintf("-i %s ! -o %s -j %s", n.config.BridgeName, n.config.BridgeName, IsolationChain2))
|
|
|
|
matches := re.FindAllString(string(out1[:]), -1)
|
|
|
|
if len(matches) != 1 {
|
|
|
|
t.Fatalf("Cannot find expected inter-network isolation rules in IP Tables for network %s:\n%s.", n.id, string(out1[:]))
|
|
|
|
}
|
|
|
|
re = regexp.MustCompile(fmt.Sprintf("-o %s -j DROP", n.config.BridgeName))
|
|
|
|
matches = re.FindAllString(string(out2[:]), -1)
|
|
|
|
if len(matches) != 1 {
|
|
|
|
t.Fatalf("Cannot find expected inter-network isolation rules in IP Tables for network %s:\n%s.", n.id, string(out2[:]))
|
|
|
|
}
|
2015-12-15 00:57:56 -05:00
|
|
|
}
|
2015-06-02 21:40:55 -04:00
|
|
|
}
|
|
|
|
|
2015-05-14 02:23:45 -04:00
|
|
|
type testInterface struct {
|
|
|
|
mac net.HardwareAddr
|
2015-10-03 19:11:50 -04:00
|
|
|
addr *net.IPNet
|
|
|
|
addrv6 *net.IPNet
|
2015-05-14 02:23:45 -04:00
|
|
|
srcName string
|
|
|
|
dstName string
|
|
|
|
}
|
|
|
|
|
|
|
|
type testEndpoint struct {
|
2015-09-09 19:06:35 -04:00
|
|
|
iface *testInterface
|
2015-05-14 02:23:45 -04:00
|
|
|
gw net.IP
|
|
|
|
gw6 net.IP
|
|
|
|
hostsPath string
|
|
|
|
resolvConfPath string
|
2015-05-19 20:08:56 -04:00
|
|
|
routes []types.StaticRoute
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
func newTestEndpoint(nw *net.IPNet, ordinal byte) *testEndpoint {
|
|
|
|
addr := types.GetIPNetCopy(nw)
|
|
|
|
addr.IP[len(addr.IP)-1] = ordinal
|
|
|
|
return &testEndpoint{iface: &testInterface{addr: addr}}
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
func (te *testEndpoint) Interface() driverapi.InterfaceInfo {
|
|
|
|
if te.iface != nil {
|
|
|
|
return te.iface
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *testInterface) MacAddress() net.HardwareAddr {
|
|
|
|
return i.mac
|
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
func (i *testInterface) Address() *net.IPNet {
|
2015-05-14 02:23:45 -04:00
|
|
|
return i.addr
|
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
func (i *testInterface) AddressIPv6() *net.IPNet {
|
2015-05-14 02:23:45 -04:00
|
|
|
return i.addrv6
|
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
func (i *testInterface) SetMacAddress(mac net.HardwareAddr) error {
|
|
|
|
if i.mac != nil {
|
|
|
|
return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", i.mac, mac)
|
|
|
|
}
|
|
|
|
if mac == nil {
|
|
|
|
return types.BadRequestErrorf("tried to set nil MAC address to endpoint interface")
|
|
|
|
}
|
|
|
|
i.mac = types.GetMacCopy(mac)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *testInterface) SetIPAddress(address *net.IPNet) error {
|
|
|
|
if address.IP == nil {
|
|
|
|
return types.BadRequestErrorf("tried to set nil IP address to endpoint interface")
|
|
|
|
}
|
|
|
|
if address.IP.To4() == nil {
|
|
|
|
return setAddress(&i.addrv6, address)
|
|
|
|
}
|
|
|
|
return setAddress(&i.addr, address)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setAddress(ifaceAddr **net.IPNet, address *net.IPNet) error {
|
|
|
|
if *ifaceAddr != nil {
|
|
|
|
return types.ForbiddenErrorf("endpoint interface IP present (%s). Cannot be modified with (%s).", *ifaceAddr, address)
|
|
|
|
}
|
|
|
|
*ifaceAddr = types.GetIPNetCopy(address)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-14 02:23:45 -04:00
|
|
|
func (i *testInterface) SetNames(srcName string, dstName string) error {
|
|
|
|
i.srcName = srcName
|
|
|
|
i.dstName = dstName
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
func (te *testEndpoint) InterfaceName() driverapi.InterfaceNameInfo {
|
|
|
|
if te.iface != nil {
|
|
|
|
return te.iface
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
return nil
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (te *testEndpoint) SetGateway(gw net.IP) error {
|
|
|
|
te.gw = gw
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (te *testEndpoint) SetGatewayIPv6(gw6 net.IP) error {
|
|
|
|
te.gw6 = gw6
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
func (te *testEndpoint) AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error {
|
|
|
|
te.routes = append(te.routes, types.StaticRoute{Destination: destination, RouteType: routeType, NextHop: nextHop})
|
2015-05-19 20:08:56 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:55:39 -04:00
|
|
|
func (te *testEndpoint) AddTableEntry(tableName string, key string, value []byte) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-02 21:07:44 -05:00
|
|
|
func (te *testEndpoint) DisableGatewayService() {}
|
|
|
|
|
2015-05-04 14:49:53 -04:00
|
|
|
func TestQueryEndpointInfo(t *testing.T) {
|
2015-05-18 19:49:12 -04:00
|
|
|
testQueryEndpointInfo(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryEndpointInfoHairpin(t *testing.T) {
|
|
|
|
testQueryEndpointInfo(t, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testQueryEndpointInfo(t *testing.T, ulPxyEnabled bool) {
|
2017-07-21 13:44:41 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
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 08:46:29 -04:00
|
|
|
d := newDriver()
|
2015-05-04 14:49:53 -04:00
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
config := &configuration{
|
2015-05-18 19:49:12 -04:00
|
|
|
EnableIPTables: true,
|
|
|
|
EnableUserlandProxy: ulPxyEnabled,
|
2015-05-04 14:49:53 -04:00
|
|
|
}
|
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 00:19:57 -04:00
|
|
|
genericOption[netlabel.GenericData] = config
|
2015-05-04 14:49:53 -04:00
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
if err := d.configure(genericOption); err != nil {
|
2015-06-11 21:12:00 -04:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
netconfig := &networkConfiguration{
|
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
EnableICC: false,
|
|
|
|
}
|
|
|
|
genericOption = make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = netconfig
|
|
|
|
|
2016-06-27 23:42:50 -04:00
|
|
|
ipdList := getIPv4Data(t, "")
|
2016-04-18 22:55:39 -04:00
|
|
|
err := d.CreateNetwork("net1", genericOption, nil, ipdList, nil)
|
2015-05-04 14:49:53 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
sbOptions := make(map[string]interface{})
|
|
|
|
sbOptions[netlabel.PortMap] = getPortMapping()
|
2015-05-04 14:49:53 -04:00
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
te := newTestEndpoint(ipdList[0].Pool, 11)
|
2015-12-07 17:45:51 -05:00
|
|
|
err = d.CreateEndpoint("net1", "ep1", te.Interface(), nil)
|
2015-05-04 14:49:53 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create an endpoint : %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
err = d.Join("net1", "ep1", "sbox", te, sbOptions)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to join the endpoint: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.ProgramExternalConnectivity("net1", "ep1", sbOptions)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to program external connectivity: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
network, ok := d.networks["net1"]
|
2015-05-21 16:13:19 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Cannot find network %s inside driver", "net1")
|
|
|
|
}
|
|
|
|
ep, _ := network.endpoints["ep1"]
|
|
|
|
data, err := d.EndpointOperInfo(network.id, ep.id)
|
2015-05-04 14:49:53 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to ask for endpoint operational data: %v", err)
|
|
|
|
}
|
2015-05-06 00:19:57 -04:00
|
|
|
pmd, ok := data[netlabel.PortMap]
|
2015-05-04 14:49:53 -04:00
|
|
|
if !ok {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Endpoint operational data does not contain port mapping data")
|
2015-05-04 14:49:53 -04:00
|
|
|
}
|
2015-05-20 16:28:46 -04:00
|
|
|
pm, ok := pmd.([]types.PortBinding)
|
2015-05-04 14:49:53 -04:00
|
|
|
if !ok {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Unexpected format for port mapping in endpoint operational data")
|
2015-05-04 14:49:53 -04:00
|
|
|
}
|
|
|
|
if len(ep.portMapping) != len(pm) {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Incomplete data for port mapping in endpoint operational data")
|
2015-05-04 14:49:53 -04:00
|
|
|
}
|
|
|
|
for i, pb := range ep.portMapping {
|
|
|
|
if !pb.Equal(&pm[i]) {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Unexpected data for port mapping in endpoint operational data")
|
2015-05-04 14:49:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
err = d.RevokeExternalConnectivity("net1", "ep1")
|
2015-05-04 14:49:53 -04:00
|
|
|
if err != nil {
|
2015-12-07 17:45:51 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// release host mapped ports
|
|
|
|
err = d.Leave("net1", "ep1")
|
2015-05-04 14:49:53 -04:00
|
|
|
if err != nil {
|
2015-12-07 17:45:51 -05:00
|
|
|
t.Fatal(err)
|
2015-05-04 14:49:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 16:28:46 -04:00
|
|
|
func getExposedPorts() []types.TransportPort {
|
|
|
|
return []types.TransportPort{
|
2016-02-12 08:07:00 -05:00
|
|
|
{Proto: types.TCP, Port: uint16(5000)},
|
|
|
|
{Proto: types.UDP, Port: uint16(400)},
|
|
|
|
{Proto: types.TCP, Port: uint16(600)},
|
2015-05-05 16:46:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 16:28:46 -04:00
|
|
|
func getPortMapping() []types.PortBinding {
|
|
|
|
return []types.PortBinding{
|
2016-02-12 08:07:00 -05:00
|
|
|
{Proto: types.TCP, Port: uint16(230), HostPort: uint16(23000)},
|
|
|
|
{Proto: types.UDP, Port: uint16(200), HostPort: uint16(22000)},
|
|
|
|
{Proto: types.TCP, Port: uint16(120), HostPort: uint16(12000)},
|
2015-05-01 20:14:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkContainers(t *testing.T) {
|
2016-05-16 14:51:40 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
}
|
2015-05-01 20:14:04 -04:00
|
|
|
|
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 08:46:29 -04:00
|
|
|
d := newDriver()
|
2017-11-28 16:15:55 -05:00
|
|
|
iptable := iptables.GetIptable(iptables.IPv4)
|
2015-05-01 20:14:04 -04:00
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
config := &configuration{
|
2015-05-01 20:14:04 -04:00
|
|
|
EnableIPTables: true,
|
|
|
|
}
|
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 00:19:57 -04:00
|
|
|
genericOption[netlabel.GenericData] = config
|
2015-05-01 20:14:04 -04:00
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
if err := d.configure(genericOption); err != nil {
|
2015-06-11 21:12:00 -04:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
netconfig := &networkConfiguration{
|
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
EnableICC: false,
|
|
|
|
}
|
|
|
|
genericOption = make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = netconfig
|
|
|
|
|
2016-06-27 23:42:50 -04:00
|
|
|
ipdList := getIPv4Data(t, "")
|
2016-04-18 22:55:39 -04:00
|
|
|
err := d.CreateNetwork("net1", genericOption, nil, ipdList, nil)
|
2015-05-01 20:14:04 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
te1 := newTestEndpoint(ipdList[0].Pool, 11)
|
2015-12-07 17:45:51 -05:00
|
|
|
err = d.CreateEndpoint("net1", "ep1", te1.Interface(), nil)
|
2015-05-01 20:14:04 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create an endpoint : %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
exposedPorts := getExposedPorts()
|
|
|
|
sbOptions := make(map[string]interface{})
|
|
|
|
sbOptions[netlabel.ExposedPorts] = exposedPorts
|
|
|
|
|
|
|
|
err = d.Join("net1", "ep1", "sbox", te1, sbOptions)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to join the endpoint: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.ProgramExternalConnectivity("net1", "ep1", sbOptions)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to program external connectivity: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
addr1 := te1.iface.addr
|
2015-05-14 02:23:45 -04:00
|
|
|
if addr1.IP.To4() == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("No Ipv4 address assigned to the endpoint: ep1")
|
2015-05-01 20:14:04 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
te2 := newTestEndpoint(ipdList[0].Pool, 22)
|
2015-10-03 19:11:50 -04:00
|
|
|
err = d.CreateEndpoint("net1", "ep2", te2.Interface(), nil)
|
2015-05-01 20:14:04 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create an endpoint : %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
addr2 := te2.iface.addr
|
2015-05-14 02:23:45 -04:00
|
|
|
if addr2.IP.To4() == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("No Ipv4 address assigned to the endpoint: ep2")
|
2015-05-01 20:14:04 -04:00
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
sbOptions = make(map[string]interface{})
|
2016-03-08 01:21:17 -05:00
|
|
|
sbOptions[netlabel.GenericData] = options.Generic{
|
|
|
|
"ChildEndpoints": []string{"ep1"},
|
|
|
|
}
|
2015-05-01 20:14:04 -04:00
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
err = d.Join("net1", "ep2", "", te2, sbOptions)
|
2015-05-01 20:14:04 -04:00
|
|
|
if err != nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Failed to link ep1 and ep2")
|
2015-05-01 20:14:04 -04:00
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
err = d.ProgramExternalConnectivity("net1", "ep2", sbOptions)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to program external connectivity: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-28 16:15:55 -05:00
|
|
|
out, err := iptable.Raw("-L", DockerChain)
|
2015-05-05 16:46:12 -04:00
|
|
|
for _, pm := range exposedPorts {
|
2015-05-01 20:14:04 -04:00
|
|
|
regex := fmt.Sprintf("%s dpt:%d", pm.Proto.String(), pm.Port)
|
|
|
|
re := regexp.MustCompile(regex)
|
|
|
|
matches := re.FindAllString(string(out[:]), -1)
|
2015-05-05 16:46:12 -04:00
|
|
|
if len(matches) != 1 {
|
2015-05-01 20:14:04 -04:00
|
|
|
t.Fatalf("IP Tables programming failed %s", string(out[:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
regex = fmt.Sprintf("%s spt:%d", pm.Proto.String(), pm.Port)
|
|
|
|
matched, _ := regexp.MatchString(regex, string(out[:]))
|
|
|
|
if !matched {
|
|
|
|
t.Fatalf("IP Tables programming failed %s", string(out[:]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
err = d.RevokeExternalConnectivity("net1", "ep2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to revoke external connectivity: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-14 02:23:45 -04:00
|
|
|
err = d.Leave("net1", "ep2")
|
2015-05-01 20:14:04 -04:00
|
|
|
if err != nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Failed to unlink ep1 and ep2")
|
2015-05-01 20:14:04 -04:00
|
|
|
}
|
|
|
|
|
2017-11-28 16:15:55 -05:00
|
|
|
out, err = iptable.Raw("-L", DockerChain)
|
2015-05-05 16:46:12 -04:00
|
|
|
for _, pm := range exposedPorts {
|
2015-05-01 20:14:04 -04:00
|
|
|
regex := fmt.Sprintf("%s dpt:%d", pm.Proto.String(), pm.Port)
|
|
|
|
re := regexp.MustCompile(regex)
|
|
|
|
matches := re.FindAllString(string(out[:]), -1)
|
2015-05-05 16:46:12 -04:00
|
|
|
if len(matches) != 0 {
|
2015-05-01 20:14:04 -04:00
|
|
|
t.Fatalf("Leave should have deleted relevant IPTables rules %s", string(out[:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
regex = fmt.Sprintf("%s spt:%d", pm.Proto.String(), pm.Port)
|
|
|
|
matched, _ := regexp.MatchString(regex, string(out[:]))
|
|
|
|
if matched {
|
|
|
|
t.Fatalf("Leave should have deleted relevant IPTables rules %s", string(out[:]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error condition test with an invalid endpoint-id "ep4"
|
2015-12-07 17:45:51 -05:00
|
|
|
sbOptions = make(map[string]interface{})
|
2016-03-08 01:21:17 -05:00
|
|
|
sbOptions[netlabel.GenericData] = options.Generic{
|
|
|
|
"ChildEndpoints": []string{"ep1", "ep4"},
|
|
|
|
}
|
2015-05-01 20:14:04 -04:00
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
err = d.Join("net1", "ep2", "", te2, sbOptions)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = d.ProgramExternalConnectivity("net1", "ep2", sbOptions)
|
2015-05-01 20:14:04 -04:00
|
|
|
if err != nil {
|
2017-11-28 16:15:55 -05:00
|
|
|
out, err = iptable.Raw("-L", DockerChain)
|
2015-05-05 16:46:12 -04:00
|
|
|
for _, pm := range exposedPorts {
|
2015-05-01 20:14:04 -04:00
|
|
|
regex := fmt.Sprintf("%s dpt:%d", pm.Proto.String(), pm.Port)
|
|
|
|
re := regexp.MustCompile(regex)
|
|
|
|
matches := re.FindAllString(string(out[:]), -1)
|
2015-05-05 16:46:12 -04:00
|
|
|
if len(matches) != 0 {
|
2015-05-01 20:14:04 -04:00
|
|
|
t.Fatalf("Error handling should rollback relevant IPTables rules %s", string(out[:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
regex = fmt.Sprintf("%s spt:%d", pm.Proto.String(), pm.Port)
|
|
|
|
matched, _ := regexp.MatchString(regex, string(out[:]))
|
|
|
|
if matched {
|
|
|
|
t.Fatalf("Error handling should rollback relevant IPTables rules %s", string(out[:]))
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 16:46:12 -04:00
|
|
|
} else {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Expected Join to fail given link conditions are not satisfied")
|
2015-05-01 20:14:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 18:13:44 -04:00
|
|
|
func TestValidateConfig(t *testing.T) {
|
2016-05-16 14:51:40 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
}
|
2015-04-24 18:13:44 -04:00
|
|
|
|
|
|
|
// Test mtu
|
2015-05-22 13:56:36 -04:00
|
|
|
c := networkConfiguration{Mtu: -2}
|
2015-04-24 18:13:44 -04:00
|
|
|
err := c.Validate()
|
|
|
|
if err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Failed to detect invalid MTU number")
|
2015-04-24 18:13:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Mtu = 9000
|
|
|
|
err = c.Validate()
|
|
|
|
if err != nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("unexpected validation error on MTU number")
|
2015-04-24 18:13:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bridge network
|
|
|
|
_, network, _ := net.ParseCIDR("172.28.0.0/16")
|
2015-10-05 17:53:25 -04:00
|
|
|
c = networkConfiguration{
|
|
|
|
AddressIPv4: network,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.Validate()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-24 18:13:44 -04:00
|
|
|
|
|
|
|
// Test v4 gw
|
|
|
|
c.DefaultGatewayIPv4 = net.ParseIP("172.27.30.234")
|
|
|
|
err = c.Validate()
|
|
|
|
if err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Failed to detect invalid default gateway")
|
2015-04-24 18:13:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
c.DefaultGatewayIPv4 = net.ParseIP("172.28.30.234")
|
|
|
|
err = c.Validate()
|
|
|
|
if err != nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Unexpected validation error on default gateway")
|
2015-04-24 18:13:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test v6 gw
|
2015-11-15 12:28:53 -05:00
|
|
|
_, v6nw, _ := net.ParseCIDR("2001:db8:ae:b004::/64")
|
2015-05-22 13:56:36 -04:00
|
|
|
c = networkConfiguration{
|
2015-04-24 18:13:44 -04:00
|
|
|
EnableIPv6: true,
|
2015-10-05 17:53:25 -04:00
|
|
|
AddressIPv6: v6nw,
|
2015-11-15 12:28:53 -05:00
|
|
|
DefaultGatewayIPv6: net.ParseIP("2001:db8:ac:b004::bad:a55"),
|
2015-04-24 18:13:44 -04:00
|
|
|
}
|
|
|
|
err = c.Validate()
|
|
|
|
if err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Failed to detect invalid v6 default gateway")
|
2015-04-24 18:13:44 -04:00
|
|
|
}
|
|
|
|
|
2015-11-15 12:28:53 -05:00
|
|
|
c.DefaultGatewayIPv6 = net.ParseIP("2001:db8:ae:b004::bad:a55")
|
2015-04-24 18:13:44 -04:00
|
|
|
err = c.Validate()
|
|
|
|
if err != nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Unexpected validation error on v6 default gateway")
|
2015-04-24 18:13:44 -04:00
|
|
|
}
|
2015-10-05 17:53:25 -04:00
|
|
|
|
|
|
|
c.AddressIPv6 = nil
|
|
|
|
err = c.Validate()
|
|
|
|
if err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Failed to detect invalid v6 default gateway")
|
2015-10-05 17:53:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
c.AddressIPv6 = nil
|
|
|
|
err = c.Validate()
|
|
|
|
if err == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Failed to detect invalid v6 default gateway")
|
2015-10-05 17:53:25 -04:00
|
|
|
}
|
2015-04-24 18:13:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetDefaultGw(t *testing.T) {
|
2016-05-16 14:51:40 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
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 08:46:29 -04:00
|
|
|
d := newDriver()
|
2015-04-24 18:13:44 -04:00
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
if err := d.configure(nil); err != nil {
|
2015-06-11 21:12:00 -04:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-24 18:13:44 -04:00
|
|
|
_, subnetv6, _ := net.ParseCIDR("2001:db8:ea9:9abc:b0c4::/80")
|
2015-08-06 18:45:38 -04:00
|
|
|
|
2016-06-27 23:42:50 -04:00
|
|
|
ipdList := getIPv4Data(t, "")
|
2015-10-05 17:53:25 -04:00
|
|
|
gw4 := types.GetIPCopy(ipdList[0].Pool.IP).To4()
|
2015-04-24 18:13:44 -04:00
|
|
|
gw4[3] = 254
|
|
|
|
gw6 := net.ParseIP("2001:db8:ea9:9abc:b0c4::254")
|
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
config := &networkConfiguration{
|
2015-04-24 18:13:44 -04:00
|
|
|
BridgeName: DefaultBridgeName,
|
2015-10-05 17:53:25 -04:00
|
|
|
AddressIPv6: subnetv6,
|
2015-04-24 18:13:44 -04:00
|
|
|
DefaultGatewayIPv4: gw4,
|
|
|
|
DefaultGatewayIPv6: gw6,
|
|
|
|
}
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
genericOption := make(map[string]interface{})
|
2016-01-29 19:54:57 -05:00
|
|
|
genericOption[netlabel.EnableIPv6] = true
|
2015-05-06 00:19:57 -04:00
|
|
|
genericOption[netlabel.GenericData] = config
|
2015-04-30 20:57:06 -04:00
|
|
|
|
2016-04-18 22:55:39 -04:00
|
|
|
err := d.CreateNetwork("dummy", genericOption, nil, ipdList, nil)
|
2015-04-24 18:13:44 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
te := newTestEndpoint(ipdList[0].Pool, 10)
|
2015-10-03 19:11:50 -04:00
|
|
|
err = d.CreateEndpoint("dummy", "ep", te.Interface(), nil)
|
2015-04-24 18:13:44 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create endpoint: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-14 02:23:45 -04:00
|
|
|
err = d.Join("dummy", "ep", "sbox", te, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to join endpoint: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !gw4.Equal(te.gw) {
|
|
|
|
t.Fatalf("Failed to configure default gateway. Expected %v. Found %v", gw4, te.gw)
|
2015-04-24 18:13:44 -04:00
|
|
|
}
|
|
|
|
|
2015-05-14 02:23:45 -04:00
|
|
|
if !gw6.Equal(te.gw6) {
|
|
|
|
t.Fatalf("Failed to configure default gateway. Expected %v. Found %v", gw6, te.gw6)
|
2015-04-24 18:13:44 -04:00
|
|
|
}
|
|
|
|
}
|
2015-09-15 04:30:10 -04:00
|
|
|
|
|
|
|
func TestCleanupIptableRules(t *testing.T) {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
bridgeChain := []iptables.ChainInfo{
|
2016-02-12 08:07:00 -05:00
|
|
|
{Name: DockerChain, Table: iptables.Nat},
|
|
|
|
{Name: DockerChain, Table: iptables.Filter},
|
2016-11-02 13:14:09 -04:00
|
|
|
{Name: IsolationChain1, Table: iptables.Filter},
|
2015-09-15 04:30:10 -04:00
|
|
|
}
|
2017-11-28 16:15:55 -05:00
|
|
|
|
|
|
|
ipVersions := []iptables.IPVersion{iptables.IPv4, iptables.IPv6}
|
|
|
|
|
|
|
|
for _, version := range ipVersions {
|
|
|
|
if _, _, _, _, err := setupIPChains(&configuration{EnableIPTables: true}, version); err != nil {
|
|
|
|
t.Fatalf("Error setting up ip chains for %s: %v", version, err)
|
2015-09-15 04:30:10 -04:00
|
|
|
}
|
2017-11-28 16:15:55 -05:00
|
|
|
|
|
|
|
iptable := iptables.GetIptable(version)
|
|
|
|
for _, chainInfo := range bridgeChain {
|
|
|
|
if !iptable.ExistChain(chainInfo.Name, chainInfo.Table) {
|
|
|
|
t.Fatalf("iptables version %s chain %s of %s table should have been created", version, chainInfo.Name, chainInfo.Table)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
removeIPChains(version)
|
|
|
|
for _, chainInfo := range bridgeChain {
|
|
|
|
if iptable.ExistChain(chainInfo.Name, chainInfo.Table) {
|
|
|
|
t.Fatalf("iptables version %s chain %s of %s table should have been deleted", version, chainInfo.Name, chainInfo.Table)
|
|
|
|
}
|
2015-09-15 04:30:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-27 23:42:50 -04:00
|
|
|
|
|
|
|
func TestCreateWithExistingBridge(t *testing.T) {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
d := newDriver()
|
|
|
|
|
|
|
|
if err := d.configure(nil); err != nil {
|
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
brName := "br111"
|
|
|
|
br := &netlink.Bridge{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{
|
|
|
|
Name: brName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := netlink.LinkAdd(br); err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge interface: %v", err)
|
|
|
|
}
|
|
|
|
defer netlink.LinkDel(br)
|
|
|
|
if err := netlink.LinkSetUp(br); err != nil {
|
|
|
|
t.Fatalf("Failed to set bridge interface up: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.IP{192, 168, 122, 1}
|
|
|
|
addr := &netlink.Addr{IPNet: &net.IPNet{
|
|
|
|
IP: ip,
|
|
|
|
Mask: net.IPv4Mask(255, 255, 255, 0),
|
|
|
|
}}
|
|
|
|
if err := netlink.AddrAdd(br, addr); err != nil {
|
|
|
|
t.Fatalf("Failed to add IP address to bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
netconfig := &networkConfiguration{BridgeName: brName}
|
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = netconfig
|
|
|
|
|
|
|
|
if err := d.CreateNetwork(brName, genericOption, nil, getIPv4Data(t, brName), nil); err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge network: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nw, err := d.getNetwork(brName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to getNetwork(%s): %v", brName, err)
|
|
|
|
}
|
|
|
|
|
2016-09-17 01:40:44 -04:00
|
|
|
addrs4, _, err := nw.bridge.addresses()
|
2016-06-27 23:42:50 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get the bridge network's address: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-09-17 01:40:44 -04:00
|
|
|
if !addrs4[0].IP.Equal(ip) {
|
2016-06-27 23:42:50 -04:00
|
|
|
t.Fatal("Creating bridge network with existing bridge interface unexpectedly modified the IP address of the bridge")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.DeleteNetwork(brName); err != nil {
|
|
|
|
t.Fatalf("Failed to delete network %s: %v", brName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := netlink.LinkByName(brName); err != nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("Deleting bridge network that using existing bridge interface unexpectedly deleted the bridge interface")
|
2016-06-27 23:42:50 -04:00
|
|
|
}
|
|
|
|
}
|
2017-09-08 17:48:03 -04:00
|
|
|
|
|
|
|
func TestCreateParallel(t *testing.T) {
|
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
}
|
|
|
|
|
|
|
|
d := newDriver()
|
|
|
|
|
|
|
|
if err := d.configure(nil); err != nil {
|
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan error, 100)
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
go func(name string, ch chan<- error) {
|
|
|
|
config := &networkConfiguration{BridgeName: name}
|
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = config
|
|
|
|
if err := d.CreateNetwork(name, genericOption, nil, getIPv4Data(t, "docker0"), nil); err != nil {
|
|
|
|
ch <- fmt.Errorf("failed to create %s", name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := d.CreateNetwork(name, genericOption, nil, getIPv4Data(t, "docker0"), nil); err == nil {
|
|
|
|
ch <- fmt.Errorf("failed was able to create overlap %s", name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ch <- nil
|
|
|
|
}("net"+strconv.Itoa(i), ch)
|
|
|
|
}
|
|
|
|
// wait for the go routines
|
|
|
|
var success int
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
val := <-ch
|
|
|
|
if val == nil {
|
|
|
|
success++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if success != 1 {
|
|
|
|
t.Fatalf("Success should be 1 instead: %d", success)
|
|
|
|
}
|
|
|
|
}
|