docker_cli_daemon_plugins_test.go 9.5 KB

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