docker_cli_daemon_plugins_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. 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, 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, Network, IsAmd64)
  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. cmd := exec.Command("pgrep", "-f", pluginProcessName)
  77. if out, ec, err := runCommandWithOutput(cmd); ec != 0 {
  78. c.Fatalf("Expected exit code '0', got %d err: %v output: %s ", ec, err, out)
  79. }
  80. }
  81. // TestDaemonShutdownLiveRestoreWithPlugins SIGTERMs daemon started with --live-restore.
  82. // Plugins should continue to run.
  83. func (s *DockerDaemonSuite) TestDaemonShutdownLiveRestoreWithPlugins(c *check.C) {
  84. testRequires(c, Network, IsAmd64)
  85. s.d.Start(c, "--live-restore")
  86. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
  87. c.Fatalf("Could not install plugin: %v %s", err, out)
  88. }
  89. defer func() {
  90. s.d.Restart(c, "--live-restore")
  91. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  92. c.Fatalf("Could not disable plugin: %v %s", err, out)
  93. }
  94. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  95. c.Fatalf("Could not remove plugin: %v %s", err, out)
  96. }
  97. }()
  98. if err := s.d.Interrupt(); err != nil {
  99. c.Fatalf("Could not kill daemon: %v", err)
  100. }
  101. cmd := exec.Command("pgrep", "-f", pluginProcessName)
  102. if out, ec, err := runCommandWithOutput(cmd); ec != 0 {
  103. c.Fatalf("Expected exit code '0', got %d err: %v output: %s ", ec, err, out)
  104. }
  105. }
  106. // TestDaemonShutdownWithPlugins shuts down running plugins.
  107. func (s *DockerDaemonSuite) TestDaemonShutdownWithPlugins(c *check.C) {
  108. testRequires(c, Network)
  109. s.d.Start(c)
  110. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
  111. c.Fatalf("Could not install plugin: %v %s", err, out)
  112. }
  113. defer func() {
  114. s.d.Restart(c)
  115. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  116. c.Fatalf("Could not disable plugin: %v %s", err, out)
  117. }
  118. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  119. c.Fatalf("Could not remove plugin: %v %s", err, out)
  120. }
  121. }()
  122. if err := s.d.Interrupt(); err != nil {
  123. c.Fatalf("Could not kill daemon: %v", err)
  124. }
  125. for {
  126. if err := syscall.Kill(s.d.Pid(), 0); err == syscall.ESRCH {
  127. break
  128. }
  129. }
  130. cmd := exec.Command("pgrep", "-f", pluginProcessName)
  131. if out, ec, err := runCommandWithOutput(cmd); ec != 1 {
  132. c.Fatalf("Expected exit code '1', got %d err: %v output: %s ", ec, err, out)
  133. }
  134. }
  135. // TestVolumePlugin tests volume creation using a plugin.
  136. func (s *DockerDaemonSuite) TestVolumePlugin(c *check.C) {
  137. testRequires(c, Network, IsAmd64)
  138. volName := "plugin-volume"
  139. destDir := "/tmp/data/"
  140. destFile := "foo"
  141. s.d.Start(c)
  142. out, err := s.d.Cmd("plugin", "install", pName, "--grant-all-permissions")
  143. if err != nil {
  144. c.Fatalf("Could not install plugin: %v %s", err, out)
  145. }
  146. pluginID, err := s.d.Cmd("plugin", "inspect", "-f", "{{.Id}}", pName)
  147. pluginID = strings.TrimSpace(pluginID)
  148. if err != nil {
  149. c.Fatalf("Could not retrieve plugin ID: %v %s", err, pluginID)
  150. }
  151. mountpointPrefix := filepath.Join(s.d.RootDir(), "plugins", pluginID, "rootfs")
  152. defer func() {
  153. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  154. c.Fatalf("Could not disable plugin: %v %s", err, out)
  155. }
  156. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  157. c.Fatalf("Could not remove plugin: %v %s", err, out)
  158. }
  159. exists, err := existsMountpointWithPrefix(mountpointPrefix)
  160. c.Assert(err, checker.IsNil)
  161. c.Assert(exists, checker.Equals, false)
  162. }()
  163. out, err = s.d.Cmd("volume", "create", "-d", pName, volName)
  164. if err != nil {
  165. c.Fatalf("Could not create volume: %v %s", err, out)
  166. }
  167. defer func() {
  168. if out, err := s.d.Cmd("volume", "remove", volName); err != nil {
  169. c.Fatalf("Could not remove volume: %v %s", err, out)
  170. }
  171. }()
  172. out, err = s.d.Cmd("volume", "ls")
  173. if err != nil {
  174. c.Fatalf("Could not list volume: %v %s", err, out)
  175. }
  176. c.Assert(out, checker.Contains, volName)
  177. c.Assert(out, checker.Contains, pName)
  178. mountPoint, err := s.d.Cmd("volume", "inspect", volName, "--format", "{{.Mountpoint}}")
  179. if err != nil {
  180. c.Fatalf("Could not inspect volume: %v %s", err, mountPoint)
  181. }
  182. mountPoint = strings.TrimSpace(mountPoint)
  183. out, err = s.d.Cmd("run", "--rm", "-v", volName+":"+destDir, "busybox", "touch", destDir+destFile)
  184. c.Assert(err, checker.IsNil, check.Commentf(out))
  185. path := filepath.Join(s.d.RootDir(), "plugins", pluginID, "rootfs", mountPoint, destFile)
  186. _, err = os.Lstat(path)
  187. c.Assert(err, checker.IsNil)
  188. exists, err := existsMountpointWithPrefix(mountpointPrefix)
  189. c.Assert(err, checker.IsNil)
  190. c.Assert(exists, checker.Equals, true)
  191. }
  192. func existsMountpointWithPrefix(mountpointPrefix string) (bool, error) {
  193. mounts, err := mount.GetMounts()
  194. if err != nil {
  195. return false, err
  196. }
  197. for _, mnt := range mounts {
  198. if strings.HasPrefix(mnt.Mountpoint, mountpointPrefix) {
  199. return true, nil
  200. }
  201. }
  202. return false, nil
  203. }