docker_cli_daemon_plugins_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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, 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, 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, Network, IsAmd64)
  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, Network, IsAmd64)
  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.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, Network)
  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.Interrupt(); err != nil {
  143. c.Fatalf("Could not kill daemon: %v", err)
  144. }
  145. for {
  146. if err := syscall.Kill(s.d.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. }
  155. // TestVolumePlugin tests volume creation using a plugin.
  156. func (s *DockerDaemonSuite) TestVolumePlugin(c *check.C) {
  157. testRequires(c, Network, IsAmd64)
  158. volName := "plugin-volume"
  159. destDir := "/tmp/data/"
  160. destFile := "foo"
  161. if err := s.d.Start(); err != nil {
  162. c.Fatalf("Could not start daemon: %v", err)
  163. }
  164. out, err := s.d.Cmd("plugin", "install", pName, "--grant-all-permissions")
  165. if err != nil {
  166. c.Fatalf("Could not install plugin: %v %s", err, out)
  167. }
  168. pluginID, err := s.d.Cmd("plugin", "inspect", "-f", "{{.Id}}", pName)
  169. pluginID = strings.TrimSpace(pluginID)
  170. if err != nil {
  171. c.Fatalf("Could not retrieve plugin ID: %v %s", err, pluginID)
  172. }
  173. mountpointPrefix := filepath.Join(s.d.RootDir(), "plugins", pluginID, "rootfs")
  174. defer func() {
  175. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  176. c.Fatalf("Could not disable plugin: %v %s", err, out)
  177. }
  178. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  179. c.Fatalf("Could not remove plugin: %v %s", err, out)
  180. }
  181. exists, err := existsMountpointWithPrefix(mountpointPrefix)
  182. c.Assert(err, checker.IsNil)
  183. c.Assert(exists, checker.Equals, false)
  184. }()
  185. out, err = s.d.Cmd("volume", "create", "-d", pName, volName)
  186. if err != nil {
  187. c.Fatalf("Could not create volume: %v %s", err, out)
  188. }
  189. defer func() {
  190. if out, err := s.d.Cmd("volume", "remove", volName); err != nil {
  191. c.Fatalf("Could not remove volume: %v %s", err, out)
  192. }
  193. }()
  194. out, err = s.d.Cmd("volume", "ls")
  195. if err != nil {
  196. c.Fatalf("Could not list volume: %v %s", err, out)
  197. }
  198. c.Assert(out, checker.Contains, volName)
  199. c.Assert(out, checker.Contains, pName)
  200. mountPoint, err := s.d.Cmd("volume", "inspect", volName, "--format", "{{.Mountpoint}}")
  201. if err != nil {
  202. c.Fatalf("Could not inspect volume: %v %s", err, mountPoint)
  203. }
  204. mountPoint = strings.TrimSpace(mountPoint)
  205. out, err = s.d.Cmd("run", "--rm", "-v", volName+":"+destDir, "busybox", "touch", destDir+destFile)
  206. c.Assert(err, checker.IsNil, check.Commentf(out))
  207. path := filepath.Join(s.d.RootDir(), "plugins", pluginID, "rootfs", mountPoint, destFile)
  208. _, err = os.Lstat(path)
  209. c.Assert(err, checker.IsNil)
  210. exists, err := existsMountpointWithPrefix(mountpointPrefix)
  211. c.Assert(err, checker.IsNil)
  212. c.Assert(exists, checker.Equals, true)
  213. }
  214. func existsMountpointWithPrefix(mountpointPrefix string) (bool, error) {
  215. mounts, err := mount.GetMounts()
  216. if err != nil {
  217. return false, err
  218. }
  219. for _, mnt := range mounts {
  220. if strings.HasPrefix(mnt.Mountpoint, mountpointPrefix) {
  221. return true, nil
  222. }
  223. }
  224. return false, nil
  225. }