docker_cli_plugins_test.go 17 KB

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