discovery_unix_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 p.Name() != pluginNamesNotEmpty[0] {
  83. t.Fatalf("Unable to scan plugin with name %s", p.name)
  84. }
  85. }