discovery_test.go 3.7 KB

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