2016-02-10 21:32:27 -05:00
|
|
|
// +build !windows
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package plugins // import "github.com/docker/docker/pkg/plugins"
|
2016-02-10 21:32:27 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-05-22 05:38:23 -04:00
|
|
|
"io/ioutil"
|
2016-02-10 21:32:27 -05:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2017-09-08 18:00:14 -04:00
|
|
|
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2016-02-10 21:32:27 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-05-16 11:50:55 -04:00
|
|
|
if p.name != "echo" {
|
2016-09-02 21:27:20 -04:00
|
|
|
t.Fatalf("Expected plugin `echo`, got %s\n", p.name)
|
2016-02-10 21:32:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("unix://%s", c)
|
|
|
|
if p.Addr != addr {
|
|
|
|
t.Fatalf("Expected plugin addr `%s`, got %s\n", addr, p.Addr)
|
|
|
|
}
|
2017-03-30 05:26:16 -04:00
|
|
|
if !p.TLSConfig.InsecureSkipVerify {
|
2016-02-10 21:32:27 -05:00
|
|
|
t.Fatalf("Expected TLS verification to be skipped")
|
|
|
|
}
|
|
|
|
l.Close()
|
|
|
|
}
|
|
|
|
}
|
2017-05-22 05:38:23 -04:00
|
|
|
|
|
|
|
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 = ioutil.WriteFile(path, []byte(addr), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := newLocalRegistry()
|
|
|
|
p, err := r.Plugin(name)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-05-22 05:38:23 -04:00
|
|
|
|
|
|
|
pluginNamesNotEmpty, err := Scan()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-25 20:41:45 -05:00
|
|
|
if len(pluginNamesNotEmpty) != 1 {
|
|
|
|
t.Fatalf("expected 1 plugin entry: %v", pluginNamesNotEmpty)
|
|
|
|
}
|
2017-05-22 05:38:23 -04:00
|
|
|
if p.Name() != pluginNamesNotEmpty[0] {
|
|
|
|
t.Fatalf("Unable to scan plugin with name %s", p.name)
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 20:41:45 -05:00
|
|
|
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|