github.com/wulonghui/docker@v1.8.0-rc2/pkg/plugins/discovery_test.go (about)

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