docker_cli_daemon_experimental_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // TestDaemonShutdownLiveRestoreWithPlugins leaves plugin running.
  63. func (s *DockerDaemonSuite) TestDaemonShutdownLiveRestoreWithPlugins(c *check.C) {
  64. if err := s.d.Start("--live-restore"); err != nil {
  65. c.Fatalf("Could not start daemon: %v", err)
  66. }
  67. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pluginName); err != nil {
  68. c.Fatalf("Could not install plugin: %v %s", err, out)
  69. }
  70. defer func() {
  71. if err := s.d.Restart("--live-restore"); err != nil {
  72. c.Fatalf("Could not restart daemon: %v", err)
  73. }
  74. if out, err := s.d.Cmd("plugin", "disable", pluginName); err != nil {
  75. c.Fatalf("Could not disable plugin: %v %s", err, out)
  76. }
  77. if out, err := s.d.Cmd("plugin", "remove", pluginName); err != nil {
  78. c.Fatalf("Could not remove plugin: %v %s", err, out)
  79. }
  80. }()
  81. if err := s.d.Kill(); err != nil {
  82. c.Fatalf("Could not kill daemon: %v", err)
  83. }
  84. cmd := exec.Command("pgrep", "-f", "plugin-no-remove")
  85. if out, ec, err := runCommandWithOutput(cmd); ec != 0 {
  86. c.Fatalf("Expected exit code '0', got %d err: %v output: %s ", ec, err, out)
  87. }
  88. }
  89. // TestDaemonShutdownWithPlugins shuts down running plugins.
  90. func (s *DockerDaemonSuite) TestDaemonShutdownWithPlugins(c *check.C) {
  91. if err := s.d.Start(); err != nil {
  92. c.Fatalf("Could not start daemon: %v", err)
  93. }
  94. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pluginName); err != nil {
  95. c.Fatalf("Could not install plugin: %v %s", err, out)
  96. }
  97. defer func() {
  98. if err := s.d.Restart(); err != nil {
  99. c.Fatalf("Could not restart daemon: %v", err)
  100. }
  101. if out, err := s.d.Cmd("plugin", "disable", pluginName); err != nil {
  102. c.Fatalf("Could not disable plugin: %v %s", err, out)
  103. }
  104. if out, err := s.d.Cmd("plugin", "remove", pluginName); err != nil {
  105. c.Fatalf("Could not remove plugin: %v %s", err, out)
  106. }
  107. }()
  108. if err := s.d.cmd.Process.Signal(os.Interrupt); err != nil {
  109. c.Fatalf("Could not kill daemon: %v", err)
  110. }
  111. for {
  112. if err := syscall.Kill(s.d.cmd.Process.Pid, 0); err == syscall.ESRCH {
  113. break
  114. }
  115. }
  116. cmd := exec.Command("pgrep", "-f", "plugin-no-remove")
  117. if out, ec, err := runCommandWithOutput(cmd); ec != 1 {
  118. c.Fatalf("Expected exit code '1', got %d err: %v output: %s ", ec, err, out)
  119. }
  120. }
  121. // TestVolumePlugin tests volume creation using a plugin.
  122. func (s *DockerDaemonSuite) TestVolumePlugin(c *check.C) {
  123. volName := "plugin-volume"
  124. volRoot := "/data"
  125. destDir := "/tmp/data/"
  126. destFile := "foo"
  127. if err := s.d.Start(); err != nil {
  128. c.Fatalf("Could not start daemon: %v", err)
  129. }
  130. out, err := s.d.Cmd("plugin", "install", pluginName, "--grant-all-permissions")
  131. if err != nil {
  132. c.Fatalf("Could not install plugin: %v %s", err, out)
  133. }
  134. defer func() {
  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. out, err = s.d.Cmd("volume", "create", "-d", pluginName, "--name", volName)
  143. if err != nil {
  144. c.Fatalf("Could not create volume: %v %s", err, out)
  145. }
  146. defer func() {
  147. if out, err := s.d.Cmd("volume", "remove", volName); err != nil {
  148. c.Fatalf("Could not remove volume: %v %s", err, out)
  149. }
  150. }()
  151. mountPoint, err := s.d.Cmd("volume", "inspect", volName, "--format", "{{.Mountpoint}}")
  152. if err != nil {
  153. c.Fatalf("Could not inspect volume: %v %s", err, mountPoint)
  154. }
  155. mountPoint = strings.TrimSpace(mountPoint)
  156. out, err = s.d.Cmd("run", "--rm", "-v", volName+":"+destDir, "busybox", "touch", destDir+destFile)
  157. c.Assert(err, checker.IsNil, check.Commentf(out))
  158. path := filepath.Join(mountPoint, destFile)
  159. _, err = os.Lstat(path)
  160. c.Assert(err, checker.IsNil)
  161. // tiborvass/no-remove is a volume plugin that persists data on disk at /data,
  162. // even after the volume is removed. So perform an explicit filesystem cleanup.
  163. os.RemoveAll(volRoot)
  164. }