docker_cli_plugins_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/docker/docker/pkg/integration/checker"
  5. "github.com/go-check/check"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. )
  11. var (
  12. pluginProcessName = "sample-volume-plugin"
  13. pName = "tonistiigi/sample-volume-plugin"
  14. pTag = "latest"
  15. pNameWithTag = pName + ":" + pTag
  16. )
  17. func (s *DockerSuite) TestPluginBasicOps(c *check.C) {
  18. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  19. _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
  20. c.Assert(err, checker.IsNil)
  21. out, _, err := dockerCmdWithError("plugin", "ls")
  22. c.Assert(err, checker.IsNil)
  23. c.Assert(out, checker.Contains, pName)
  24. c.Assert(out, checker.Contains, pTag)
  25. c.Assert(out, checker.Contains, "true")
  26. id, _, err := dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag)
  27. id = strings.TrimSpace(id)
  28. c.Assert(err, checker.IsNil)
  29. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  30. c.Assert(err, checker.NotNil)
  31. c.Assert(out, checker.Contains, "is enabled")
  32. _, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag)
  33. c.Assert(err, checker.IsNil)
  34. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  35. c.Assert(err, checker.IsNil)
  36. c.Assert(out, checker.Contains, pNameWithTag)
  37. _, err = os.Stat(filepath.Join(dockerBasePath, "plugins", id))
  38. if !os.IsNotExist(err) {
  39. c.Fatal(err)
  40. }
  41. }
  42. func (s *DockerSuite) TestPluginForceRemove(c *check.C) {
  43. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  44. out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
  45. c.Assert(err, checker.IsNil)
  46. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  47. c.Assert(out, checker.Contains, "is enabled")
  48. out, _, err = dockerCmdWithError("plugin", "remove", "--force", pNameWithTag)
  49. c.Assert(err, checker.IsNil)
  50. c.Assert(out, checker.Contains, pNameWithTag)
  51. }
  52. func (s *DockerSuite) TestPluginActive(c *check.C) {
  53. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  54. _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
  55. c.Assert(err, checker.IsNil)
  56. _, _, err = dockerCmdWithError("volume", "create", "-d", pNameWithTag, "--name", "testvol1")
  57. c.Assert(err, checker.IsNil)
  58. out, _, err := dockerCmdWithError("plugin", "disable", pNameWithTag)
  59. c.Assert(out, checker.Contains, "in use")
  60. _, _, err = dockerCmdWithError("volume", "rm", "testvol1")
  61. c.Assert(err, checker.IsNil)
  62. _, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag)
  63. c.Assert(err, checker.IsNil)
  64. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  65. c.Assert(err, checker.IsNil)
  66. c.Assert(out, checker.Contains, pNameWithTag)
  67. }
  68. func (s *DockerSuite) TestPluginInstallDisable(c *check.C) {
  69. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  70. out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", "--disable", pName)
  71. c.Assert(err, checker.IsNil)
  72. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  73. out, _, err = dockerCmdWithError("plugin", "ls")
  74. c.Assert(err, checker.IsNil)
  75. c.Assert(out, checker.Contains, "false")
  76. out, _, err = dockerCmdWithError("plugin", "enable", pName)
  77. c.Assert(err, checker.IsNil)
  78. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  79. out, _, err = dockerCmdWithError("plugin", "disable", pName)
  80. c.Assert(err, checker.IsNil)
  81. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  82. out, _, err = dockerCmdWithError("plugin", "remove", pName)
  83. c.Assert(err, checker.IsNil)
  84. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  85. }
  86. func (s *DockerSuite) TestPluginInstallDisableVolumeLs(c *check.C) {
  87. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  88. out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", "--disable", pName)
  89. c.Assert(err, checker.IsNil)
  90. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  91. dockerCmd(c, "volume", "ls")
  92. }
  93. func (s *DockerSuite) TestPluginSet(c *check.C) {
  94. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  95. out, _ := dockerCmd(c, "plugin", "install", "--grant-all-permissions", "--disable", pName)
  96. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  97. env, _ := dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", pName)
  98. c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=0]")
  99. dockerCmd(c, "plugin", "set", pName, "DEBUG=1")
  100. env, _ = dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", pName)
  101. c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=1]")
  102. }
  103. func (s *DockerSuite) TestPluginInstallArgs(c *check.C) {
  104. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  105. out, _ := dockerCmd(c, "plugin", "install", "--grant-all-permissions", "--disable", pName, "DEBUG=1")
  106. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  107. env, _ := dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", pName)
  108. c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=1]")
  109. }
  110. func (s *DockerRegistrySuite) TestPluginInstallImage(c *check.C) {
  111. testRequires(c, DaemonIsLinux, IsAmd64)
  112. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  113. // tag the image to upload it to the private registry
  114. dockerCmd(c, "tag", "busybox", repoName)
  115. // push the image to the registry
  116. dockerCmd(c, "push", repoName)
  117. out, _, err := dockerCmdWithError("plugin", "install", repoName)
  118. c.Assert(err, checker.NotNil)
  119. c.Assert(out, checker.Contains, "target is image")
  120. }
  121. func (s *DockerSuite) TestPluginEnableDisableNegative(c *check.C) {
  122. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  123. out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pName)
  124. c.Assert(err, checker.IsNil)
  125. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  126. out, _, err = dockerCmdWithError("plugin", "enable", pName)
  127. c.Assert(err, checker.NotNil)
  128. c.Assert(strings.TrimSpace(out), checker.Contains, "already enabled")
  129. _, _, err = dockerCmdWithError("plugin", "disable", pName)
  130. c.Assert(err, checker.IsNil)
  131. out, _, err = dockerCmdWithError("plugin", "disable", pName)
  132. c.Assert(err, checker.NotNil)
  133. c.Assert(strings.TrimSpace(out), checker.Contains, "already disabled")
  134. _, _, err = dockerCmdWithError("plugin", "remove", pName)
  135. c.Assert(err, checker.IsNil)
  136. }
  137. func (s *DockerSuite) TestPluginCreate(c *check.C) {
  138. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  139. name := "foo/bar-driver"
  140. temp, err := ioutil.TempDir("", "foo")
  141. c.Assert(err, checker.IsNil)
  142. defer os.RemoveAll(temp)
  143. data := `{"description": "foo plugin"}`
  144. err = ioutil.WriteFile(filepath.Join(temp, "config.json"), []byte(data), 0644)
  145. c.Assert(err, checker.IsNil)
  146. err = os.MkdirAll(filepath.Join(temp, "rootfs"), 0700)
  147. c.Assert(err, checker.IsNil)
  148. out, _, err := dockerCmdWithError("plugin", "create", name, temp)
  149. c.Assert(err, checker.IsNil)
  150. c.Assert(out, checker.Contains, name)
  151. out, _, err = dockerCmdWithError("plugin", "ls")
  152. c.Assert(err, checker.IsNil)
  153. c.Assert(out, checker.Contains, name)
  154. out, _, err = dockerCmdWithError("plugin", "create", name, temp)
  155. c.Assert(err, checker.NotNil)
  156. c.Assert(out, checker.Contains, "already exist")
  157. out, _, err = dockerCmdWithError("plugin", "ls")
  158. c.Assert(err, checker.IsNil)
  159. c.Assert(out, checker.Contains, name)
  160. // The output will consists of one HEADER line and one line of foo/bar-driver
  161. c.Assert(len(strings.Split(strings.TrimSpace(out), "\n")), checker.Equals, 2)
  162. }
  163. func (s *DockerSuite) TestPluginInspect(c *check.C) {
  164. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  165. _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
  166. c.Assert(err, checker.IsNil)
  167. out, _, err := dockerCmdWithError("plugin", "ls")
  168. c.Assert(err, checker.IsNil)
  169. c.Assert(out, checker.Contains, pName)
  170. c.Assert(out, checker.Contains, pTag)
  171. c.Assert(out, checker.Contains, "true")
  172. // Find the ID first
  173. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag)
  174. c.Assert(err, checker.IsNil)
  175. id := strings.TrimSpace(out)
  176. c.Assert(id, checker.Not(checker.Equals), "")
  177. // Long form
  178. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id)
  179. c.Assert(err, checker.IsNil)
  180. c.Assert(strings.TrimSpace(out), checker.Equals, id)
  181. // Short form
  182. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id[:5])
  183. c.Assert(err, checker.IsNil)
  184. c.Assert(strings.TrimSpace(out), checker.Equals, id)
  185. // Name with tag form
  186. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag)
  187. c.Assert(err, checker.IsNil)
  188. c.Assert(strings.TrimSpace(out), checker.Equals, id)
  189. // Name without tag form
  190. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pName)
  191. c.Assert(err, checker.IsNil)
  192. c.Assert(strings.TrimSpace(out), checker.Equals, id)
  193. _, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag)
  194. c.Assert(err, checker.IsNil)
  195. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  196. c.Assert(err, checker.IsNil)
  197. c.Assert(out, checker.Contains, pNameWithTag)
  198. // After remove nothing should be found
  199. _, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id[:5])
  200. c.Assert(err, checker.NotNil)
  201. }
  202. // Test case for https://github.com/docker/docker/pull/29186#discussion_r91277345
  203. func (s *DockerSuite) TestPluginInspectOnWindows(c *check.C) {
  204. // This test should work on Windows only
  205. testRequires(c, DaemonIsWindows)
  206. out, _, err := dockerCmdWithError("plugin", "inspect", "foobar")
  207. c.Assert(err, checker.NotNil)
  208. c.Assert(out, checker.Contains, "plugins are not supported on this platform")
  209. c.Assert(err.Error(), checker.Contains, "plugins are not supported on this platform")
  210. }