moby/libnetwork/drvregistry/ipams_test.go
Cory Snider 5595311209 libnetwork/drvregistry: split up the registries
There is no benefit to having a single registry for both IPAM drivers
and network drivers. IPAM drivers are registered in a separate namespace
from network drivers, have separate registration methods, separate
accessor methods and do not interact with network drivers within a
DrvRegistry in any way. The only point of commonality is

    interface { GetPluginGetter() plugingetter.PluginGetter }

which is only used by the respective remote drivers and therefore should
be outside of the scope of a driver registry.

Create new, separate registry types for network drivers and IPAM
drivers, respectively. These types are "legacy-free". Neither type has
GetPluginGetter methods. The IPAMs registry does not have an
IPAMDefaultAddressSpaces method as that information can be queried
directly from the driver using its GetDefaultAddressSpaces method.
The Networks registry does not have an AddDriver method as that method
is a trivial wrapper around calling one of its arguments with its other
arguments.

Refactor DrvRegistry in terms of the new IPAMs and Networks registries
so that existing code in libnetwork and Swarmkit will continue to work.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-27 11:47:42 -05:00

51 lines
1.2 KiB
Go

package drvregistry
import (
"runtime"
"sort"
"testing"
"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"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func getNewIPAMs(t *testing.T) *IPAMs {
r := &IPAMs{}
assert.Assert(t, builtinIpam.Register(r))
assert.Assert(t, remoteIpam.Register(r, nil))
assert.Assert(t, nullIpam.Register(r))
return r
}
func TestIPAMs(t *testing.T) {
t.Run("IPAM", func(t *testing.T) {
reg := getNewIPAMs(t)
i, cap := reg.IPAM("default")
assert.Check(t, i != nil)
assert.Check(t, cap != nil)
})
t.Run("WalkIPAMs", func(t *testing.T) {
reg := getNewIPAMs(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)
expected := []string{"default", "null"}
if runtime.GOOS == "windows" {
expected = append(expected, "windows")
}
assert.Check(t, is.DeepEqual(ipams, expected))
})
}