docker_cli_daemon_plugins_test.go 7.3 KB

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