github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/pkg/plugins/discovery_unix_test.go (about)

     1  // +build !windows
     2  
     3  package plugins // import "github.com/docker/docker/pkg/plugins"
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"net"
     9  	"os"
    10  	"path/filepath"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"gotest.tools/v3/assert"
    15  )
    16  
    17  func TestLocalSocket(t *testing.T) {
    18  	// TODO Windows: Enable a similar version for Windows named pipes
    19  	tmpdir, unregister := Setup(t)
    20  	defer unregister()
    21  
    22  	cases := []string{
    23  		filepath.Join(tmpdir, "echo.sock"),
    24  		filepath.Join(tmpdir, "echo", "echo.sock"),
    25  	}
    26  
    27  	for _, c := range cases {
    28  		if err := os.MkdirAll(filepath.Dir(c), 0755); err != nil {
    29  			t.Fatal(err)
    30  		}
    31  
    32  		l, err := net.Listen("unix", c)
    33  		if err != nil {
    34  			t.Fatal(err)
    35  		}
    36  
    37  		r := newLocalRegistry()
    38  		p, err := r.Plugin("echo")
    39  		if err != nil {
    40  			t.Fatal(err)
    41  		}
    42  
    43  		pp, err := r.Plugin("echo")
    44  		if err != nil {
    45  			t.Fatal(err)
    46  		}
    47  		if !reflect.DeepEqual(p, pp) {
    48  			t.Fatalf("Expected %v, was %v\n", p, pp)
    49  		}
    50  
    51  		if p.name != "echo" {
    52  			t.Fatalf("Expected plugin `echo`, got %s\n", p.name)
    53  		}
    54  
    55  		addr := fmt.Sprintf("unix://%s", c)
    56  		if p.Addr != addr {
    57  			t.Fatalf("Expected plugin addr `%s`, got %s\n", addr, p.Addr)
    58  		}
    59  		if !p.TLSConfig.InsecureSkipVerify {
    60  			t.Fatalf("Expected TLS verification to be skipped")
    61  		}
    62  		l.Close()
    63  	}
    64  }
    65  
    66  func TestScan(t *testing.T) {
    67  	tmpdir, unregister := Setup(t)
    68  	defer unregister()
    69  
    70  	pluginNames, err := Scan()
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	if pluginNames != nil {
    75  		t.Fatal("Plugin names should be empty.")
    76  	}
    77  
    78  	path := filepath.Join(tmpdir, "echo.spec")
    79  	addr := "unix://var/lib/docker/plugins/echo.sock"
    80  	name := "echo"
    81  
    82  	err = os.MkdirAll(filepath.Dir(path), 0755)
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	err = ioutil.WriteFile(path, []byte(addr), 0644)
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  
    92  	r := newLocalRegistry()
    93  	p, err := r.Plugin(name)
    94  	assert.NilError(t, err)
    95  
    96  	pluginNamesNotEmpty, err := Scan()
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	if len(pluginNamesNotEmpty) != 1 {
   101  		t.Fatalf("expected 1 plugin entry: %v", pluginNamesNotEmpty)
   102  	}
   103  	if p.Name() != pluginNamesNotEmpty[0] {
   104  		t.Fatalf("Unable to scan plugin with name %s", p.name)
   105  	}
   106  }
   107  
   108  func TestScanNotPlugins(t *testing.T) {
   109  	tmpdir, unregister := Setup(t)
   110  	defer unregister()
   111  
   112  	// not that `Setup()` above sets the sockets path and spec path dirs, which
   113  	// `Scan()` uses to find plugins to the returned `tmpdir`
   114  
   115  	notPlugin := filepath.Join(tmpdir, "not-a-plugin")
   116  	if err := os.MkdirAll(notPlugin, 0700); err != nil {
   117  		t.Fatal(err)
   118  	}
   119  
   120  	// this is named differently than the dir it's in, so the scanner should ignore it
   121  	l, err := net.Listen("unix", filepath.Join(notPlugin, "foo.sock"))
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  	defer l.Close()
   126  
   127  	// same let's test a spec path
   128  	f, err := os.Create(filepath.Join(notPlugin, "foo.spec"))
   129  	if err != nil {
   130  		t.Fatal(err)
   131  	}
   132  	defer f.Close()
   133  
   134  	names, err := Scan()
   135  	if err != nil {
   136  		t.Fatal(err)
   137  	}
   138  	if len(names) != 0 {
   139  		t.Fatalf("expected no plugins, got %v", names)
   140  	}
   141  
   142  	// Just as a sanity check, let's make an entry that the scanner should read
   143  	f, err = os.Create(filepath.Join(notPlugin, "not-a-plugin.spec"))
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  	defer f.Close()
   148  
   149  	names, err = Scan()
   150  	if err != nil {
   151  		t.Fatal(err)
   152  	}
   153  	if len(names) != 1 {
   154  		t.Fatalf("expected 1 entry in result: %v", names)
   155  	}
   156  	if names[0] != "not-a-plugin" {
   157  		t.Fatalf("expected plugin named `not-a-plugin`, got: %s", names[0])
   158  	}
   159  }