discovery_unix_test.go 3.4 KB

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