docker_cli_plugins_test.go 9.2 KB

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