1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/libnetwork/drvregistry/drvregistry_test.go
Jana Radhakrishnan 6fb69f0816 Add driver api enhancements for gossip
With the introduction of a driver generic gossip in libnetwork it is not
necessary for drivers to run their own gossip protocol (like what
overlay driver is doing currently) but instead rely on the gossip
instance run centrally in libnetwork. In order to achieve this, certain
enhancements to driver api are needed. This api aims to provide these
enhancements.

The new api provides a way for drivers to register interest on table
names of their choice by returning a list of table names of interest as
a response to CreateNetwork. By doing that they will get notified if a
CRUD operation happened on the tables of their interest, via the newly
added EventNotify call.

Drivers themselves can add entries to any table during a Join call by
invoking AddTableEntry method any number of times during the Join
call. These entries lifetime is the same as the endpoint itself. As soon
as the container leaves the endpoint, those entries added by driver
during that endpoint's Join call will be automatically removed by
libnetwork. This action may trigger notification of such deletion to all
driver instances in the cluster who have registered interest in that
table's notification.

Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
2016-04-18 19:55:39 -07:00

172 lines
4 KiB
Go

package drvregistry
import (
"flag"
"sort"
"testing"
"github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/discoverapi"
"github.com/docker/libnetwork/driverapi"
"github.com/docker/libnetwork/ipamapi"
"github.com/stretchr/testify/assert"
)
var runningInContainer = flag.Bool("incontainer", false,
"Indicates if the test is running in a container")
const mockDriverName = "mock-driver"
type mockDriver struct{}
var md = mockDriver{}
func mockDriverInit(reg driverapi.DriverCallback, opt map[string]interface{}) error {
return reg.RegisterDriver(mockDriverName, &md, driverapi.Capability{DataScope: datastore.LocalScope})
}
func (m *mockDriver) CreateNetwork(nid string, options map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
return nil
}
func (m *mockDriver) DeleteNetwork(nid string) error {
return nil
}
func (m *mockDriver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, options map[string]interface{}) error {
return nil
}
func (m *mockDriver) DeleteEndpoint(nid, eid string) error {
return nil
}
func (m *mockDriver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
return nil, nil
}
func (m *mockDriver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
return nil
}
func (m *mockDriver) Leave(nid, eid string) error {
return nil
}
func (m *mockDriver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}
func (m *mockDriver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}
func (m *mockDriver) Type() string {
return mockDriverName
}
func (m *mockDriver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
return nil
}
func (m *mockDriver) RevokeExternalConnectivity(nid, eid string) error {
return nil
}
func (m *mockDriver) NetworkAllocate(id string, option map[string]string, ipV4Data, ipV6Data []driverapi.IPAMData) (map[string]string, error) {
return nil, nil
}
func (m *mockDriver) NetworkFree(id string) error {
return nil
}
func (m *mockDriver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func getNew(t *testing.T) *DrvRegistry {
reg, err := New(nil, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
return reg
}
func TestNew(t *testing.T) {
getNew(t)
}
func TestAddDriver(t *testing.T) {
reg := getNew(t)
err := reg.AddDriver(mockDriverName, mockDriverInit, nil)
assert.NoError(t, err)
}
func TestAddDuplicateDriver(t *testing.T) {
reg := getNew(t)
err := reg.AddDriver(mockDriverName, mockDriverInit, nil)
assert.NoError(t, err)
// Try adding the same driver
err = reg.AddDriver(mockDriverName, mockDriverInit, nil)
assert.Error(t, err)
}
func TestIPAMDefaultAddressSpaces(t *testing.T) {
reg := getNew(t)
as1, as2, err := reg.IPAMDefaultAddressSpaces("default")
assert.NoError(t, err)
assert.NotEqual(t, as1, "")
assert.NotEqual(t, as2, "")
}
func TestDriver(t *testing.T) {
reg := getNew(t)
err := reg.AddDriver(mockDriverName, mockDriverInit, nil)
assert.NoError(t, err)
d, cap := reg.Driver(mockDriverName)
assert.NotEqual(t, d, nil)
assert.NotEqual(t, cap, nil)
}
func TestIPAM(t *testing.T) {
reg := getNew(t)
i, cap := reg.IPAM("default")
assert.NotEqual(t, i, nil)
assert.NotEqual(t, cap, nil)
}
func TestWalkIPAMs(t *testing.T) {
reg := getNew(t)
ipams := make([]string, 0, 2)
reg.WalkIPAMs(func(name string, driver ipamapi.Ipam, cap *ipamapi.Capability) bool {
ipams = append(ipams, name)
return false
})
sort.Strings(ipams)
assert.Equal(t, ipams, []string{"default", "null"})
}
func TestWalkDrivers(t *testing.T) {
reg := getNew(t)
err := reg.AddDriver(mockDriverName, mockDriverInit, nil)
assert.NoError(t, err)
var driverName string
reg.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
driverName = name
return false
})
assert.Equal(t, driverName, mockDriverName)
}