github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/pkg/plugins/discovery_test.go (about)

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