docker_cli_daemon_plugins_test.go 8.6 KB

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