github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/plugins/discovery_test.go (about)

     1  package plugins // import "github.com/demonoid81/moby/pkg/plugins"
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func Setup(t *testing.T) (string, func()) {
    11  	tmpdir, err := ioutil.TempDir("", "docker-test")
    12  	if err != nil {
    13  		t.Fatal(err)
    14  	}
    15  	backup := socketsPath
    16  	socketsPath = tmpdir
    17  	specsPaths = []string{tmpdir}
    18  
    19  	return tmpdir, func() {
    20  		socketsPath = backup
    21  		os.RemoveAll(tmpdir)
    22  	}
    23  }
    24  
    25  func TestFileSpecPlugin(t *testing.T) {
    26  	tmpdir, unregister := Setup(t)
    27  	defer unregister()
    28  
    29  	cases := []struct {
    30  		path string
    31  		name string
    32  		addr string
    33  		fail bool
    34  	}{
    35  		// TODO Windows: Factor out the unix:// variants.
    36  		{filepath.Join(tmpdir, "echo.spec"), "echo", "unix://var/lib/docker/plugins/echo.sock", false},
    37  		{filepath.Join(tmpdir, "echo", "echo.spec"), "echo", "unix://var/lib/docker/plugins/echo.sock", false},
    38  		{filepath.Join(tmpdir, "foo.spec"), "foo", "tcp://localhost:8080", false},
    39  		{filepath.Join(tmpdir, "foo", "foo.spec"), "foo", "tcp://localhost:8080", false},
    40  		{filepath.Join(tmpdir, "bar.spec"), "bar", "localhost:8080", true}, // unknown transport
    41  	}
    42  
    43  	for _, c := range cases {
    44  		if err := os.MkdirAll(filepath.Dir(c.path), 0755); err != nil {
    45  			t.Fatal(err)
    46  		}
    47  		if err := ioutil.WriteFile(c.path, []byte(c.addr), 0644); err != nil {
    48  			t.Fatal(err)
    49  		}
    50  
    51  		r := newLocalRegistry()
    52  		p, err := r.Plugin(c.name)
    53  		if c.fail && err == nil {
    54  			continue
    55  		}
    56  
    57  		if err != nil {
    58  			t.Fatal(err)
    59  		}
    60  
    61  		if p.name != c.name {
    62  			t.Fatalf("Expected plugin `%s`, got %s\n", c.name, p.name)
    63  		}
    64  
    65  		if p.Addr != c.addr {
    66  			t.Fatalf("Expected plugin addr `%s`, got %s\n", c.addr, p.Addr)
    67  		}
    68  
    69  		if !p.TLSConfig.InsecureSkipVerify {
    70  			t.Fatalf("Expected TLS verification to be skipped")
    71  		}
    72  	}
    73  }
    74  
    75  func TestFileJSONSpecPlugin(t *testing.T) {
    76  	tmpdir, unregister := Setup(t)
    77  	defer unregister()
    78  
    79  	p := filepath.Join(tmpdir, "example.json")
    80  	spec := `{
    81    "Name": "plugin-example",
    82    "Addr": "https://example.com/docker/plugin",
    83    "TLSConfig": {
    84      "CAFile": "/usr/shared/docker/certs/example-ca.pem",
    85      "CertFile": "/usr/shared/docker/certs/example-cert.pem",
    86      "KeyFile": "/usr/shared/docker/certs/example-key.pem"
    87  	}
    88  }`
    89  
    90  	if err := ioutil.WriteFile(p, []byte(spec), 0644); err != nil {
    91  		t.Fatal(err)
    92  	}
    93  
    94  	r := newLocalRegistry()
    95  	plugin, err := r.Plugin("example")
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	if expected, actual := "example", plugin.name; expected != actual {
   101  		t.Fatalf("Expected plugin %q, got %s\n", expected, actual)
   102  	}
   103  
   104  	if plugin.Addr != "https://example.com/docker/plugin" {
   105  		t.Fatalf("Expected plugin addr `https://example.com/docker/plugin`, got %s\n", plugin.Addr)
   106  	}
   107  
   108  	if plugin.TLSConfig.CAFile != "/usr/shared/docker/certs/example-ca.pem" {
   109  		t.Fatalf("Expected plugin CA `/usr/shared/docker/certs/example-ca.pem`, got %s\n", plugin.TLSConfig.CAFile)
   110  	}
   111  
   112  	if plugin.TLSConfig.CertFile != "/usr/shared/docker/certs/example-cert.pem" {
   113  		t.Fatalf("Expected plugin Certificate `/usr/shared/docker/certs/example-cert.pem`, got %s\n", plugin.TLSConfig.CertFile)
   114  	}
   115  
   116  	if plugin.TLSConfig.KeyFile != "/usr/shared/docker/certs/example-key.pem" {
   117  		t.Fatalf("Expected plugin Key `/usr/shared/docker/certs/example-key.pem`, got %s\n", plugin.TLSConfig.KeyFile)
   118  	}
   119  }
   120  
   121  func TestFileJSONSpecPluginWithoutTLSConfig(t *testing.T) {
   122  	tmpdir, unregister := Setup(t)
   123  	defer unregister()
   124  
   125  	p := filepath.Join(tmpdir, "example.json")
   126  	spec := `{
   127    "Name": "plugin-example",
   128    "Addr": "https://example.com/docker/plugin"
   129  }`
   130  
   131  	if err := ioutil.WriteFile(p, []byte(spec), 0644); err != nil {
   132  		t.Fatal(err)
   133  	}
   134  
   135  	r := newLocalRegistry()
   136  	plugin, err := r.Plugin("example")
   137  	if err != nil {
   138  		t.Fatal(err)
   139  	}
   140  
   141  	if expected, actual := "example", plugin.name; expected != actual {
   142  		t.Fatalf("Expected plugin %q, got %s\n", expected, actual)
   143  	}
   144  
   145  	if plugin.Addr != "https://example.com/docker/plugin" {
   146  		t.Fatalf("Expected plugin addr `https://example.com/docker/plugin`, got %s\n", plugin.Addr)
   147  	}
   148  
   149  	if plugin.TLSConfig != nil {
   150  		t.Fatalf("Expected plugin TLSConfig nil, got %v\n", plugin.TLSConfig)
   151  	}
   152  }