discovery_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package plugins
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. )
  8. func Setup(t *testing.T) (string, func()) {
  9. tmpdir, err := ioutil.TempDir("", "docker-test")
  10. if err != nil {
  11. t.Fatal(err)
  12. }
  13. backup := socketsPath
  14. socketsPath = tmpdir
  15. specsPaths = []string{tmpdir}
  16. return tmpdir, func() {
  17. socketsPath = backup
  18. os.RemoveAll(tmpdir)
  19. }
  20. }
  21. func TestFileSpecPlugin(t *testing.T) {
  22. tmpdir, unregister := Setup(t)
  23. defer unregister()
  24. cases := []struct {
  25. path string
  26. name string
  27. addr string
  28. fail bool
  29. }{
  30. // TODO Windows: Factor out the unix:// variants.
  31. {filepath.Join(tmpdir, "echo.spec"), "echo", "unix://var/lib/docker/plugins/echo.sock", false},
  32. {filepath.Join(tmpdir, "echo", "echo.spec"), "echo", "unix://var/lib/docker/plugins/echo.sock", false},
  33. {filepath.Join(tmpdir, "foo.spec"), "foo", "tcp://localhost:8080", false},
  34. {filepath.Join(tmpdir, "foo", "foo.spec"), "foo", "tcp://localhost:8080", false},
  35. {filepath.Join(tmpdir, "bar.spec"), "bar", "localhost:8080", true}, // unknown transport
  36. }
  37. for _, c := range cases {
  38. if err := os.MkdirAll(filepath.Dir(c.path), 0755); err != nil {
  39. t.Fatal(err)
  40. }
  41. if err := ioutil.WriteFile(c.path, []byte(c.addr), 0644); err != nil {
  42. t.Fatal(err)
  43. }
  44. r := newLocalRegistry()
  45. p, err := r.Plugin(c.name)
  46. if c.fail && err == nil {
  47. continue
  48. }
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if p.name != c.name {
  53. t.Fatalf("Expected plugin `%s`, got %s\n", c.name, p.name)
  54. }
  55. if p.Addr != c.addr {
  56. t.Fatalf("Expected plugin addr `%s`, got %s\n", c.addr, p.Addr)
  57. }
  58. if !p.TLSConfig.InsecureSkipVerify {
  59. t.Fatalf("Expected TLS verification to be skipped")
  60. }
  61. }
  62. }
  63. func TestFileJSONSpecPlugin(t *testing.T) {
  64. tmpdir, unregister := Setup(t)
  65. defer unregister()
  66. p := filepath.Join(tmpdir, "example.json")
  67. spec := `{
  68. "Name": "plugin-example",
  69. "Addr": "https://example.com/docker/plugin",
  70. "TLSConfig": {
  71. "CAFile": "/usr/shared/docker/certs/example-ca.pem",
  72. "CertFile": "/usr/shared/docker/certs/example-cert.pem",
  73. "KeyFile": "/usr/shared/docker/certs/example-key.pem"
  74. }
  75. }`
  76. if err := ioutil.WriteFile(p, []byte(spec), 0644); err != nil {
  77. t.Fatal(err)
  78. }
  79. r := newLocalRegistry()
  80. plugin, err := r.Plugin("example")
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. if expected, actual := "example", plugin.name; expected != actual {
  85. t.Fatalf("Expected plugin %q, got %s\n", expected, actual)
  86. }
  87. if plugin.Addr != "https://example.com/docker/plugin" {
  88. t.Fatalf("Expected plugin addr `https://example.com/docker/plugin`, got %s\n", plugin.Addr)
  89. }
  90. if plugin.TLSConfig.CAFile != "/usr/shared/docker/certs/example-ca.pem" {
  91. t.Fatalf("Expected plugin CA `/usr/shared/docker/certs/example-ca.pem`, got %s\n", plugin.TLSConfig.CAFile)
  92. }
  93. if plugin.TLSConfig.CertFile != "/usr/shared/docker/certs/example-cert.pem" {
  94. t.Fatalf("Expected plugin Certificate `/usr/shared/docker/certs/example-cert.pem`, got %s\n", plugin.TLSConfig.CertFile)
  95. }
  96. if plugin.TLSConfig.KeyFile != "/usr/shared/docker/certs/example-key.pem" {
  97. t.Fatalf("Expected plugin Key `/usr/shared/docker/certs/example-key.pem`, got %s\n", plugin.TLSConfig.KeyFile)
  98. }
  99. }
  100. func TestFileJSONSpecPluginWithoutTLSConfig(t *testing.T) {
  101. tmpdir, unregister := Setup(t)
  102. defer unregister()
  103. p := filepath.Join(tmpdir, "example.json")
  104. spec := `{
  105. "Name": "plugin-example",
  106. "Addr": "https://example.com/docker/plugin"
  107. }`
  108. if err := ioutil.WriteFile(p, []byte(spec), 0644); err != nil {
  109. t.Fatal(err)
  110. }
  111. r := newLocalRegistry()
  112. plugin, err := r.Plugin("example")
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. if expected, actual := "example", plugin.name; expected != actual {
  117. t.Fatalf("Expected plugin %q, got %s\n", expected, actual)
  118. }
  119. if plugin.Addr != "https://example.com/docker/plugin" {
  120. t.Fatalf("Expected plugin addr `https://example.com/docker/plugin`, got %s\n", plugin.Addr)
  121. }
  122. if plugin.TLSConfig != nil {
  123. t.Fatalf("Expected plugin TLSConfig nil, got %v\n", plugin.TLSConfig)
  124. }
  125. }