plugin_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package common // import "github.com/docker/docker/integration/plugin/common"
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "encoding/json"
  6. "io"
  7. "io/ioutil"
  8. "net"
  9. "net/http"
  10. "path"
  11. "strings"
  12. "testing"
  13. "github.com/docker/docker/api/types"
  14. "github.com/docker/docker/testutil/daemon"
  15. "github.com/docker/docker/testutil/fixtures/plugin"
  16. "github.com/docker/docker/testutil/registry"
  17. "github.com/docker/docker/testutil/request"
  18. "gotest.tools/v3/assert"
  19. is "gotest.tools/v3/assert/cmp"
  20. "gotest.tools/v3/skip"
  21. )
  22. func TestPluginInvalidJSON(t *testing.T) {
  23. defer setupTest(t)()
  24. endpoints := []string{"/plugins/foobar/set"}
  25. for _, ep := range endpoints {
  26. t.Run(ep, func(t *testing.T) {
  27. t.Parallel()
  28. res, body, err := request.Post(ep, request.RawString("{invalid json"), request.JSON)
  29. assert.NilError(t, err)
  30. assert.Equal(t, res.StatusCode, http.StatusBadRequest)
  31. buf, err := request.ReadBody(body)
  32. assert.NilError(t, err)
  33. assert.Check(t, is.Contains(string(buf), "invalid character 'i' looking for beginning of object key string"))
  34. res, body, err = request.Post(ep, request.JSON)
  35. assert.NilError(t, err)
  36. assert.Equal(t, res.StatusCode, http.StatusBadRequest)
  37. buf, err = request.ReadBody(body)
  38. assert.NilError(t, err)
  39. assert.Check(t, is.Contains(string(buf), "got EOF while reading request body"))
  40. })
  41. }
  42. }
  43. func TestPluginInstall(t *testing.T) {
  44. skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
  45. skip.If(t, testEnv.OSType == "windows")
  46. skip.If(t, testEnv.IsRootless, "rootless mode has different view of localhost")
  47. ctx := context.Background()
  48. client := testEnv.APIClient()
  49. t.Run("no auth", func(t *testing.T) {
  50. defer setupTest(t)()
  51. reg := registry.NewV2(t)
  52. defer reg.Close()
  53. name := "test-" + strings.ToLower(t.Name())
  54. repo := path.Join(registry.DefaultURL, name+":latest")
  55. assert.NilError(t, plugin.CreateInRegistry(ctx, repo, nil))
  56. rdr, err := client.PluginInstall(ctx, repo, types.PluginInstallOptions{Disabled: true, RemoteRef: repo})
  57. assert.NilError(t, err)
  58. defer rdr.Close()
  59. _, err = io.Copy(ioutil.Discard, rdr)
  60. assert.NilError(t, err)
  61. _, _, err = client.PluginInspectWithRaw(ctx, repo)
  62. assert.NilError(t, err)
  63. })
  64. t.Run("with htpasswd", func(t *testing.T) {
  65. defer setupTest(t)()
  66. reg := registry.NewV2(t, registry.Htpasswd)
  67. defer reg.Close()
  68. name := "test-" + strings.ToLower(t.Name())
  69. repo := path.Join(registry.DefaultURL, name+":latest")
  70. auth := &types.AuthConfig{ServerAddress: registry.DefaultURL, Username: "testuser", Password: "testpassword"}
  71. assert.NilError(t, plugin.CreateInRegistry(ctx, repo, auth))
  72. authEncoded, err := json.Marshal(auth)
  73. assert.NilError(t, err)
  74. rdr, err := client.PluginInstall(ctx, repo, types.PluginInstallOptions{
  75. RegistryAuth: base64.URLEncoding.EncodeToString(authEncoded),
  76. Disabled: true,
  77. RemoteRef: repo,
  78. })
  79. assert.NilError(t, err)
  80. defer rdr.Close()
  81. _, err = io.Copy(ioutil.Discard, rdr)
  82. assert.NilError(t, err)
  83. _, _, err = client.PluginInspectWithRaw(ctx, repo)
  84. assert.NilError(t, err)
  85. })
  86. t.Run("with insecure", func(t *testing.T) {
  87. skip.If(t, !testEnv.IsLocalDaemon())
  88. addrs, err := net.InterfaceAddrs()
  89. assert.NilError(t, err)
  90. var bindTo string
  91. for _, addr := range addrs {
  92. ip, ok := addr.(*net.IPNet)
  93. if !ok {
  94. continue
  95. }
  96. if ip.IP.IsLoopback() || ip.IP.To4() == nil {
  97. continue
  98. }
  99. bindTo = ip.IP.String()
  100. }
  101. if bindTo == "" {
  102. t.Skip("No suitable interface to bind registry to")
  103. }
  104. regURL := bindTo + ":5000"
  105. d := daemon.New(t)
  106. defer d.Stop(t)
  107. d.Start(t, "--insecure-registry="+regURL)
  108. defer d.Stop(t)
  109. reg := registry.NewV2(t, registry.URL(regURL))
  110. defer reg.Close()
  111. name := "test-" + strings.ToLower(t.Name())
  112. repo := path.Join(regURL, name+":latest")
  113. assert.NilError(t, plugin.CreateInRegistry(ctx, repo, nil, plugin.WithInsecureRegistry(regURL)))
  114. client := d.NewClientT(t)
  115. rdr, err := client.PluginInstall(ctx, repo, types.PluginInstallOptions{Disabled: true, RemoteRef: repo})
  116. assert.NilError(t, err)
  117. defer rdr.Close()
  118. _, err = io.Copy(ioutil.Discard, rdr)
  119. assert.NilError(t, err)
  120. _, _, err = client.PluginInspectWithRaw(ctx, repo)
  121. assert.NilError(t, err)
  122. })
  123. // TODO: test insecure registry with https
  124. }