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