docker_cli_daemon_plugins_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //go:build linux
  2. package main
  3. import (
  4. "strings"
  5. "testing"
  6. "golang.org/x/sys/unix"
  7. "gotest.tools/v3/assert"
  8. "gotest.tools/v3/icmd"
  9. )
  10. // TestDaemonRestartWithPluginEnabled tests state restore for an enabled plugin
  11. func (s *DockerDaemonSuite) TestDaemonRestartWithPluginEnabled(c *testing.T) {
  12. testRequires(c, IsAmd64, Network)
  13. s.d.Start(c)
  14. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
  15. c.Fatalf("Could not install plugin: %v %s", err, out)
  16. }
  17. defer func() {
  18. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  19. c.Fatalf("Could not disable plugin: %v %s", err, out)
  20. }
  21. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  22. c.Fatalf("Could not remove plugin: %v %s", err, out)
  23. }
  24. }()
  25. s.d.Restart(c)
  26. out, err := s.d.Cmd("plugin", "ls")
  27. if err != nil {
  28. c.Fatalf("Could not list plugins: %v %s", err, out)
  29. }
  30. assert.Assert(c, strings.Contains(out, pName))
  31. assert.Assert(c, strings.Contains(out, "true"))
  32. }
  33. // TestDaemonRestartWithPluginDisabled tests state restore for a disabled plugin
  34. func (s *DockerDaemonSuite) TestDaemonRestartWithPluginDisabled(c *testing.T) {
  35. testRequires(c, IsAmd64, Network)
  36. s.d.Start(c)
  37. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName, "--disable"); err != nil {
  38. c.Fatalf("Could not install plugin: %v %s", err, out)
  39. }
  40. defer func() {
  41. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  42. c.Fatalf("Could not remove plugin: %v %s", err, out)
  43. }
  44. }()
  45. s.d.Restart(c)
  46. out, err := s.d.Cmd("plugin", "ls")
  47. if err != nil {
  48. c.Fatalf("Could not list plugins: %v %s", err, out)
  49. }
  50. assert.Assert(c, strings.Contains(out, pName))
  51. assert.Assert(c, strings.Contains(out, "false"))
  52. }
  53. // TestDaemonKillLiveRestoreWithPlugins SIGKILLs daemon started with --live-restore.
  54. // Plugins should continue to run.
  55. func (s *DockerDaemonSuite) TestDaemonKillLiveRestoreWithPlugins(c *testing.T) {
  56. testRequires(c, IsAmd64, Network)
  57. s.d.Start(c, "--live-restore")
  58. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
  59. c.Fatalf("Could not install plugin: %v %s", err, out)
  60. }
  61. defer func() {
  62. s.d.Restart(c, "--live-restore")
  63. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  64. c.Fatalf("Could not disable plugin: %v %s", err, out)
  65. }
  66. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  67. c.Fatalf("Could not remove plugin: %v %s", err, out)
  68. }
  69. }()
  70. if err := s.d.Kill(); err != nil {
  71. c.Fatalf("Could not kill daemon: %v", err)
  72. }
  73. icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Success)
  74. }
  75. // TestDaemonShutdownLiveRestoreWithPlugins SIGTERMs daemon started with --live-restore.
  76. // Plugins should continue to run.
  77. func (s *DockerDaemonSuite) TestDaemonShutdownLiveRestoreWithPlugins(c *testing.T) {
  78. testRequires(c, IsAmd64, Network)
  79. s.d.Start(c, "--live-restore")
  80. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
  81. c.Fatalf("Could not install plugin: %v %s", err, out)
  82. }
  83. defer func() {
  84. s.d.Restart(c, "--live-restore")
  85. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  86. c.Fatalf("Could not disable plugin: %v %s", err, out)
  87. }
  88. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  89. c.Fatalf("Could not remove plugin: %v %s", err, out)
  90. }
  91. }()
  92. if err := s.d.Interrupt(); err != nil {
  93. c.Fatalf("Could not kill daemon: %v", err)
  94. }
  95. icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Success)
  96. }
  97. // TestDaemonShutdownWithPlugins shuts down running plugins.
  98. func (s *DockerDaemonSuite) TestDaemonShutdownWithPlugins(c *testing.T) {
  99. testRequires(c, IsAmd64, Network)
  100. s.d.Start(c)
  101. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
  102. c.Fatalf("Could not install plugin: %v %s", err, out)
  103. }
  104. defer func() {
  105. s.d.Restart(c)
  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.Interrupt(); err != nil {
  114. c.Fatalf("Could not kill daemon: %v", err)
  115. }
  116. for {
  117. if err := unix.Kill(s.d.Pid(), 0); err == unix.ESRCH {
  118. break
  119. }
  120. }
  121. icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Expected{
  122. ExitCode: 1,
  123. Error: "exit status 1",
  124. })
  125. s.d.Start(c)
  126. icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Success)
  127. }
  128. // TestDaemonKillWithPlugins leaves plugins running.
  129. func (s *DockerDaemonSuite) TestDaemonKillWithPlugins(c *testing.T) {
  130. testRequires(c, IsAmd64, Network)
  131. s.d.Start(c)
  132. if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
  133. c.Fatalf("Could not install plugin: %v %s", err, out)
  134. }
  135. defer func() {
  136. s.d.Restart(c)
  137. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  138. c.Fatalf("Could not disable plugin: %v %s", err, out)
  139. }
  140. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  141. c.Fatalf("Could not remove plugin: %v %s", err, out)
  142. }
  143. }()
  144. if err := s.d.Kill(); err != nil {
  145. c.Fatalf("Could not kill daemon: %v", err)
  146. }
  147. // assert that plugins are running.
  148. icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Success)
  149. }
  150. // TestVolumePlugin tests volume creation using a plugin.
  151. func (s *DockerDaemonSuite) TestVolumePlugin(c *testing.T) {
  152. testRequires(c, IsAmd64, Network)
  153. volName := "plugin-volume"
  154. destDir := "/tmp/data/"
  155. destFile := "foo"
  156. s.d.Start(c)
  157. out, err := s.d.Cmd("plugin", "install", pName, "--grant-all-permissions")
  158. if err != nil {
  159. c.Fatalf("Could not install plugin: %v %s", err, out)
  160. }
  161. defer func() {
  162. if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
  163. c.Fatalf("Could not disable plugin: %v %s", err, out)
  164. }
  165. if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
  166. c.Fatalf("Could not remove plugin: %v %s", err, out)
  167. }
  168. }()
  169. out, err = s.d.Cmd("volume", "create", "-d", pName, volName)
  170. if err != nil {
  171. c.Fatalf("Could not create volume: %v %s", err, out)
  172. }
  173. defer func() {
  174. if out, err := s.d.Cmd("volume", "remove", volName); err != nil {
  175. c.Fatalf("Could not remove volume: %v %s", err, out)
  176. }
  177. }()
  178. out, err = s.d.Cmd("volume", "ls")
  179. if err != nil {
  180. c.Fatalf("Could not list volume: %v %s", err, out)
  181. }
  182. assert.Assert(c, strings.Contains(out, volName))
  183. assert.Assert(c, strings.Contains(out, pName))
  184. out, err = s.d.Cmd("run", "--rm", "-v", volName+":"+destDir, "busybox", "touch", destDir+destFile)
  185. assert.NilError(c, err, out)
  186. out, err = s.d.Cmd("run", "--rm", "-v", volName+":"+destDir, "busybox", "ls", destDir+destFile)
  187. assert.NilError(c, err, out)
  188. }
  189. func (s *DockerDaemonSuite) TestPluginVolumeRemoveOnRestart(c *testing.T) {
  190. testRequires(c, IsAmd64, Network)
  191. s.d.Start(c, "--live-restore=true")
  192. out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName)
  193. assert.NilError(c, err, out)
  194. assert.Assert(c, strings.Contains(out, pName))
  195. out, err = s.d.Cmd("volume", "create", "--driver", pName, "test")
  196. assert.NilError(c, err, out)
  197. s.d.Restart(c, "--live-restore=true")
  198. out, err = s.d.Cmd("plugin", "disable", pName)
  199. assert.ErrorContains(c, err, "", out)
  200. assert.Assert(c, strings.Contains(out, "in use"))
  201. out, err = s.d.Cmd("volume", "rm", "test")
  202. assert.NilError(c, err, out)
  203. out, err = s.d.Cmd("plugin", "disable", pName)
  204. assert.NilError(c, err, out)
  205. out, err = s.d.Cmd("plugin", "rm", pName)
  206. assert.NilError(c, err, out)
  207. }
  208. func (s *DockerDaemonSuite) TestPluginListFilterEnabled(c *testing.T) {
  209. testRequires(c, IsAmd64, Network)
  210. s.d.Start(c)
  211. out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pNameWithTag, "--disable")
  212. assert.NilError(c, err, out)
  213. defer func() {
  214. if out, err := s.d.Cmd("plugin", "remove", pNameWithTag); err != nil {
  215. c.Fatalf("Could not remove plugin: %v %s", err, out)
  216. }
  217. }()
  218. out, err = s.d.Cmd("plugin", "ls", "--filter", "enabled=true")
  219. assert.NilError(c, err, out)
  220. assert.Assert(c, !strings.Contains(out, pName))
  221. out, err = s.d.Cmd("plugin", "ls", "--filter", "enabled=false")
  222. assert.NilError(c, err, out)
  223. assert.Assert(c, strings.Contains(out, pName))
  224. assert.Assert(c, strings.Contains(out, "false"))
  225. out, err = s.d.Cmd("plugin", "ls")
  226. assert.NilError(c, err, out)
  227. assert.Assert(c, strings.Contains(out, pName))
  228. }
  229. func (s *DockerDaemonSuite) TestPluginListFilterCapability(c *testing.T) {
  230. testRequires(c, IsAmd64, Network)
  231. s.d.Start(c)
  232. out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pNameWithTag, "--disable")
  233. assert.NilError(c, err, out)
  234. defer func() {
  235. if out, err := s.d.Cmd("plugin", "remove", pNameWithTag); err != nil {
  236. c.Fatalf("Could not remove plugin: %v %s", err, out)
  237. }
  238. }()
  239. out, err = s.d.Cmd("plugin", "ls", "--filter", "capability=volumedriver")
  240. assert.NilError(c, err, out)
  241. assert.Assert(c, strings.Contains(out, pName))
  242. out, err = s.d.Cmd("plugin", "ls", "--filter", "capability=authz")
  243. assert.NilError(c, err, out)
  244. assert.Assert(c, !strings.Contains(out, pName))
  245. out, err = s.d.Cmd("plugin", "ls")
  246. assert.NilError(c, err, out)
  247. assert.Assert(c, strings.Contains(out, pName))
  248. }