2021-08-23 13:14:53 +00:00
|
|
|
//go:build linux
|
2021-05-28 20:59:41 +00:00
|
|
|
|
2015-03-20 20:54:49 +00:00
|
|
|
package libnetwork_test
|
|
|
|
|
|
|
|
import (
|
2021-10-25 12:57:18 +00:00
|
|
|
"errors"
|
2015-05-09 04:50:03 +00:00
|
|
|
"fmt"
|
2015-05-16 01:14:36 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-05-02 00:01:21 +00:00
|
|
|
"os"
|
2021-04-27 22:40:39 +00:00
|
|
|
"path/filepath"
|
2015-03-20 20:54:49 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork"
|
|
|
|
"github.com/docker/docker/libnetwork/config"
|
|
|
|
"github.com/docker/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/ipamapi"
|
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/docker/libnetwork/options"
|
|
|
|
"github.com/docker/docker/libnetwork/testutils"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2015-05-16 01:14:36 +00:00
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2015-03-20 20:54:49 +00:00
|
|
|
)
|
|
|
|
|
2015-05-02 00:01:21 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2022-11-08 21:35:14 +00:00
|
|
|
// Cleanup local datastore file
|
2023-01-13 23:38:46 +00:00
|
|
|
_ = os.Remove(datastore.DefaultScope("").Client.Address)
|
2015-05-25 04:27:13 +00:00
|
|
|
|
2022-11-08 21:35:14 +00:00
|
|
|
os.Exit(m.Run())
|
2015-05-23 04:52:02 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 22:43:32 +00:00
|
|
|
func newController(t *testing.T) *libnetwork.Controller {
|
2022-11-08 21:35:14 +00:00
|
|
|
t.Helper()
|
|
|
|
genericOption := map[string]interface{}{
|
|
|
|
netlabel.GenericData: options.Generic{
|
|
|
|
"EnableIPForwarding": true,
|
|
|
|
},
|
2015-09-18 21:00:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 23:06:30 +00:00
|
|
|
c, err := libnetwork.New(
|
|
|
|
libnetwork.OptionBoltdbWithRandomDBFile(t),
|
|
|
|
config.OptionDriverConfig(bridgeNetType, genericOption),
|
|
|
|
)
|
2022-11-08 21:35:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-09-16 11:42:35 +00:00
|
|
|
}
|
2022-11-08 21:35:14 +00:00
|
|
|
t.Cleanup(c.Stop)
|
|
|
|
return c
|
2015-05-25 04:27:13 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 22:43:32 +00:00
|
|
|
func createTestNetwork(c *libnetwork.Controller, networkType, networkName string, netOption options.Generic, ipamV4Configs, ipamV6Configs []*libnetwork.IpamConf) (libnetwork.Network, error) {
|
2022-11-08 20:21:11 +00:00
|
|
|
return c.NewNetwork(networkType, networkName, "",
|
2015-10-05 21:53:25 +00:00
|
|
|
libnetwork.NetworkOptionGeneric(netOption),
|
2016-01-07 20:57:22 +00:00
|
|
|
libnetwork.NetworkOptionIpam(ipamapi.DefaultIPAM, "", ipamV4Configs, ipamV6Configs, nil))
|
2015-04-16 05:01:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
func getEmptyGenericOption() map[string]interface{} {
|
|
|
|
genericOption := make(map[string]interface{})
|
2021-05-28 20:59:41 +00:00
|
|
|
genericOption[netlabel.GenericData] = map[string]string{}
|
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{
|
2015-06-12 01:19:42 +00: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-02 00:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 09:58:09 +00:00
|
|
|
func isNotFound(err error) bool {
|
|
|
|
_, ok := (err).(types.NotFoundError)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2015-04-30 05:58:12 +00:00
|
|
|
func TestNull(t *testing.T) {
|
2022-11-08 21:35:14 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
controller := newController(t)
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
cnt, err := controller.NewSandbox("null_container",
|
|
|
|
libnetwork.OptionHostname("test"),
|
2023-01-10 14:16:51 +00:00
|
|
|
libnetwork.OptionDomainname("example.com"),
|
2015-07-02 05:00:48 +00:00
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
network, err := createTestNetwork(controller, "null", "testnull", options.Generic{}, nil, nil)
|
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-07-02 05:00:48 +00:00
|
|
|
err = ep.Join(cnt)
|
2015-04-30 05:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
err = ep.Leave(cnt)
|
2015-04-30 05:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-01-08 19:24:14 +00:00
|
|
|
if err := ep.Delete(false); err != nil {
|
2015-04-30 05:58:12 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
if err := cnt.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-08 15:24:43 +00:00
|
|
|
// host type is special network. Cannot be removed.
|
|
|
|
err = network.Delete()
|
|
|
|
if err == nil {
|
2015-05-03 20:37:22 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-08 15:24:43 +00:00
|
|
|
if _, ok := err.(types.ForbiddenError); !ok {
|
|
|
|
t.Fatalf("Unexpected error type")
|
|
|
|
}
|
2015-05-03 20:37:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 05:01:29 +00:00
|
|
|
func TestUnknownDriver(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-04-20 11:23:04 +00:00
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
_, err := createTestNetwork(controller, "unknowndriver", "testnetwork", options.Generic{}, nil, nil)
|
2015-04-16 05:01:29 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
|
|
|
|
2020-06-04 09:58:09 +00:00
|
|
|
if !isNotFound(err) {
|
2015-04-16 05:01:29 +00:00
|
|
|
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) {
|
2022-11-08 21:35:14 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
controller := newController(t)
|
|
|
|
|
2016-02-29 19:49:04 +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")
|
|
|
|
}
|
|
|
|
|
2020-06-04 09:58:09 +00:00
|
|
|
if !isNotFound(err) {
|
2015-04-16 05:01:29 +00:00
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkName(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-04-16 05:01:29 +00:00
|
|
|
|
2015-06-08 15:24:43 +00:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 15:24:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
_, err := createTestNetwork(controller, bridgeNetType, "", netOption, nil, nil)
|
2015-05-08 02:59:06 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
2015-05-14 21:56:15 +00:00
|
|
|
|
|
|
|
if _, ok := err.(libnetwork.ErrInvalidName); !ok {
|
|
|
|
t.Fatalf("Expected to fail with ErrInvalidName error. Got %v", err)
|
2015-05-08 02:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
networkName := "testnetwork"
|
2022-11-08 20:21:11 +00:00
|
|
|
n, err := createTestNetwork(controller, bridgeNetType, networkName, netOption, nil, nil)
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-16 05:01:29 +00:00
|
|
|
|
|
|
|
if n.Name() != networkName {
|
|
|
|
t.Fatalf("Expected network name %s, got %s", networkName, n.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkType(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-05-12 23:39:30 +00:00
|
|
|
|
2015-06-08 15:24:43 +00:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 15:24:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
n, err := createTestNetwork(controller, bridgeNetType, "testnetwork", netOption, nil, nil)
|
2015-03-20 20:54:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-03-20 20:54:49 +00:00
|
|
|
|
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) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-04-16 05:01:29 +00:00
|
|
|
|
2015-06-08 15:24:43 +00:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 15:24:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
n, err := createTestNetwork(controller, bridgeNetType, "testnetwork", netOption, nil, nil)
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-16 05:01:29 +00:00
|
|
|
|
|
|
|
if n.ID() == "" {
|
|
|
|
t.Fatal("Expected non-empty network id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteNetworkWithActiveEndpoints(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-05-12 23:39:30 +00:00
|
|
|
|
2015-05-25 04:27:13 +00:00
|
|
|
netOption := options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "testnetwork",
|
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
option := options.Generic{
|
|
|
|
netlabel.GenericData: netOption,
|
|
|
|
}
|
2015-04-16 05:01:29 +00:00
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
network, err := createTestNetwork(controller, bridgeNetType, "testnetwork", option, nil, nil)
|
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.
|
2016-01-08 19:24:14 +00:00
|
|
|
if err := ep.Delete(false); err != nil {
|
2015-04-15 05:25:42 +00:00
|
|
|
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
|
|
|
|
2017-04-07 17:51:39 +00:00
|
|
|
func TestNetworkConfig(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2017-04-07 17:51:39 +00:00
|
|
|
|
|
|
|
// Verify config network cannot inherit another config network
|
2021-05-28 00:15:56 +00:00
|
|
|
_, err := controller.NewNetwork("bridge", "config_network0", "",
|
2017-04-07 17:51:39 +00:00
|
|
|
libnetwork.NetworkOptionConfigOnly(),
|
|
|
|
libnetwork.NetworkOptionConfigFrom("anotherConfigNw"))
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create supported config network
|
|
|
|
netOption := options.Generic{
|
|
|
|
"EnableICC": false,
|
|
|
|
}
|
|
|
|
option := options.Generic{
|
|
|
|
netlabel.GenericData: netOption,
|
|
|
|
}
|
|
|
|
ipamV4ConfList := []*libnetwork.IpamConf{{PreferredPool: "192.168.100.0/24", SubPool: "192.168.100.128/25", Gateway: "192.168.100.1"}}
|
|
|
|
ipamV6ConfList := []*libnetwork.IpamConf{{PreferredPool: "2001:db8:abcd::/64", SubPool: "2001:db8:abcd::ef99/80", Gateway: "2001:db8:abcd::22"}}
|
|
|
|
|
|
|
|
netOptions := []libnetwork.NetworkOption{
|
|
|
|
libnetwork.NetworkOptionConfigOnly(),
|
|
|
|
libnetwork.NetworkOptionEnableIPv6(true),
|
|
|
|
libnetwork.NetworkOptionGeneric(option),
|
|
|
|
libnetwork.NetworkOptionIpam("default", "", ipamV4ConfList, ipamV6ConfList, nil),
|
|
|
|
}
|
|
|
|
|
2021-05-28 00:15:56 +00:00
|
|
|
configNetwork, err := controller.NewNetwork(bridgeNetType, "config_network0", "", netOptions...)
|
2017-04-07 17:51:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify a config-only network cannot be created with network operator configurations
|
|
|
|
for i, opt := range []libnetwork.NetworkOption{
|
|
|
|
libnetwork.NetworkOptionInternalNetwork(),
|
|
|
|
libnetwork.NetworkOptionAttachable(true),
|
|
|
|
libnetwork.NetworkOptionIngress(true),
|
|
|
|
} {
|
|
|
|
_, err = controller.NewNetwork(bridgeNetType, "testBR", "",
|
|
|
|
libnetwork.NetworkOptionConfigOnly(), opt)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected to fail. But instead succeeded for option: %d", i)
|
|
|
|
}
|
|
|
|
if _, ok := err.(types.ForbiddenError); !ok {
|
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify a network cannot be created with both config-from and network specific configurations
|
|
|
|
for i, opt := range []libnetwork.NetworkOption{
|
|
|
|
libnetwork.NetworkOptionEnableIPv6(true),
|
|
|
|
libnetwork.NetworkOptionIpam("my-ipam", "", nil, nil, nil),
|
|
|
|
libnetwork.NetworkOptionIpam("", "", ipamV4ConfList, nil, nil),
|
|
|
|
libnetwork.NetworkOptionIpam("", "", nil, ipamV6ConfList, nil),
|
|
|
|
libnetwork.NetworkOptionLabels(map[string]string{"number": "two"}),
|
2017-08-14 14:22:11 +00:00
|
|
|
libnetwork.NetworkOptionDriverOpts(map[string]string{"com.docker.network.driver.mtu": "1600"}),
|
2017-04-07 17:51:39 +00:00
|
|
|
} {
|
|
|
|
_, err = controller.NewNetwork(bridgeNetType, "testBR", "",
|
|
|
|
libnetwork.NetworkOptionConfigFrom("config_network0"), opt)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected to fail. But instead succeeded for option: %d", i)
|
|
|
|
}
|
|
|
|
if _, ok := err.(types.ForbiddenError); !ok {
|
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a valid network
|
|
|
|
network, err := controller.NewNetwork(bridgeNetType, "testBR", "",
|
|
|
|
libnetwork.NetworkOptionConfigFrom("config_network0"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the config network cannot be removed
|
|
|
|
err = configNetwork.Delete()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete network
|
|
|
|
if err := network.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the config network can now be removed
|
|
|
|
if err := configNetwork.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 05:01:29 +00:00
|
|
|
func TestUnknownNetwork(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-05-12 23:39:30 +00:00
|
|
|
|
2015-05-25 04:27:13 +00:00
|
|
|
netOption := options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "testnetwork",
|
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
option := options.Generic{
|
|
|
|
netlabel.GenericData: netOption,
|
|
|
|
}
|
2015-04-16 05:01:29 +00:00
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
network, err := createTestNetwork(controller, bridgeNetType, "testnetwork", option, nil, nil)
|
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) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-05-12 23:39:30 +00:00
|
|
|
|
2015-05-25 04:27:13 +00:00
|
|
|
netOption := options.Generic{
|
2015-10-05 21:53:25 +00:00
|
|
|
"BridgeName": "testnetwork",
|
2015-09-24 01:01:04 +00:00
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
option := options.Generic{
|
|
|
|
netlabel.GenericData: netOption,
|
|
|
|
}
|
2016-02-12 13:07:00 +00:00
|
|
|
ipamV4ConfList := []*libnetwork.IpamConf{{PreferredPool: "192.168.100.0/24"}}
|
2015-04-16 05:01:29 +00:00
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
network, err := createTestNetwork(controller, bridgeNetType, "testnetwork", option, ipamV4ConfList, nil)
|
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-14 21:56:15 +00:00
|
|
|
if _, ok := err.(libnetwork.ErrInvalidName); !ok {
|
|
|
|
t.Fatalf("Expected to fail with ErrInvalidName error. Actual error: %v", err)
|
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)
|
|
|
|
}
|
|
|
|
|
2016-01-08 19:24:14 +00:00
|
|
|
err = ep.Delete(false)
|
2015-04-16 05:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(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) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-05-12 23:39:30 +00:00
|
|
|
|
2015-04-20 16:46:11 +00:00
|
|
|
// Create network 1 and add 2 endpoint: ep11, ep12
|
2015-06-08 15:24:43 +00:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "network1",
|
2015-06-08 15:24:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
net1, err := createTestNetwork(controller, bridgeNetType, "network1", netOption, nil, nil)
|
2015-04-20 16:46:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
defer func() {
|
|
|
|
if err := net1.Delete(); 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-25 04:27:13 +00:00
|
|
|
defer func() {
|
2016-01-08 19:24:14 +00:00
|
|
|
if err := ep11.Delete(false); err != nil {
|
2015-05-25 04:27:13 +00:00
|
|
|
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)
|
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
defer func() {
|
2016-01-08 19:24:14 +00:00
|
|
|
if err := ep12.Delete(false); err != nil {
|
2015-05-25 04:27:13 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-20 16:46:11 +00: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
|
2023-01-12 01:42:24 +00:00
|
|
|
var epWanted *libnetwork.Endpoint
|
|
|
|
wlk := func(ep *libnetwork.Endpoint) bool {
|
2015-04-20 16:46:11 +00:00
|
|
|
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 15:24:43 +00:00
|
|
|
current := len(controller.Networks())
|
|
|
|
|
|
|
|
// Create network 2
|
|
|
|
netOption = options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "network2",
|
2015-06-08 15:24:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
net2, err := createTestNetwork(controller, bridgeNetType, "network2", netOption, nil, nil)
|
2015-06-08 15:24:43 +00: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 16:46:11 +00: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 15:24:43 +00:00
|
|
|
// Look for network named "network1" and "network2"
|
2015-04-20 16:46:11 +00:00
|
|
|
netName = "network1"
|
|
|
|
controller.WalkNetworks(nwWlk)
|
|
|
|
if netWanted == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
if net1.ID() != netWanted.ID() {
|
2015-04-20 16:46:11 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-08 15:24:43 +00:00
|
|
|
|
|
|
|
netName = "network2"
|
|
|
|
controller.WalkNetworks(nwWlk)
|
|
|
|
if netWanted == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
if net2.ID() != netWanted.ID() {
|
2015-06-08 15:24:43 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-20 16:46:11 +00:00
|
|
|
}
|
2015-04-24 19:05:33 +00:00
|
|
|
|
2015-06-04 16:41:42 +00:00
|
|
|
func TestDuplicateEndpoint(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-06-04 16:41:42 +00:00
|
|
|
|
2015-06-08 15:24:43 +00:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 15:24:43 +00:00
|
|
|
},
|
|
|
|
}
|
2022-11-08 20:21:11 +00:00
|
|
|
n, err := createTestNetwork(controller, bridgeNetType, "testnetwork", netOption, nil, nil)
|
2015-06-04 16:41:42 +00: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() {
|
2016-01-08 19:24:14 +00:00
|
|
|
if err := ep.Delete(false); err != nil {
|
2015-06-04 16:41:42 +00:00
|
|
|
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 {
|
2016-01-08 19:24:14 +00:00
|
|
|
if err := ep2.Delete(false); err != nil {
|
2015-06-04 16:41:42 +00:00
|
|
|
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 19:05:33 +00:00
|
|
|
func TestControllerQuery(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-05-12 23:39:30 +00:00
|
|
|
|
2015-04-24 19:05:33 +00:00
|
|
|
// Create network 1
|
2015-06-08 15:24:43 +00:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "network1",
|
2015-06-08 15:24:43 +00:00
|
|
|
},
|
|
|
|
}
|
2022-11-08 20:21:11 +00:00
|
|
|
net1, err := createTestNetwork(controller, bridgeNetType, "network1", netOption, nil, nil)
|
2015-04-24 19:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
defer func() {
|
|
|
|
if err := net1.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-24 19:05:33 +00:00
|
|
|
|
2015-06-08 15:24:43 +00:00
|
|
|
// Create network 2
|
|
|
|
netOption = options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "network2",
|
2015-06-08 15:24:43 +00:00
|
|
|
},
|
|
|
|
}
|
2022-11-08 20:21:11 +00:00
|
|
|
net2, err := createTestNetwork(controller, bridgeNetType, "network2", netOption, nil, nil)
|
2015-06-08 15:24:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := net2.Delete(); 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-14 21:56:15 +00:00
|
|
|
if _, ok := err.(libnetwork.ErrInvalidName); !ok {
|
|
|
|
t.Fatalf("Expected NetworkByName() to fail with ErrInvalidName error. Got: %v", err)
|
2015-05-11 23:13:27 +00:00
|
|
|
}
|
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")
|
|
|
|
}
|
2015-05-14 21:56:15 +00:00
|
|
|
if _, ok := err.(libnetwork.ErrInvalidID); !ok {
|
2015-05-11 23:13:27 +00:00
|
|
|
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-14 21:56:15 +00:00
|
|
|
if _, ok := err.(libnetwork.ErrNoSuchNetwork); !ok {
|
2015-05-15 23:04:09 +00:00
|
|
|
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-10-05 11:21:15 +00:00
|
|
|
if net1.ID() != g.ID() {
|
2015-04-24 19:05:33 +00:00
|
|
|
t.Fatalf("NetworkByID() returned unexpected element: %v", g)
|
|
|
|
}
|
2015-06-08 15:24:43 +00: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 11:21:15 +00:00
|
|
|
if net2.ID() != g.ID() {
|
2015-06-08 15:24:43 +00:00
|
|
|
t.Fatalf("NetworkByID() returned unexpected element: %v", g)
|
|
|
|
}
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkQuery(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-05-12 23:39:30 +00:00
|
|
|
|
2015-04-24 19:05:33 +00:00
|
|
|
// Create network 1 and add 2 endpoint: ep11, ep12
|
2015-06-08 15:24:43 +00:00
|
|
|
netOption := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "network1",
|
2015-06-08 15:24:43 +00:00
|
|
|
},
|
|
|
|
}
|
2022-11-08 20:21:11 +00:00
|
|
|
net1, err := createTestNetwork(controller, bridgeNetType, "network1", netOption, nil, nil)
|
2015-04-24 19:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
defer func() {
|
|
|
|
if err := net1.Delete(); 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-25 04:27:13 +00:00
|
|
|
defer func() {
|
2016-01-08 19:24:14 +00:00
|
|
|
if err := ep11.Delete(false); err != nil {
|
2015-05-25 04:27:13 +00:00
|
|
|
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-25 04:27:13 +00:00
|
|
|
defer func() {
|
2016-01-08 19:24:14 +00:00
|
|
|
if err := ep12.Delete(false); err != nil {
|
2015-05-25 04:27:13 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-24 19:05:33 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-05-28 00:15:56 +00:00
|
|
|
_, err = net1.EndpointByName("")
|
2015-05-11 23:13:27 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("EndpointByName() succeeded with invalid target name")
|
|
|
|
}
|
2015-05-14 21:56:15 +00:00
|
|
|
if _, ok := err.(libnetwork.ErrInvalidName); !ok {
|
|
|
|
t.Fatalf("Expected EndpointByName() to fail with ErrInvalidName error. Got: %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")
|
|
|
|
}
|
2015-05-14 21:56:15 +00:00
|
|
|
if _, ok := err.(libnetwork.ErrNoSuchEndpoint); !ok {
|
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-10-05 11:21:15 +00:00
|
|
|
if ep12.ID() != e.ID() {
|
2015-04-24 19:05:33 +00:00
|
|
|
t.Fatalf("EndpointByID() returned %v instead of %v", e, ep12)
|
|
|
|
}
|
|
|
|
|
2021-05-28 00:15:56 +00:00
|
|
|
_, err = net1.EndpointByID("")
|
2015-05-11 23:13:27 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("EndpointByID() succeeded with invalid target id")
|
|
|
|
}
|
2015-05-14 21:56:15 +00:00
|
|
|
if _, ok := err.(libnetwork.ErrInvalidID); !ok {
|
2015-05-11 23:13:27 +00:00
|
|
|
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
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
const containerID = "valid_c"
|
2015-04-28 05:57:36 +00:00
|
|
|
|
2016-05-20 20:23:53 +00:00
|
|
|
func TestEndpointDeleteWithActiveContainer(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-09-09 23:20:54 +00:00
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
n, err := createTestNetwork(controller, bridgeNetType, "testnetwork", options.Generic{
|
2015-09-09 23:20:54 +00:00
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "testnetwork",
|
2015-09-09 23:20:54 +00:00
|
|
|
},
|
2015-10-05 21:53:25 +00:00
|
|
|
}, nil, nil)
|
2015-09-09 23:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
n2, err := createTestNetwork(controller, bridgeNetType, "testnetwork2", options.Generic{
|
2016-10-13 18:14:39 +00:00
|
|
|
netlabel.GenericData: options.Generic{
|
|
|
|
"BridgeName": "testnetwork2",
|
|
|
|
},
|
|
|
|
}, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := n2.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-09-09 23:20:54 +00:00
|
|
|
ep, err := n.CreateEndpoint("ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2016-01-08 19:24:14 +00:00
|
|
|
err = ep.Delete(false)
|
2015-09-09 23:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cnt, err := controller.NewSandbox(containerID,
|
|
|
|
libnetwork.OptionHostname("test"),
|
2023-01-10 14:16:51 +00:00
|
|
|
libnetwork.OptionDomainname("example.com"),
|
2015-09-09 23:20:54 +00:00
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"))
|
|
|
|
defer func() {
|
|
|
|
if err := cnt.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = ep.Join(cnt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = ep.Leave(cnt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-05-20 20:23:53 +00:00
|
|
|
err = ep.Delete(false)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
2015-09-09 23:20:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 20:23:53 +00:00
|
|
|
if _, ok := err.(*libnetwork.ActiveContainerError); !ok {
|
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
2015-09-09 23:20:54 +00:00
|
|
|
}
|
2016-05-20 20:23:53 +00:00
|
|
|
}
|
2015-05-06 20:02:40 +00:00
|
|
|
|
2015-04-28 05:57:36 +00:00
|
|
|
func TestEndpointMultipleJoins(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-04-28 05:57:36 +00:00
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
n, err := createTestNetwork(controller, bridgeNetType, "testmultiple", options.Generic{
|
2015-06-08 15:24:43 +00:00
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "testmultiple",
|
2015-06-08 15:24:43 +00:00
|
|
|
},
|
2015-10-05 21:53:25 +00:00
|
|
|
}, nil, nil)
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 05:57:36 +00:00
|
|
|
|
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-25 04:27:13 +00:00
|
|
|
defer func() {
|
2016-01-08 19:24:14 +00:00
|
|
|
if err := ep.Delete(false); err != nil {
|
2015-05-25 04:27:13 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 05:57:36 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
sbx1, err := controller.NewSandbox(containerID,
|
|
|
|
libnetwork.OptionHostname("test"),
|
2023-01-10 14:16:51 +00:00
|
|
|
libnetwork.OptionDomainname("example.com"),
|
2022-01-28 11:28:39 +00:00
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-02 05:00:48 +00:00
|
|
|
defer func() {
|
|
|
|
if err := sbx1.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-06-20 01:41:31 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
sbx2, err := controller.NewSandbox("c2")
|
2022-01-28 11:28:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-02 05:00:48 +00:00
|
|
|
defer func() {
|
|
|
|
if err := sbx2.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = ep.Join(sbx1)
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-07 20:03:47 +00:00
|
|
|
defer func() {
|
2015-07-02 05:00:48 +00:00
|
|
|
err = ep.Leave(sbx1)
|
2015-05-07 20:03:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 05:57:36 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
err = ep.Join(sbx2)
|
2015-04-28 05:57:36 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail multiple joins for the same endpoint")
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
if _, ok := err.(types.ForbiddenError); !ok {
|
|
|
|
t.Fatalf("Failed with unexpected error type: %T. Desc: %s", err, err.Error())
|
2015-04-28 05:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-20 01:41:31 +00:00
|
|
|
func TestLeaveAll(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-06-20 01:41:31 +00:00
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
n, err := createTestNetwork(controller, bridgeNetType, "testnetwork", options.Generic{
|
2015-06-20 01:41:31 +00:00
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-20 01:41:31 +00:00
|
|
|
},
|
2015-10-05 21:53:25 +00:00
|
|
|
}, nil, nil)
|
2015-06-20 01:41:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2015-07-02 05:00:48 +00:00
|
|
|
// If this goes through, it means cnt.Delete() effectively detached from all the endpoints
|
2015-06-20 01:41:31 +00:00
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
n2, err := createTestNetwork(controller, bridgeNetType, "testnetwork2", options.Generic{
|
2016-10-13 18:14:39 +00:00
|
|
|
netlabel.GenericData: options.Generic{
|
|
|
|
"BridgeName": "testnetwork2",
|
|
|
|
},
|
|
|
|
}, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := n2.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-06-20 01:41:31 +00:00
|
|
|
ep1, err := n.CreateEndpoint("ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:14:39 +00:00
|
|
|
ep2, err := n2.CreateEndpoint("ep2")
|
2015-06-20 01:41:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
cnt, err := controller.NewSandbox("leaveall")
|
2015-06-20 01:41:31 +00:00
|
|
|
if err != nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal(err)
|
2015-06-20 01:41:31 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
err = ep1.Join(cnt)
|
2015-06-20 01:41:31 +00:00
|
|
|
if err != nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatalf("Failed to join ep1: %v", err)
|
2015-06-20 01:41:31 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
err = ep2.Join(cnt)
|
2015-06-20 01:41:31 +00:00
|
|
|
if err != nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatalf("Failed to join ep2: %v", err)
|
2015-06-20 01:41:31 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
err = cnt.Delete()
|
2015-06-20 01:41:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 18:09:12 +00:00
|
|
|
func TestContainerInvalidLeave(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-04-28 05:57:36 +00:00
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
n, err := createTestNetwork(controller, bridgeNetType, "testnetwork", options.Generic{
|
2015-06-08 15:24:43 +00:00
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 15:24:43 +00:00
|
|
|
},
|
2015-10-05 21:53:25 +00:00
|
|
|
}, nil, nil)
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 05:57:36 +00:00
|
|
|
|
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-25 04:27:13 +00:00
|
|
|
defer func() {
|
2016-01-08 19:24:14 +00:00
|
|
|
if err := ep.Delete(false); err != nil {
|
2015-05-25 04:27:13 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 05:57:36 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
cnt, err := controller.NewSandbox(containerID,
|
|
|
|
libnetwork.OptionHostname("test"),
|
2023-01-10 14:16:51 +00:00
|
|
|
libnetwork.OptionDomainname("example.com"),
|
2015-07-02 05:00:48 +00:00
|
|
|
libnetwork.OptionExtraHost("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() {
|
2015-07-02 05:00:48 +00:00
|
|
|
if err := cnt.Delete(); err != nil {
|
2015-05-07 20:03:47 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-04-28 05:57:36 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
err = ep.Leave(cnt)
|
2015-04-28 05:57:36 +00:00
|
|
|
if err == nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatal("Expected to fail leave from an endpoint which has no active join")
|
2015-04-28 05:57:36 +00:00
|
|
|
}
|
2015-07-02 05:00:48 +00:00
|
|
|
if _, ok := err.(types.ForbiddenError); !ok {
|
|
|
|
t.Fatalf("Failed with unexpected error type: %T. Desc: %s", err, err.Error())
|
2015-04-28 05:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 18:09:12 +00:00
|
|
|
if err = ep.Leave(nil); err == nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
t.Fatalf("Expected to fail leave nil Sandbox")
|
|
|
|
}
|
|
|
|
if _, ok := err.(types.BadRequestError); !ok {
|
2016-09-20 18:09:12 +00:00
|
|
|
t.Fatalf("Unexpected error type returned: %T. Desc: %s", err, err.Error())
|
2015-04-28 05:57:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
fsbx := &libnetwork.Sandbox{}
|
2015-07-02 05:00:48 +00:00
|
|
|
if err = ep.Leave(fsbx); err == nil {
|
|
|
|
t.Fatalf("Expected to fail leave with invalid Sandbox")
|
|
|
|
}
|
|
|
|
if _, ok := err.(types.BadRequestError); !ok {
|
2016-09-20 18:09:12 +00:00
|
|
|
t.Fatalf("Unexpected error type returned: %T. Desc: %s", err, err.Error())
|
2015-04-28 05:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-03 20:29:43 +00:00
|
|
|
|
|
|
|
func TestEndpointUpdateParent(t *testing.T) {
|
2022-11-05 01:27:38 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2015-05-03 20:29:43 +00:00
|
|
|
|
2022-11-08 20:21:11 +00:00
|
|
|
n, err := createTestNetwork(controller, bridgeNetType, "testnetwork", options.Generic{
|
2015-06-08 15:24:43 +00:00
|
|
|
netlabel.GenericData: options.Generic{
|
2015-09-24 01:01:04 +00:00
|
|
|
"BridgeName": "testnetwork",
|
2015-06-08 15:24:43 +00:00
|
|
|
},
|
2015-10-05 21:53:25 +00:00
|
|
|
}, nil, nil)
|
2015-05-03 20:29:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-03 20:29:43 +00:00
|
|
|
|
2015-05-25 04:27:13 +00:00
|
|
|
ep1, err := n.CreateEndpoint("ep1")
|
2015-05-03 20:29:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
ep2, err := n.CreateEndpoint("ep2")
|
2015-05-03 20:29:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
sbx1, err := controller.NewSandbox(containerID,
|
|
|
|
libnetwork.OptionHostname("test"),
|
2023-01-10 14:16:51 +00:00
|
|
|
libnetwork.OptionDomainname("example.com"),
|
2015-07-02 05:00:48 +00:00
|
|
|
libnetwork.OptionExtraHost("web", "192.168.0.1"))
|
2015-05-03 20:29:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
defer func() {
|
2015-07-02 05:00:48 +00:00
|
|
|
if err := sbx1.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
sbx2, err := controller.NewSandbox("c2",
|
|
|
|
libnetwork.OptionHostname("test2"),
|
2023-01-10 14:16:51 +00:00
|
|
|
libnetwork.OptionDomainname("example.com"),
|
2015-07-02 05:00:48 +00:00
|
|
|
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 04:27:13 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-03 20:29:43 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
err = ep1.Join(sbx1)
|
2015-05-03 20:29:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
err = ep2.Join(sbx2)
|
2015-06-20 01:41:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-03 20:29:43 +00:00
|
|
|
}
|
2015-05-06 06:41:20 +00:00
|
|
|
|
2015-05-16 01:14:36 +00:00
|
|
|
func TestInvalidRemoteDriver(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
if server == nil {
|
2016-06-02 05:37:39 +00:00
|
|
|
t.Fatal("Failed to start an HTTP Server")
|
2015-05-16 01:14:36 +00:00
|
|
|
}
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
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"]}`)
|
|
|
|
})
|
|
|
|
|
2022-01-20 13:10:24 +00:00
|
|
|
if err := os.MkdirAll(specPath, 0o755); err != nil {
|
2015-05-16 01:14:36 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2021-04-27 22:40:39 +00:00
|
|
|
if err := os.RemoveAll(specPath); err != nil {
|
2015-05-16 01:14:36 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-01-20 13:10:24 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(specPath, "invalid-network-driver.spec"), []byte(server.URL), 0o644); err != nil {
|
2015-05-16 01:14:36 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-09-22 21:07:23 +00:00
|
|
|
ctrlr, err := libnetwork.New()
|
2015-05-16 01:14:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-22 21:07:23 +00:00
|
|
|
defer ctrlr.Stop()
|
2015-05-16 01:14:36 +00:00
|
|
|
|
2016-02-29 19:49:04 +00:00
|
|
|
_, err = ctrlr.NewNetwork("invalid-network-driver", "dummy", "",
|
2015-05-16 01:14:36 +00:00
|
|
|
libnetwork.NetworkOptionGeneric(getEmptyGenericOption()))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected to fail. But instead succeeded")
|
|
|
|
}
|
|
|
|
|
2021-10-25 12:57:18 +00:00
|
|
|
if !errors.Is(err, plugins.ErrNotImplements) {
|
2015-05-16 01:14:36 +00:00
|
|
|
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidRemoteDriver(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
if server == nil {
|
2016-06-02 05:37:39 +00:00
|
|
|
t.Fatal("Failed to start an HTTP Server")
|
2015-05-16 01:14:36 +00:00
|
|
|
}
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
2019-04-10 13:27:17 +00:00
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.GetCapabilities", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
|
|
|
fmt.Fprintf(w, `{"Scope":"local"}`)
|
|
|
|
})
|
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 16:27:21 +00: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")
|
|
|
|
})
|
2019-04-10 16:23:51 +00:00
|
|
|
mux.HandleFunc(fmt.Sprintf("/%s.DeleteNetwork", 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-16 01:14:36 +00:00
|
|
|
|
2022-01-20 13:10:24 +00:00
|
|
|
if err := os.MkdirAll(specPath, 0o755); err != nil {
|
2015-05-16 01:14:36 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2021-04-27 22:40:39 +00:00
|
|
|
if err := os.RemoveAll(specPath); err != nil {
|
2015-05-16 01:14:36 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-01-20 13:10:24 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(specPath, "valid-network-driver.spec"), []byte(server.URL), 0o644); err != nil {
|
2015-05-16 01:14:36 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:35:14 +00:00
|
|
|
controller := newController(t)
|
2016-02-29 19:49:04 +00:00
|
|
|
n, err := controller.NewNetwork("valid-network-driver", "dummy", "",
|
2015-05-16 01:14:36 +00:00
|
|
|
libnetwork.NetworkOptionGeneric(getEmptyGenericOption()))
|
2015-05-14 21:56:15 +00:00
|
|
|
if err != nil {
|
2015-05-25 04:27:13 +00:00
|
|
|
// Only fail if we could not find the plugin driver
|
2020-06-04 09:58:09 +00:00
|
|
|
if isNotFound(err) {
|
2015-05-25 04:27:13 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return
|
2015-05-16 01:14:36 +00:00
|
|
|
}
|
2015-05-25 04:27:13 +00:00
|
|
|
defer func() {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-16 01:14:36 +00:00
|
|
|
}
|