1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
//go:build !windows
// +build !windows
package plugins // import "github.com/docker/docker/pkg/plugins"
import (
"fmt"
"net"
"os"
"path/filepath"
"reflect"
"testing"
"gotest.tools/v3/assert"
)
func TestLocalSocket(t *testing.T) {
// TODO Windows: Enable a similar version for Windows named pipes
tmpdir, unregister := Setup(t)
defer unregister()
cases := []string{
filepath.Join(tmpdir, "echo.sock"),
filepath.Join(tmpdir, "echo", "echo.sock"),
}
for _, c := range cases {
if err := os.MkdirAll(filepath.Dir(c), 0755); err != nil {
t.Fatal(err)
}
l, err := net.Listen("unix", c)
if err != nil {
t.Fatal(err)
}
r := newLocalRegistry()
p, err := r.Plugin("echo")
if err != nil {
t.Fatal(err)
}
pp, err := r.Plugin("echo")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(p, pp) {
t.Fatalf("Expected %v, was %v\n", p, pp)
}
if p.name != "echo" {
t.Fatalf("Expected plugin `echo`, got %s\n", p.name)
}
addr := fmt.Sprintf("unix://%s", c)
if p.Addr != addr {
t.Fatalf("Expected plugin addr `%s`, got %s\n", addr, p.Addr)
}
if !p.TLSConfig.InsecureSkipVerify {
t.Fatalf("Expected TLS verification to be skipped")
}
l.Close()
}
}
func TestScan(t *testing.T) {
tmpdir, unregister := Setup(t)
defer unregister()
pluginNames, err := Scan()
if err != nil {
t.Fatal(err)
}
if pluginNames != nil {
t.Fatal("Plugin names should be empty.")
}
path := filepath.Join(tmpdir, "echo.spec")
addr := "unix://var/lib/docker/plugins/echo.sock"
name := "echo"
err = os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(path, []byte(addr), 0644)
if err != nil {
t.Fatal(err)
}
r := newLocalRegistry()
p, err := r.Plugin(name)
assert.NilError(t, err)
pluginNamesNotEmpty, err := Scan()
if err != nil {
t.Fatal(err)
}
if len(pluginNamesNotEmpty) != 1 {
t.Fatalf("expected 1 plugin entry: %v", pluginNamesNotEmpty)
}
if p.Name() != pluginNamesNotEmpty[0] {
t.Fatalf("Unable to scan plugin with name %s", p.name)
}
}
func TestScanNotPlugins(t *testing.T) {
tmpdir, unregister := Setup(t)
defer unregister()
// not that `Setup()` above sets the sockets path and spec path dirs, which
// `Scan()` uses to find plugins to the returned `tmpdir`
notPlugin := filepath.Join(tmpdir, "not-a-plugin")
if err := os.MkdirAll(notPlugin, 0700); err != nil {
t.Fatal(err)
}
// this is named differently than the dir it's in, so the scanner should ignore it
l, err := net.Listen("unix", filepath.Join(notPlugin, "foo.sock"))
if err != nil {
t.Fatal(err)
}
defer l.Close()
// same let's test a spec path
f, err := os.Create(filepath.Join(notPlugin, "foo.spec"))
if err != nil {
t.Fatal(err)
}
defer f.Close()
names, err := Scan()
if err != nil {
t.Fatal(err)
}
if len(names) != 0 {
t.Fatalf("expected no plugins, got %v", names)
}
// Just as a sanity check, let's make an entry that the scanner should read
f, err = os.Create(filepath.Join(notPlugin, "not-a-plugin.spec"))
if err != nil {
t.Fatal(err)
}
defer f.Close()
names, err = Scan()
if err != nil {
t.Fatal(err)
}
if len(names) != 1 {
t.Fatalf("expected 1 entry in result: %v", names)
}
if names[0] != "not-a-plugin" {
t.Fatalf("expected plugin named `not-a-plugin`, got: %s", names[0])
}
}
|