docker_cli_daemon_plugins_test.go 9.1 KB

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