2018-02-05 16:05:59 -05:00
|
|
|
package plugins // import "github.com/docker/docker/pkg/plugins"
|
2015-05-14 13:05:39 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-02-10 21:32:27 -05:00
|
|
|
func Setup(t *testing.T) (string, func()) {
|
2021-08-24 06:10:50 -04:00
|
|
|
tmpdir, err := os.MkdirTemp("", "docker-test")
|
2015-05-14 13:05:39 -04:00
|
|
|
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 TestFileSpecPlugin(t *testing.T) {
|
2016-02-10 21:32:27 -05:00
|
|
|
tmpdir, unregister := Setup(t)
|
2015-06-15 18:35:49 -04:00
|
|
|
defer unregister()
|
2015-05-14 13:05:39 -04:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
path string
|
|
|
|
name string
|
|
|
|
addr string
|
|
|
|
fail bool
|
|
|
|
}{
|
2016-02-11 18:21:52 -05:00
|
|
|
// TODO Windows: Factor out the unix:// variants.
|
2015-05-14 13:05:39 -04:00
|
|
|
{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)
|
|
|
|
}
|
2021-08-24 06:10:50 -04:00
|
|
|
if err := os.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)
|
|
|
|
}
|
|
|
|
|
2016-05-16 11:50:55 -04:00
|
|
|
if p.name != c.name {
|
2016-09-02 21:27:20 -04:00
|
|
|
t.Fatalf("Expected plugin `%s`, got %s\n", c.name, p.name)
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-03-30 05:26:16 -04:00
|
|
|
if !p.TLSConfig.InsecureSkipVerify {
|
2015-06-15 18:35:49 -04:00
|
|
|
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) {
|
2016-02-10 21:32:27 -05:00
|
|
|
tmpdir, unregister := Setup(t)
|
2015-06-15 18:35:49 -04:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
if err := os.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)
|
|
|
|
}
|
|
|
|
|
2016-09-02 21:27:20 -04:00
|
|
|
if expected, actual := "example", plugin.name; expected != actual {
|
|
|
|
t.Fatalf("Expected plugin %q, got %s\n", expected, actual)
|
2015-05-27 18:21:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 04:37:30 -04:00
|
|
|
|
|
|
|
func TestFileJSONSpecPluginWithoutTLSConfig(t *testing.T) {
|
|
|
|
tmpdir, unregister := Setup(t)
|
|
|
|
defer unregister()
|
|
|
|
|
|
|
|
p := filepath.Join(tmpdir, "example.json")
|
|
|
|
spec := `{
|
|
|
|
"Name": "plugin-example",
|
|
|
|
"Addr": "https://example.com/docker/plugin"
|
|
|
|
}`
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
if err := os.WriteFile(p, []byte(spec), 0644); err != nil {
|
2016-07-26 04:37:30 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := newLocalRegistry()
|
|
|
|
plugin, err := r.Plugin("example")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-02 21:27:20 -04:00
|
|
|
if expected, actual := "example", plugin.name; expected != actual {
|
|
|
|
t.Fatalf("Expected plugin %q, got %s\n", expected, actual)
|
2016-07-26 04:37:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 != nil {
|
|
|
|
t.Fatalf("Expected plugin TLSConfig nil, got %v\n", plugin.TLSConfig)
|
|
|
|
}
|
|
|
|
}
|