2015-05-14 13:05:39 -04:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
func setup(t *testing.T) (string, func()) {
|
2015-05-14 13:05:39 -04:00
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-15 18:35:49 -04:00
|
|
|
backup := socketsPath
|
|
|
|
socketsPath = tmpdir
|
|
|
|
specsPaths = []string{tmpdir}
|
2015-05-14 13:05:39 -04:00
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
return tmpdir, func() {
|
|
|
|
socketsPath = backup
|
|
|
|
os.RemoveAll(tmpdir)
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocalSocket(t *testing.T) {
|
2015-06-15 18:35:49 -04:00
|
|
|
tmpdir, unregister := setup(t)
|
|
|
|
defer unregister()
|
2015-05-14 13:05:39 -04:00
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
cases := []string{
|
|
|
|
filepath.Join(tmpdir, "echo.sock"),
|
|
|
|
filepath.Join(tmpdir, "echo", "echo.sock"),
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
for _, c := range cases {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(c), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-14 13:05:39 -04:00
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
l, err := net.Listen("unix", c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-14 13:05:39 -04:00
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
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()
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileSpecPlugin(t *testing.T) {
|
2015-06-15 18:35:49 -04:00
|
|
|
tmpdir, unregister := setup(t)
|
|
|
|
defer unregister()
|
2015-05-14 13:05:39 -04:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
path string
|
|
|
|
name string
|
|
|
|
addr string
|
|
|
|
fail bool
|
|
|
|
}{
|
|
|
|
{filepath.Join(tmpdir, "echo.spec"), "echo", "unix://var/lib/docker/plugins/echo.sock", false},
|
2015-06-15 18:35:49 -04:00
|
|
|
{filepath.Join(tmpdir, "echo", "echo.spec"), "echo", "unix://var/lib/docker/plugins/echo.sock", false},
|
2015-05-14 13:05:39 -04:00
|
|
|
{filepath.Join(tmpdir, "foo.spec"), "foo", "tcp://localhost:8080", false},
|
2015-06-15 18:35:49 -04:00
|
|
|
{filepath.Join(tmpdir, "foo", "foo.spec"), "foo", "tcp://localhost:8080", false},
|
2015-05-14 13:05:39 -04:00
|
|
|
{filepath.Join(tmpdir, "bar.spec"), "bar", "localhost:8080", true}, // unknown transport
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2015-06-15 18:35:49 -04:00
|
|
|
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 {
|
2015-05-14 13:05:39 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
r := newLocalRegistry()
|
2015-05-14 13:05:39 -04:00
|
|
|
p, err := r.Plugin(c.name)
|
|
|
|
if c.fail && err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Name != c.name {
|
|
|
|
t.Fatalf("Expected plugin `%s`, got %s\n", c.name, p.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Addr != c.addr {
|
|
|
|
t.Fatalf("Expected plugin addr `%s`, got %s\n", c.addr, p.Addr)
|
|
|
|
}
|
2015-06-15 18:35:49 -04:00
|
|
|
|
|
|
|
if p.TLSConfig.InsecureSkipVerify != true {
|
|
|
|
t.Fatalf("Expected TLS verification to be skipped")
|
|
|
|
}
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-27 18:21:18 -04:00
|
|
|
|
|
|
|
func TestFileJSONSpecPlugin(t *testing.T) {
|
2015-06-15 18:35:49 -04:00
|
|
|
tmpdir, unregister := setup(t)
|
|
|
|
defer unregister()
|
2015-05-27 18:21:18 -04:00
|
|
|
|
|
|
|
p := filepath.Join(tmpdir, "example.json")
|
|
|
|
spec := `{
|
|
|
|
"Name": "plugin-example",
|
|
|
|
"Addr": "https://example.com/docker/plugin",
|
|
|
|
"TLSConfig": {
|
|
|
|
"CAFile": "/usr/shared/docker/certs/example-ca.pem",
|
|
|
|
"CertFile": "/usr/shared/docker/certs/example-cert.pem",
|
|
|
|
"KeyFile": "/usr/shared/docker/certs/example-key.pem"
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
if err := ioutil.WriteFile(p, []byte(spec), 0644); err != nil {
|
2015-05-27 18:21:18 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
r := newLocalRegistry()
|
2015-05-27 18:21:18 -04:00
|
|
|
plugin, err := r.Plugin("example")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if plugin.Name != "example" {
|
|
|
|
t.Fatalf("Expected plugin `plugin-example`, got %s\n", plugin.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if plugin.Addr != "https://example.com/docker/plugin" {
|
|
|
|
t.Fatalf("Expected plugin addr `https://example.com/docker/plugin`, got %s\n", plugin.Addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if plugin.TLSConfig.CAFile != "/usr/shared/docker/certs/example-ca.pem" {
|
|
|
|
t.Fatalf("Expected plugin CA `/usr/shared/docker/certs/example-ca.pem`, got %s\n", plugin.TLSConfig.CAFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if plugin.TLSConfig.CertFile != "/usr/shared/docker/certs/example-cert.pem" {
|
|
|
|
t.Fatalf("Expected plugin Certificate `/usr/shared/docker/certs/example-cert.pem`, got %s\n", plugin.TLSConfig.CertFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if plugin.TLSConfig.KeyFile != "/usr/shared/docker/certs/example-key.pem" {
|
|
|
|
t.Fatalf("Expected plugin Key `/usr/shared/docker/certs/example-key.pem`, got %s\n", plugin.TLSConfig.KeyFile)
|
|
|
|
}
|
|
|
|
}
|