github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/pkg/plugins/discovery_unix_test.go (about)

     1  // +build !windows
     2  
     3  package plugins
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"net"
     9  	"os"
    10  	"path/filepath"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/require"
    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  	require.NoError(t, err)
    95  
    96  	pluginNamesNotEmpty, err := Scan()
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	if p.Name() != pluginNamesNotEmpty[0] {
   101  		t.Fatalf("Unable to scan plugin with name %s", p.name)
   102  	}
   103  }