moby/libnetwork/drvregistry/networks_test.go
Sebastiaan van Stijn 6ec03d6745
libnetwork: move datastore Scope consts to libnetwork/scope
The "Capability" type defines DataScope and ConnectivityScope fields,
but their value was set from consts in the datastore package, which
required importing that package and its dependencies for the consts
only.

This patch:

- Moves the consts to a separate "scope" package
- Adds aliases for the consts in the datastore package.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-28 21:56:48 +02:00

70 lines
1.6 KiB
Go

package drvregistry
import (
"testing"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/scope"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
const mockDriverName = "mock-driver"
type mockDriver struct {
driverapi.Driver
}
var mockDriverCaps = driverapi.Capability{DataScope: scope.Local}
var md = mockDriver{}
func (m *mockDriver) Type() string {
return mockDriverName
}
func (m *mockDriver) IsBuiltIn() bool {
return true
}
func TestNetworks(t *testing.T) {
t.Run("RegisterDriver", func(t *testing.T) {
var reg Networks
err := reg.RegisterDriver(mockDriverName, &md, mockDriverCaps)
assert.NilError(t, err)
})
t.Run("RegisterDuplicateDriver", func(t *testing.T) {
var reg Networks
err := reg.RegisterDriver(mockDriverName, &md, mockDriverCaps)
assert.NilError(t, err)
// Try adding the same driver
err = reg.RegisterDriver(mockDriverName, &md, mockDriverCaps)
assert.Check(t, is.ErrorContains(err, ""))
})
t.Run("Driver", func(t *testing.T) {
var reg Networks
err := reg.RegisterDriver(mockDriverName, &md, mockDriverCaps)
assert.NilError(t, err)
d, cap := reg.Driver(mockDriverName)
assert.Check(t, d != nil)
assert.Check(t, is.DeepEqual(cap, mockDriverCaps))
})
t.Run("WalkDrivers", func(t *testing.T) {
var reg Networks
err := reg.RegisterDriver(mockDriverName, &md, mockDriverCaps)
assert.NilError(t, err)
var driverName string
reg.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
driverName = name
return false
})
assert.Check(t, is.Equal(driverName, mockDriverName))
})
}