pkg/plugins: make package-level socketsPath var a LocalRegistry field

This variable was only accessed from within LocalRegistry methods, but
due to being a package-level variable, tests had to deal with setting
and resetting it.

Move it to be a field scoped to the LocalRegistry. This simplifies the
tests, and to make this more transparent, also removing the "Setup()"
helper (which, wasn't marked as a t.Helper()).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-07-18 23:38:21 +02:00
parent 5bd44cf3c4
commit e1ad4aa002
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
4 changed files with 46 additions and 38 deletions

View file

@ -13,34 +13,35 @@ import (
"github.com/pkg/errors"
)
var (
// ErrNotFound plugin not found
ErrNotFound = errors.New("plugin not found")
socketsPath = "/run/docker/plugins"
)
// ErrNotFound plugin not found
var ErrNotFound = errors.New("plugin not found")
const defaultSocketsPath = "/run/docker/plugins"
// LocalRegistry defines a registry that is local (using unix socket).
type LocalRegistry struct {
specsPaths []string
socketsPath string
specsPaths []string
}
func NewLocalRegistry() LocalRegistry {
return LocalRegistry{
specsPaths: specsPaths(),
socketsPath: defaultSocketsPath,
specsPaths: specsPaths(),
}
}
// Scan scans all the plugin paths and returns all the names it found
func (l *LocalRegistry) Scan() ([]string, error) {
var names []string
dirEntries, err := os.ReadDir(socketsPath)
dirEntries, err := os.ReadDir(l.socketsPath)
if err != nil && !os.IsNotExist(err) {
return nil, errors.Wrap(err, "error reading dir entries")
}
for _, entry := range dirEntries {
if entry.IsDir() {
fi, err := os.Stat(filepath.Join(socketsPath, entry.Name(), entry.Name()+".sock"))
fi, err := os.Stat(filepath.Join(l.socketsPath, entry.Name(), entry.Name()+".sock"))
if err != nil {
continue
}
@ -87,7 +88,7 @@ func (l *LocalRegistry) Scan() ([]string, error) {
// Plugin returns the plugin registered with the given name (or returns an error).
func (l *LocalRegistry) Plugin(name string) (*Plugin, error) {
socketPaths := pluginPaths(socketsPath, name, ".sock")
socketPaths := pluginPaths(l.socketsPath, name, ".sock")
for _, p := range socketPaths {
if fi, err := os.Stat(p); err == nil && fi.Mode()&os.ModeSocket != 0 {
return NewLocalPlugin(name, "unix://"+p), nil

View file

@ -6,23 +6,12 @@ import (
"testing"
)
func Setup(t *testing.T) (string, func(), LocalRegistry) {
tmpdir, err := os.MkdirTemp("", "docker-test")
if err != nil {
t.Fatal(err)
}
backup := socketsPath
socketsPath = tmpdir
return tmpdir, func() {
socketsPath = backup
os.RemoveAll(tmpdir)
}, LocalRegistry{specsPaths: []string{tmpdir}}
}
func TestFileSpecPlugin(t *testing.T) {
tmpdir, unregister, r := Setup(t)
defer unregister()
tmpdir := t.TempDir()
r := LocalRegistry{
socketsPath: tmpdir,
specsPaths: []string{tmpdir},
}
cases := []struct {
path string
@ -70,8 +59,11 @@ func TestFileSpecPlugin(t *testing.T) {
}
func TestFileJSONSpecPlugin(t *testing.T) {
tmpdir, unregister, r := Setup(t)
defer unregister()
tmpdir := t.TempDir()
r := LocalRegistry{
socketsPath: tmpdir,
specsPaths: []string{tmpdir},
}
p := filepath.Join(tmpdir, "example.json")
spec := `{
@ -115,8 +107,11 @@ func TestFileJSONSpecPlugin(t *testing.T) {
}
func TestFileJSONSpecPluginWithoutTLSConfig(t *testing.T) {
tmpdir, unregister, r := Setup(t)
defer unregister()
tmpdir := t.TempDir()
r := LocalRegistry{
socketsPath: tmpdir,
specsPaths: []string{tmpdir},
}
p := filepath.Join(tmpdir, "example.json")
spec := `{

View file

@ -15,8 +15,11 @@ import (
func TestLocalSocket(t *testing.T) {
// TODO Windows: Enable a similar version for Windows named pipes
tmpdir, unregister, r := Setup(t)
defer unregister()
tmpdir := t.TempDir()
r := LocalRegistry{
socketsPath: tmpdir,
specsPaths: []string{tmpdir},
}
cases := []string{
filepath.Join(tmpdir, "echo.sock"),
@ -62,8 +65,11 @@ func TestLocalSocket(t *testing.T) {
}
func TestScan(t *testing.T) {
tmpdir, unregister, r := Setup(t)
defer unregister()
tmpdir := t.TempDir()
r := LocalRegistry{
socketsPath: tmpdir,
specsPaths: []string{tmpdir},
}
pluginNames, err := r.Scan()
if err != nil {
@ -103,8 +109,11 @@ func TestScan(t *testing.T) {
}
func TestScanNotPlugins(t *testing.T) {
tmpdir, unregister, localRegistry := Setup(t)
defer unregister()
tmpdir := t.TempDir()
localRegistry := LocalRegistry{
socketsPath: tmpdir,
specsPaths: []string{tmpdir},
}
// not that `Setup()` above sets the sockets path and spec path dirs, which
// `Scan()` uses to find plugins to the returned `tmpdir`

View file

@ -127,8 +127,11 @@ func TestPluginWithNoManifest(t *testing.T) {
func TestGetAll(t *testing.T) {
t.Parallel()
tmpdir, unregister, r := Setup(t)
defer unregister()
tmpdir := t.TempDir()
r := LocalRegistry{
socketsPath: tmpdir,
specsPaths: []string{tmpdir},
}
p := filepath.Join(tmpdir, "example.json")
spec := `{