discovery_unix_test.go 3.2 KB

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