docker_cli_plugins_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "path"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/integration-cli/checker"
  13. "github.com/docker/docker/integration-cli/cli"
  14. "github.com/docker/docker/integration-cli/daemon"
  15. "github.com/docker/docker/integration-cli/fixtures/plugin"
  16. "github.com/docker/docker/integration-cli/request"
  17. "github.com/go-check/check"
  18. "github.com/gotestyourself/gotestyourself/icmd"
  19. "golang.org/x/net/context"
  20. )
  21. var (
  22. pluginProcessName = "sample-volume-plugin"
  23. pName = "tiborvass/sample-volume-plugin"
  24. npName = "tiborvass/test-docker-netplugin"
  25. pTag = "latest"
  26. pNameWithTag = pName + ":" + pTag
  27. npNameWithTag = npName + ":" + pTag
  28. )
  29. func (ps *DockerPluginSuite) TestPluginBasicOps(c *check.C) {
  30. plugin := ps.getPluginRepoWithTag()
  31. _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", plugin)
  32. c.Assert(err, checker.IsNil)
  33. out, _, err := dockerCmdWithError("plugin", "ls")
  34. c.Assert(err, checker.IsNil)
  35. c.Assert(out, checker.Contains, plugin)
  36. c.Assert(out, checker.Contains, "true")
  37. id, _, err := dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", plugin)
  38. id = strings.TrimSpace(id)
  39. c.Assert(err, checker.IsNil)
  40. out, _, err = dockerCmdWithError("plugin", "remove", plugin)
  41. c.Assert(err, checker.NotNil)
  42. c.Assert(out, checker.Contains, "is enabled")
  43. _, _, err = dockerCmdWithError("plugin", "disable", plugin)
  44. c.Assert(err, checker.IsNil)
  45. out, _, err = dockerCmdWithError("plugin", "remove", plugin)
  46. c.Assert(err, checker.IsNil)
  47. c.Assert(out, checker.Contains, plugin)
  48. _, err = os.Stat(filepath.Join(testEnv.DaemonInfo.DockerRootDir, "plugins", id))
  49. if !os.IsNotExist(err) {
  50. c.Fatal(err)
  51. }
  52. }
  53. func (ps *DockerPluginSuite) TestPluginForceRemove(c *check.C) {
  54. pNameWithTag := ps.getPluginRepoWithTag()
  55. out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
  56. c.Assert(err, checker.IsNil)
  57. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  58. c.Assert(out, checker.Contains, "is enabled")
  59. out, _, err = dockerCmdWithError("plugin", "remove", "--force", pNameWithTag)
  60. c.Assert(err, checker.IsNil)
  61. c.Assert(out, checker.Contains, pNameWithTag)
  62. }
  63. func (s *DockerSuite) TestPluginActive(c *check.C) {
  64. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  65. _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
  66. c.Assert(err, checker.IsNil)
  67. _, _, err = dockerCmdWithError("volume", "create", "-d", pNameWithTag, "--name", "testvol1")
  68. c.Assert(err, checker.IsNil)
  69. out, _, err := dockerCmdWithError("plugin", "disable", pNameWithTag)
  70. c.Assert(out, checker.Contains, "in use")
  71. _, _, err = dockerCmdWithError("volume", "rm", "testvol1")
  72. c.Assert(err, checker.IsNil)
  73. _, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag)
  74. c.Assert(err, checker.IsNil)
  75. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  76. c.Assert(err, checker.IsNil)
  77. c.Assert(out, checker.Contains, pNameWithTag)
  78. }
  79. func (s *DockerSuite) TestPluginActiveNetwork(c *check.C) {
  80. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  81. out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", npNameWithTag)
  82. c.Assert(err, checker.IsNil)
  83. out, _, err = dockerCmdWithError("network", "create", "-d", npNameWithTag, "test")
  84. c.Assert(err, checker.IsNil)
  85. nID := strings.TrimSpace(out)
  86. out, _, err = dockerCmdWithError("plugin", "remove", npNameWithTag)
  87. c.Assert(out, checker.Contains, "is in use")
  88. _, _, err = dockerCmdWithError("network", "rm", nID)
  89. c.Assert(err, checker.IsNil)
  90. out, _, err = dockerCmdWithError("plugin", "remove", npNameWithTag)
  91. c.Assert(out, checker.Contains, "is enabled")
  92. _, _, err = dockerCmdWithError("plugin", "disable", npNameWithTag)
  93. c.Assert(err, checker.IsNil)
  94. out, _, err = dockerCmdWithError("plugin", "remove", npNameWithTag)
  95. c.Assert(err, checker.IsNil)
  96. c.Assert(out, checker.Contains, npNameWithTag)
  97. }
  98. func (ps *DockerPluginSuite) TestPluginInstallDisable(c *check.C) {
  99. pName := ps.getPluginRepoWithTag()
  100. out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", "--disable", pName)
  101. c.Assert(err, checker.IsNil)
  102. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  103. out, _, err = dockerCmdWithError("plugin", "ls")
  104. c.Assert(err, checker.IsNil)
  105. c.Assert(out, checker.Contains, "false")
  106. out, _, err = dockerCmdWithError("plugin", "enable", pName)
  107. c.Assert(err, checker.IsNil)
  108. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  109. out, _, err = dockerCmdWithError("plugin", "disable", pName)
  110. c.Assert(err, checker.IsNil)
  111. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  112. out, _, err = dockerCmdWithError("plugin", "remove", pName)
  113. c.Assert(err, checker.IsNil)
  114. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  115. }
  116. func (s *DockerSuite) TestPluginInstallDisableVolumeLs(c *check.C) {
  117. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  118. out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", "--disable", pName)
  119. c.Assert(err, checker.IsNil)
  120. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  121. dockerCmd(c, "volume", "ls")
  122. }
  123. func (ps *DockerPluginSuite) TestPluginSet(c *check.C) {
  124. // Create a new plugin with extra settings
  125. client, err := request.NewClient()
  126. c.Assert(err, checker.IsNil, check.Commentf("failed to create test client"))
  127. name := "test"
  128. ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  129. defer cancel()
  130. initialValue := "0"
  131. mntSrc := "foo"
  132. devPath := "/dev/bar"
  133. err = plugin.Create(ctx, client, name, func(cfg *plugin.Config) {
  134. cfg.Env = []types.PluginEnv{{Name: "DEBUG", Value: &initialValue, Settable: []string{"value"}}}
  135. cfg.Mounts = []types.PluginMount{
  136. {Name: "pmount1", Settable: []string{"source"}, Type: "none", Source: &mntSrc},
  137. {Name: "pmount2", Settable: []string{"source"}, Type: "none"}, // Mount without source is invalid.
  138. }
  139. cfg.Linux.Devices = []types.PluginDevice{
  140. {Name: "pdev1", Path: &devPath, Settable: []string{"path"}},
  141. {Name: "pdev2", Settable: []string{"path"}}, // Device without Path is invalid.
  142. }
  143. })
  144. c.Assert(err, checker.IsNil, check.Commentf("failed to create test plugin"))
  145. env, _ := dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", name)
  146. c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=0]")
  147. dockerCmd(c, "plugin", "set", name, "DEBUG=1")
  148. env, _ = dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", name)
  149. c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=1]")
  150. env, _ = dockerCmd(c, "plugin", "inspect", "-f", "{{with $mount := index .Settings.Mounts 0}}{{$mount.Source}}{{end}}", name)
  151. c.Assert(strings.TrimSpace(env), checker.Contains, mntSrc)
  152. dockerCmd(c, "plugin", "set", name, "pmount1.source=bar")
  153. env, _ = dockerCmd(c, "plugin", "inspect", "-f", "{{with $mount := index .Settings.Mounts 0}}{{$mount.Source}}{{end}}", name)
  154. c.Assert(strings.TrimSpace(env), checker.Contains, "bar")
  155. out, _, err := dockerCmdWithError("plugin", "set", name, "pmount2.source=bar2")
  156. c.Assert(err, checker.NotNil)
  157. c.Assert(out, checker.Contains, "Plugin config has no mount source")
  158. out, _, err = dockerCmdWithError("plugin", "set", name, "pdev2.path=/dev/bar2")
  159. c.Assert(err, checker.NotNil)
  160. c.Assert(out, checker.Contains, "Plugin config has no device path")
  161. }
  162. func (ps *DockerPluginSuite) TestPluginInstallArgs(c *check.C) {
  163. pName := path.Join(ps.registryHost(), "plugin", "testplugininstallwithargs")
  164. ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  165. defer cancel()
  166. plugin.CreateInRegistry(ctx, pName, nil, func(cfg *plugin.Config) {
  167. cfg.Env = []types.PluginEnv{{Name: "DEBUG", Settable: []string{"value"}}}
  168. })
  169. out, _ := dockerCmd(c, "plugin", "install", "--grant-all-permissions", "--disable", pName, "DEBUG=1")
  170. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  171. env, _ := dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", pName)
  172. c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=1]")
  173. }
  174. func (ps *DockerPluginSuite) TestPluginInstallImage(c *check.C) {
  175. testRequires(c, IsAmd64)
  176. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  177. // tag the image to upload it to the private registry
  178. dockerCmd(c, "tag", "busybox", repoName)
  179. // push the image to the registry
  180. dockerCmd(c, "push", repoName)
  181. out, _, err := dockerCmdWithError("plugin", "install", repoName)
  182. c.Assert(err, checker.NotNil)
  183. c.Assert(out, checker.Contains, `Encountered remote "application/vnd.docker.container.image.v1+json"(image) when fetching`)
  184. }
  185. func (ps *DockerPluginSuite) TestPluginEnableDisableNegative(c *check.C) {
  186. pName := ps.getPluginRepoWithTag()
  187. out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pName)
  188. c.Assert(err, checker.IsNil)
  189. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  190. out, _, err = dockerCmdWithError("plugin", "enable", pName)
  191. c.Assert(err, checker.NotNil)
  192. c.Assert(strings.TrimSpace(out), checker.Contains, "already enabled")
  193. _, _, err = dockerCmdWithError("plugin", "disable", pName)
  194. c.Assert(err, checker.IsNil)
  195. out, _, err = dockerCmdWithError("plugin", "disable", pName)
  196. c.Assert(err, checker.NotNil)
  197. c.Assert(strings.TrimSpace(out), checker.Contains, "already disabled")
  198. _, _, err = dockerCmdWithError("plugin", "remove", pName)
  199. c.Assert(err, checker.IsNil)
  200. }
  201. func (ps *DockerPluginSuite) TestPluginCreate(c *check.C) {
  202. name := "foo/bar-driver"
  203. temp, err := ioutil.TempDir("", "foo")
  204. c.Assert(err, checker.IsNil)
  205. defer os.RemoveAll(temp)
  206. data := `{"description": "foo plugin"}`
  207. err = ioutil.WriteFile(filepath.Join(temp, "config.json"), []byte(data), 0644)
  208. c.Assert(err, checker.IsNil)
  209. err = os.MkdirAll(filepath.Join(temp, "rootfs"), 0700)
  210. c.Assert(err, checker.IsNil)
  211. out, _, err := dockerCmdWithError("plugin", "create", name, temp)
  212. c.Assert(err, checker.IsNil)
  213. c.Assert(out, checker.Contains, name)
  214. out, _, err = dockerCmdWithError("plugin", "ls")
  215. c.Assert(err, checker.IsNil)
  216. c.Assert(out, checker.Contains, name)
  217. out, _, err = dockerCmdWithError("plugin", "create", name, temp)
  218. c.Assert(err, checker.NotNil)
  219. c.Assert(out, checker.Contains, "already exist")
  220. out, _, err = dockerCmdWithError("plugin", "ls")
  221. c.Assert(err, checker.IsNil)
  222. c.Assert(out, checker.Contains, name)
  223. // The output will consists of one HEADER line and one line of foo/bar-driver
  224. c.Assert(len(strings.Split(strings.TrimSpace(out), "\n")), checker.Equals, 2)
  225. }
  226. func (ps *DockerPluginSuite) TestPluginInspect(c *check.C) {
  227. pNameWithTag := ps.getPluginRepoWithTag()
  228. _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
  229. c.Assert(err, checker.IsNil)
  230. out, _, err := dockerCmdWithError("plugin", "ls")
  231. c.Assert(err, checker.IsNil)
  232. c.Assert(out, checker.Contains, pNameWithTag)
  233. c.Assert(out, checker.Contains, "true")
  234. // Find the ID first
  235. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag)
  236. c.Assert(err, checker.IsNil)
  237. id := strings.TrimSpace(out)
  238. c.Assert(id, checker.Not(checker.Equals), "")
  239. // Long form
  240. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id)
  241. c.Assert(err, checker.IsNil)
  242. c.Assert(strings.TrimSpace(out), checker.Equals, id)
  243. // Short form
  244. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id[:5])
  245. c.Assert(err, checker.IsNil)
  246. c.Assert(strings.TrimSpace(out), checker.Equals, id)
  247. // Name with tag form
  248. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag)
  249. c.Assert(err, checker.IsNil)
  250. c.Assert(strings.TrimSpace(out), checker.Equals, id)
  251. // Name without tag form
  252. out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", ps.getPluginRepo())
  253. c.Assert(err, checker.IsNil)
  254. c.Assert(strings.TrimSpace(out), checker.Equals, id)
  255. _, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag)
  256. c.Assert(err, checker.IsNil)
  257. out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
  258. c.Assert(err, checker.IsNil)
  259. c.Assert(out, checker.Contains, pNameWithTag)
  260. // After remove nothing should be found
  261. _, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id[:5])
  262. c.Assert(err, checker.NotNil)
  263. }
  264. // Test case for https://github.com/docker/docker/pull/29186#discussion_r91277345
  265. func (s *DockerSuite) TestPluginInspectOnWindows(c *check.C) {
  266. // This test should work on Windows only
  267. testRequires(c, DaemonIsWindows)
  268. out, _, err := dockerCmdWithError("plugin", "inspect", "foobar")
  269. c.Assert(err, checker.NotNil)
  270. c.Assert(out, checker.Contains, "plugins are not supported on this platform")
  271. c.Assert(err.Error(), checker.Contains, "plugins are not supported on this platform")
  272. }
  273. func (s *DockerTrustSuite) TestPluginTrustedInstall(c *check.C) {
  274. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  275. trustedName := s.setupTrustedplugin(c, pNameWithTag, "trusted-plugin-install")
  276. cli.Docker(cli.Args("plugin", "install", "--grant-all-permissions", trustedName), trustedCmd).Assert(c, icmd.Expected{
  277. Out: trustedName,
  278. })
  279. out := cli.DockerCmd(c, "plugin", "ls").Combined()
  280. c.Assert(out, checker.Contains, "true")
  281. out = cli.DockerCmd(c, "plugin", "disable", trustedName).Combined()
  282. c.Assert(strings.TrimSpace(out), checker.Contains, trustedName)
  283. out = cli.DockerCmd(c, "plugin", "enable", trustedName).Combined()
  284. c.Assert(strings.TrimSpace(out), checker.Contains, trustedName)
  285. out = cli.DockerCmd(c, "plugin", "rm", "-f", trustedName).Combined()
  286. c.Assert(strings.TrimSpace(out), checker.Contains, trustedName)
  287. // Try untrusted pull to ensure we pushed the tag to the registry
  288. cli.Docker(cli.Args("plugin", "install", "--disable-content-trust=true", "--grant-all-permissions", trustedName), trustedCmd).Assert(c, SuccessDownloaded)
  289. out = cli.DockerCmd(c, "plugin", "ls").Combined()
  290. c.Assert(out, checker.Contains, "true")
  291. }
  292. func (s *DockerTrustSuite) TestPluginUntrustedInstall(c *check.C) {
  293. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  294. pluginName := fmt.Sprintf("%v/dockercliuntrusted/plugintest:latest", privateRegistryURL)
  295. // install locally and push to private registry
  296. cli.DockerCmd(c, "plugin", "install", "--grant-all-permissions", "--alias", pluginName, pNameWithTag)
  297. cli.DockerCmd(c, "plugin", "push", pluginName)
  298. cli.DockerCmd(c, "plugin", "rm", "-f", pluginName)
  299. // Try trusted install on untrusted plugin
  300. cli.Docker(cli.Args("plugin", "install", "--grant-all-permissions", pluginName), trustedCmd).Assert(c, icmd.Expected{
  301. ExitCode: 1,
  302. Err: "Error: remote trust data does not exist",
  303. })
  304. }
  305. func (ps *DockerPluginSuite) TestPluginIDPrefix(c *check.C) {
  306. name := "test"
  307. client, err := request.NewClient()
  308. c.Assert(err, checker.IsNil, check.Commentf("error creating test client"))
  309. ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  310. initialValue := "0"
  311. err = plugin.Create(ctx, client, name, func(cfg *plugin.Config) {
  312. cfg.Env = []types.PluginEnv{{Name: "DEBUG", Value: &initialValue, Settable: []string{"value"}}}
  313. })
  314. cancel()
  315. c.Assert(err, checker.IsNil, check.Commentf("failed to create test plugin"))
  316. // Find ID first
  317. id, _, err := dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", name)
  318. id = strings.TrimSpace(id)
  319. c.Assert(err, checker.IsNil)
  320. // List current state
  321. out, _, err := dockerCmdWithError("plugin", "ls")
  322. c.Assert(err, checker.IsNil)
  323. c.Assert(out, checker.Contains, name)
  324. c.Assert(out, checker.Contains, "false")
  325. env, _ := dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", id[:5])
  326. c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=0]")
  327. dockerCmd(c, "plugin", "set", id[:5], "DEBUG=1")
  328. env, _ = dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", id[:5])
  329. c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=1]")
  330. // Enable
  331. _, _, err = dockerCmdWithError("plugin", "enable", id[:5])
  332. c.Assert(err, checker.IsNil)
  333. out, _, err = dockerCmdWithError("plugin", "ls")
  334. c.Assert(err, checker.IsNil)
  335. c.Assert(out, checker.Contains, name)
  336. c.Assert(out, checker.Contains, "true")
  337. // Disable
  338. _, _, err = dockerCmdWithError("plugin", "disable", id[:5])
  339. c.Assert(err, checker.IsNil)
  340. out, _, err = dockerCmdWithError("plugin", "ls")
  341. c.Assert(err, checker.IsNil)
  342. c.Assert(out, checker.Contains, name)
  343. c.Assert(out, checker.Contains, "false")
  344. // Remove
  345. out, _, err = dockerCmdWithError("plugin", "remove", id[:5])
  346. c.Assert(err, checker.IsNil)
  347. // List returns none
  348. out, _, err = dockerCmdWithError("plugin", "ls")
  349. c.Assert(err, checker.IsNil)
  350. c.Assert(out, checker.Not(checker.Contains), name)
  351. }
  352. func (ps *DockerPluginSuite) TestPluginListDefaultFormat(c *check.C) {
  353. config, err := ioutil.TempDir("", "config-file-")
  354. c.Assert(err, check.IsNil)
  355. defer os.RemoveAll(config)
  356. err = ioutil.WriteFile(filepath.Join(config, "config.json"), []byte(`{"pluginsFormat": "raw"}`), 0644)
  357. c.Assert(err, check.IsNil)
  358. name := "test:latest"
  359. client, err := request.NewClient()
  360. c.Assert(err, checker.IsNil, check.Commentf("error creating test client"))
  361. ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  362. defer cancel()
  363. err = plugin.Create(ctx, client, name, func(cfg *plugin.Config) {
  364. cfg.Description = "test plugin"
  365. })
  366. c.Assert(err, checker.IsNil, check.Commentf("failed to create test plugin"))
  367. out, _ := dockerCmd(c, "plugin", "inspect", "--format", "{{.ID}}", name)
  368. id := strings.TrimSpace(out)
  369. // We expect the format to be in `raw + --no-trunc`
  370. expectedOutput := fmt.Sprintf(`plugin_id: %s
  371. name: %s
  372. description: test plugin
  373. enabled: false`, id, name)
  374. out, _ = dockerCmd(c, "--config", config, "plugin", "ls", "--no-trunc")
  375. c.Assert(strings.TrimSpace(out), checker.Contains, expectedOutput)
  376. }
  377. func (s *DockerSuite) TestPluginUpgrade(c *check.C) {
  378. testRequires(c, DaemonIsLinux, Network, SameHostDaemon, IsAmd64, NotUserNamespace)
  379. plugin := "cpuguy83/docker-volume-driver-plugin-local:latest"
  380. pluginV2 := "cpuguy83/docker-volume-driver-plugin-local:v2"
  381. dockerCmd(c, "plugin", "install", "--grant-all-permissions", plugin)
  382. dockerCmd(c, "volume", "create", "--driver", plugin, "bananas")
  383. dockerCmd(c, "run", "--rm", "-v", "bananas:/apple", "busybox", "sh", "-c", "touch /apple/core")
  384. out, _, err := dockerCmdWithError("plugin", "upgrade", "--grant-all-permissions", plugin, pluginV2)
  385. c.Assert(err, checker.NotNil, check.Commentf(out))
  386. c.Assert(out, checker.Contains, "disabled before upgrading")
  387. out, _ = dockerCmd(c, "plugin", "inspect", "--format={{.ID}}", plugin)
  388. id := strings.TrimSpace(out)
  389. // make sure "v2" does not exists
  390. _, err = os.Stat(filepath.Join(testEnv.DaemonInfo.DockerRootDir, "plugins", id, "rootfs", "v2"))
  391. c.Assert(os.IsNotExist(err), checker.True, check.Commentf(out))
  392. dockerCmd(c, "plugin", "disable", "-f", plugin)
  393. dockerCmd(c, "plugin", "upgrade", "--grant-all-permissions", "--skip-remote-check", plugin, pluginV2)
  394. // make sure "v2" file exists
  395. _, err = os.Stat(filepath.Join(testEnv.DaemonInfo.DockerRootDir, "plugins", id, "rootfs", "v2"))
  396. c.Assert(err, checker.IsNil)
  397. dockerCmd(c, "plugin", "enable", plugin)
  398. dockerCmd(c, "volume", "inspect", "bananas")
  399. dockerCmd(c, "run", "--rm", "-v", "bananas:/apple", "busybox", "sh", "-c", "ls -lh /apple/core")
  400. }
  401. func (s *DockerSuite) TestPluginMetricsCollector(c *check.C) {
  402. testRequires(c, DaemonIsLinux, Network, SameHostDaemon, IsAmd64)
  403. d := daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{})
  404. d.Start(c)
  405. defer d.Stop(c)
  406. name := "cpuguy83/docker-metrics-plugin-test:latest"
  407. r := cli.Docker(cli.Args("plugin", "install", "--grant-all-permissions", name), cli.Daemon(d))
  408. c.Assert(r.Error, checker.IsNil, check.Commentf(r.Combined()))
  409. // plugin lisens on localhost:19393 and proxies the metrics
  410. resp, err := http.Get("http://localhost:19393/metrics")
  411. c.Assert(err, checker.IsNil)
  412. defer resp.Body.Close()
  413. b, err := ioutil.ReadAll(resp.Body)
  414. c.Assert(err, checker.IsNil)
  415. // check that a known metric is there... don't expect this metric to change over time.. probably safe
  416. c.Assert(string(b), checker.Contains, "container_actions")
  417. }