2015-03-20 16:54:49 -04:00
|
|
|
package libnetwork_test
|
|
|
|
|
|
|
|
import (
|
2015-05-06 02:41:20 -04:00
|
|
|
"bytes"
|
2015-09-09 17:24:05 -04:00
|
|
|
"encoding/json"
|
2015-05-09 00:50:03 -04:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2015-05-06 02:41:20 -04:00
|
|
|
"io/ioutil"
|
2015-05-15 21:14:36 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-05-01 20:01:21 -04:00
|
|
|
"os"
|
2015-09-09 17:24:05 -04:00
|
|
|
"os/exec"
|
2015-05-09 00:50:03 -04:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2015-09-09 17:24:05 -04:00
|
|
|
"strings"
|
2015-05-09 00:50:03 -04:00
|
|
|
"sync"
|
2015-03-20 16:54:49 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-05-15 21:14:36 -04:00
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2015-05-01 20:01:21 -04:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2015-03-20 16:54:49 -04:00
|
|
|
"github.com/docker/libnetwork"
|
2015-09-18 17:00:36 -04:00
|
|
|
"github.com/docker/libnetwork/config"
|
2015-05-23 00:52:02 -04:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-05-15 21:14:36 -04:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-10-05 17:53:25 -04:00
|
|
|
"github.com/docker/libnetwork/ipamapi"
|
2015-05-16 19:02:51 -04:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/options"
|
2015-07-02 01:00:48 -04:00
|
|
|
"github.com/docker/libnetwork/osl"
|
2015-09-07 13:33:28 -04:00
|
|
|
"github.com/docker/libnetwork/testutils"
|
2015-05-20 16:28:46 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-09-09 17:24:05 -04:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
2015-05-21 14:04:49 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
2015-05-09 00:50:03 -04:00
|
|
|
"github.com/vishvananda/netns"
|
2015-03-20 16:54:49 -04:00
|
|
|
)
|
|
|
|
|
2015-04-22 19:47:07 -04:00
|
|
|
const (
|
2015-05-01 20:01:21 -04:00
|
|
|
bridgeNetType = "bridge"
|
2015-04-22 19:47:07 -04:00
|
|
|
)
|
2015-04-09 16:48:13 -04:00
|
|
|
|
2015-05-25 00:27:13 -04:00
|
|
|
var controller libnetwork.NetworkController
|
|
|
|
|
2015-05-01 20:01:21 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if reexec.Init() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-25 00:27:13 -04:00
|
|
|
if err := createController(); err != nil {
|
2015-09-16 07:42:35 -04:00
|
|
|
log.Errorf("Error creating controller: %v", err)
|
2015-05-25 00:27:13 -04:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
//libnetwork.SetTestDataStore(controller, datastore.NewCustomDataStore(datastore.NewMockStore()))
|
2015-05-25 00:27:13 -04:00
|
|
|
|
2015-09-22 17:07:23 -04:00
|
|
|
x := m.Run()
|
|
|
|
controller.Stop()
|
|
|
|
os.Exit(x)
|
2015-05-23 00:52:02 -04:00
|
|
|
}
|
|
|
|
|
2015-05-25 00:27:13 -04:00
|
|
|
func createController() error {
|
|
|
|
var err error
|
2015-04-16 01:01:29 -04:00
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
// Cleanup local datastore file
|
|
|
|
os.Remove(datastore.DefaultScopes("")[datastore.LocalScope].Client.Address)
|
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
option := options.Generic{
|
|
|
|
"EnableIPForwarding": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = option
|
|
|
|
|
2015-09-16 07:42:35 -04:00
|
|
|
cfgOptions, err := libnetwork.OptionBoltdbWithRandomDBFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
controller, err = libnetwork.New(append(cfgOptions, config.OptionDriverConfig(bridgeNetType, genericOption))...)
|
2015-04-16 01:01:29 -04:00
|
|
|
if err != nil {
|
2015-05-25 00:27:13 -04:00
|
|
|
return err
|
2015-04-16 01:01:29 -04:00
|
|
|
}
|
2015-03-20 16:54:49 -04:00
|
|
|
|
2015-05-25 00:27:13 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
func createTestNetwork(networkType, networkName string, netOption options.Generic, ipamV4Configs, ipamV6Configs []*libnetwork.IpamConf) (libnetwork.Network, error) {
|
|
|
|
return controller.NewNetwork(networkType, networkName,
|
|
|
|
libnetwork.NetworkOptionGeneric(netOption),
|
|
|
|
libnetwork.NetworkOptionIpam(ipamapi.DefaultIPAM, "", ipamV4Configs, ipamV6Configs))
|
2015-04-16 01:01:29 -04:00
|
|
|
}
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
func getEmptyGenericOption() map[string]interface{} {
|
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 00:19:57 -04:00
|
|
|
genericOption[netlabel.GenericData] = options.Generic{}
|
2015-04-30 20:57:06 -04:00
|
|
|
return genericOption
|
|
|
|
}
|
|
|
|
|
2015-05-20 16:28:46 -04:00
|
|
|
func getPortMapping() []types.PortBinding {
|
|
|
|
return []types.PortBinding{
|
2015-06-11 21:19:42 -04: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)},
|
|
|
|
{Proto: types.TCP, Port: uint16(320), HostPort: uint16(32000), HostPortEnd: uint16(32999)},
|
|
|
|
{Proto: types.UDP, Port: uint16(420), HostPort: uint16(42000), HostPortEnd: uint16(42001)},
|
2015-05-01 20:01:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:58:12 -04:00
|
|
|
func TestNull(t *testing.T) {
|
2015-07-02 01:00:48 -04:00
|
|
|
cnt, err := controller.NewSandbox("null_container",
|
|
|
|
libnetwork.OptionHostname("test"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"),
|
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
network, err := createTestNetwork("null", "testnull", options.Generic{}, nil, nil)
|
2015-04-30 01:58:12 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
ep, err := network.CreateEndpoint("testep")
|
2015-04-30 01:58:12 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep.Join(cnt)
|
2015-04-30 01:58:12 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep.Leave(cnt)
|
2015-04-30 01:58:12 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := cnt.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
// host type is special network. Cannot be removed.
|
|
|
|
err = network.Delete()
|
|
|
|
if err == nil {
|
2015-05-03 16:37:22 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-08 11:24:43 -04:00
|
|
|
if _, ok := err.(types.ForbiddenError); !ok {
|
|
|
|
t.Fatalf("Unexpected error type")
|
|
|
|
}
|
2015-05-03 16:37:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHost(t *testing.T) {
|
2015-07-02 01:00:48 -04:00
|
|
|
sbx1, err := controller.NewSandbox("host_c1",
|
|
|
|
libnetwork.OptionHostname("test1"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"),
|
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"),
|
|
|
|
libnetwork.OptionUseDefaultSandbox())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sbx1.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
sbx2, err := controller.NewSandbox("host_c2",
|
|
|
|
libnetwork.OptionHostname("test2"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"),
|
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"),
|
|
|
|
libnetwork.OptionUseDefaultSandbox())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sbx2.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
network, err := createTestNetwork("host", "testhost", options.Generic{}, nil, nil)
|
2015-05-03 16:37:22 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-19 12:12:47 -04:00
|
|
|
ep1, err := network.CreateEndpoint("testep1")
|
2015-05-03 16:37:22 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := ep1.Join(sbx1); err != nil {
|
2015-05-03 16:37:22 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-19 12:12:47 -04:00
|
|
|
ep2, err := network.CreateEndpoint("testep2")
|
2015-05-03 16:37:22 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := ep2.Join(sbx2); err != nil {
|
2015-05-19 12:12:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := ep1.Leave(sbx1); err != nil {
|
2015-05-19 12:12:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := ep2.Leave(sbx2); err != nil {
|
2015-05-19 12:12:47 -04:00
|
|
|
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.
|
2015-07-02 01:00:48 -04:00
|
|
|
cnt3, err := controller.NewSandbox("host_c3",
|
|
|
|
libnetwork.OptionHostname("test3"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"),
|
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"),
|
|
|
|
libnetwork.OptionUseDefaultSandbox())
|
2015-05-19 12:12:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
defer func() {
|
|
|
|
if err := cnt3.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-19 12:12:47 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
ep3, err := network.CreateEndpoint("testep3")
|
2015-05-19 12:12:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := ep3.Join(sbx2); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep3.Leave(sbx2); err != nil {
|
2015-05-19 12:12:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep3.Delete(); err != nil {
|
2015-05-03 16:37:22 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
// host type is special network. Cannot be removed.
|
|
|
|
err = network.Delete()
|
|
|
|
if err == nil {
|
2015-04-30 01:58:12 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-08 11:24:43 -04:00
|
|
|
if _, ok := err.(types.ForbiddenError); !ok {
|
|
|
|
t.Fatalf("Unexpected error type")
|
|
|
|
}
|
2015-04-30 01:58:12 -04:00
|
|
|
}
|
|
|
|
|
2015-04-23 20:38:05 -04:00
|
|
|
func TestBridge(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
|
|
|
|
2015-05-06 01:51:26 -04:00
|
|
|
netOption := options.Generic{
|
2015-05-17 03:33:08 -04:00
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork",
|
|
|
|
"EnableIPv6": true,
|
|
|
|
"EnableICC": true,
|
|
|
|
"EnableIPMasquerade": true,
|
2015-05-17 03:33:08 -04:00
|
|
|
},
|
|
|
|
}
|
2015-10-05 17:53:25 -04:00
|
|
|
ipamV4ConfList := []*libnetwork.IpamConf{&libnetwork.IpamConf{PreferredPool: "192.168.100.0/24", Gateway: "192.168.100.1"}}
|
|
|
|
ipamV6ConfList := []*libnetwork.IpamConf{&libnetwork.IpamConf{PreferredPool: "fe90::/98", Gateway: "fe90::22"}}
|
2015-03-20 16:54:49 -04:00
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
network, err := createTestNetwork(bridgeNetType, "testnetwork", netOption, ipamV4ConfList, ipamV6ConfList)
|
2015-04-16 01:01:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-01 20:01:21 -04:00
|
|
|
ep, err := network.CreateEndpoint("testep", libnetwork.CreateOptionPortMapping(getPortMapping()))
|
2015-04-16 01:01:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-14 02:23:45 -04:00
|
|
|
epInfo, err := ep.DriverInfo()
|
2015-05-04 14:49:53 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-06 00:19:57 -04:00
|
|
|
pmd, ok := epInfo[netlabel.PortMap]
|
2015-05-04 14:49:53 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Could not find expected info in endpoint data")
|
|
|
|
}
|
2015-05-20 16:28:46 -04:00
|
|
|
pm, ok := pmd.([]types.PortBinding)
|
2015-05-04 14:49:53 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Unexpected format for port mapping in endpoint operational data")
|
|
|
|
}
|
2015-06-11 21:19:42 -04:00
|
|
|
if len(pm) != 5 {
|
2015-05-05 16:46:12 -04:00
|
|
|
t.Fatalf("Incomplete data for port mapping in endpoint operational data: %d", len(pm))
|
2015-05-04 14:49:53 -04:00
|
|
|
}
|
|
|
|
|
2015-04-16 01:01:29 -04: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-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
2015-04-20 07:23:04 -04:00
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
_, err := createTestNetwork("unknowndriver", "testnetwork", options.Generic{}, nil, nil)
|
2015-04-16 01:01:29 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
|
|
|
|
2015-05-25 00:27:13 -04:00
|
|
|
if _, ok := err.(types.NotFoundError); !ok {
|
2015-04-16 01:01:29 -04:00
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:14:36 -04:00
|
|
|
func TestNilRemoteDriver(t *testing.T) {
|
2015-05-25 00:27:13 -04:00
|
|
|
_, err := controller.NewNetwork("framerelay", "dummy",
|
2015-04-30 20:57:06 -04:00
|
|
|
libnetwork.NetworkOptionGeneric(getEmptyGenericOption()))
|
2015-04-16 01:01:29 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
|
|
|
|
2015-05-14 17:56:15 -04:00
|
|
|
if _, ok := err.(types.NotFoundError); !ok {
|
2015-04-16 01:01:29 -04:00
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkName(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
2015-04-16 01:01:29 -04:00
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
_, err := createTestNetwork(bridgeNetType, "", netOption, nil, nil)
|
2015-05-07 22:59:06 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
2015-05-14 17:56:15 -04:00
|
|
|
|
|
|
|
if _, ok := err.(libnetwork.ErrInvalidName); !ok {
|
|
|
|
t.Fatalf("Expected to fail with ErrInvalidName error. Got %v", err)
|
2015-05-07 22:59:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
networkName := "testnetwork"
|
2015-10-05 17:53:25 -04:00
|
|
|
n, err := createTestNetwork(bridgeNetType, networkName, netOption, nil, nil)
|
2015-04-16 01:01:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-16 01:01:29 -04:00
|
|
|
|
|
|
|
if n.Name() != networkName {
|
|
|
|
t.Fatalf("Expected network name %s, got %s", networkName, n.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkType(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", netOption, nil, nil)
|
2015-03-20 16:54:49 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-03-20 16:54:49 -04:00
|
|
|
|
2015-05-01 20:01:21 -04:00
|
|
|
if n.Type() != bridgeNetType {
|
|
|
|
t.Fatalf("Expected network type %s, got %s", bridgeNetType, n.Type())
|
2015-04-16 01:01:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkID(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
2015-04-16 01:01:29 -04:00
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", netOption, nil, nil)
|
2015-04-16 01:01:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-16 01:01:29 -04:00
|
|
|
|
|
|
|
if n.ID() == "" {
|
|
|
|
t.Fatal("Expected non-empty network id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteNetworkWithActiveEndpoints(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
|
|
|
|
2015-05-25 00:27:13 -04:00
|
|
|
netOption := options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork",
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
option := options.Generic{
|
|
|
|
netlabel.GenericData: netOption,
|
|
|
|
}
|
2015-04-16 01:01:29 -04:00
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
network, err := createTestNetwork(bridgeNetType, "testnetwork", option, nil, nil)
|
2015-04-15 01:25:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
ep, err := network.CreateEndpoint("testep")
|
2015-04-15 01:25:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-04-16 01:01:29 -04: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 01:25:42 -04:00
|
|
|
if err := ep.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-03-20 16:54:49 -04:00
|
|
|
if err := network.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 01:01:29 -04:00
|
|
|
|
|
|
|
func TestUnknownNetwork(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
|
|
|
|
2015-05-25 00:27:13 -04:00
|
|
|
netOption := options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork",
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
option := options.Generic{
|
|
|
|
netlabel.GenericData: netOption,
|
|
|
|
}
|
2015-04-16 01:01:29 -04:00
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
network, err := createTestNetwork(bridgeNetType, "testnetwork", option, nil, nil)
|
2015-04-16 01:01:29 -04: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-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
|
|
|
|
2015-05-25 00:27:13 -04:00
|
|
|
netOption := options.Generic{
|
2015-10-05 17:53:25 -04:00
|
|
|
"BridgeName": "testnetwork",
|
2015-09-23 21:01:04 -04:00
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
option := options.Generic{
|
|
|
|
netlabel.GenericData: netOption,
|
|
|
|
}
|
2015-10-05 17:53:25 -04:00
|
|
|
ipamV4ConfList := []*libnetwork.IpamConf{&libnetwork.IpamConf{PreferredPool: "192.168.100.0/24"}}
|
2015-04-16 01:01:29 -04:00
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
network, err := createTestNetwork(bridgeNetType, "testnetwork", option, ipamV4ConfList, nil)
|
2015-04-16 01:01:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-05-07 22:59:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = network.CreateEndpoint("")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
2015-05-14 17:56:15 -04:00
|
|
|
if _, ok := err.(libnetwork.ErrInvalidName); !ok {
|
|
|
|
t.Fatalf("Expected to fail with ErrInvalidName error. Actual error: %v", err)
|
2015-04-16 01:01:29 -04:00
|
|
|
}
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
ep, err := network.CreateEndpoint("testep")
|
2015-04-16 01:01:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ep.Delete()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done testing. Now cleanup
|
|
|
|
if err := network.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 12:46:11 -04:00
|
|
|
|
|
|
|
func TestNetworkEndpointsWalkers(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
|
|
|
|
2015-04-20 12:46:11 -04:00
|
|
|
// Create network 1 and add 2 endpoint: ep11, ep12
|
2015-06-08 11:24:43 -04:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "network1",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
net1, err := createTestNetwork(bridgeNetType, "network1", netOption, nil, nil)
|
2015-04-20 12:46:11 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := net1.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
ep11, err := net1.CreateEndpoint("ep11")
|
2015-04-20 12:46:11 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := ep11.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
ep12, err := net1.CreateEndpoint("ep12")
|
2015-04-20 12:46:11 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := ep12.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-20 12:46:11 -04:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
current := len(controller.Networks())
|
|
|
|
|
|
|
|
// Create network 2
|
|
|
|
netOption = options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "network2",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
net2, err := createTestNetwork(bridgeNetType, "network2", netOption, nil, nil)
|
2015-06-08 11:24:43 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := net2.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Test Networks method
|
|
|
|
if len(controller.Networks()) != current+1 {
|
|
|
|
t.Fatalf("Did not find the expected number of networks")
|
|
|
|
}
|
|
|
|
|
2015-04-20 12:46:11 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
// Look for network named "network1" and "network2"
|
2015-04-20 12:46:11 -04:00
|
|
|
netName = "network1"
|
|
|
|
controller.WalkNetworks(nwWlk)
|
|
|
|
if netWanted == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
if net1.ID() != netWanted.ID() {
|
2015-04-20 12:46:11 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-08 11:24:43 -04:00
|
|
|
|
|
|
|
netName = "network2"
|
|
|
|
controller.WalkNetworks(nwWlk)
|
|
|
|
if netWanted == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
if net2.ID() != netWanted.ID() {
|
2015-06-08 11:24:43 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-20 12:46:11 -04:00
|
|
|
}
|
2015-04-24 15:05:33 -04:00
|
|
|
|
2015-06-04 12:41:42 -04:00
|
|
|
func TestDuplicateEndpoint(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-06-04 12:41:42 -04:00
|
|
|
}
|
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
|
|
|
}
|
2015-10-05 17:53:25 -04:00
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", netOption, nil, nil)
|
2015-06-04 12:41:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ep, err := n.CreateEndpoint("ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := ep.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ep2, err := n.CreateEndpoint("ep1")
|
|
|
|
defer func() {
|
|
|
|
// Cleanup ep2 as well, else network cleanup might fail for failure cases
|
|
|
|
if ep2 != nil {
|
|
|
|
if err := ep2.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := err.(types.ForbiddenError); !ok {
|
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:05:33 -04:00
|
|
|
func TestControllerQuery(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
|
|
|
|
2015-04-24 15:05:33 -04:00
|
|
|
// Create network 1
|
2015-06-08 11:24:43 -04:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "network1",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
|
|
|
}
|
2015-10-05 17:53:25 -04:00
|
|
|
net1, err := createTestNetwork(bridgeNetType, "network1", netOption, nil, nil)
|
2015-04-24 15:05:33 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := net1.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-24 15:05:33 -04:00
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
// Create network 2
|
|
|
|
netOption = options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "network2",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
|
|
|
}
|
2015-10-05 17:53:25 -04:00
|
|
|
net2, err := createTestNetwork(bridgeNetType, "network2", netOption, nil, nil)
|
2015-06-08 11:24:43 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := net2.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
_, err = controller.NetworkByName("")
|
|
|
|
if err == nil {
|
2015-04-24 15:05:33 -04:00
|
|
|
t.Fatalf("NetworkByName() succeeded with invalid target name")
|
|
|
|
}
|
2015-05-14 17:56:15 -04:00
|
|
|
if _, ok := err.(libnetwork.ErrInvalidName); !ok {
|
|
|
|
t.Fatalf("Expected NetworkByName() to fail with ErrInvalidName error. Got: %v", err)
|
2015-05-11 19:13:27 -04:00
|
|
|
}
|
2015-04-24 15:05:33 -04:00
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
_, err = controller.NetworkByID("")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("NetworkByID() succeeded with invalid target id")
|
|
|
|
}
|
2015-05-14 17:56:15 -04:00
|
|
|
if _, ok := err.(libnetwork.ErrInvalidID); !ok {
|
2015-05-11 19:13:27 -04:00
|
|
|
t.Fatalf("NetworkByID() failed with unexpected error: %v", err)
|
2015-04-24 15:05:33 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
g, err := controller.NetworkByID("network1")
|
2015-05-15 19:04:09 -04:00
|
|
|
if err == nil {
|
2015-05-18 19:49:12 -04:00
|
|
|
t.Fatalf("Unexpected success for NetworkByID(): %v", g)
|
2015-05-11 19:13:27 -04:00
|
|
|
}
|
2015-05-14 17:56:15 -04:00
|
|
|
if _, ok := err.(libnetwork.ErrNoSuchNetwork); !ok {
|
2015-05-15 19:04:09 -04:00
|
|
|
t.Fatalf("NetworkByID() failed with unexpected error: %v", err)
|
2015-04-24 15:05:33 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
g, err = controller.NetworkByName("network1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected failure for NetworkByName(): %v", err)
|
|
|
|
}
|
2015-04-24 15:05:33 -04:00
|
|
|
if g == nil {
|
|
|
|
t.Fatalf("NetworkByName() did not find the network")
|
|
|
|
}
|
2015-05-11 19:13:27 -04:00
|
|
|
|
2015-04-24 15:05:33 -04:00
|
|
|
if g != net1 {
|
|
|
|
t.Fatalf("NetworkByName() returned the wrong network")
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
g, err = controller.NetworkByID(net1.ID())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected failure for NetworkByID(): %v", err)
|
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
if net1.ID() != g.ID() {
|
2015-04-24 15:05:33 -04:00
|
|
|
t.Fatalf("NetworkByID() returned unexpected element: %v", g)
|
|
|
|
}
|
2015-06-08 11:24:43 -04:00
|
|
|
|
|
|
|
g, err = controller.NetworkByName("network2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected failure for NetworkByName(): %v", err)
|
|
|
|
}
|
|
|
|
if g == nil {
|
|
|
|
t.Fatalf("NetworkByName() did not find the network")
|
|
|
|
}
|
|
|
|
|
|
|
|
if g != net2 {
|
|
|
|
t.Fatalf("NetworkByName() returned the wrong network")
|
|
|
|
}
|
|
|
|
|
|
|
|
g, err = controller.NetworkByID(net2.ID())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected failure for NetworkByID(): %v", err)
|
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
if net2.ID() != g.ID() {
|
2015-06-08 11:24:43 -04:00
|
|
|
t.Fatalf("NetworkByID() returned unexpected element: %v", g)
|
|
|
|
}
|
2015-04-24 15:05:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkQuery(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
|
|
|
|
2015-04-24 15:05:33 -04:00
|
|
|
// Create network 1 and add 2 endpoint: ep11, ep12
|
2015-06-08 11:24:43 -04:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "network1",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
|
|
|
}
|
2015-10-05 17:53:25 -04:00
|
|
|
net1, err := createTestNetwork(bridgeNetType, "network1", netOption, nil, nil)
|
2015-04-24 15:05:33 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := net1.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
ep11, err := net1.CreateEndpoint("ep11")
|
2015-04-24 15:05:33 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := ep11.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
ep12, err := net1.CreateEndpoint("ep12")
|
2015-04-24 15:05:33 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := ep12.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-24 15:05:33 -04:00
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
e, err := net1.EndpointByName("ep11")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-24 15:05:33 -04:00
|
|
|
if ep11 != e {
|
|
|
|
t.Fatalf("EndpointByName() returned %v instead of %v", e, ep11)
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
e, err = net1.EndpointByName("")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("EndpointByName() succeeded with invalid target name")
|
|
|
|
}
|
2015-05-14 17:56:15 -04:00
|
|
|
if _, ok := err.(libnetwork.ErrInvalidName); !ok {
|
|
|
|
t.Fatalf("Expected EndpointByName() to fail with ErrInvalidName error. Got: %v", err)
|
2015-04-24 15:05:33 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
e, err = net1.EndpointByName("IamNotAnEndpoint")
|
2015-05-15 19:04:09 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("EndpointByName() succeeded with unknown target name")
|
|
|
|
}
|
2015-05-14 17:56:15 -04:00
|
|
|
if _, ok := err.(libnetwork.ErrNoSuchEndpoint); !ok {
|
2015-05-11 19:13:27 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-24 15:05:33 -04:00
|
|
|
if e != nil {
|
|
|
|
t.Fatalf("EndpointByName(): expected nil, got %v", e)
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
e, err = net1.EndpointByID(ep12.ID())
|
2015-05-15 19:04:09 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
if ep12.ID() != e.ID() {
|
2015-04-24 15:05:33 -04:00
|
|
|
t.Fatalf("EndpointByID() returned %v instead of %v", e, ep12)
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
e, err = net1.EndpointByID("")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("EndpointByID() succeeded with invalid target id")
|
|
|
|
}
|
2015-05-14 17:56:15 -04:00
|
|
|
if _, ok := err.(libnetwork.ErrInvalidID); !ok {
|
2015-05-11 19:13:27 -04:00
|
|
|
t.Fatalf("EndpointByID() failed with unexpected error: %v", err)
|
2015-04-24 15:05:33 -04:00
|
|
|
}
|
|
|
|
}
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
const containerID = "valid_c"
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-05-21 14:04:49 -04:00
|
|
|
func checkSandbox(t *testing.T, info libnetwork.EndpointInfo) {
|
|
|
|
origns, err := netns.Get()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not get the current netns: %v", err)
|
|
|
|
}
|
|
|
|
defer origns.Close()
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
key := info.Sandbox().Key()
|
2015-05-21 14:04:49 -04:00
|
|
|
f, err := os.OpenFile(key, os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to open network namespace path %q: %v", key, err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
nsFD := f.Fd()
|
|
|
|
if err = netns.Set(netns.NsHandle(nsFD)); err != nil {
|
|
|
|
t.Fatalf("Setting to the namespace pointed to by the sandbox %s failed: %v", key, err)
|
|
|
|
}
|
|
|
|
defer netns.Set(origns)
|
|
|
|
|
|
|
|
_, err = netlink.LinkByName("eth0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not find the interface eth0 inside the sandbox: %v", err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
|
|
|
|
_, err = netlink.LinkByName("eth1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not find the interface eth1 inside the sandbox: %v", err)
|
|
|
|
}
|
2015-05-21 14:04:49 -04:00
|
|
|
}
|
|
|
|
|
2015-04-28 01:57:36 -04:00
|
|
|
func TestEndpointJoin(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
// Create network 1 and add 2 endpoint: ep11, ep12
|
|
|
|
n1, err := createTestNetwork(bridgeNetType, "testnetwork1", options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork1",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
2015-10-05 17:53:25 -04:00
|
|
|
}, nil, nil)
|
2015-04-28 01:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := n1.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-05-25 00:27:13 -04:00
|
|
|
ep1, err := n1.CreateEndpoint("ep1")
|
2015-04-28 01:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := ep1.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-05-14 02:23:45 -04:00
|
|
|
// Validate if ep.Info() only gives me IP address info and not names and gateway during CreateEndpoint()
|
2015-05-25 00:27:13 -04:00
|
|
|
info := ep1.Info()
|
2015-10-03 19:11:50 -04:00
|
|
|
iface := info.Iface()
|
|
|
|
if iface.Address() != nil && iface.Address().IP.To4() == nil {
|
|
|
|
t.Fatalf("Invalid IP address returned: %v", iface.Address())
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if info.Gateway().To4() != nil {
|
|
|
|
t.Fatalf("Expected empty gateway for an empty endpoint. Instead found a gateway: %v", info.Gateway())
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if info.Sandbox() != nil {
|
|
|
|
t.Fatalf("Expected an empty sandbox key for an empty endpoint. Instead found a non-empty sandbox key: %s", info.Sandbox().Key())
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
// test invalid joins
|
|
|
|
err = ep1.Join(nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected to fail join with nil Sandbox")
|
|
|
|
}
|
|
|
|
if _, ok := err.(types.BadRequestError); !ok {
|
|
|
|
t.Fatalf("Unexpected error type returned: %T", err)
|
|
|
|
}
|
2015-06-19 21:41:31 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
fsbx := &fakeSandbox{}
|
|
|
|
if err = ep1.Join(fsbx); err == nil {
|
|
|
|
t.Fatalf("Expected to fail join with invalid Sandbox")
|
|
|
|
}
|
|
|
|
if _, ok := err.(types.BadRequestError); !ok {
|
|
|
|
t.Fatalf("Unexpected error type returned: %T", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := controller.NewSandbox(containerID,
|
|
|
|
libnetwork.OptionHostname("test"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"),
|
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := sb.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = ep1.Join(sb)
|
2015-06-08 11:24:43 -04:00
|
|
|
runtime.LockOSThread()
|
2015-04-28 01:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-07 16:03:47 -04:00
|
|
|
defer func() {
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep1.Leave(sb)
|
2015-06-08 11:24:43 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-07 16:03:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-14 02:23:45 -04:00
|
|
|
|
|
|
|
// Validate if ep.Info() only gives valid gateway and sandbox key after has container has joined.
|
2015-05-25 00:27:13 -04:00
|
|
|
info = ep1.Info()
|
2015-05-14 02:23:45 -04:00
|
|
|
if info.Gateway().To4() == nil {
|
|
|
|
t.Fatalf("Expected a valid gateway for a joined endpoint. Instead found an invalid gateway: %v", info.Gateway())
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if info.Sandbox() == nil {
|
2015-05-14 02:23:45 -04:00
|
|
|
t.Fatalf("Expected an non-empty sandbox key for a joined endpoint. Instead found a empty sandbox key")
|
|
|
|
}
|
2015-05-21 14:04:49 -04:00
|
|
|
|
2015-06-10 21:50:19 -04:00
|
|
|
// Check endpoint provided container information
|
2015-07-02 01:00:48 -04:00
|
|
|
if ep1.Info().Sandbox().Key() != sb.Key() {
|
|
|
|
t.Fatalf("Endpoint Info returned unexpected sandbox key: %s", sb.Key())
|
2015-06-10 21:50:19 -04:00
|
|
|
}
|
|
|
|
|
2015-06-29 16:32:07 -04:00
|
|
|
// Attempt retrieval of endpoint interfaces statistics
|
2015-07-02 01:00:48 -04:00
|
|
|
stats, err := sb.Statistics()
|
2015-06-29 16:32:07 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, ok := stats["eth0"]; !ok {
|
|
|
|
t.Fatalf("Did not find eth0 statistics")
|
|
|
|
}
|
|
|
|
|
2015-05-25 00:27:13 -04:00
|
|
|
// Now test the container joining another network
|
|
|
|
n2, err := createTestNetwork(bridgeNetType, "testnetwork2",
|
|
|
|
options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork2",
|
2015-05-25 00:27:13 -04:00
|
|
|
},
|
2015-10-05 17:53:25 -04:00
|
|
|
}, nil, nil)
|
2015-05-25 00:27:13 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := n2.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ep2, err := n2.CreateEndpoint("ep2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := ep2.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep2.Join(sb)
|
2015-05-25 00:27:13 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-19 21:41:31 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep2.Leave(sb)
|
2015-06-19 21:41:31 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-25 00:27:13 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if ep1.Info().Sandbox().Key() != ep2.Info().Sandbox().Key() {
|
|
|
|
t.Fatalf("ep1 and ep2 returned different container sandbox key")
|
2015-06-19 21:41:31 -04:00
|
|
|
}
|
|
|
|
|
2015-05-21 14:04:49 -04:00
|
|
|
checkSandbox(t, info)
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
2015-06-19 21:41:31 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
type fakeSandbox struct{}
|
|
|
|
|
|
|
|
func (f *fakeSandbox) ID() string {
|
|
|
|
return "fake sandbox"
|
2015-04-28 01:57:36 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (f *fakeSandbox) ContainerID() string {
|
|
|
|
return ""
|
|
|
|
}
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (f *fakeSandbox) Key() string {
|
|
|
|
return "fake key"
|
|
|
|
}
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (f *fakeSandbox) Labels() map[string]interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-09-26 13:12:20 -04:00
|
|
|
func (f *fakeSandbox) Statistics() (map[string]*types.InterfaceStatistics, error) {
|
2015-07-02 01:00:48 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-09-01 21:55:53 -04:00
|
|
|
func (f *fakeSandbox) Refresh(opts ...libnetwork.SandboxOption) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (f *fakeSandbox) Delete() error {
|
|
|
|
return nil
|
2015-04-28 01:57:36 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 19:20:54 -04:00
|
|
|
func (f *fakeSandbox) SetKey(key string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExternalKey(t *testing.T) {
|
2015-09-09 17:24:05 -04:00
|
|
|
externalKeyTest(t, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExternalKeyWithReexec(t *testing.T) {
|
|
|
|
externalKeyTest(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func externalKeyTest(t *testing.T, reexec bool) {
|
2015-09-10 01:27:39 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-09-09 19:20:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork",
|
2015-09-09 19:20:54 -04:00
|
|
|
},
|
2015-10-05 17:53:25 -04:00
|
|
|
}, nil, nil)
|
2015-09-09 19:20:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ep, err := n.CreateEndpoint("ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = ep.Delete()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ep2, err := n.CreateEndpoint("ep2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = ep2.Delete()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cnt, err := controller.NewSandbox(containerID,
|
|
|
|
libnetwork.OptionHostname("test"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"),
|
|
|
|
libnetwork.OptionUseExternalKey(),
|
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"))
|
|
|
|
defer func() {
|
|
|
|
if err := cnt.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-11 17:34:09 -04:00
|
|
|
osl.GC()
|
2015-09-09 19:20:54 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Join endpoint to sandbox before SetKey
|
|
|
|
err = ep.Join(cnt)
|
|
|
|
runtime.LockOSThread()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = ep.Leave(cnt)
|
|
|
|
runtime.LockOSThread()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
sbox := ep.Info().Sandbox()
|
|
|
|
if sbox == nil {
|
|
|
|
t.Fatalf("Expected to have a valid Sandbox")
|
|
|
|
}
|
|
|
|
|
2015-09-09 17:24:05 -04:00
|
|
|
if reexec {
|
|
|
|
err := reexecSetKey("this-must-fail", containerID, controller.ID())
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("SetExternalKey must fail if the corresponding namespace is not created")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Setting an non-existing key (namespace) must fail
|
|
|
|
if err := sbox.SetKey("this-must-fail"); err == nil {
|
|
|
|
t.Fatalf("Setkey must fail if the corresponding namespace is not created")
|
|
|
|
}
|
2015-09-09 19:20:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new OS sandbox using the osl API before using it in SetKey
|
2015-09-11 17:34:09 -04:00
|
|
|
if extOsBox, err := osl.NewSandbox("ValidKey", true); err != nil {
|
2015-09-09 19:20:54 -04:00
|
|
|
t.Fatalf("Failed to create new osl sandbox")
|
2015-09-11 17:34:09 -04:00
|
|
|
} else {
|
|
|
|
defer func() {
|
|
|
|
if err := extOsBox.Destroy(); err != nil {
|
|
|
|
log.Warnf("Failed to remove os sandbox: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2015-09-09 19:20:54 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 17:24:05 -04:00
|
|
|
if reexec {
|
|
|
|
err := reexecSetKey("ValidKey", containerID, controller.ID())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("SetExternalKey failed with %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := sbox.SetKey("ValidKey"); err != nil {
|
|
|
|
t.Fatalf("Setkey failed with %v", err)
|
|
|
|
}
|
2015-09-09 19:20:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Join endpoint to sandbox after SetKey
|
|
|
|
err = ep2.Join(sbox)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer func() {
|
|
|
|
err = ep2.Leave(sbox)
|
|
|
|
runtime.LockOSThread()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if ep.Info().Sandbox().Key() != ep2.Info().Sandbox().Key() {
|
|
|
|
t.Fatalf("ep1 and ep2 returned different container sandbox key")
|
|
|
|
}
|
|
|
|
|
|
|
|
checkSandbox(t, ep.Info())
|
|
|
|
}
|
|
|
|
|
2015-09-09 17:24:05 -04:00
|
|
|
func reexecSetKey(key string, containerID string, controllerID string) error {
|
|
|
|
var (
|
|
|
|
state libcontainer.State
|
|
|
|
b []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
state.NamespacePaths = make(map[configs.NamespaceType]string)
|
|
|
|
state.NamespacePaths[configs.NamespaceType("NEWNET")] = key
|
|
|
|
if b, err = json.Marshal(state); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd := &exec.Cmd{
|
|
|
|
Path: reexec.Self(),
|
|
|
|
Args: append([]string{"libnetwork-setkey"}, containerID, controllerID),
|
|
|
|
Stdin: strings.NewReader(string(b)),
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stderr,
|
|
|
|
}
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
2015-05-06 16:02:40 -04:00
|
|
|
func TestEndpointDeleteWithActiveContainer(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
2015-05-06 16:02:40 -04:00
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
2015-10-05 17:53:25 -04:00
|
|
|
}, nil, nil)
|
2015-05-06 16:02:40 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-06 16:02:40 -04:00
|
|
|
|
|
|
|
ep, err := n.CreateEndpoint("ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-19 21:41:31 -04:00
|
|
|
defer func() {
|
|
|
|
err = ep.Delete()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
cnt, err := controller.NewSandbox(containerID,
|
|
|
|
libnetwork.OptionHostname("test"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"),
|
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"))
|
|
|
|
defer func() {
|
|
|
|
if err := cnt.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-06 16:02:40 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep.Join(cnt)
|
2015-06-08 11:24:43 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-06 16:02:40 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-07 16:03:47 -04:00
|
|
|
defer func() {
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep.Leave(cnt)
|
2015-06-08 11:24:43 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-07 16:03:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-06 16:02:40 -04: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 01:57:36 -04:00
|
|
|
func TestEndpointMultipleJoins(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
n, err := createTestNetwork(bridgeNetType, "testmultiple", options.Generic{
|
2015-06-08 11:24:43 -04:00
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testmultiple",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
2015-10-05 17:53:25 -04:00
|
|
|
}, nil, nil)
|
2015-04-28 01:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
ep, err := n.CreateEndpoint("ep1")
|
2015-04-28 01:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := ep.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
sbx1, err := controller.NewSandbox(containerID,
|
|
|
|
libnetwork.OptionHostname("test"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"),
|
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"))
|
|
|
|
defer func() {
|
|
|
|
if err := sbx1.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-06-19 21:41:31 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
sbx2, err := controller.NewSandbox("c2")
|
|
|
|
defer func() {
|
|
|
|
if err := sbx2.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
runtime.LockOSThread()
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = ep.Join(sbx1)
|
2015-06-08 11:24:43 -04:00
|
|
|
runtime.LockOSThread()
|
2015-04-28 01:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-07 16:03:47 -04:00
|
|
|
defer func() {
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep.Leave(sbx1)
|
2015-06-08 11:24:43 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-07 16:03:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep.Join(sbx2)
|
2015-04-28 01:57:36 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail multiple joins for the same endpoint")
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if _, ok := err.(types.ForbiddenError); !ok {
|
|
|
|
t.Fatalf("Failed with unexpected error type: %T. Desc: %s", err, err.Error())
|
2015-04-28 01:57:36 -04:00
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
|
2015-04-28 01:57:36 -04:00
|
|
|
}
|
|
|
|
|
2015-06-19 21:41:31 -04:00
|
|
|
func TestLeaveAll(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-06-19 21:41:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-19 21:41:31 -04:00
|
|
|
},
|
2015-10-05 17:53:25 -04:00
|
|
|
}, nil, nil)
|
2015-06-19 21:41:31 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2015-07-02 01:00:48 -04:00
|
|
|
// If this goes through, it means cnt.Delete() effectively detached from all the endpoints
|
2015-06-19 21:41:31 -04:00
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ep1, err := n.CreateEndpoint("ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ep2, err := n.CreateEndpoint("ep2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
cnt, err := controller.NewSandbox("leaveall")
|
2015-06-19 21:41:31 -04:00
|
|
|
if err != nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
t.Fatal(err)
|
2015-06-19 21:41:31 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep1.Join(cnt)
|
2015-06-19 21:41:31 -04:00
|
|
|
if err != nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
t.Fatalf("Failed to join ep1: %v", err)
|
2015-06-19 21:41:31 -04:00
|
|
|
}
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep2.Join(cnt)
|
2015-06-19 21:41:31 -04:00
|
|
|
if err != nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
t.Fatalf("Failed to join ep2: %v", err)
|
2015-06-19 21:41:31 -04:00
|
|
|
}
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = cnt.Delete()
|
2015-06-19 21:41:31 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func TestontainerInvalidLeave(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
2015-10-05 17:53:25 -04:00
|
|
|
}, nil, nil)
|
2015-04-28 01:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
ep, err := n.CreateEndpoint("ep1")
|
2015-04-28 01:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := ep.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
cnt, err := controller.NewSandbox(containerID,
|
|
|
|
libnetwork.OptionHostname("test"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"),
|
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"))
|
2015-04-28 01:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-07 16:03:47 -04:00
|
|
|
defer func() {
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := cnt.Delete(); err != nil {
|
2015-05-07 16:03:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep.Leave(cnt)
|
2015-04-28 01:57:36 -04:00
|
|
|
if err == nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
t.Fatal("Expected to fail leave from an endpoint which has no active join")
|
2015-04-28 01:57:36 -04:00
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
if _, ok := err.(types.ForbiddenError); !ok {
|
|
|
|
t.Fatalf("Failed with unexpected error type: %T. Desc: %s", err, err.Error())
|
2015-04-28 01:57:36 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := ep.Leave(nil); err == nil {
|
|
|
|
t.Fatalf("Expected to fail leave nil Sandbox")
|
|
|
|
}
|
|
|
|
if _, ok := err.(types.BadRequestError); !ok {
|
|
|
|
t.Fatalf("Unexpected error type returned: %T", err)
|
2015-04-28 01:57:36 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
fsbx := &fakeSandbox{}
|
|
|
|
if err = ep.Leave(fsbx); err == nil {
|
|
|
|
t.Fatalf("Expected to fail leave with invalid Sandbox")
|
|
|
|
}
|
|
|
|
if _, ok := err.(types.BadRequestError); !ok {
|
|
|
|
t.Fatalf("Unexpected error type returned: %T", err)
|
2015-04-28 01:57:36 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-03 16:29:43 -04:00
|
|
|
|
|
|
|
func TestEndpointUpdateParent(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
2015-05-03 16:29:43 -04:00
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
n, err := createTestNetwork("bridge", "testnetwork", options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
2015-10-05 17:53:25 -04:00
|
|
|
}, nil, nil)
|
2015-05-03 16:29:43 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-03 16:29:43 -04:00
|
|
|
|
2015-05-25 00:27:13 -04:00
|
|
|
ep1, err := n.CreateEndpoint("ep1")
|
2015-05-03 16:29:43 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
ep2, err := n.CreateEndpoint("ep2")
|
2015-05-03 16:29:43 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
sbx1, err := controller.NewSandbox(containerID,
|
|
|
|
libnetwork.OptionHostname("test"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"),
|
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"))
|
2015-05-03 16:29:43 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := sbx1.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
sbx2, err := controller.NewSandbox("c2",
|
|
|
|
libnetwork.OptionHostname("test2"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"),
|
|
|
|
libnetwork.OptionHostsPath("/var/lib/docker/test_network/container2/hosts"),
|
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.2"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sbx2.Delete(); err != nil {
|
2015-05-25 00:27:13 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-03 16:29:43 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep1.Join(sbx1)
|
2015-06-08 11:24:43 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-03 16:29:43 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep2.Join(sbx2)
|
2015-06-19 21:41:31 -04:00
|
|
|
runtime.LockOSThread()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-03 16:29:43 -04:00
|
|
|
}
|
2015-05-06 02:41:20 -04:00
|
|
|
|
|
|
|
func TestEnableIPv6(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
2015-05-06 02:41:20 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
tmpResolvConf := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\nnameserver 2001:4860:4860::8888\n")
|
2015-05-06 02:41:20 -04: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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.EnableIPv6: true,
|
2015-05-12 19:39:30 -04:00
|
|
|
netlabel.GenericData: options.Generic{
|
2015-10-04 22:08:31 -04:00
|
|
|
"BridgeName": "testnetwork",
|
2015-05-12 19:39:30 -04:00
|
|
|
},
|
2015-05-06 02:41:20 -04:00
|
|
|
}
|
2015-10-05 17:53:25 -04:00
|
|
|
ipamV6ConfList := []*libnetwork.IpamConf{&libnetwork.IpamConf{PreferredPool: "fe80::/98"}}
|
2015-05-06 02:41:20 -04:00
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
n, err := createTestNetwork("bridge", "testnetwork", netOption, nil, ipamV6ConfList)
|
2015-05-06 02:41:20 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-06 02:41:20 -04:00
|
|
|
|
2015-05-25 00:27:13 -04:00
|
|
|
ep1, err := n.CreateEndpoint("ep1")
|
2015-05-06 02:41:20 -04:00
|
|
|
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 08:13:17 -04:00
|
|
|
defer os.Remove(resolvConfPath)
|
2015-05-06 02:41:20 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
sb, err := controller.NewSandbox(containerID, libnetwork.OptionResolvConfPath(resolvConfPath))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sb.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = ep1.Join(sb)
|
2015-05-07 16:03:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-06 02:41:20 -04:00
|
|
|
content, err := ioutil.ReadFile(resolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(content, tmpResolvConf) {
|
2015-07-02 01:00:48 -04:00
|
|
|
t.Fatalf("Expected:\n%s\nGot:\n%s", string(tmpResolvConf), string(content))
|
2015-05-06 02:41:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 16:41:28 -04:00
|
|
|
func TestResolvConfHost(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-06-30 16:41:28 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
tmpResolvConf := []byte("search localhost.net\nnameserver 127.0.0.1\nnameserver 2001:4860:4860::8888\n")
|
2015-06-30 16:41:28 -04: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 := controller.NetworkByName("testhost")
|
|
|
|
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"
|
|
|
|
defer os.Remove(resolvConfPath)
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
sb, err := controller.NewSandbox(containerID,
|
|
|
|
libnetwork.OptionResolvConfPath(resolvConfPath),
|
|
|
|
libnetwork.OptionOriginResolvConfPath("/etc/resolv.conf"))
|
2015-06-30 16:41:28 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := sb.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = ep1.Join(sb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = ep1.Leave(sb)
|
2015-06-30 16:41:28 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(resolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(content, tmpResolvConf) {
|
2015-07-02 01:00:48 -04:00
|
|
|
t.Fatalf("Expected:\n%s\nGot:\n%s", string(tmpResolvConf), string(content))
|
2015-06-30 16:41:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 17:01:53 -04:00
|
|
|
func TestResolvConf(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-05-12 19:39:30 -04:00
|
|
|
}
|
2015-05-06 02:41:20 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
tmpResolvConf1 := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\nnameserver 2001:4860:4860::8888\n")
|
2015-05-15 17:01:53 -04:00
|
|
|
expectedResolvConf1 := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\n")
|
2015-07-02 01:00:48 -04:00
|
|
|
tmpResolvConf2 := []byte("search pommesfrites.fr\nnameserver 112.34.56.78\nnameserver 2001:4860:4860::8888\n")
|
2015-05-15 17:01:53 -04:00
|
|
|
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 02:41:20 -04: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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
|
|
|
}
|
2015-10-05 17:53:25 -04:00
|
|
|
n, err := createTestNetwork("bridge", "testnetwork", netOption, nil, nil)
|
2015-05-06 02:41:20 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-06 02:41:20 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
ep, err := n.CreateEndpoint("ep")
|
2015-05-06 02:41:20 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-15 17:01:53 -04:00
|
|
|
if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf1, 0644); err != nil {
|
2015-05-06 02:41:20 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resolvConfPath := "/tmp/libnetwork_test/resolv.conf"
|
2015-05-16 08:13:17 -04:00
|
|
|
defer os.Remove(resolvConfPath)
|
2015-05-06 02:41:20 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
sb1, err := controller.NewSandbox(containerID, libnetwork.OptionResolvConfPath(resolvConfPath))
|
2015-05-07 16:03:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := sb1.Delete(); err != nil {
|
2015-05-07 16:03:47 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-06 02:41:20 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep.Join(sb1)
|
|
|
|
runtime.LockOSThread()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-20 01:40:23 -04: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 02:41:20 -04:00
|
|
|
content, err := ioutil.ReadFile(resolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-15 17:01:53 -04:00
|
|
|
if !bytes.Equal(content, expectedResolvConf1) {
|
2015-07-02 01:00:48 -04:00
|
|
|
fmt.Printf("\n%v\n%v\n", expectedResolvConf1, content)
|
|
|
|
t.Fatalf("Expected:\n%s\nGot:\n%s", string(expectedResolvConf1), string(content))
|
2015-05-06 02:41:20 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep.Leave(sb1)
|
2015-06-08 11:24:43 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-06 02:41:20 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-15 17:01:53 -04:00
|
|
|
|
|
|
|
if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf2, 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
sb2, err := controller.NewSandbox(containerID+"_2", libnetwork.OptionResolvConfPath(resolvConfPath))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sb2.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = ep.Join(sb2)
|
2015-06-08 11:24:43 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-15 17:01:53 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err = ioutil.ReadFile(resolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(content, expectedResolvConf2) {
|
2015-07-02 01:00:48 -04:00
|
|
|
t.Fatalf("Expected:\n%s\nGot:\n%s", string(expectedResolvConf2), string(content))
|
2015-05-15 17:01:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(resolvConfPath, tmpResolvConf3, 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep.Leave(sb2)
|
2015-06-08 11:24:43 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-15 17:01:53 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = ep.Join(sb2)
|
2015-06-08 11:24:43 -04:00
|
|
|
runtime.LockOSThread()
|
2015-05-15 17:01:53 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err = ioutil.ReadFile(resolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(content, tmpResolvConf3) {
|
2015-07-02 01:00:48 -04:00
|
|
|
t.Fatalf("Expected:\n%s\nGot:\n%s", string(tmpResolvConf3), string(content))
|
2015-05-15 17:01:53 -04:00
|
|
|
}
|
2015-05-06 02:41:20 -04:00
|
|
|
}
|
2015-05-09 00:50:03 -04:00
|
|
|
|
2015-05-15 21:14:36 -04:00
|
|
|
func TestInvalidRemoteDriver(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
2015-05-15 21:14:36 -04:00
|
|
|
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"]}`)
|
|
|
|
})
|
|
|
|
|
2015-09-30 11:16:58 -04:00
|
|
|
if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil {
|
2015-05-15 21:14:36 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2015-09-30 11:16:58 -04:00
|
|
|
if err := os.RemoveAll("/etc/docker/plugins"); err != nil {
|
2015-05-15 21:14:36 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-09-30 11:16:58 -04:00
|
|
|
if err := ioutil.WriteFile("/etc/docker/plugins/invalid-network-driver.spec", []byte(server.URL), 0644); err != nil {
|
2015-05-15 21:14:36 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:07:23 -04:00
|
|
|
ctrlr, err := libnetwork.New()
|
2015-05-15 21:14:36 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-22 17:07:23 -04:00
|
|
|
defer ctrlr.Stop()
|
2015-05-15 21:14:36 -04:00
|
|
|
|
2015-09-22 17:07:23 -04:00
|
|
|
_, err = ctrlr.NewNetwork("invalid-network-driver", "dummy",
|
2015-05-15 21:14:36 -04:00
|
|
|
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) {
|
2015-09-07 13:33:28 -04:00
|
|
|
if !testutils.IsRunningInContainer() {
|
2015-05-15 21:14:36 -04:00
|
|
|
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)
|
|
|
|
})
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.CreateNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
fmt.Fprintf(w, "null")
|
|
|
|
})
|
2015-05-15 21:14:36 -04:00
|
|
|
|
2015-09-30 11:16:58 -04:00
|
|
|
if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil {
|
2015-05-15 21:14:36 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2015-09-30 11:16:58 -04:00
|
|
|
if err := os.RemoveAll("/etc/docker/plugins"); err != nil {
|
2015-05-15 21:14:36 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-09-30 11:16:58 -04:00
|
|
|
if err := ioutil.WriteFile("/etc/docker/plugins/valid-network-driver.spec", []byte(server.URL), 0644); err != nil {
|
2015-05-15 21:14:36 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-25 00:27:13 -04:00
|
|
|
n, err := controller.NewNetwork("valid-network-driver", "dummy",
|
2015-05-15 21:14:36 -04:00
|
|
|
libnetwork.NetworkOptionGeneric(getEmptyGenericOption()))
|
2015-05-14 17:56:15 -04:00
|
|
|
if err != nil {
|
2015-05-25 00:27:13 -04:00
|
|
|
// Only fail if we could not find the plugin driver
|
|
|
|
if _, ok := err.(types.NotFoundError); ok {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return
|
2015-05-15 21:14:36 -04:00
|
|
|
}
|
2015-05-25 00:27:13 -04:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-15 21:14:36 -04:00
|
|
|
}
|
|
|
|
|
2015-05-09 00:50:03 -04:00
|
|
|
var (
|
|
|
|
once sync.Once
|
|
|
|
start = make(chan struct{})
|
|
|
|
done = make(chan chan struct{}, numThreads-1)
|
|
|
|
origns = netns.None()
|
|
|
|
testns = netns.None()
|
2015-07-02 01:00:48 -04:00
|
|
|
sboxes = make([]libnetwork.Sandbox, numThreads)
|
2015-05-09 00:50:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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-09-07 13:33:28 -04:00
|
|
|
if testutils.IsRunningInContainer() {
|
2015-05-12 19:39:30 -04:00
|
|
|
testns = origns
|
|
|
|
} else {
|
|
|
|
testns, err = netns.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-09 00:50:03 -04:00
|
|
|
}
|
|
|
|
|
2015-06-08 11:24:43 -04:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-23 21:01:04 -04:00
|
|
|
"BridgeName": "network",
|
2015-06-08 11:24:43 -04:00
|
|
|
},
|
|
|
|
}
|
2015-07-30 02:57:50 -04:00
|
|
|
|
|
|
|
net1, err := controller.NetworkByName("testhost")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
net2, err := createTestNetwork("bridge", "network2", netOption, nil, nil)
|
2015-07-30 02:57:50 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = net1.CreateEndpoint("pep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = net2.CreateEndpoint("pep2")
|
2015-05-09 00:50:03 -04:00
|
|
|
if err != nil {
|
2015-07-30 02:57:50 -04:00
|
|
|
t.Fatal(err)
|
2015-05-09 00:50:03 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 02:57:50 -04:00
|
|
|
_, err = net2.CreateEndpoint("pep3")
|
2015-05-09 00:50:03 -04:00
|
|
|
if err != nil {
|
2015-07-30 02:57:50 -04:00
|
|
|
t.Fatal(err)
|
2015-05-09 00:50:03 -04:00
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
|
|
|
|
if sboxes[first-1], err = controller.NewSandbox(fmt.Sprintf("%drace", first), libnetwork.OptionUseDefaultSandbox()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for thd := first + 1; thd <= last; thd++ {
|
|
|
|
if sboxes[thd-1], err = controller.NewSandbox(fmt.Sprintf("%drace", thd)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2015-05-09 00:50:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func debugf(format string, a ...interface{}) (int, error) {
|
|
|
|
if debug {
|
|
|
|
return fmt.Printf(format, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func parallelJoin(t *testing.T, rc libnetwork.Sandbox, ep libnetwork.Endpoint, thrNumber int) {
|
2015-05-09 00:50:03 -04:00
|
|
|
debugf("J%d.", thrNumber)
|
2015-07-30 02:57:50 -04:00
|
|
|
var err error
|
2015-07-02 01:00:48 -04:00
|
|
|
|
|
|
|
sb := sboxes[thrNumber-1]
|
|
|
|
err = ep.Join(sb)
|
2015-07-30 02:57:50 -04:00
|
|
|
|
2015-05-09 00:50:03 -04:00
|
|
|
runtime.LockOSThread()
|
|
|
|
if err != nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
if _, ok := err.(types.ForbiddenError); !ok {
|
|
|
|
t.Fatalf("thread %d: %v", thrNumber, err)
|
2015-05-09 00:50:03 -04:00
|
|
|
}
|
|
|
|
debugf("JE%d(%v).", thrNumber, err)
|
|
|
|
}
|
|
|
|
debugf("JD%d.", thrNumber)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func parallelLeave(t *testing.T, rc libnetwork.Sandbox, ep libnetwork.Endpoint, thrNumber int) {
|
2015-05-09 00:50:03 -04:00
|
|
|
debugf("L%d.", thrNumber)
|
2015-07-30 02:57:50 -04:00
|
|
|
var err error
|
2015-07-02 01:00:48 -04:00
|
|
|
|
|
|
|
sb := sboxes[thrNumber-1]
|
|
|
|
|
2015-10-07 23:01:38 -04:00
|
|
|
err = ep.Leave(sb)
|
2015-05-09 00:50:03 -04:00
|
|
|
runtime.LockOSThread()
|
|
|
|
if err != nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
if _, ok := err.(types.ForbiddenError); !ok {
|
|
|
|
t.Fatalf("thread %d: %v", thrNumber, err)
|
2015-05-09 00:50:03 -04:00
|
|
|
}
|
|
|
|
debugf("LE%d(%v).", thrNumber, err)
|
|
|
|
}
|
|
|
|
debugf("LD%d.", thrNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runParallelTests(t *testing.T, thrNumber int) {
|
2015-07-02 01:00:48 -04:00
|
|
|
var (
|
|
|
|
ep libnetwork.Endpoint
|
|
|
|
sb libnetwork.Sandbox
|
|
|
|
err error
|
|
|
|
)
|
2015-05-09 00:50:03 -04:00
|
|
|
|
|
|
|
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-07-30 02:57:50 -04:00
|
|
|
net1, err := controller.NetworkByName("testhost")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if net1 == nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
t.Fatal("Could not find testhost")
|
2015-07-30 02:57:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
net2, err := controller.NetworkByName("network2")
|
2015-05-11 19:13:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-30 02:57:50 -04:00
|
|
|
if net2 == nil {
|
|
|
|
t.Fatal("Could not find network2")
|
|
|
|
}
|
|
|
|
|
|
|
|
epName := fmt.Sprintf("pep%d", thrNumber)
|
|
|
|
|
|
|
|
if thrNumber == first {
|
|
|
|
ep, err = net1.EndpointByName(epName)
|
|
|
|
} else {
|
|
|
|
ep, err = net2.EndpointByName(epName)
|
2015-05-09 00:50:03 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-09 00:50:03 -04:00
|
|
|
if ep == nil {
|
2015-05-15 19:04:09 -04:00
|
|
|
t.Fatal("Got nil ep with no error")
|
2015-05-09 00:50:03 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
cid := fmt.Sprintf("%drace", thrNumber)
|
|
|
|
controller.WalkSandboxes(libnetwork.SandboxContainerWalker(&sb, cid))
|
|
|
|
if sb == nil {
|
|
|
|
t.Fatalf("Got nil sandbox for container: %s", cid)
|
|
|
|
}
|
|
|
|
|
2015-05-09 00:50:03 -04:00
|
|
|
for i := 0; i < iterCnt; i++ {
|
2015-07-02 01:00:48 -04:00
|
|
|
parallelJoin(t, sb, ep, thrNumber)
|
|
|
|
parallelLeave(t, sb, ep, thrNumber)
|
2015-05-09 00:50:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
debugf("\n")
|
|
|
|
|
2015-10-07 23:01:38 -04:00
|
|
|
err = sb.Delete()
|
2015-07-30 02:57:50 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-09 00:50:03 -04:00
|
|
|
if thrNumber == first {
|
|
|
|
for thrdone := range done {
|
|
|
|
select {
|
|
|
|
case <-thrdone:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testns.Close()
|
2015-10-07 23:01:38 -04:00
|
|
|
if err := net2.Delete(); err != nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-07 23:01:38 -04:00
|
|
|
} else {
|
|
|
|
err = ep.Delete()
|
2015-07-02 01:00:48 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-09 00:50:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParallel1(t *testing.T) {
|
|
|
|
runParallelTests(t, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParallel2(t *testing.T) {
|
|
|
|
runParallelTests(t, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParallel3(t *testing.T) {
|
|
|
|
runParallelTests(t, 3)
|
|
|
|
}
|