docker_cli_daemon_experimental_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. if err := s.d.Start(); err != nil {
  16. c.Fatalf("Could not start daemon: %v", err)
  17. }
  18. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pluginName); 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", pluginName); err != nil {
  23. c.Fatalf("Could not disable plugin: %v %s", err, out)
  24. }
  25. if out, err := s.d.Cmd("plugin", "remove", pluginName); err != nil {
  26. c.Fatalf("Could not remove plugin: %v %s", err, out)
  27. }
  28. }()
  29. if err := s.d.Restart(); err != nil {
  30. c.Fatalf("Could not restart daemon: %v", err)
  31. }
  32. out, err := s.d.Cmd("plugin", "ls")
  33. if err != nil {
  34. c.Fatalf("Could not list plugins: %v %s", err, out)
  35. }
  36. c.Assert(out, checker.Contains, pluginName)
  37. c.Assert(out, checker.Contains, "true")
  38. }
  39. // TestDaemonRestartWithPluginEnabled tests state restore for a disabled plugin
  40. func (s *DockerDaemonSuite) TestDaemonRestartWithPluginDisabled(c *check.C) {
  41. if err := s.d.Start(); err != nil {
  42. c.Fatalf("Could not start daemon: %v", err)
  43. }
  44. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pluginName, "--disable"); err != nil {
  45. c.Fatalf("Could not install plugin: %v %s", err, out)
  46. }
  47. defer func() {
  48. if out, err := s.d.Cmd("plugin", "remove", pluginName); err != nil {
  49. c.Fatalf("Could not remove plugin: %v %s", err, out)
  50. }
  51. }()
  52. if err := s.d.Restart(); err != nil {
  53. c.Fatalf("Could not restart daemon: %v", err)
  54. }
  55. out, err := s.d.Cmd("plugin", "ls")
  56. if err != nil {
  57. c.Fatalf("Could not list plugins: %v %s", err, out)
  58. }
  59. c.Assert(out, checker.Contains, pluginName)
  60. c.Assert(out, checker.Contains, "false")
  61. }
  62. // TestDaemonKillLiveRestoreWithPlugins SIGKILLs daemon started with --live-restore.
  63. // Plugins should continue to run.
  64. func (s *DockerDaemonSuite) TestDaemonKillLiveRestoreWithPlugins(c *check.C) {
  65. if err := s.d.Start("--live-restore"); err != nil {
  66. c.Fatalf("Could not start daemon: %v", err)
  67. }
  68. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pluginName); err != nil {
  69. c.Fatalf("Could not install plugin: %v %s", err, out)
  70. }
  71. defer func() {
  72. if err := s.d.Restart("--live-restore"); err != nil {
  73. c.Fatalf("Could not restart daemon: %v", err)
  74. }
  75. if out, err := s.d.Cmd("plugin", "disable", pluginName); err != nil {
  76. c.Fatalf("Could not disable plugin: %v %s", err, out)
  77. }
  78. if out, err := s.d.Cmd("plugin", "remove", pluginName); err != nil {
  79. c.Fatalf("Could not remove plugin: %v %s", err, out)
  80. }
  81. }()
  82. if err := s.d.Kill(); err != nil {
  83. c.Fatalf("Could not kill daemon: %v", err)
  84. }
  85. cmd := exec.Command("pgrep", "-f", "plugin-no-remove")
  86. if out, ec, err := runCommandWithOutput(cmd); ec != 0 {
  87. c.Fatalf("Expected exit code '0', got %d err: %v output: %s ", ec, err, out)
  88. }
  89. }
  90. // TestDaemonShutdownLiveRestoreWithPlugins SIGTERMs daemon started with --live-restore.
  91. // Plugins should continue to run.
  92. func (s *DockerDaemonSuite) TestDaemonShutdownLiveRestoreWithPlugins(c *check.C) {
  93. if err := s.d.Start("--live-restore"); err != nil {
  94. c.Fatalf("Could not start daemon: %v", err)
  95. }
  96. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pluginName); err != nil {
  97. c.Fatalf("Could not install plugin: %v %s", err, out)
  98. }
  99. defer func() {
  100. if err := s.d.Restart("--live-restore"); err != nil {
  101. c.Fatalf("Could not restart daemon: %v", err)
  102. }
  103. if out, err := s.d.Cmd("plugin", "disable", pluginName); err != nil {
  104. c.Fatalf("Could not disable plugin: %v %s", err, out)
  105. }
  106. if out, err := s.d.Cmd("plugin", "remove", pluginName); err != nil {
  107. c.Fatalf("Could not remove plugin: %v %s", err, out)
  108. }
  109. }()
  110. if err := s.d.cmd.Process.Signal(os.Interrupt); err != nil {
  111. c.Fatalf("Could not kill daemon: %v", err)
  112. }
  113. cmd := exec.Command("pgrep", "-f", "plugin-no-remove")
  114. if out, ec, err := runCommandWithOutput(cmd); ec != 0 {
  115. c.Fatalf("Expected exit code '0', got %d err: %v output: %s ", ec, err, out)
  116. }
  117. }
  118. // TestDaemonShutdownWithPlugins shuts down running plugins.
  119. func (s *DockerDaemonSuite) TestDaemonShutdownWithPlugins(c *check.C) {
  120. if err := s.d.Start(); err != nil {
  121. c.Fatalf("Could not start daemon: %v", err)
  122. }
  123. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pluginName); err != nil {
  124. c.Fatalf("Could not install plugin: %v %s", err, out)
  125. }
  126. defer func() {
  127. if err := s.d.Restart(); err != nil {
  128. c.Fatalf("Could not restart daemon: %v", err)
  129. }
  130. if out, err := s.d.Cmd("plugin", "disable", pluginName); err != nil {
  131. c.Fatalf("Could not disable plugin: %v %s", err, out)
  132. }
  133. if out, err := s.d.Cmd("plugin", "remove", pluginName); err != nil {
  134. c.Fatalf("Could not remove plugin: %v %s", err, out)
  135. }
  136. }()
  137. if err := s.d.cmd.Process.Signal(os.Interrupt); err != nil {
  138. c.Fatalf("Could not kill daemon: %v", err)
  139. }
  140. for {
  141. if err := syscall.Kill(s.d.cmd.Process.Pid, 0); err == syscall.ESRCH {
  142. break
  143. }
  144. }
  145. cmd := exec.Command("pgrep", "-f", "plugin-no-remove")
  146. if out, ec, err := runCommandWithOutput(cmd); ec != 1 {
  147. c.Fatalf("Expected exit code '1', got %d err: %v output: %s ", ec, err, out)
  148. }
  149. }
  150. // TestVolumePlugin tests volume creation using a plugin.
  151. func (s *DockerDaemonSuite) TestVolumePlugin(c *check.C) {
  152. volName := "plugin-volume"
  153. volRoot := "/data"
  154. destDir := "/tmp/data/"
  155. destFile := "foo"
  156. if err := s.d.Start(); err != nil {
  157. c.Fatalf("Could not start daemon: %v", err)
  158. }
  159. out, err := s.d.Cmd("plugin", "install", pluginName, "--grant-all-permissions")
  160. if err != nil {
  161. c.Fatalf("Could not install plugin: %v %s", err, out)
  162. }
  163. defer func() {
  164. if out, err := s.d.Cmd("plugin", "disable", pluginName); err != nil {
  165. c.Fatalf("Could not disable plugin: %v %s", err, out)
  166. }
  167. if out, err := s.d.Cmd("plugin", "remove", pluginName); err != nil {
  168. c.Fatalf("Could not remove plugin: %v %s", err, out)
  169. }
  170. }()
  171. out, err = s.d.Cmd("volume", "create", "-d", pluginName, "--name", volName)
  172. if err != nil {
  173. c.Fatalf("Could not create volume: %v %s", err, out)
  174. }
  175. defer func() {
  176. if out, err := s.d.Cmd("volume", "remove", volName); err != nil {
  177. c.Fatalf("Could not remove volume: %v %s", err, out)
  178. }
  179. }()
  180. mountPoint, err := s.d.Cmd("volume", "inspect", volName, "--format", "{{.Mountpoint}}")
  181. if err != nil {
  182. c.Fatalf("Could not inspect volume: %v %s", err, mountPoint)
  183. }
  184. mountPoint = strings.TrimSpace(mountPoint)
  185. out, err = s.d.Cmd("run", "--rm", "-v", volName+":"+destDir, "busybox", "touch", destDir+destFile)
  186. c.Assert(err, checker.IsNil, check.Commentf(out))
  187. path := filepath.Join(mountPoint, destFile)
  188. _, err = os.Lstat(path)
  189. c.Assert(err, checker.IsNil)
  190. // tiborvass/no-remove is a volume plugin that persists data on disk at /data,
  191. // even after the volume is removed. So perform an explicit filesystem cleanup.
  192. os.RemoveAll(volRoot)
  193. }