discovery_unix_test.go 3.3 KB

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