docker_cli_daemon_plugins_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // +build linux
  2. package main
  3. import (
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "syscall"
  8. "github.com/docker/docker/integration-cli/checker"
  9. "github.com/docker/docker/pkg/mount"
  10. icmd "github.com/docker/docker/pkg/testutil/cmd"
  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. 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 := syscall.Kill(s.d.Pid(), 0); err == syscall.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, "--live-restore")
  129. icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Success)
  130. }
  131. // TestVolumePlugin tests volume creation using a plugin.
  132. func (s *DockerDaemonSuite) TestVolumePlugin(c *check.C) {
  133. testRequires(c, IsAmd64, Network)
  134. volName := "plugin-volume"
  135. destDir := "/tmp/data/"
  136. destFile := "foo"
  137. s.d.Start(c)
  138. out, err := s.d.Cmd("plugin", "install", pName, "--grant-all-permissions")
  139. if err != nil {
  140. c.Fatalf("Could not install plugin: %v %s", err, out)
  141. }
  142. pluginID, err := s.d.Cmd("plugin", "inspect", "-f", "{{.Id}}", pName)
  143. pluginID = strings.TrimSpace(pluginID)
  144. if err != nil {
  145. c.Fatalf("Could not retrieve plugin ID: %v %s", err, pluginID)
  146. }
  147. mountpointPrefix := filepath.Join(s.d.RootDir(), "plugins", pluginID, "rootfs")
  148. defer func() {
  149. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  150. c.Fatalf("Could not disable plugin: %v %s", err, out)
  151. }
  152. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  153. c.Fatalf("Could not remove plugin: %v %s", err, out)
  154. }
  155. exists, err := existsMountpointWithPrefix(mountpointPrefix)
  156. c.Assert(err, checker.IsNil)
  157. c.Assert(exists, checker.Equals, false)
  158. }()
  159. out, err = s.d.Cmd("volume", "create", "-d", pName, volName)
  160. if err != nil {
  161. c.Fatalf("Could not create volume: %v %s", err, out)
  162. }
  163. defer func() {
  164. if out, err := s.d.Cmd("volume", "remove", volName); err != nil {
  165. c.Fatalf("Could not remove volume: %v %s", err, out)
  166. }
  167. }()
  168. out, err = s.d.Cmd("volume", "ls")
  169. if err != nil {
  170. c.Fatalf("Could not list volume: %v %s", err, out)
  171. }
  172. c.Assert(out, checker.Contains, volName)
  173. c.Assert(out, checker.Contains, pName)
  174. mountPoint, err := s.d.Cmd("volume", "inspect", volName, "--format", "{{.Mountpoint}}")
  175. if err != nil {
  176. c.Fatalf("Could not inspect volume: %v %s", err, mountPoint)
  177. }
  178. mountPoint = strings.TrimSpace(mountPoint)
  179. out, err = s.d.Cmd("run", "--rm", "-v", volName+":"+destDir, "busybox", "touch", destDir+destFile)
  180. c.Assert(err, checker.IsNil, check.Commentf(out))
  181. path := filepath.Join(s.d.RootDir(), "plugins", pluginID, "rootfs", mountPoint, destFile)
  182. _, err = os.Lstat(path)
  183. c.Assert(err, checker.IsNil)
  184. exists, err := existsMountpointWithPrefix(mountpointPrefix)
  185. c.Assert(err, checker.IsNil)
  186. c.Assert(exists, checker.Equals, true)
  187. }
  188. func (s *DockerDaemonSuite) TestGraphdriverPlugin(c *check.C) {
  189. testRequires(c, Network, IsAmd64, DaemonIsLinux, overlay2Supported, ExperimentalDaemon)
  190. s.d.Start(c)
  191. // install the plugin
  192. plugin := "cpuguy83/docker-overlay2-graphdriver-plugin"
  193. out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", plugin)
  194. c.Assert(err, checker.IsNil, check.Commentf(out))
  195. // restart the daemon with the plugin set as the storage driver
  196. s.d.Restart(c, "-s", plugin, "--storage-opt", "overlay2.override_kernel_check=1")
  197. // run a container
  198. out, err = s.d.Cmd("run", "--rm", "busybox", "true") // this will pull busybox using the plugin
  199. c.Assert(err, checker.IsNil, check.Commentf(out))
  200. }
  201. func (s *DockerDaemonSuite) TestPluginVolumeRemoveOnRestart(c *check.C) {
  202. testRequires(c, DaemonIsLinux, Network, IsAmd64)
  203. s.d.Start(c, "--live-restore=true")
  204. out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName)
  205. c.Assert(err, checker.IsNil, check.Commentf(out))
  206. c.Assert(strings.TrimSpace(out), checker.Contains, pName)
  207. out, err = s.d.Cmd("volume", "create", "--driver", pName, "test")
  208. c.Assert(err, checker.IsNil, check.Commentf(out))
  209. s.d.Restart(c, "--live-restore=true")
  210. out, err = s.d.Cmd("plugin", "disable", pName)
  211. c.Assert(err, checker.NotNil, check.Commentf(out))
  212. c.Assert(out, checker.Contains, "in use")
  213. out, err = s.d.Cmd("volume", "rm", "test")
  214. c.Assert(err, checker.IsNil, check.Commentf(out))
  215. out, err = s.d.Cmd("plugin", "disable", pName)
  216. c.Assert(err, checker.IsNil, check.Commentf(out))
  217. out, err = s.d.Cmd("plugin", "rm", pName)
  218. c.Assert(err, checker.IsNil, check.Commentf(out))
  219. }
  220. func existsMountpointWithPrefix(mountpointPrefix string) (bool, error) {
  221. mounts, err := mount.GetMounts()
  222. if err != nil {
  223. return false, err
  224. }
  225. for _, mnt := range mounts {
  226. if strings.HasPrefix(mnt.Mountpoint, mountpointPrefix) {
  227. return true, nil
  228. }
  229. }
  230. return false, nil
  231. }
  232. func (s *DockerDaemonSuite) TestPluginListFilterEnabled(c *check.C) {
  233. testRequires(c, Network)
  234. s.d.Start(c)
  235. out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pNameWithTag, "--disable")
  236. c.Assert(err, check.IsNil, check.Commentf(out))
  237. defer func() {
  238. if out, err := s.d.Cmd("plugin", "remove", pNameWithTag); err != nil {
  239. c.Fatalf("Could not remove plugin: %v %s", err, out)
  240. }
  241. }()
  242. out, err = s.d.Cmd("plugin", "ls", "--filter", "enabled=true")
  243. c.Assert(err, checker.IsNil)
  244. c.Assert(out, checker.Not(checker.Contains), pName)
  245. out, err = s.d.Cmd("plugin", "ls", "--filter", "enabled=false")
  246. c.Assert(err, checker.IsNil)
  247. c.Assert(out, checker.Contains, pName)
  248. c.Assert(out, checker.Contains, "false")
  249. out, err = s.d.Cmd("plugin", "ls")
  250. c.Assert(err, checker.IsNil)
  251. c.Assert(out, checker.Contains, pName)
  252. }