discovery_unix_test.go 3.3 KB

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