github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/pkg/plugins/discovery_test.go (about)

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