Separate plugin sockets and specs.

Check if there is a plugin socket first under `/run/docker/plugins/NAME.sock`.
If there is no socket for a plugin, check `/etc/docker/plugins/NAME.spec` and
`/usr/lib/docker/plugins/NAME.spec` for spec files.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-06-15 15:35:49 -07:00
parent f80a5673c1
commit bee3972856
3 changed files with 94 additions and 82 deletions

View file

@ -10,62 +10,72 @@ import (
"testing"
)
func TestUnknownLocalPath(t *testing.T) {
func setup(t *testing.T) (string, func()) {
tmpdir, err := ioutil.TempDir("", "docker-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
backup := socketsPath
socketsPath = tmpdir
specsPaths = []string{tmpdir}
l := newLocalRegistry(filepath.Join(tmpdir, "unknown"))
_, err = l.Plugin("foo")
if err == nil || err != ErrNotFound {
t.Fatalf("Expected error for unknown directory")
return tmpdir, func() {
socketsPath = backup
os.RemoveAll(tmpdir)
}
}
func TestLocalSocket(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "docker-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
l, err := net.Listen("unix", filepath.Join(tmpdir, "echo.sock"))
if err != nil {
t.Fatal(err)
}
defer l.Close()
tmpdir, unregister := setup(t)
defer unregister()
r := newLocalRegistry(tmpdir)
p, err := r.Plugin("echo")
if err != nil {
t.Fatal(err)
cases := []string{
filepath.Join(tmpdir, "echo.sock"),
filepath.Join(tmpdir, "echo", "echo.sock"),
}
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)
}
for _, c := range cases {
if err := os.MkdirAll(filepath.Dir(c), 0755); err != nil {
t.Fatal(err)
}
if p.Name != "echo" {
t.Fatalf("Expected plugin `echo`, got %s\n", p.Name)
}
l, err := net.Listen("unix", c)
if err != nil {
t.Fatal(err)
}
addr := fmt.Sprintf("unix://%s/echo.sock", tmpdir)
if p.Addr != addr {
t.Fatalf("Expected plugin addr `%s`, got %s\n", addr, p.Addr)
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 != true {
t.Fatalf("Expected TLS verification to be skipped")
}
l.Close()
}
}
func TestFileSpecPlugin(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "docker-test-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
tmpdir, unregister := setup(t)
defer unregister()
cases := []struct {
path string
@ -74,16 +84,21 @@ func TestFileSpecPlugin(t *testing.T) {
fail bool
}{
{filepath.Join(tmpdir, "echo.spec"), "echo", "unix://var/lib/docker/plugins/echo.sock", false},
{filepath.Join(tmpdir, "echo", "echo.spec"), "echo", "unix://var/lib/docker/plugins/echo.sock", false},
{filepath.Join(tmpdir, "foo.spec"), "foo", "tcp://localhost:8080", false},
{filepath.Join(tmpdir, "foo", "foo.spec"), "foo", "tcp://localhost:8080", false},
{filepath.Join(tmpdir, "bar.spec"), "bar", "localhost:8080", true}, // unknown transport
}
for _, c := range cases {
if err = ioutil.WriteFile(c.path, []byte(c.addr), 0644); err != nil {
if err := os.MkdirAll(filepath.Dir(c.path), 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(c.path, []byte(c.addr), 0644); err != nil {
t.Fatal(err)
}
r := newLocalRegistry(tmpdir)
r := newLocalRegistry()
p, err := r.Plugin(c.name)
if c.fail && err == nil {
continue
@ -100,14 +115,16 @@ func TestFileSpecPlugin(t *testing.T) {
if p.Addr != c.addr {
t.Fatalf("Expected plugin addr `%s`, got %s\n", c.addr, p.Addr)
}
if p.TLSConfig.InsecureSkipVerify != true {
t.Fatalf("Expected TLS verification to be skipped")
}
}
}
func TestFileJSONSpecPlugin(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "docker-test-")
if err != nil {
t.Fatal(err)
}
tmpdir, unregister := setup(t)
defer unregister()
p := filepath.Join(tmpdir, "example.json")
spec := `{
@ -120,11 +137,11 @@ func TestFileJSONSpecPlugin(t *testing.T) {
}
}`
if err = ioutil.WriteFile(p, []byte(spec), 0644); err != nil {
if err := ioutil.WriteFile(p, []byte(spec), 0644); err != nil {
t.Fatal(err)
}
r := newLocalRegistry(tmpdir)
r := newLocalRegistry()
plugin, err := r.Plugin("example")
if err != nil {
t.Fatal(err)