2015-03-20 20:54:49 +00:00
|
|
|
package libnetwork_test
|
|
|
|
|
|
|
|
import (
|
2015-05-06 06:41:20 +00:00
|
|
|
"bytes"
|
2015-05-09 04:50:03 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2015-05-06 06:41:20 +00:00
|
|
|
"io/ioutil"
|
2015-03-20 20:54:49 +00:00
|
|
|
"net"
|
2015-05-16 01:14:36 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-05-02 00:01:21 +00:00
|
|
|
"os"
|
2015-05-09 04:50:03 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
2015-03-20 20:54:49 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-05-16 01:14:36 +00:00
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2015-05-02 00:01:21 +00:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2015-03-20 20:54:49 +00:00
|
|
|
"github.com/docker/libnetwork"
|
2015-05-16 01:14:36 +00:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-05-16 23:02:51 +00:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2015-04-18 04:18:55 +00:00
|
|
|
"github.com/docker/libnetwork/netutils"
|
2015-05-16 23:02:51 +00:00
|
|
|
"github.com/docker/libnetwork/options"
|
2015-05-20 20:28:46 +00:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-05-09 04:50:03 +00:00
|
|
|
"github.com/vishvananda/netns"
|
2015-03-20 20:54:49 +00:00
|
|
|
)
|
|
|
|
|
2015-04-22 23:47:07 +00:00
|
|
|
const (
|
2015-05-02 00:01:21 +00:00
|
|
|
bridgeNetType = "bridge"
|
|
|
|
bridgeName = "docker0"
|
2015-04-22 23:47:07 +00:00
|
|
|
)
|
2015-04-09 20:48:13 +00:00
|
|
|
|
2015-05-02 00:01:21 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if reexec.Init() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
func createTestNetwork(networkType, networkName string, option options.Generic, netOption options.Generic) (libnetwork.Network, error) {
|
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 12:46:29 +00:00
|
|
|
controller, err := libnetwork.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-01 00:57:06 +00:00
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 04:19:57 +00:00
|
|
|
genericOption[netlabel.GenericData] = option
|
2015-04-16 05:01:29 +00: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 12:46:29 +00:00
|
|
|
err = controller.ConfigureNetworkDriver(networkType, genericOption)
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-20 20:54:49 +00:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
network, err := controller.NewNetwork(networkType, networkName,
|
2015-05-06 06:41:20 +00:00
|
|
|
libnetwork.NetworkOptionGeneric(netOption))
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return network, nil
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
func getEmptyGenericOption() map[string]interface{} {
|
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 04:19:57 +00:00
|
|
|
genericOption[netlabel.GenericData] = options.Generic{}
|
2015-05-01 00:57:06 +00:00
|
|
|
return genericOption
|
|
|
|
}
|
|
|
|
|
2015-05-20 20:28:46 +00:00
|
|
|
func getPortMapping() []types.PortBinding {
|
|
|
|
return []types.PortBinding{
|
|
|
|
types.PortBinding{Proto: types.TCP, Port: uint16(230), HostPort: uint16(23000)},
|
|
|
|
types.PortBinding{Proto: types.UDP, Port: uint16(200), HostPort: uint16(22000)},
|
|
|
|
types.PortBinding{Proto: types.TCP, Port: uint16(120), HostPort: uint16(12000)},
|
2015-05-02 00:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 05:58:12 +00:00
|
|
|
func TestNull(t *testing.T) {
|
2015-05-06 05:51:26 +00:00
|
|
|
network, err := createTestNetwork("null", "testnetwork", options.Generic{},
|
|
|
|
options.Generic{})
|
2015-04-30 05:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
ep, err := network.CreateEndpoint("testep")
|
2015-04-30 05:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-03 20:29:43 +00:00
|
|
|
_, err = ep.Join("null_container",
|
2015-04-30 05:58:12 +00:00
|
|
|
libnetwork.JoinOptionHostname("test"),
|
2015-05-03 20:29:43 +00:00
|
|
|
libnetwork.JoinOptionDomainname("docker.io"),
|
|
|
|
libnetwork.JoinOptionExtraHost("web", "192.168.0.1"))
|
2015-04-30 05:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-03 20:29:43 +00:00
|
|
|
err = ep.Leave("null_container")
|
2015-04-30 05:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := network.Delete(); err != nil {
|
2015-05-03 20:37:22 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHost(t *testing.T) {
|
2015-05-06 05:51:26 +00:00
|
|
|
network, err := createTestNetwork("host", "testnetwork", options.Generic{}, options.Generic{})
|
2015-05-03 20:37:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:12:47 +00:00
|
|
|
ep1, err := network.CreateEndpoint("testep1")
|
2015-05-03 20:37:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:12:47 +00:00
|
|
|
_, err = ep1.Join("host_container1",
|
|
|
|
libnetwork.JoinOptionHostname("test1"),
|
2015-05-03 20:37:22 +00:00
|
|
|
libnetwork.JoinOptionDomainname("docker.io"),
|
2015-05-05 04:20:45 +00:00
|
|
|
libnetwork.JoinOptionExtraHost("web", "192.168.0.1"),
|
|
|
|
libnetwork.JoinOptionUseDefaultSandbox())
|
2015-05-03 20:37:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:12:47 +00:00
|
|
|
ep2, err := network.CreateEndpoint("testep2")
|
2015-05-03 20:37:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:12:47 +00:00
|
|
|
_, err = ep2.Join("host_container2",
|
|
|
|
libnetwork.JoinOptionHostname("test2"),
|
|
|
|
libnetwork.JoinOptionDomainname("docker.io"),
|
|
|
|
libnetwork.JoinOptionExtraHost("web", "192.168.0.1"),
|
|
|
|
libnetwork.JoinOptionUseDefaultSandbox())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ep1.Leave("host_container1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ep2.Leave("host_container2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep1.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep2.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to create another host endpoint and join/leave that.
|
|
|
|
ep3, err := network.CreateEndpoint("testep3")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ep3.Join("host_container3",
|
|
|
|
libnetwork.JoinOptionHostname("test3"),
|
|
|
|
libnetwork.JoinOptionDomainname("docker.io"),
|
|
|
|
libnetwork.JoinOptionExtraHost("web", "192.168.0.1"),
|
|
|
|
libnetwork.JoinOptionUseDefaultSandbox())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ep3.Leave("host_container3")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep3.Delete(); err != nil {
|
2015-05-03 20:37:22 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := network.Delete(); err != nil {
|
2015-04-30 05:58:12 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 00:38:05 +00:00
|
|
|
func TestBridge(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
|
|
|
|
2015-03-20 20:54:49 +00:00
|
|
|
ip, subnet, err := net.ParseCIDR("192.168.100.1/24")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
subnet.IP = ip
|
|
|
|
|
|
|
|
ip, cidr, err := net.ParseCIDR("192.168.100.2/28")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cidr.IP = ip
|
|
|
|
|
|
|
|
ip, cidrv6, err := net.ParseCIDR("fe90::1/96")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cidrv6.IP = ip
|
|
|
|
|
2015-04-23 17:49:57 +00:00
|
|
|
log.Debug("Adding a bridge")
|
2015-04-16 05:01:29 +00:00
|
|
|
option := options.Generic{
|
2015-05-06 05:51:26 +00:00
|
|
|
"EnableIPForwarding": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
netOption := options.Generic{
|
2015-04-15 05:25:42 +00:00
|
|
|
"BridgeName": bridgeName,
|
|
|
|
"AddressIPv4": subnet,
|
|
|
|
"FixedCIDR": cidr,
|
|
|
|
"FixedCIDRv6": cidrv6,
|
|
|
|
"EnableIPv6": true,
|
|
|
|
"EnableIPTables": true,
|
|
|
|
"EnableIPMasquerade": true,
|
|
|
|
"EnableICC": true,
|
|
|
|
"AllowNonDefaultBridge": true}
|
2015-03-20 20:54:49 +00:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
network, err := createTestNetwork(bridgeNetType, "testnetwork", option, netOption)
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-02 00:01:21 +00:00
|
|
|
ep, err := network.CreateEndpoint("testep", libnetwork.CreateOptionPortMapping(getPortMapping()))
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
epInfo, err := ep.DriverInfo()
|
2015-05-04 18:49:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-06 04:19:57 +00:00
|
|
|
pmd, ok := epInfo[netlabel.PortMap]
|
2015-05-04 18:49:53 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Could not find expected info in endpoint data")
|
|
|
|
}
|
2015-05-20 20:28:46 +00:00
|
|
|
pm, ok := pmd.([]types.PortBinding)
|
2015-05-04 18:49:53 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Unexpected format for port mapping in endpoint operational data")
|
|
|
|
}
|
|
|
|
if len(pm) != 3 {
|
2015-05-05 20:46:12 +00:00
|
|
|
t.Fatalf("Incomplete data for port mapping in endpoint operational data: %d", len(pm))
|
2015-05-04 18:49:53 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 05:01:29 +00:00
|
|
|
if err := ep.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := network.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnknownDriver(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
2015-04-20 11:23:04 +00:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
_, err := createTestNetwork("unknowndriver", "testnetwork", options.Generic{}, options.Generic{})
|
2015-04-16 05:01:29 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := err.(libnetwork.NetworkTypeError); !ok {
|
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-16 01:14:36 +00:00
|
|
|
func TestNilRemoteDriver(t *testing.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 12:46:29 +00:00
|
|
|
controller, err := libnetwork.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-13 18:40:42 +00: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 12:46:29 +00:00
|
|
|
_, err = controller.NewNetwork("framerelay", "dummy",
|
2015-05-01 00:57:06 +00:00
|
|
|
libnetwork.NetworkOptionGeneric(getEmptyGenericOption()))
|
2015-04-16 05:01:29 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
|
|
|
|
2015-05-16 01:14:36 +00:00
|
|
|
if err != plugins.ErrNotFound {
|
2015-04-16 05:01:29 +00:00
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateNetwork(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
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 12:46:29 +00:00
|
|
|
controller, err := libnetwork.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-16 05:01:29 +00:00
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 04:19:57 +00:00
|
|
|
genericOption[netlabel.GenericData] = options.Generic{}
|
2015-05-01 00:57:06 +00: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 12:46:29 +00:00
|
|
|
err = controller.ConfigureNetworkDriver(bridgeNetType, genericOption)
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
2015-04-30 21:52:46 +00:00
|
|
|
t.Fatal(err)
|
2015-04-16 05:01:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
_, err = controller.NewNetwork(bridgeNetType, "testnetwork",
|
|
|
|
libnetwork.NetworkOptionGeneric(genericOption))
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
2015-04-30 21:52:46 +00:00
|
|
|
t.Fatal(err)
|
2015-04-16 05:01:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 00:01:21 +00:00
|
|
|
_, err = controller.NewNetwork(bridgeNetType, "testnetwork")
|
2015-04-16 05:01:29 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := err.(libnetwork.NetworkNameError); !ok {
|
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkName(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
2015-04-16 05:01:29 +00:00
|
|
|
|
2015-05-08 02:59:06 +00:00
|
|
|
_, err := createTestNetwork(bridgeNetType, "", options.Generic{}, options.Generic{})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
2015-05-11 23:13:27 +00:00
|
|
|
if err != libnetwork.ErrInvalidName {
|
|
|
|
t.Fatal("Expected to fail with ErrInvalidName error")
|
2015-05-08 02:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
networkName := "testnetwork"
|
2015-05-06 05:51:26 +00:00
|
|
|
n, err := createTestNetwork(bridgeNetType, networkName, options.Generic{}, options.Generic{})
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.Name() != networkName {
|
|
|
|
t.Fatalf("Expected network name %s, got %s", networkName, n.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkType(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
|
2015-03-20 20:54:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-02 00:01:21 +00:00
|
|
|
if n.Type() != bridgeNetType {
|
|
|
|
t.Fatalf("Expected network type %s, got %s", bridgeNetType, n.Type())
|
2015-04-16 05:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkID(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
2015-04-16 05:01:29 +00:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.ID() == "" {
|
|
|
|
t.Fatal("Expected non-empty network id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteNetworkWithActiveEndpoints(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
|
|
|
|
2015-04-16 05:01:29 +00:00
|
|
|
option := options.Generic{
|
|
|
|
"BridgeName": bridgeName,
|
|
|
|
"AllowNonDefaultBridge": true}
|
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
network, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, option)
|
2015-04-15 05:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
ep, err := network.CreateEndpoint("testep")
|
2015-04-15 05:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-04-16 05:01:29 +00:00
|
|
|
err = network.Delete()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := err.(*libnetwork.ActiveEndpointsError); !ok {
|
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done testing. Now cleanup.
|
2015-04-15 05:25:42 +00:00
|
|
|
if err := ep.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-03-20 20:54:49 +00:00
|
|
|
if err := network.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 05:01:29 +00:00
|
|
|
|
|
|
|
func TestUnknownNetwork(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
|
|
|
|
2015-04-16 05:01:29 +00:00
|
|
|
option := options.Generic{
|
|
|
|
"BridgeName": bridgeName,
|
|
|
|
"AllowNonDefaultBridge": true}
|
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
network, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, option)
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = network.Delete()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = network.Delete()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := err.(*libnetwork.UnknownNetworkError); !ok {
|
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnknownEndpoint(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
|
|
|
|
2015-04-16 05:01:29 +00:00
|
|
|
ip, subnet, err := net.ParseCIDR("192.168.100.1/24")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
subnet.IP = ip
|
|
|
|
|
|
|
|
option := options.Generic{
|
|
|
|
"BridgeName": bridgeName,
|
|
|
|
"AddressIPv4": subnet,
|
|
|
|
"AllowNonDefaultBridge": true}
|
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
network, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, option)
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-05-08 02:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = network.CreateEndpoint("")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
2015-05-11 23:13:27 +00:00
|
|
|
if err != libnetwork.ErrInvalidName {
|
|
|
|
t.Fatal("Expected to fail with ErrInvalidName error")
|
2015-04-16 05:01:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
ep, err := network.CreateEndpoint("testep")
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ep.Delete()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ep.Delete()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := err.(*libnetwork.UnknownEndpointError); !ok {
|
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done testing. Now cleanup
|
|
|
|
if err := network.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 16:46:11 +00:00
|
|
|
|
|
|
|
func TestNetworkEndpointsWalkers(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
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 12:46:29 +00:00
|
|
|
controller, err := libnetwork.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-20 16:46:11 +00: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 12:46:29 +00:00
|
|
|
err = controller.ConfigureNetworkDriver(bridgeNetType, getEmptyGenericOption())
|
2015-04-20 16:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create network 1 and add 2 endpoint: ep11, ep12
|
2015-05-02 00:01:21 +00:00
|
|
|
net1, err := controller.NewNetwork(bridgeNetType, "network1")
|
2015-04-20 16:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-01 00:57:06 +00:00
|
|
|
ep11, err := net1.CreateEndpoint("ep11")
|
2015-04-20 16:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-01 00:57:06 +00:00
|
|
|
ep12, err := net1.CreateEndpoint("ep12")
|
2015-04-20 16:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test list methods on net1
|
|
|
|
epList1 := net1.Endpoints()
|
|
|
|
if len(epList1) != 2 {
|
|
|
|
t.Fatalf("Endpoints() returned wrong number of elements: %d instead of 2", len(epList1))
|
|
|
|
}
|
|
|
|
// endpoint order is not guaranteed
|
|
|
|
for _, e := range epList1 {
|
|
|
|
if e != ep11 && e != ep12 {
|
|
|
|
t.Fatal("Endpoints() did not return all the expected elements")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Endpoint Walk method
|
|
|
|
var epName string
|
|
|
|
var epWanted libnetwork.Endpoint
|
|
|
|
wlk := func(ep libnetwork.Endpoint) bool {
|
|
|
|
if ep.Name() == epName {
|
|
|
|
epWanted = ep
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for ep1 on network1
|
|
|
|
epName = "ep11"
|
|
|
|
net1.WalkEndpoints(wlk)
|
|
|
|
if epWanted == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if ep11 != epWanted {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Network Walk method
|
|
|
|
var netName string
|
|
|
|
var netWanted libnetwork.Network
|
|
|
|
nwWlk := func(nw libnetwork.Network) bool {
|
|
|
|
if nw.Name() == netName {
|
|
|
|
netWanted = nw
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for network named "network1"
|
|
|
|
netName = "network1"
|
|
|
|
controller.WalkNetworks(nwWlk)
|
|
|
|
if netWanted == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if net1 != netWanted {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 19:05:33 +00:00
|
|
|
|
|
|
|
func TestControllerQuery(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
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 12:46:29 +00:00
|
|
|
controller, err := libnetwork.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-24 19:05:33 +00: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 12:46:29 +00:00
|
|
|
err = controller.ConfigureNetworkDriver(bridgeNetType, getEmptyGenericOption())
|
2015-04-24 19:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create network 1
|
2015-05-02 00:01:21 +00:00
|
|
|
net1, err := controller.NewNetwork(bridgeNetType, "network1")
|
2015-04-24 19:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
_, err = controller.NetworkByName("")
|
|
|
|
if err == nil {
|
2015-04-24 19:05:33 +00:00
|
|
|
t.Fatalf("NetworkByName() succeeded with invalid target name")
|
|
|
|
}
|
2015-05-11 23:13:27 +00:00
|
|
|
if err != libnetwork.ErrInvalidName {
|
|
|
|
t.Fatalf("NetworkByName() failed with unexpected error: %v", err)
|
|
|
|
}
|
2015-04-24 19:05:33 +00:00
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
_, err = controller.NetworkByID("")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("NetworkByID() succeeded with invalid target id")
|
|
|
|
}
|
|
|
|
if err != libnetwork.ErrInvalidID {
|
|
|
|
t.Fatalf("NetworkByID() failed with unexpected error: %v", err)
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
g, err := controller.NetworkByID("network1")
|
2015-05-15 23:04:09 +00:00
|
|
|
if err == nil {
|
2015-05-18 23:49:12 +00:00
|
|
|
t.Fatalf("Unexpected success for NetworkByID(): %v", g)
|
2015-05-11 23:13:27 +00:00
|
|
|
}
|
2015-05-15 23:04:09 +00:00
|
|
|
if err != libnetwork.ErrNoSuchNetwork {
|
|
|
|
t.Fatalf("NetworkByID() failed with unexpected error: %v", err)
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
g, err = controller.NetworkByName("network1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected failure for NetworkByName(): %v", err)
|
|
|
|
}
|
2015-04-24 19:05:33 +00:00
|
|
|
if g == nil {
|
|
|
|
t.Fatalf("NetworkByName() did not find the network")
|
|
|
|
}
|
2015-05-11 23:13:27 +00:00
|
|
|
|
2015-04-24 19:05:33 +00:00
|
|
|
if g != net1 {
|
|
|
|
t.Fatalf("NetworkByName() returned the wrong network")
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
g, err = controller.NetworkByID(net1.ID())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected failure for NetworkByID(): %v", err)
|
|
|
|
}
|
2015-04-24 19:05:33 +00:00
|
|
|
if net1 != g {
|
|
|
|
t.Fatalf("NetworkByID() returned unexpected element: %v", g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkQuery(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
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 12:46:29 +00:00
|
|
|
controller, err := libnetwork.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-24 19:05:33 +00: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 12:46:29 +00:00
|
|
|
err = controller.ConfigureNetworkDriver(bridgeNetType, getEmptyGenericOption())
|
2015-04-24 19:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create network 1 and add 2 endpoint: ep11, ep12
|
2015-05-02 00:01:21 +00:00
|
|
|
net1, err := controller.NewNetwork(bridgeNetType, "network1")
|
2015-04-24 19:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-01 00:57:06 +00:00
|
|
|
ep11, err := net1.CreateEndpoint("ep11")
|
2015-04-24 19:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-01 00:57:06 +00:00
|
|
|
ep12, err := net1.CreateEndpoint("ep12")
|
2015-04-24 19:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
e, err := net1.EndpointByName("ep11")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-24 19:05:33 +00:00
|
|
|
if ep11 != e {
|
|
|
|
t.Fatalf("EndpointByName() returned %v instead of %v", e, ep11)
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
e, err = net1.EndpointByName("")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("EndpointByName() succeeded with invalid target name")
|
|
|
|
}
|
|
|
|
if err != libnetwork.ErrInvalidName {
|
|
|
|
t.Fatalf("EndpointByName() failed with unexpected error: %v", err)
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
e, err = net1.EndpointByName("IamNotAnEndpoint")
|
2015-05-15 23:04:09 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("EndpointByName() succeeded with unknown target name")
|
|
|
|
}
|
|
|
|
if err != libnetwork.ErrNoSuchEndpoint {
|
2015-05-11 23:13:27 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-24 19:05:33 +00:00
|
|
|
if e != nil {
|
|
|
|
t.Fatalf("EndpointByName(): expected nil, got %v", e)
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
e, err = net1.EndpointByID(ep12.ID())
|
2015-05-15 23:04:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-24 19:05:33 +00:00
|
|
|
if ep12 != e {
|
|
|
|
t.Fatalf("EndpointByID() returned %v instead of %v", e, ep12)
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
e, err = net1.EndpointByID("")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("EndpointByID() succeeded with invalid target id")
|
|
|
|
}
|
|
|
|
if err != libnetwork.ErrInvalidID {
|
|
|
|
t.Fatalf("EndpointByID() failed with unexpected error: %v", err)
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-28 05:57:36 +00:00
|
|
|
|
|
|
|
const containerID = "valid_container"
|
|
|
|
|
|
|
|
func TestEndpointJoin(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
2015-04-28 05:57:36 +00:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
ep, err := n.CreateEndpoint("ep1")
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
// Validate if ep.Info() only gives me IP address info and not names and gateway during CreateEndpoint()
|
|
|
|
info := ep.Info()
|
|
|
|
|
|
|
|
for _, iface := range info.InterfaceList() {
|
|
|
|
if iface.Address().IP.To4() == nil {
|
|
|
|
t.Fatalf("Invalid IP address returned: %v", iface.Address())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Gateway().To4() != nil {
|
|
|
|
t.Fatalf("Expected empty gateway for an empty endpoint. Instead found a gateway: %v", info.Gateway())
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.SandboxKey() != "" {
|
|
|
|
t.Fatalf("Expected an empty sandbox key for an empty endpoint. Instead found a non-empty sandbox key: %s", info.SandboxKey())
|
|
|
|
}
|
|
|
|
|
2015-04-30 05:58:12 +00:00
|
|
|
_, err = ep.Join(containerID,
|
|
|
|
libnetwork.JoinOptionHostname("test"),
|
2015-05-03 20:29:43 +00:00
|
|
|
libnetwork.JoinOptionDomainname("docker.io"),
|
|
|
|
libnetwork.JoinOptionExtraHost("web", "192.168.0.1"))
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-07 20:03:47 +00:00
|
|
|
defer func() {
|
|
|
|
err = ep.Leave(containerID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-14 06:23:45 +00:00
|
|
|
|
|
|
|
// Validate if ep.Info() only gives valid gateway and sandbox key after has container has joined.
|
|
|
|
info = ep.Info()
|
|
|
|
if info.Gateway().To4() == nil {
|
|
|
|
t.Fatalf("Expected a valid gateway for a joined endpoint. Instead found an invalid gateway: %v", info.Gateway())
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.SandboxKey() == "" {
|
|
|
|
t.Fatalf("Expected an non-empty sandbox key for a joined endpoint. Instead found a empty sandbox key")
|
|
|
|
}
|
2015-04-28 05:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEndpointJoinInvalidContainerId(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
2015-04-28 05:57:36 +00:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
ep, err := n.CreateEndpoint("ep1")
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ep.Join("")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail join with empty container id string")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := err.(libnetwork.InvalidContainerIDError); !ok {
|
|
|
|
t.Fatalf("Failed for unexpected reason: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 20:02:40 +00:00
|
|
|
func TestEndpointDeleteWithActiveContainer(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
2015-05-06 20:02:40 +00:00
|
|
|
|
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ep, err := n.CreateEndpoint("ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ep.Join(containerID,
|
|
|
|
libnetwork.JoinOptionHostname("test"),
|
|
|
|
libnetwork.JoinOptionDomainname("docker.io"),
|
|
|
|
libnetwork.JoinOptionExtraHost("web", "192.168.0.1"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-07 20:03:47 +00:00
|
|
|
defer func() {
|
|
|
|
err = ep.Leave(containerID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ep.Delete()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-06 20:02:40 +00:00
|
|
|
|
|
|
|
err = ep.Delete()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := err.(*libnetwork.ActiveContainerError); !ok {
|
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 05:57:36 +00:00
|
|
|
func TestEndpointMultipleJoins(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
2015-04-28 05:57:36 +00:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
ep, err := n.CreateEndpoint("ep1")
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-04-30 05:58:12 +00:00
|
|
|
_, err = ep.Join(containerID,
|
|
|
|
libnetwork.JoinOptionHostname("test"),
|
2015-05-03 20:29:43 +00:00
|
|
|
libnetwork.JoinOptionDomainname("docker.io"),
|
|
|
|
libnetwork.JoinOptionExtraHost("web", "192.168.0.1"))
|
2015-04-30 05:58:12 +00:00
|
|
|
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-07 20:03:47 +00:00
|
|
|
defer func() {
|
|
|
|
err = ep.Leave(containerID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 05:57:36 +00:00
|
|
|
|
|
|
|
_, err = ep.Join("container2")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail multiple joins for the same endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != libnetwork.ErrInvalidJoin {
|
|
|
|
t.Fatalf("Failed for unexpected reason: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEndpointInvalidLeave(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
2015-04-28 05:57:36 +00:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
ep, err := n.CreateEndpoint("ep1")
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ep.Leave(containerID)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail leave from an endpoint which has no active join")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := err.(libnetwork.InvalidContainerIDError); !ok {
|
2015-05-09 04:50:03 +00:00
|
|
|
if err != libnetwork.ErrNoContainer {
|
|
|
|
t.Fatalf("Failed for unexpected reason: %v", err)
|
|
|
|
}
|
2015-04-28 05:57:36 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 05:58:12 +00:00
|
|
|
_, err = ep.Join(containerID,
|
|
|
|
libnetwork.JoinOptionHostname("test"),
|
2015-05-03 20:29:43 +00:00
|
|
|
libnetwork.JoinOptionDomainname("docker.io"),
|
|
|
|
libnetwork.JoinOptionExtraHost("web", "192.168.0.1"))
|
2015-04-30 05:58:12 +00:00
|
|
|
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-07 20:03:47 +00:00
|
|
|
defer func() {
|
|
|
|
err = ep.Leave(containerID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 05:57:36 +00:00
|
|
|
|
|
|
|
err = ep.Leave("")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail leave with empty container id")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := err.(libnetwork.InvalidContainerIDError); !ok {
|
|
|
|
t.Fatalf("Failed for unexpected reason: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ep.Leave("container2")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail leave with wrong container id")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := err.(libnetwork.InvalidContainerIDError); !ok {
|
|
|
|
t.Fatalf("Failed for unexpected reason: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-05-03 20:29:43 +00:00
|
|
|
|
|
|
|
func TestEndpointUpdateParent(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
2015-05-03 20:29:43 +00:00
|
|
|
|
2015-05-06 05:51:26 +00:00
|
|
|
n, err := createTestNetwork("bridge", "testnetwork", options.Generic{}, options.Generic{})
|
2015-05-03 20:29:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ep1, err := n.CreateEndpoint("ep1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ep1.Join(containerID,
|
|
|
|
libnetwork.JoinOptionHostname("test1"),
|
|
|
|
libnetwork.JoinOptionDomainname("docker.io"),
|
|
|
|
libnetwork.JoinOptionExtraHost("web", "192.168.0.1"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-07 20:03:47 +00:00
|
|
|
defer func() {
|
|
|
|
err = ep1.Leave(containerID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-03 20:29:43 +00:00
|
|
|
|
|
|
|
ep2, err := n.CreateEndpoint("ep2", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ep2.Join("container2",
|
|
|
|
libnetwork.JoinOptionHostname("test2"),
|
|
|
|
libnetwork.JoinOptionDomainname("docker.io"),
|
|
|
|
libnetwork.JoinOptionHostsPath("/var/lib/docker/test_network/container2/hosts"),
|
|
|
|
libnetwork.JoinOptionParentUpdate(ep1.ID(), "web", "192.168.0.2"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-07 20:03:47 +00:00
|
|
|
defer func() {
|
|
|
|
err = ep2.Leave("container2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-03 20:29:43 +00:00
|
|
|
|
|
|
|
}
|
2015-05-06 06:41:20 +00:00
|
|
|
|
|
|
|
func TestEnableIPv6(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
2015-05-06 06:41:20 +00:00
|
|
|
|
|
|
|
tmpResolvConf := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\nnameserver 2001:4860:4860::8888")
|
|
|
|
//take a copy of resolv.conf for restoring after test completes
|
|
|
|
resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
//cleanup
|
|
|
|
defer func() {
|
|
|
|
if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-05-12 23:39:30 +00:00
|
|
|
ip, cidrv6, err := net.ParseCIDR("fe80::1/64")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cidrv6.IP = ip
|
|
|
|
|
2015-05-06 06:41:20 +00:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.EnableIPv6: true,
|
2015-05-12 23:39:30 +00:00
|
|
|
netlabel.GenericData: options.Generic{
|
|
|
|
"FixedCIDRv6": cidrv6,
|
|
|
|
},
|
2015-05-06 06:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err := createTestNetwork("bridge", "testnetwork", options.Generic{}, netOption)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ep1, err := n.CreateEndpoint("ep1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resolvConfPath := "/tmp/libnetwork_test/resolv.conf"
|
2015-05-16 12:13:17 +00:00
|
|
|
defer os.Remove(resolvConfPath)
|
2015-05-06 06:41:20 +00:00
|
|
|
|
|
|
|
_, err = ep1.Join(containerID,
|
|
|
|
libnetwork.JoinOptionResolvConfPath(resolvConfPath))
|
|
|
|
|
2015-05-07 20:03:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = ep1.Leave(containerID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-05-06 06:41:20 +00:00
|
|
|
content, err := ioutil.ReadFile(resolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(content, tmpResolvConf) {
|
|
|
|
t.Fatalf("Expected %s, Got %s", string(tmpResolvConf), string(content))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:01:53 +00:00
|
|
|
func TestResolvConf(t *testing.T) {
|
2015-05-12 23:39:30 +00:00
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
defer netutils.SetupTestNetNS(t)()
|
|
|
|
}
|
2015-05-06 06:41:20 +00:00
|
|
|
|
2015-05-15 21:01:53 +00:00
|
|
|
tmpResolvConf1 := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\nnameserver 2001:4860:4860::8888")
|
|
|
|
expectedResolvConf1 := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\n")
|
|
|
|
tmpResolvConf2 := []byte("search pommesfrites.fr\nnameserver 112.34.56.78\nnameserver 2001:4860:4860::8888")
|
|
|
|
expectedResolvConf2 := []byte("search pommesfrites.fr\nnameserver 112.34.56.78\n")
|
|
|
|
tmpResolvConf3 := []byte("search pommesfrites.fr\nnameserver 113.34.56.78\n")
|
|
|
|
|
2015-05-06 06:41:20 +00:00
|
|
|
//take a copy of resolv.conf for restoring after test completes
|
|
|
|
resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
//cleanup
|
|
|
|
defer func() {
|
|
|
|
if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
n, err := createTestNetwork("bridge", "testnetwork", options.Generic{}, options.Generic{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ep1, err := n.CreateEndpoint("ep1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:01:53 +00:00
|
|
|
if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf1, 0644); err != nil {
|
2015-05-06 06:41:20 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resolvConfPath := "/tmp/libnetwork_test/resolv.conf"
|
2015-05-16 12:13:17 +00:00
|
|
|
defer os.Remove(resolvConfPath)
|
2015-05-06 06:41:20 +00:00
|
|
|
|
|
|
|
_, err = ep1.Join(containerID,
|
|
|
|
libnetwork.JoinOptionResolvConfPath(resolvConfPath))
|
2015-05-07 20:03:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = ep1.Leave(containerID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-06 06:41:20 +00:00
|
|
|
|
2015-05-20 05:40:23 +00:00
|
|
|
finfo, err := os.Stat(resolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmode := (os.FileMode)(0644)
|
|
|
|
if finfo.Mode() != fmode {
|
|
|
|
t.Fatalf("Expected file mode %s, got %s", fmode.String(), finfo.Mode().String())
|
|
|
|
}
|
|
|
|
|
2015-05-06 06:41:20 +00:00
|
|
|
content, err := ioutil.ReadFile(resolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:01:53 +00:00
|
|
|
if !bytes.Equal(content, expectedResolvConf1) {
|
|
|
|
t.Fatalf("Expected %s, Got %s", string(expectedResolvConf1), string(content))
|
2015-05-06 06:41:20 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 21:01:53 +00:00
|
|
|
err = ep1.Leave(containerID)
|
2015-05-06 06:41:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-15 21:01:53 +00:00
|
|
|
|
|
|
|
if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf2, 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ep1.Join(containerID,
|
|
|
|
libnetwork.JoinOptionResolvConfPath(resolvConfPath))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err = ioutil.ReadFile(resolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(content, expectedResolvConf2) {
|
|
|
|
t.Fatalf("Expected %s, Got %s", string(expectedResolvConf2), string(content))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(resolvConfPath, tmpResolvConf3, 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ep1.Leave(containerID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ep1.Join(containerID,
|
|
|
|
libnetwork.JoinOptionResolvConfPath(resolvConfPath))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err = ioutil.ReadFile(resolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(content, tmpResolvConf3) {
|
|
|
|
t.Fatalf("Expected %s, Got %s", string(tmpResolvConf3), string(content))
|
|
|
|
}
|
2015-05-06 06:41:20 +00:00
|
|
|
}
|
2015-05-09 04:50:03 +00:00
|
|
|
|
2015-05-16 01:14:36 +00:00
|
|
|
func TestInvalidRemoteDriver(t *testing.T) {
|
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
t.Skip("Skipping test when not running inside a Container")
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
if server == nil {
|
|
|
|
t.Fatal("Failed to start a HTTP Server")
|
|
|
|
}
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
type pluginRequest struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
fmt.Fprintln(w, `{"Implements": ["InvalidDriver"]}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := os.MkdirAll("/usr/share/docker/plugins", 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll("/usr/share/docker/plugins"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile("/usr/share/docker/plugins/invalid-network-driver.spec", []byte(server.URL), 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
controller, err := libnetwork.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = controller.NewNetwork("invalid-network-driver", "dummy",
|
|
|
|
libnetwork.NetworkOptionGeneric(getEmptyGenericOption()))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != plugins.ErrNotImplements {
|
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidRemoteDriver(t *testing.T) {
|
|
|
|
if !netutils.IsRunningInContainer() {
|
|
|
|
t.Skip("Skipping test when not running inside a Container")
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
if server == nil {
|
|
|
|
t.Fatal("Failed to start a HTTP Server")
|
|
|
|
}
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
type pluginRequest struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
fmt.Fprintf(w, `{"Implements": ["%s"]}`, driverapi.NetworkPluginEndpointType)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := os.MkdirAll("/usr/share/docker/plugins", 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll("/usr/share/docker/plugins"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile("/usr/share/docker/plugins/valid-network-driver.spec", []byte(server.URL), 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
controller, err := libnetwork.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = controller.NewNetwork("valid-network-driver", "dummy",
|
|
|
|
libnetwork.NetworkOptionGeneric(getEmptyGenericOption()))
|
|
|
|
if err != nil && err != driverapi.ErrNotImplemented {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
var (
|
|
|
|
once sync.Once
|
|
|
|
ctrlr libnetwork.NetworkController
|
|
|
|
start = make(chan struct{})
|
|
|
|
done = make(chan chan struct{}, numThreads-1)
|
|
|
|
origns = netns.None()
|
|
|
|
testns = netns.None()
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
iterCnt = 25
|
|
|
|
numThreads = 3
|
|
|
|
first = 1
|
|
|
|
last = numThreads
|
|
|
|
debug = false
|
|
|
|
)
|
|
|
|
|
|
|
|
func createGlobalInstance(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
defer close(start)
|
|
|
|
|
|
|
|
origns, err = netns.Get()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-12 23:39:30 +00:00
|
|
|
if netutils.IsRunningInContainer() {
|
|
|
|
testns = origns
|
|
|
|
} else {
|
|
|
|
testns, err = netns.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-09 04:50:03 +00: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 12:46:29 +00:00
|
|
|
ctrlr, err = libnetwork.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-09 04:50:03 +00:00
|
|
|
|
|
|
|
err = ctrlr.ConfigureNetworkDriver(bridgeNetType, getEmptyGenericOption())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("configure driver")
|
|
|
|
}
|
|
|
|
|
|
|
|
net, err := ctrlr.NewNetwork(bridgeNetType, "network1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("new network")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = net.CreateEndpoint("ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("createendpoint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func debugf(format string, a ...interface{}) (int, error) {
|
|
|
|
if debug {
|
|
|
|
return fmt.Printf(format, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parallelJoin(t *testing.T, ep libnetwork.Endpoint, thrNumber int) {
|
|
|
|
debugf("J%d.", thrNumber)
|
|
|
|
_, err := ep.Join("racing_container")
|
|
|
|
runtime.LockOSThread()
|
|
|
|
if err != nil {
|
|
|
|
if err != libnetwork.ErrNoContainer && err != libnetwork.ErrInvalidJoin {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
debugf("JE%d(%v).", thrNumber, err)
|
|
|
|
}
|
|
|
|
debugf("JD%d.", thrNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parallelLeave(t *testing.T, ep libnetwork.Endpoint, thrNumber int) {
|
|
|
|
debugf("L%d.", thrNumber)
|
|
|
|
err := ep.Leave("racing_container")
|
|
|
|
runtime.LockOSThread()
|
|
|
|
if err != nil {
|
|
|
|
if err != libnetwork.ErrNoContainer && err != libnetwork.ErrInvalidJoin {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
debugf("LE%d(%v).", thrNumber, err)
|
|
|
|
}
|
|
|
|
debugf("LD%d.", thrNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runParallelTests(t *testing.T, thrNumber int) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
pTest := flag.Lookup("test.parallel")
|
|
|
|
if pTest == nil {
|
|
|
|
t.Skip("Skipped because test.parallel flag not set;")
|
|
|
|
}
|
|
|
|
numParallel, err := strconv.Atoi(pTest.Value.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if numParallel < numThreads {
|
|
|
|
t.Skip("Skipped because t.parallel was less than ", numThreads)
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
if thrNumber == first {
|
|
|
|
createGlobalInstance(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
if thrNumber != first {
|
|
|
|
select {
|
|
|
|
case <-start:
|
|
|
|
}
|
|
|
|
|
|
|
|
thrdone := make(chan struct{})
|
|
|
|
done <- thrdone
|
|
|
|
defer close(thrdone)
|
|
|
|
|
|
|
|
if thrNumber == last {
|
|
|
|
defer close(done)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = netns.Set(testns)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer netns.Set(origns)
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
net, err := ctrlr.NetworkByName("network1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-09 04:50:03 +00:00
|
|
|
if net == nil {
|
|
|
|
t.Fatal("Could not find network1")
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
ep, err := net.EndpointByName("ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-09 04:50:03 +00:00
|
|
|
if ep == nil {
|
2015-05-15 23:04:09 +00:00
|
|
|
t.Fatal("Got nil ep with no error")
|
2015-05-09 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < iterCnt; i++ {
|
|
|
|
parallelJoin(t, ep, thrNumber)
|
|
|
|
parallelLeave(t, ep, thrNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
debugf("\n")
|
|
|
|
|
|
|
|
if thrNumber == first {
|
|
|
|
for thrdone := range done {
|
|
|
|
select {
|
|
|
|
case <-thrdone:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testns.Close()
|
|
|
|
err = ep.Delete()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParallel1(t *testing.T) {
|
|
|
|
runParallelTests(t, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParallel2(t *testing.T) {
|
|
|
|
runParallelTests(t, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParallel3(t *testing.T) {
|
|
|
|
runParallelTests(t, 3)
|
|
|
|
}
|