docker_cli_plugins_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "github.com/docker/docker/pkg/integration/checker"
  6. "github.com/go-check/check"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. )
  12. var (
  13. pluginProcessName = "sample-volume-plugin"
  14. pName = "tonistiigi/sample-volume-plugin"
  15. pTag = "latest"
  16. pNameWithTag = pName + ":" + pTag
  17. )
  18. func (s *DockerSuite) TestPluginBasicOps(c *check.C) {
  19. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  20. _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
  21. c.Assert(err, checker.IsNil)
  22. out, _, err := dockerCmdWithError("plugin", "ls")
  23. c.Assert(err, checker.IsNil)
  24. c.Assert(out, checker.Contains, pName)
  25. c.Assert(out, checker.Contains, pTag)
  26. c.Assert(out, checker.Contains, "true")
  27. id, _, err := dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag)
  28. id = strings.TrimSpace(id)
  29. c.Assert(err, checker.IsNil)
  30. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  31. c.Assert(err, checker.NotNil)
  32. c.Assert(out, checker.Contains, "is enabled")
  33. _, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag)
  34. c.Assert(err, checker.IsNil)
  35. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  36. c.Assert(err, checker.IsNil)
  37. c.Assert(out, checker.Contains, pNameWithTag)
  38. _, err = os.Stat(filepath.Join(dockerBasePath, "plugins", id))
  39. if !os.IsNotExist(err) {
  40. c.Fatal(err)
  41. }
  42. }
  43. func (s *DockerSuite) TestPluginForceRemove(c *check.C) {
  44. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  45. out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
  46. c.Assert(err, checker.IsNil)
  47. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  48. c.Assert(out, checker.Contains, "is enabled")
  49. out, _, err = dockerCmdWithError("plugin", "remove", "--force", pNameWithTag)
  50. c.Assert(err, checker.IsNil)
  51. c.Assert(out, checker.Contains, pNameWithTag)
  52. }
  53. func (s *DockerSuite) TestPluginActive(c *check.C) {
  54. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  55. _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
  56. c.Assert(err, checker.IsNil)
  57. _, _, err = dockerCmdWithError("volume", "create", "-d", pNameWithTag, "--name", "testvol1")
  58. c.Assert(err, checker.IsNil)
  59. out, _, err := dockerCmdWithError("plugin", "disable", pNameWithTag)
  60. c.Assert(out, checker.Contains, "in use")
  61. _, _, err = dockerCmdWithError("volume", "rm", "testvol1")
  62. c.Assert(err, checker.IsNil)
  63. _, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag)
  64. c.Assert(err, checker.IsNil)
  65. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  66. c.Assert(err, checker.IsNil)
  67. c.Assert(out, checker.Contains, pNameWithTag)
  68. }
  69. func (s *DockerSuite) TestPluginInstallDisable(c *check.C) {
  70. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  71. out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", "--disable", pName)
  72. c.Assert(err, checker.IsNil)
  73. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  74. out, _, err = dockerCmdWithError("plugin", "ls")
  75. c.Assert(err, checker.IsNil)
  76. c.Assert(out, checker.Contains, "false")
  77. out, _, err = dockerCmdWithError("plugin", "enable", pName)
  78. c.Assert(err, checker.IsNil)
  79. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  80. out, _, err = dockerCmdWithError("plugin", "disable", pName)
  81. c.Assert(err, checker.IsNil)
  82. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  83. out, _, err = dockerCmdWithError("plugin", "remove", pName)
  84. c.Assert(err, checker.IsNil)
  85. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  86. }
  87. func (s *DockerSuite) TestPluginInstallDisableVolumeLs(c *check.C) {
  88. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  89. out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", "--disable", pName)
  90. c.Assert(err, checker.IsNil)
  91. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  92. dockerCmd(c, "volume", "ls")
  93. }
  94. func (s *DockerSuite) TestPluginSet(c *check.C) {
  95. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  96. out, _ := dockerCmd(c, "plugin", "install", "--grant-all-permissions", "--disable", pName)
  97. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  98. env, _ := dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", pName)
  99. c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=0]")
  100. dockerCmd(c, "plugin", "set", pName, "DEBUG=1")
  101. env, _ = dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", pName)
  102. c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=1]")
  103. }
  104. func (s *DockerSuite) TestPluginInstallArgs(c *check.C) {
  105. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  106. out, _ := dockerCmd(c, "plugin", "install", "--grant-all-permissions", "--disable", pName, "DEBUG=1")
  107. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  108. env, _ := dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", pName)
  109. c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=1]")
  110. }
  111. func (s *DockerRegistrySuite) TestPluginInstallImage(c *check.C) {
  112. testRequires(c, DaemonIsLinux, IsAmd64)
  113. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  114. // tag the image to upload it to the private registry
  115. dockerCmd(c, "tag", "busybox", repoName)
  116. // push the image to the registry
  117. dockerCmd(c, "push", repoName)
  118. out, _, err := dockerCmdWithError("plugin", "install", repoName)
  119. c.Assert(err, checker.NotNil)
  120. c.Assert(out, checker.Contains, "target is image")
  121. }
  122. func (s *DockerSuite) TestPluginEnableDisableNegative(c *check.C) {
  123. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  124. out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pName)
  125. c.Assert(err, checker.IsNil)
  126. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  127. out, _, err = dockerCmdWithError("plugin", "enable", pName)
  128. c.Assert(err, checker.NotNil)
  129. c.Assert(strings.TrimSpace(out), checker.Contains, "already enabled")
  130. _, _, err = dockerCmdWithError("plugin", "disable", pName)
  131. c.Assert(err, checker.IsNil)
  132. out, _, err = dockerCmdWithError("plugin", "disable", pName)
  133. c.Assert(err, checker.NotNil)
  134. c.Assert(strings.TrimSpace(out), checker.Contains, "already disabled")
  135. _, _, err = dockerCmdWithError("plugin", "remove", pName)
  136. c.Assert(err, checker.IsNil)
  137. }
  138. func (s *DockerSuite) TestPluginCreate(c *check.C) {
  139. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  140. name := "foo/bar-driver"
  141. temp, err := ioutil.TempDir("", "foo")
  142. c.Assert(err, checker.IsNil)
  143. defer os.RemoveAll(temp)
  144. data := `{"description": "foo plugin"}`
  145. err = ioutil.WriteFile(filepath.Join(temp, "config.json"), []byte(data), 0644)
  146. c.Assert(err, checker.IsNil)
  147. err = os.MkdirAll(filepath.Join(temp, "rootfs"), 0700)
  148. c.Assert(err, checker.IsNil)
  149. out, _, err := dockerCmdWithError("plugin", "create", name, temp)
  150. c.Assert(err, checker.IsNil)
  151. c.Assert(out, checker.Contains, name)
  152. out, _, err = dockerCmdWithError("plugin", "ls")
  153. c.Assert(err, checker.IsNil)
  154. c.Assert(out, checker.Contains, name)
  155. out, _, err = dockerCmdWithError("plugin", "create", name, temp)
  156. c.Assert(err, checker.NotNil)
  157. c.Assert(out, checker.Contains, "already exist")
  158. out, _, err = dockerCmdWithError("plugin", "ls")
  159. c.Assert(err, checker.IsNil)
  160. c.Assert(out, checker.Contains, name)
  161. // The output will consists of one HEADER line and one line of foo/bar-driver
  162. c.Assert(len(strings.Split(strings.TrimSpace(out), "\n")), checker.Equals, 2)
  163. }
  164. func (s *DockerSuite) TestPluginInspect(c *check.C) {
  165. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  166. _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
  167. c.Assert(err, checker.IsNil)
  168. out, _, err := dockerCmdWithError("plugin", "ls")
  169. c.Assert(err, checker.IsNil)
  170. c.Assert(out, checker.Contains, pName)
  171. c.Assert(out, checker.Contains, pTag)
  172. c.Assert(out, checker.Contains, "true")
  173. // Find the ID first
  174. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag)
  175. c.Assert(err, checker.IsNil)
  176. id := strings.TrimSpace(out)
  177. c.Assert(id, checker.Not(checker.Equals), "")
  178. // Long form
  179. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id)
  180. c.Assert(err, checker.IsNil)
  181. c.Assert(strings.TrimSpace(out), checker.Equals, id)
  182. // Short form
  183. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id[:5])
  184. c.Assert(err, checker.IsNil)
  185. c.Assert(strings.TrimSpace(out), checker.Equals, id)
  186. // Name with tag form
  187. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag)
  188. c.Assert(err, checker.IsNil)
  189. c.Assert(strings.TrimSpace(out), checker.Equals, id)
  190. // Name without tag form
  191. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pName)
  192. c.Assert(err, checker.IsNil)
  193. c.Assert(strings.TrimSpace(out), checker.Equals, id)
  194. _, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag)
  195. c.Assert(err, checker.IsNil)
  196. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  197. c.Assert(err, checker.IsNil)
  198. c.Assert(out, checker.Contains, pNameWithTag)
  199. // After remove nothing should be found
  200. _, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id[:5])
  201. c.Assert(err, checker.NotNil)
  202. }
  203. // Test case for https://github.com/docker/docker/pull/29186#discussion_r91277345
  204. func (s *DockerSuite) TestPluginInspectOnWindows(c *check.C) {
  205. // This test should work on Windows only
  206. testRequires(c, DaemonIsWindows)
  207. out, _, err := dockerCmdWithError("plugin", "inspect", "foobar")
  208. c.Assert(err, checker.NotNil)
  209. c.Assert(out, checker.Contains, "plugins are not supported on this platform")
  210. c.Assert(err.Error(), checker.Contains, "plugins are not supported on this platform")
  211. }
  212. func (s *DockerTrustSuite) TestPluginTrustedInstall(c *check.C) {
  213. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  214. trustedName := s.setupTrustedplugin(c, pNameWithTag, "trusted-plugin-install")
  215. installCmd := exec.Command(dockerBinary, "plugin", "install", "--grant-all-permissions", trustedName)
  216. s.trustedCmd(installCmd)
  217. out, _, err := runCommandWithOutput(installCmd)
  218. c.Assert(strings.TrimSpace(out), checker.Contains, trustedName)
  219. c.Assert(err, checker.IsNil)
  220. c.Assert(strings.TrimSpace(out), checker.Contains, trustedName)
  221. out, _, err = dockerCmdWithError("plugin", "ls")
  222. c.Assert(err, checker.IsNil)
  223. c.Assert(out, checker.Contains, "true")
  224. out, _, err = dockerCmdWithError("plugin", "disable", trustedName)
  225. c.Assert(err, checker.IsNil)
  226. c.Assert(strings.TrimSpace(out), checker.Contains, trustedName)
  227. out, _, err = dockerCmdWithError("plugin", "enable", trustedName)
  228. c.Assert(err, checker.IsNil)
  229. c.Assert(strings.TrimSpace(out), checker.Contains, trustedName)
  230. out, _, err = dockerCmdWithError("plugin", "rm", "-f", trustedName)
  231. c.Assert(err, checker.IsNil)
  232. c.Assert(strings.TrimSpace(out), checker.Contains, trustedName)
  233. // Try untrusted pull to ensure we pushed the tag to the registry
  234. installCmd = exec.Command(dockerBinary, "plugin", "install", "--disable-content-trust=true", "--grant-all-permissions", trustedName)
  235. s.trustedCmd(installCmd)
  236. out, _, err = runCommandWithOutput(installCmd)
  237. c.Assert(err, check.IsNil, check.Commentf(out))
  238. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  239. out, _, err = dockerCmdWithError("plugin", "ls")
  240. c.Assert(err, checker.IsNil)
  241. c.Assert(out, checker.Contains, "true")
  242. }
  243. func (s *DockerTrustSuite) TestPluginUntrustedInstall(c *check.C) {
  244. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  245. pluginName := fmt.Sprintf("%v/dockercliuntrusted/plugintest:latest", privateRegistryURL)
  246. // install locally and push to private registry
  247. dockerCmd(c, "plugin", "install", "--grant-all-permissions", "--alias", pluginName, pNameWithTag)
  248. dockerCmd(c, "plugin", "push", pluginName)
  249. dockerCmd(c, "plugin", "rm", "-f", pluginName)
  250. // Try trusted install on untrusted plugin
  251. installCmd := exec.Command(dockerBinary, "plugin", "install", "--grant-all-permissions", pluginName)
  252. s.trustedCmd(installCmd)
  253. out, _, err := runCommandWithOutput(installCmd)
  254. c.Assert(err, check.NotNil, check.Commentf(out))
  255. c.Assert(string(out), checker.Contains, "Error: remote trust data does not exist", check.Commentf(out))
  256. }
  257. func (s *DockerSuite) TestPluginIDPrefix(c *check.C) {
  258. testRequires(c, DaemonIsLinux, Network)
  259. _, _, err := dockerCmdWithError("plugin", "install", "--disable", "--grant-all-permissions", pNameWithTag)
  260. c.Assert(err, checker.IsNil)
  261. // Find ID first
  262. id, _, err := dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag)
  263. id = strings.TrimSpace(id)
  264. c.Assert(err, checker.IsNil)
  265. // List current state
  266. out, _, err := dockerCmdWithError("plugin", "ls")
  267. c.Assert(err, checker.IsNil)
  268. c.Assert(out, checker.Contains, pName)
  269. c.Assert(out, checker.Contains, pTag)
  270. c.Assert(out, checker.Contains, "false")
  271. env, _ := dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", id[:5])
  272. c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=0]")
  273. dockerCmd(c, "plugin", "set", id[:5], "DEBUG=1")
  274. env, _ = dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", id[:5])
  275. c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=1]")
  276. // Enable
  277. _, _, err = dockerCmdWithError("plugin", "enable", id[:5])
  278. c.Assert(err, checker.IsNil)
  279. out, _, err = dockerCmdWithError("plugin", "ls")
  280. c.Assert(err, checker.IsNil)
  281. c.Assert(out, checker.Contains, pName)
  282. c.Assert(out, checker.Contains, pTag)
  283. c.Assert(out, checker.Contains, "true")
  284. // Disable
  285. _, _, err = dockerCmdWithError("plugin", "disable", id[:5])
  286. c.Assert(err, checker.IsNil)
  287. out, _, err = dockerCmdWithError("plugin", "ls")
  288. c.Assert(err, checker.IsNil)
  289. c.Assert(out, checker.Contains, pName)
  290. c.Assert(out, checker.Contains, pTag)
  291. c.Assert(out, checker.Contains, "false")
  292. // Remove
  293. out, _, err = dockerCmdWithError("plugin", "remove", id[:5])
  294. c.Assert(err, checker.IsNil)
  295. // List returns none
  296. out, _, err = dockerCmdWithError("plugin", "ls")
  297. c.Assert(err, checker.IsNil)
  298. c.Assert(out, checker.Not(checker.Contains), pName)
  299. c.Assert(out, checker.Not(checker.Contains), pTag)
  300. }