2016-02-29 21:17:04 -05:00
|
|
|
package drvregistry
|
|
|
|
|
|
|
|
import (
|
2021-05-25 19:48:54 -04:00
|
|
|
"runtime"
|
2016-02-29 21:17:04 -05:00
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/docker/libnetwork/discoverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/ipamapi"
|
|
|
|
builtinIpam "github.com/docker/docker/libnetwork/ipams/builtin"
|
|
|
|
nullIpam "github.com/docker/docker/libnetwork/ipams/null"
|
|
|
|
remoteIpam "github.com/docker/docker/libnetwork/ipams/remote"
|
2020-08-31 11:55:22 -04:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2018-05-18 17:10:14 -04:00
|
|
|
)
|
2016-02-29 21:17:04 -05:00
|
|
|
|
|
|
|
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})
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:55:39 -04:00
|
|
|
func (m *mockDriver) CreateNetwork(nid string, options map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
|
2016-02-29 21:17:04 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-12-18 23:01:09 -05:00
|
|
|
func (m *mockDriver) IsBuiltIn() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
func (m *mockDriver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockDriver) RevokeExternalConnectivity(nid, eid string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-26 14:54:35 -05:00
|
|
|
func (m *mockDriver) NetworkAllocate(id string, option map[string]string, ipV4Data, ipV6Data []driverapi.IPAMData) (map[string]string, error) {
|
2016-04-13 04:04:58 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockDriver) NetworkFree(id string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:55:39 -04:00
|
|
|
func (m *mockDriver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
|
|
|
|
}
|
|
|
|
|
2017-03-02 02:57:37 -05:00
|
|
|
func (m *mockDriver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
func getNew(t *testing.T) *DrvRegistry {
|
2016-09-27 16:54:25 -04:00
|
|
|
reg, err := New(nil, nil, nil, nil, nil)
|
2016-02-29 21:17:04 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-07-05 16:49:31 -04:00
|
|
|
err = initIPAMDrivers(reg, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-02-29 21:17:04 -05:00
|
|
|
return reg
|
|
|
|
}
|
|
|
|
|
2016-07-05 16:49:31 -04:00
|
|
|
func initIPAMDrivers(r *DrvRegistry, lDs, gDs interface{}) error {
|
|
|
|
for _, fn := range [](func(ipamapi.Callback, interface{}, interface{}) error){
|
|
|
|
builtinIpam.Init,
|
|
|
|
remoteIpam.Init,
|
|
|
|
nullIpam.Init,
|
|
|
|
} {
|
|
|
|
if err := fn(r, lDs, gDs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-29 21:17:04 -05:00
|
|
|
func TestNew(t *testing.T) {
|
|
|
|
getNew(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddDriver(t *testing.T) {
|
|
|
|
reg := getNew(t)
|
|
|
|
|
|
|
|
err := reg.AddDriver(mockDriverName, mockDriverInit, nil)
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-02-29 21:17:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddDuplicateDriver(t *testing.T) {
|
|
|
|
reg := getNew(t)
|
|
|
|
|
|
|
|
err := reg.AddDriver(mockDriverName, mockDriverInit, nil)
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-02-29 21:17:04 -05:00
|
|
|
|
|
|
|
// Try adding the same driver
|
|
|
|
err = reg.AddDriver(mockDriverName, mockDriverInit, nil)
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, ""))
|
2016-02-29 21:17:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIPAMDefaultAddressSpaces(t *testing.T) {
|
|
|
|
reg := getNew(t)
|
|
|
|
|
|
|
|
as1, as2, err := reg.IPAMDefaultAddressSpaces("default")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, as1 != "")
|
|
|
|
assert.Check(t, as2 != "")
|
2016-02-29 21:17:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDriver(t *testing.T) {
|
|
|
|
reg := getNew(t)
|
|
|
|
|
|
|
|
err := reg.AddDriver(mockDriverName, mockDriverInit, nil)
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-02-29 21:17:04 -05:00
|
|
|
|
|
|
|
d, cap := reg.Driver(mockDriverName)
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, d != nil)
|
|
|
|
assert.Check(t, cap != nil)
|
2016-02-29 21:17:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIPAM(t *testing.T) {
|
|
|
|
reg := getNew(t)
|
|
|
|
|
|
|
|
i, cap := reg.IPAM("default")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, i != nil)
|
|
|
|
assert.Check(t, cap != nil)
|
2016-02-29 21:17:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-05-25 19:48:54 -04:00
|
|
|
expected := []string{"default", "null"}
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expected = append(expected, "windows")
|
|
|
|
}
|
|
|
|
assert.Check(t, is.DeepEqual(ipams, expected))
|
2016-02-29 21:17:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWalkDrivers(t *testing.T) {
|
|
|
|
reg := getNew(t)
|
|
|
|
|
|
|
|
err := reg.AddDriver(mockDriverName, mockDriverInit, nil)
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-02-29 21:17:04 -05:00
|
|
|
|
|
|
|
var driverName string
|
|
|
|
reg.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
|
|
|
|
driverName = name
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Equal(driverName, mockDriverName))
|
2016-02-29 21:17:04 -05:00
|
|
|
}
|