plugin_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package plugins // import "github.com/docker/docker/pkg/plugins"
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "sync"
  11. "testing"
  12. "time"
  13. "github.com/docker/docker/pkg/plugins/transport"
  14. "github.com/docker/go-connections/tlsconfig"
  15. "github.com/pkg/errors"
  16. "gotest.tools/v3/assert"
  17. )
  18. const (
  19. fruitPlugin = "fruit"
  20. fruitImplements = "apple"
  21. )
  22. // regression test for deadlock in handlers
  23. func TestPluginAddHandler(t *testing.T) {
  24. // make a plugin which is pre-activated
  25. p := &Plugin{activateWait: sync.NewCond(&sync.Mutex{})}
  26. p.Manifest = &Manifest{Implements: []string{"bananas"}}
  27. storage.plugins["qwerty"] = p
  28. testActive(t, p)
  29. Handle("bananas", func(_ string, _ *Client) {})
  30. testActive(t, p)
  31. }
  32. func TestPluginWaitBadPlugin(t *testing.T) {
  33. p := &Plugin{activateWait: sync.NewCond(&sync.Mutex{})}
  34. p.activateErr = errors.New("some junk happened")
  35. testActive(t, p)
  36. }
  37. func testActive(t *testing.T, p *Plugin) {
  38. done := make(chan struct{})
  39. go func() {
  40. p.waitActive()
  41. close(done)
  42. }()
  43. select {
  44. case <-time.After(100 * time.Millisecond):
  45. _, f, l, _ := runtime.Caller(1)
  46. t.Fatalf("%s:%d: deadlock in waitActive", filepath.Base(f), l)
  47. case <-done:
  48. }
  49. }
  50. func TestGet(t *testing.T) {
  51. p := &Plugin{name: fruitPlugin, activateWait: sync.NewCond(&sync.Mutex{})}
  52. p.Manifest = &Manifest{Implements: []string{fruitImplements}}
  53. storage.plugins[fruitPlugin] = p
  54. plugin, err := Get(fruitPlugin, fruitImplements)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. if p.Name() != plugin.Name() {
  59. t.Fatalf("No matching plugin with name %s found", plugin.Name())
  60. }
  61. if plugin.Client() != nil {
  62. t.Fatal("expected nil Client but found one")
  63. }
  64. if !plugin.IsV1() {
  65. t.Fatal("Expected true for V1 plugin")
  66. }
  67. // check negative case where plugin fruit doesn't implement banana
  68. _, err = Get("fruit", "banana")
  69. assert.Assert(t, errors.Is(err, ErrNotImplements))
  70. // check negative case where plugin vegetable doesn't exist
  71. _, err = Get("vegetable", "potato")
  72. assert.Assert(t, errors.Is(err, ErrNotFound))
  73. }
  74. func TestPluginWithNoManifest(t *testing.T) {
  75. mux, addr := setupRemotePluginServer(t)
  76. m := Manifest{[]string{fruitImplements}}
  77. var buf bytes.Buffer
  78. if err := json.NewEncoder(&buf).Encode(m); err != nil {
  79. t.Fatal(err)
  80. }
  81. mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
  82. if r.Method != http.MethodPost {
  83. t.Fatalf("Expected POST, got %s\n", r.Method)
  84. }
  85. header := w.Header()
  86. header.Set("Content-Type", transport.VersionMimetype)
  87. io.Copy(w, &buf)
  88. })
  89. p := &Plugin{
  90. name: fruitPlugin,
  91. activateWait: sync.NewCond(&sync.Mutex{}),
  92. Addr: addr,
  93. TLSConfig: &tlsconfig.Options{InsecureSkipVerify: true},
  94. }
  95. storage.plugins[fruitPlugin] = p
  96. plugin, err := Get(fruitPlugin, fruitImplements)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. if p.Name() != plugin.Name() {
  101. t.Fatalf("No matching plugin with name %s found", plugin.Name())
  102. }
  103. }
  104. func TestGetAll(t *testing.T) {
  105. tmpdir, unregister, r := Setup(t)
  106. defer unregister()
  107. p := filepath.Join(tmpdir, "example.json")
  108. spec := `{
  109. "Name": "example",
  110. "Addr": "https://example.com/docker/plugin"
  111. }`
  112. if err := os.WriteFile(p, []byte(spec), 0o644); err != nil {
  113. t.Fatal(err)
  114. }
  115. plugin, err := r.Plugin("example")
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. plugin.Manifest = &Manifest{Implements: []string{"apple"}}
  120. storage.plugins["example"] = plugin
  121. fetchedPlugins, err := r.GetAll("apple")
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. if fetchedPlugins[0].Name() != plugin.Name() {
  126. t.Fatalf("Expected to get plugin with name %s", plugin.Name())
  127. }
  128. }