plugin_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package common // import "github.com/docker/docker/integration/plugin/common"
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "net"
  10. "net/http"
  11. "os"
  12. "path"
  13. "path/filepath"
  14. "strings"
  15. "testing"
  16. "github.com/docker/docker/api/types"
  17. "github.com/docker/docker/testutil/daemon"
  18. "github.com/docker/docker/testutil/fixtures/plugin"
  19. "github.com/docker/docker/testutil/registry"
  20. "github.com/docker/docker/testutil/request"
  21. "gotest.tools/v3/assert"
  22. is "gotest.tools/v3/assert/cmp"
  23. "gotest.tools/v3/skip"
  24. )
  25. func TestPluginInvalidJSON(t *testing.T) {
  26. defer setupTest(t)()
  27. endpoints := []string{"/plugins/foobar/set"}
  28. for _, ep := range endpoints {
  29. t.Run(ep, func(t *testing.T) {
  30. t.Parallel()
  31. res, body, err := request.Post(ep, request.RawString("{invalid json"), request.JSON)
  32. assert.NilError(t, err)
  33. assert.Equal(t, res.StatusCode, http.StatusBadRequest)
  34. buf, err := request.ReadBody(body)
  35. assert.NilError(t, err)
  36. assert.Check(t, is.Contains(string(buf), "invalid character 'i' looking for beginning of object key string"))
  37. res, body, err = request.Post(ep, request.JSON)
  38. assert.NilError(t, err)
  39. assert.Equal(t, res.StatusCode, http.StatusBadRequest)
  40. buf, err = request.ReadBody(body)
  41. assert.NilError(t, err)
  42. assert.Check(t, is.Contains(string(buf), "got EOF while reading request body"))
  43. })
  44. }
  45. }
  46. func TestPluginInstall(t *testing.T) {
  47. skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
  48. skip.If(t, testEnv.OSType == "windows")
  49. skip.If(t, testEnv.IsRootless, "rootless mode has different view of localhost")
  50. ctx := context.Background()
  51. client := testEnv.APIClient()
  52. t.Run("no auth", func(t *testing.T) {
  53. defer setupTest(t)()
  54. reg := registry.NewV2(t)
  55. defer reg.Close()
  56. name := "test-" + strings.ToLower(t.Name())
  57. repo := path.Join(registry.DefaultURL, name+":latest")
  58. assert.NilError(t, plugin.CreateInRegistry(ctx, repo, nil))
  59. rdr, err := client.PluginInstall(ctx, repo, types.PluginInstallOptions{Disabled: true, RemoteRef: repo})
  60. assert.NilError(t, err)
  61. defer rdr.Close()
  62. _, err = io.Copy(ioutil.Discard, rdr)
  63. assert.NilError(t, err)
  64. _, _, err = client.PluginInspectWithRaw(ctx, repo)
  65. assert.NilError(t, err)
  66. })
  67. t.Run("with htpasswd", func(t *testing.T) {
  68. defer setupTest(t)()
  69. reg := registry.NewV2(t, registry.Htpasswd)
  70. defer reg.Close()
  71. name := "test-" + strings.ToLower(t.Name())
  72. repo := path.Join(registry.DefaultURL, name+":latest")
  73. auth := &types.AuthConfig{ServerAddress: registry.DefaultURL, Username: "testuser", Password: "testpassword"}
  74. assert.NilError(t, plugin.CreateInRegistry(ctx, repo, auth))
  75. authEncoded, err := json.Marshal(auth)
  76. assert.NilError(t, err)
  77. rdr, err := client.PluginInstall(ctx, repo, types.PluginInstallOptions{
  78. RegistryAuth: base64.URLEncoding.EncodeToString(authEncoded),
  79. Disabled: true,
  80. RemoteRef: repo,
  81. })
  82. assert.NilError(t, err)
  83. defer rdr.Close()
  84. _, err = io.Copy(ioutil.Discard, rdr)
  85. assert.NilError(t, err)
  86. _, _, err = client.PluginInspectWithRaw(ctx, repo)
  87. assert.NilError(t, err)
  88. })
  89. t.Run("with insecure", func(t *testing.T) {
  90. skip.If(t, !testEnv.IsLocalDaemon())
  91. addrs, err := net.InterfaceAddrs()
  92. assert.NilError(t, err)
  93. var bindTo string
  94. for _, addr := range addrs {
  95. ip, ok := addr.(*net.IPNet)
  96. if !ok {
  97. continue
  98. }
  99. if ip.IP.IsLoopback() || ip.IP.To4() == nil {
  100. continue
  101. }
  102. bindTo = ip.IP.String()
  103. }
  104. if bindTo == "" {
  105. t.Skip("No suitable interface to bind registry to")
  106. }
  107. regURL := bindTo + ":5000"
  108. d := daemon.New(t)
  109. defer d.Stop(t)
  110. d.Start(t, "--insecure-registry="+regURL)
  111. defer d.Stop(t)
  112. reg := registry.NewV2(t, registry.URL(regURL))
  113. defer reg.Close()
  114. name := "test-" + strings.ToLower(t.Name())
  115. repo := path.Join(regURL, name+":latest")
  116. assert.NilError(t, plugin.CreateInRegistry(ctx, repo, nil, plugin.WithInsecureRegistry(regURL)))
  117. client := d.NewClientT(t)
  118. rdr, err := client.PluginInstall(ctx, repo, types.PluginInstallOptions{Disabled: true, RemoteRef: repo})
  119. assert.NilError(t, err)
  120. defer rdr.Close()
  121. _, err = io.Copy(ioutil.Discard, rdr)
  122. assert.NilError(t, err)
  123. _, _, err = client.PluginInspectWithRaw(ctx, repo)
  124. assert.NilError(t, err)
  125. })
  126. // TODO: test insecure registry with https
  127. }
  128. func TestPluginsWithRuntimes(t *testing.T) {
  129. skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
  130. skip.If(t, testEnv.IsRootless, "Test not supported on rootless due to buggy daemon setup in rootless mode due to daemon restart")
  131. skip.If(t, testEnv.OSType == "windows")
  132. dir, err := ioutil.TempDir("", t.Name())
  133. assert.NilError(t, err)
  134. defer os.RemoveAll(dir)
  135. d := daemon.New(t)
  136. defer d.Cleanup(t)
  137. d.Start(t)
  138. defer d.Stop(t)
  139. ctx := context.Background()
  140. client := d.NewClientT(t)
  141. assert.NilError(t, plugin.Create(ctx, client, "test:latest"))
  142. defer client.PluginRemove(ctx, "test:latest", types.PluginRemoveOptions{Force: true})
  143. assert.NilError(t, client.PluginEnable(ctx, "test:latest", types.PluginEnableOptions{Timeout: 30}))
  144. p := filepath.Join(dir, "myrt")
  145. script := fmt.Sprintf(`#!/bin/sh
  146. file="%s/success"
  147. if [ "$1" = "someArg" ]; then
  148. shift
  149. file="${file}_someArg"
  150. fi
  151. touch $file
  152. exec runc $@
  153. `, dir)
  154. assert.NilError(t, ioutil.WriteFile(p, []byte(script), 0777))
  155. type config struct {
  156. Runtimes map[string]types.Runtime `json:"runtimes"`
  157. }
  158. cfg, err := json.Marshal(config{
  159. Runtimes: map[string]types.Runtime{
  160. "myrt": {Path: p},
  161. "myrtArgs": {Path: p, Args: []string{"someArg"}},
  162. },
  163. })
  164. configPath := filepath.Join(dir, "config.json")
  165. ioutil.WriteFile(configPath, cfg, 0644)
  166. t.Run("No Args", func(t *testing.T) {
  167. d.Restart(t, "--default-runtime=myrt", "--config-file="+configPath)
  168. _, err = os.Stat(filepath.Join(dir, "success"))
  169. assert.NilError(t, err)
  170. })
  171. t.Run("With Args", func(t *testing.T) {
  172. d.Restart(t, "--default-runtime=myrtArgs", "--config-file="+configPath)
  173. _, err = os.Stat(filepath.Join(dir, "success_someArg"))
  174. assert.NilError(t, err)
  175. })
  176. }