docker_cli_daemon_plugins_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // +build linux
  2. package main
  3. import (
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/docker/docker/integration-cli/checker"
  8. "github.com/docker/docker/pkg/mount"
  9. "github.com/go-check/check"
  10. "github.com/gotestyourself/gotestyourself/icmd"
  11. "golang.org/x/sys/unix"
  12. )
  13. // TestDaemonRestartWithPluginEnabled tests state restore for an enabled plugin
  14. func (s *DockerDaemonSuite) TestDaemonRestartWithPluginEnabled(c *check.C) {
  15. testRequires(c, IsAmd64, Network)
  16. s.d.Start(c)
  17. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
  18. c.Fatalf("Could not install plugin: %v %s", err, out)
  19. }
  20. defer func() {
  21. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  22. c.Fatalf("Could not disable plugin: %v %s", err, out)
  23. }
  24. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  25. c.Fatalf("Could not remove plugin: %v %s", err, out)
  26. }
  27. }()
  28. s.d.Restart(c)
  29. out, err := s.d.Cmd("plugin", "ls")
  30. if err != nil {
  31. c.Fatalf("Could not list plugins: %v %s", err, out)
  32. }
  33. c.Assert(out, checker.Contains, pName)
  34. c.Assert(out, checker.Contains, "true")
  35. }
  36. // TestDaemonRestartWithPluginDisabled tests state restore for a disabled plugin
  37. func (s *DockerDaemonSuite) TestDaemonRestartWithPluginDisabled(c *check.C) {
  38. testRequires(c, IsAmd64, Network)
  39. s.d.Start(c)
  40. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName, "--disable"); err != nil {
  41. c.Fatalf("Could not install plugin: %v %s", err, out)
  42. }
  43. defer func() {
  44. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  45. c.Fatalf("Could not remove plugin: %v %s", err, out)
  46. }
  47. }()
  48. s.d.Restart(c)
  49. out, err := s.d.Cmd("plugin", "ls")
  50. if err != nil {
  51. c.Fatalf("Could not list plugins: %v %s", err, out)
  52. }
  53. c.Assert(out, checker.Contains, pName)
  54. c.Assert(out, checker.Contains, "false")
  55. }
  56. // TestDaemonKillLiveRestoreWithPlugins SIGKILLs daemon started with --live-restore.
  57. // Plugins should continue to run.
  58. func (s *DockerDaemonSuite) TestDaemonKillLiveRestoreWithPlugins(c *check.C) {
  59. testRequires(c, IsAmd64, Network)
  60. s.d.Start(c, "--live-restore")
  61. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
  62. c.Fatalf("Could not install plugin: %v %s", err, out)
  63. }
  64. defer func() {
  65. s.d.Restart(c, "--live-restore")
  66. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  67. c.Fatalf("Could not disable plugin: %v %s", err, out)
  68. }
  69. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  70. c.Fatalf("Could not remove plugin: %v %s", err, out)
  71. }
  72. }()
  73. if err := s.d.Kill(); err != nil {
  74. c.Fatalf("Could not kill daemon: %v", err)
  75. }
  76. icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Success)
  77. }
  78. // TestDaemonShutdownLiveRestoreWithPlugins SIGTERMs daemon started with --live-restore.
  79. // Plugins should continue to run.
  80. func (s *DockerDaemonSuite) TestDaemonShutdownLiveRestoreWithPlugins(c *check.C) {
  81. testRequires(c, IsAmd64, Network)
  82. s.d.Start(c, "--live-restore")
  83. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
  84. c.Fatalf("Could not install plugin: %v %s", err, out)
  85. }
  86. defer func() {
  87. s.d.Restart(c, "--live-restore")
  88. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  89. c.Fatalf("Could not disable plugin: %v %s", err, out)
  90. }
  91. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  92. c.Fatalf("Could not remove plugin: %v %s", err, out)
  93. }
  94. }()
  95. if err := s.d.Interrupt(); err != nil {
  96. c.Fatalf("Could not kill daemon: %v", err)
  97. }
  98. icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Success)
  99. }
  100. // TestDaemonShutdownWithPlugins shuts down running plugins.
  101. func (s *DockerDaemonSuite) TestDaemonShutdownWithPlugins(c *check.C) {
  102. testRequires(c, IsAmd64, Network, SameHostDaemon)
  103. s.d.Start(c)
  104. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
  105. c.Fatalf("Could not install plugin: %v %s", err, out)
  106. }
  107. defer func() {
  108. s.d.Restart(c)
  109. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  110. c.Fatalf("Could not disable plugin: %v %s", err, out)
  111. }
  112. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  113. c.Fatalf("Could not remove plugin: %v %s", err, out)
  114. }
  115. }()
  116. if err := s.d.Interrupt(); err != nil {
  117. c.Fatalf("Could not kill daemon: %v", err)
  118. }
  119. for {
  120. if err := unix.Kill(s.d.Pid(), 0); err == unix.ESRCH {
  121. break
  122. }
  123. }
  124. icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Expected{
  125. ExitCode: 1,
  126. Error: "exit status 1",
  127. })
  128. s.d.Start(c)
  129. icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Success)
  130. }
  131. // TestDaemonKillWithPlugins leaves plugins running.
  132. func (s *DockerDaemonSuite) TestDaemonKillWithPlugins(c *check.C) {
  133. testRequires(c, IsAmd64, Network, SameHostDaemon)
  134. s.d.Start(c)
  135. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
  136. c.Fatalf("Could not install plugin: %v %s", err, out)
  137. }
  138. defer func() {
  139. s.d.Restart(c)
  140. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  141. c.Fatalf("Could not disable plugin: %v %s", err, out)
  142. }
  143. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  144. c.Fatalf("Could not remove plugin: %v %s", err, out)
  145. }
  146. }()
  147. if err := s.d.Kill(); err != nil {
  148. c.Fatalf("Could not kill daemon: %v", err)
  149. }
  150. // assert that plugins are running.
  151. icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Success)
  152. }
  153. // TestVolumePlugin tests volume creation using a plugin.
  154. func (s *DockerDaemonSuite) TestVolumePlugin(c *check.C) {
  155. testRequires(c, IsAmd64, Network)
  156. volName := "plugin-volume"
  157. destDir := "/tmp/data/"
  158. destFile := "foo"
  159. s.d.Start(c)
  160. out, err := s.d.Cmd("plugin", "install", pName, "--grant-all-permissions")
  161. if err != nil {
  162. c.Fatalf("Could not install plugin: %v %s", err, out)
  163. }
  164. pluginID, err := s.d.Cmd("plugin", "inspect", "-f", "{{.Id}}", pName)
  165. pluginID = strings.TrimSpace(pluginID)
  166. if err != nil {
  167. c.Fatalf("Could not retrieve plugin ID: %v %s", err, pluginID)
  168. }
  169. mountpointPrefix := filepath.Join(s.d.RootDir(), "plugins", pluginID, "rootfs")
  170. defer func() {
  171. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  172. c.Fatalf("Could not disable plugin: %v %s", err, out)
  173. }
  174. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  175. c.Fatalf("Could not remove plugin: %v %s", err, out)
  176. }
  177. exists, err := existsMountpointWithPrefix(mountpointPrefix)
  178. c.Assert(err, checker.IsNil)
  179. c.Assert(exists, checker.Equals, false)
  180. }()
  181. out, err = s.d.Cmd("volume", "create", "-d", pName, volName)
  182. if err != nil {
  183. c.Fatalf("Could not create volume: %v %s", err, out)
  184. }
  185. defer func() {
  186. if out, err := s.d.Cmd("volume", "remove", volName); err != nil {
  187. c.Fatalf("Could not remove volume: %v %s", err, out)
  188. }
  189. }()
  190. out, err = s.d.Cmd("volume", "ls")
  191. if err != nil {
  192. c.Fatalf("Could not list volume: %v %s", err, out)
  193. }
  194. c.Assert(out, checker.Contains, volName)
  195. c.Assert(out, checker.Contains, pName)
  196. mountPoint, err := s.d.Cmd("volume", "inspect", volName, "--format", "{{.Mountpoint}}")
  197. if err != nil {
  198. c.Fatalf("Could not inspect volume: %v %s", err, mountPoint)
  199. }
  200. mountPoint = strings.TrimSpace(mountPoint)
  201. out, err = s.d.Cmd("run", "--rm", "-v", volName+":"+destDir, "busybox", "touch", destDir+destFile)
  202. c.Assert(err, checker.IsNil, check.Commentf(out))
  203. path := filepath.Join(s.d.RootDir(), "plugins", pluginID, "rootfs", mountPoint, destFile)
  204. _, err = os.Lstat(path)
  205. c.Assert(err, checker.IsNil)
  206. exists, err := existsMountpointWithPrefix(mountpointPrefix)
  207. c.Assert(err, checker.IsNil)
  208. c.Assert(exists, checker.Equals, true)
  209. }
  210. func (s *DockerDaemonSuite) TestGraphdriverPlugin(c *check.C) {
  211. testRequires(c, Network, IsAmd64, DaemonIsLinux, overlay2Supported, ExperimentalDaemon)
  212. s.d.Start(c)
  213. // install the plugin
  214. plugin := "cpuguy83/docker-overlay2-graphdriver-plugin"
  215. out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", plugin)
  216. c.Assert(err, checker.IsNil, check.Commentf(out))
  217. // restart the daemon with the plugin set as the storage driver
  218. s.d.Restart(c, "-s", plugin, "--storage-opt", "overlay2.override_kernel_check=1")
  219. // run a container
  220. out, err = s.d.Cmd("run", "--rm", "busybox", "true") // this will pull busybox using the plugin
  221. c.Assert(err, checker.IsNil, check.Commentf(out))
  222. }
  223. func (s *DockerDaemonSuite) TestPluginVolumeRemoveOnRestart(c *check.C) {
  224. testRequires(c, DaemonIsLinux, Network, IsAmd64)
  225. s.d.Start(c, "--live-restore=true")
  226. out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName)
  227. c.Assert(err, checker.IsNil, check.Commentf(out))
  228. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  229. out, err = s.d.Cmd("volume", "create", "--driver", pName, "test")
  230. c.Assert(err, checker.IsNil, check.Commentf(out))
  231. s.d.Restart(c, "--live-restore=true")
  232. out, err = s.d.Cmd("plugin", "disable", pName)
  233. c.Assert(err, checker.NotNil, check.Commentf(out))
  234. c.Assert(out, checker.Contains, "in use")
  235. out, err = s.d.Cmd("volume", "rm", "test")
  236. c.Assert(err, checker.IsNil, check.Commentf(out))
  237. out, err = s.d.Cmd("plugin", "disable", pName)
  238. c.Assert(err, checker.IsNil, check.Commentf(out))
  239. out, err = s.d.Cmd("plugin", "rm", pName)
  240. c.Assert(err, checker.IsNil, check.Commentf(out))
  241. }
  242. func existsMountpointWithPrefix(mountpointPrefix string) (bool, error) {
  243. mounts, err := mount.GetMounts()
  244. if err != nil {
  245. return false, err
  246. }
  247. for _, mnt := range mounts {
  248. if strings.HasPrefix(mnt.Mountpoint, mountpointPrefix) {
  249. return true, nil
  250. }
  251. }
  252. return false, nil
  253. }
  254. func (s *DockerDaemonSuite) TestPluginListFilterEnabled(c *check.C) {
  255. testRequires(c, IsAmd64, Network)
  256. s.d.Start(c)
  257. out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pNameWithTag, "--disable")
  258. c.Assert(err, check.IsNil, check.Commentf(out))
  259. defer func() {
  260. if out, err := s.d.Cmd("plugin", "remove", pNameWithTag); err != nil {
  261. c.Fatalf("Could not remove plugin: %v %s", err, out)
  262. }
  263. }()
  264. out, err = s.d.Cmd("plugin", "ls", "--filter", "enabled=true")
  265. c.Assert(err, checker.IsNil)
  266. c.Assert(out, checker.Not(checker.Contains), pName)
  267. out, err = s.d.Cmd("plugin", "ls", "--filter", "enabled=false")
  268. c.Assert(err, checker.IsNil)
  269. c.Assert(out, checker.Contains, pName)
  270. c.Assert(out, checker.Contains, "false")
  271. out, err = s.d.Cmd("plugin", "ls")
  272. c.Assert(err, checker.IsNil)
  273. c.Assert(out, checker.Contains, pName)
  274. }
  275. func (s *DockerDaemonSuite) TestPluginListFilterCapability(c *check.C) {
  276. testRequires(c, IsAmd64, Network)
  277. s.d.Start(c)
  278. out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pNameWithTag, "--disable")
  279. c.Assert(err, check.IsNil, check.Commentf(out))
  280. defer func() {
  281. if out, err := s.d.Cmd("plugin", "remove", pNameWithTag); err != nil {
  282. c.Fatalf("Could not remove plugin: %v %s", err, out)
  283. }
  284. }()
  285. out, err = s.d.Cmd("plugin", "ls", "--filter", "capability=volumedriver")
  286. c.Assert(err, checker.IsNil)
  287. c.Assert(out, checker.Contains, pName)
  288. out, err = s.d.Cmd("plugin", "ls", "--filter", "capability=authz")
  289. c.Assert(err, checker.IsNil)
  290. c.Assert(out, checker.Not(checker.Contains), pName)
  291. out, err = s.d.Cmd("plugin", "ls")
  292. c.Assert(err, checker.IsNil)
  293. c.Assert(out, checker.Contains, pName)
  294. }