docker_cli_daemon_experimental_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // +build linux, experimental
  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/go-check/check"
  11. )
  12. var pluginName = "tiborvass/no-remove"
  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", pluginName); 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", pluginName); err != nil {
  24. c.Fatalf("Could not disable plugin: %v %s", err, out)
  25. }
  26. if out, err := s.d.Cmd("plugin", "remove", pluginName); 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, pluginName)
  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", pluginName, "--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", pluginName); 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, pluginName)
  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)
  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", pluginName); 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", pluginName); err != nil {
  79. c.Fatalf("Could not disable plugin: %v %s", err, out)
  80. }
  81. if out, err := s.d.Cmd("plugin", "remove", pluginName); 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", "plugin-no-remove")
  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)
  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", pluginName); 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", pluginName); err != nil {
  108. c.Fatalf("Could not disable plugin: %v %s", err, out)
  109. }
  110. if out, err := s.d.Cmd("plugin", "remove", pluginName); 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", "plugin-no-remove")
  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", pluginName); 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", pluginName); err != nil {
  136. c.Fatalf("Could not disable plugin: %v %s", err, out)
  137. }
  138. if out, err := s.d.Cmd("plugin", "remove", pluginName); 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", "plugin-no-remove")
  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)
  158. volName := "plugin-volume"
  159. volRoot := "/data"
  160. destDir := "/tmp/data/"
  161. destFile := "foo"
  162. if err := s.d.Start(); err != nil {
  163. c.Fatalf("Could not start daemon: %v", err)
  164. }
  165. out, err := s.d.Cmd("plugin", "install", pluginName, "--grant-all-permissions")
  166. if err != nil {
  167. c.Fatalf("Could not install plugin: %v %s", err, out)
  168. }
  169. defer func() {
  170. if out, err := s.d.Cmd("plugin", "disable", pluginName); err != nil {
  171. c.Fatalf("Could not disable plugin: %v %s", err, out)
  172. }
  173. if out, err := s.d.Cmd("plugin", "remove", pluginName); err != nil {
  174. c.Fatalf("Could not remove plugin: %v %s", err, out)
  175. }
  176. }()
  177. out, err = s.d.Cmd("volume", "create", "-d", pluginName, volName)
  178. if err != nil {
  179. c.Fatalf("Could not create volume: %v %s", err, out)
  180. }
  181. defer func() {
  182. if out, err := s.d.Cmd("volume", "remove", volName); err != nil {
  183. c.Fatalf("Could not remove volume: %v %s", err, out)
  184. }
  185. }()
  186. out, err = s.d.Cmd("volume", "ls")
  187. if err != nil {
  188. c.Fatalf("Could not list volume: %v %s", err, out)
  189. }
  190. c.Assert(out, checker.Contains, volName)
  191. c.Assert(out, checker.Contains, pluginName)
  192. mountPoint, err := s.d.Cmd("volume", "inspect", volName, "--format", "{{.Mountpoint}}")
  193. if err != nil {
  194. c.Fatalf("Could not inspect volume: %v %s", err, mountPoint)
  195. }
  196. mountPoint = strings.TrimSpace(mountPoint)
  197. out, err = s.d.Cmd("run", "--rm", "-v", volName+":"+destDir, "busybox", "touch", destDir+destFile)
  198. c.Assert(err, checker.IsNil, check.Commentf(out))
  199. path := filepath.Join(mountPoint, destFile)
  200. _, err = os.Lstat(path)
  201. c.Assert(err, checker.IsNil)
  202. // tiborvass/no-remove is a volume plugin that persists data on disk at /data,
  203. // even after the volume is removed. So perform an explicit filesystem cleanup.
  204. os.RemoveAll(volRoot)
  205. }