manager_linux_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package plugin // import "github.com/docker/docker/plugin"
  2. import (
  3. "io"
  4. "net"
  5. "os"
  6. "path/filepath"
  7. "syscall"
  8. "testing"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/api/types/backend"
  11. "github.com/docker/docker/api/types/events"
  12. "github.com/docker/docker/pkg/containerfs"
  13. "github.com/docker/docker/pkg/stringid"
  14. v2 "github.com/docker/docker/plugin/v2"
  15. "github.com/moby/sys/mount"
  16. "github.com/moby/sys/mountinfo"
  17. specs "github.com/opencontainers/runtime-spec/specs-go"
  18. "github.com/pkg/errors"
  19. "gotest.tools/v3/skip"
  20. )
  21. func TestManagerWithPluginMounts(t *testing.T) {
  22. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  23. root, err := os.MkdirTemp("", "test-store-with-plugin-mounts")
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. defer containerfs.EnsureRemoveAll(root)
  28. s := NewStore()
  29. managerRoot := filepath.Join(root, "manager")
  30. p1 := newTestPlugin(t, "test1", "testcap", managerRoot)
  31. p2 := newTestPlugin(t, "test2", "testcap", managerRoot)
  32. p2.PluginObj.Enabled = true
  33. m, err := NewManager(
  34. ManagerConfig{
  35. Store: s,
  36. Root: managerRoot,
  37. ExecRoot: filepath.Join(root, "exec"),
  38. CreateExecutor: func(*Manager) (Executor, error) { return nil, nil },
  39. LogPluginEvent: func(_, _ string, _ events.Action) {},
  40. })
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. if err := s.Add(p1); err != nil {
  45. t.Fatal(err)
  46. }
  47. if err := s.Add(p2); err != nil {
  48. t.Fatal(err)
  49. }
  50. // Create a mount to simulate a plugin that has created it's own mounts
  51. p2Mount := filepath.Join(p2.Rootfs, "testmount")
  52. if err := os.MkdirAll(p2Mount, 0o755); err != nil {
  53. t.Fatal(err)
  54. }
  55. if err := mount.Mount("tmpfs", p2Mount, "tmpfs", ""); err != nil {
  56. t.Fatal(err)
  57. }
  58. if err := m.Remove(p1.GetID(), &backend.PluginRmConfig{ForceRemove: true}); err != nil {
  59. t.Fatal(err)
  60. }
  61. if mounted, err := mountinfo.Mounted(p2Mount); !mounted || err != nil {
  62. t.Fatalf("expected %s to be mounted, err: %v", p2Mount, err)
  63. }
  64. }
  65. func newTestPlugin(t *testing.T, name, cap, root string) *v2.Plugin {
  66. id := stringid.GenerateRandomID()
  67. rootfs := filepath.Join(root, id)
  68. if err := os.MkdirAll(rootfs, 0o755); err != nil {
  69. t.Fatal(err)
  70. }
  71. p := v2.Plugin{PluginObj: types.Plugin{ID: id, Name: name}}
  72. p.Rootfs = rootfs
  73. iType := types.PluginInterfaceType{Capability: cap, Prefix: "docker", Version: "1.0"}
  74. i := types.PluginConfigInterface{Socket: "plugin.sock", Types: []types.PluginInterfaceType{iType}}
  75. p.PluginObj.Config.Interface = i
  76. p.PluginObj.ID = id
  77. return &p
  78. }
  79. type simpleExecutor struct {
  80. Executor
  81. }
  82. func (e *simpleExecutor) Create(id string, spec specs.Spec, stdout, stderr io.WriteCloser) error {
  83. return errors.New("Create failed")
  84. }
  85. func TestCreateFailed(t *testing.T) {
  86. root, err := os.MkdirTemp("", "test-create-failed")
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. defer containerfs.EnsureRemoveAll(root)
  91. s := NewStore()
  92. managerRoot := filepath.Join(root, "manager")
  93. p := newTestPlugin(t, "create", "testcreate", managerRoot)
  94. m, err := NewManager(
  95. ManagerConfig{
  96. Store: s,
  97. Root: managerRoot,
  98. ExecRoot: filepath.Join(root, "exec"),
  99. CreateExecutor: func(*Manager) (Executor, error) { return &simpleExecutor{}, nil },
  100. LogPluginEvent: func(_, _ string, _ events.Action) {},
  101. })
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. if err := s.Add(p); err != nil {
  106. t.Fatal(err)
  107. }
  108. if err := m.enable(p, &controller{}, false); err == nil {
  109. t.Fatalf("expected Create failed error, got %v", err)
  110. }
  111. if err := m.Remove(p.GetID(), &backend.PluginRmConfig{ForceRemove: true}); err != nil {
  112. t.Fatal(err)
  113. }
  114. }
  115. type executorWithRunning struct {
  116. m *Manager
  117. root string
  118. exitChans map[string]chan struct{}
  119. }
  120. func (e *executorWithRunning) Create(id string, spec specs.Spec, stdout, stderr io.WriteCloser) error {
  121. sockAddr := filepath.Join(e.root, id, "plugin.sock")
  122. ch := make(chan struct{})
  123. if e.exitChans == nil {
  124. e.exitChans = make(map[string]chan struct{})
  125. }
  126. e.exitChans[id] = ch
  127. listenTestPlugin(sockAddr, ch)
  128. return nil
  129. }
  130. func (e *executorWithRunning) IsRunning(id string) (bool, error) {
  131. return true, nil
  132. }
  133. func (e *executorWithRunning) Restore(id string, stdout, stderr io.WriteCloser) (bool, error) {
  134. return true, nil
  135. }
  136. func (e *executorWithRunning) Signal(id string, signal syscall.Signal) error {
  137. ch := e.exitChans[id]
  138. ch <- struct{}{}
  139. <-ch
  140. e.m.HandleExitEvent(id)
  141. return nil
  142. }
  143. func TestPluginAlreadyRunningOnStartup(t *testing.T) {
  144. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  145. t.Parallel()
  146. root, err := os.MkdirTemp("", t.Name())
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. defer containerfs.EnsureRemoveAll(root)
  151. for _, test := range []struct {
  152. desc string
  153. config ManagerConfig
  154. }{
  155. {
  156. desc: "live-restore-disabled",
  157. config: ManagerConfig{
  158. LogPluginEvent: func(_, _ string, _ events.Action) {},
  159. },
  160. },
  161. {
  162. desc: "live-restore-enabled",
  163. config: ManagerConfig{
  164. LogPluginEvent: func(_, _ string, _ events.Action) {},
  165. LiveRestoreEnabled: true,
  166. },
  167. },
  168. } {
  169. t.Run(test.desc, func(t *testing.T) {
  170. config := test.config
  171. desc := test.desc
  172. t.Parallel()
  173. p := newTestPlugin(t, desc, desc, config.Root)
  174. p.PluginObj.Enabled = true
  175. // Need a short-ish path here so we don't run into unix socket path length issues.
  176. config.ExecRoot, err = os.MkdirTemp("", "plugintest")
  177. executor := &executorWithRunning{root: config.ExecRoot}
  178. config.CreateExecutor = func(m *Manager) (Executor, error) { executor.m = m; return executor, nil }
  179. if err := executor.Create(p.GetID(), specs.Spec{}, nil, nil); err != nil {
  180. t.Fatal(err)
  181. }
  182. root := filepath.Join(root, desc)
  183. config.Root = filepath.Join(root, "manager")
  184. if err := os.MkdirAll(filepath.Join(config.Root, p.GetID()), 0o755); err != nil {
  185. t.Fatal(err)
  186. }
  187. if !p.IsEnabled() {
  188. t.Fatal("plugin should be enabled")
  189. }
  190. if err := (&Manager{config: config}).save(p); err != nil {
  191. t.Fatal(err)
  192. }
  193. s := NewStore()
  194. config.Store = s
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. defer containerfs.EnsureRemoveAll(config.ExecRoot)
  199. m, err := NewManager(config)
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. defer m.Shutdown()
  204. p = s.GetAll()[p.GetID()] // refresh `p` with what the manager knows
  205. if p.Client() == nil {
  206. t.Fatal("plugin client should not be nil")
  207. }
  208. })
  209. }
  210. }
  211. func listenTestPlugin(sockAddr string, exit chan struct{}) (net.Listener, error) {
  212. if err := os.MkdirAll(filepath.Dir(sockAddr), 0o755); err != nil {
  213. return nil, err
  214. }
  215. l, err := net.Listen("unix", sockAddr)
  216. if err != nil {
  217. return nil, err
  218. }
  219. go func() {
  220. for {
  221. conn, err := l.Accept()
  222. if err != nil {
  223. return
  224. }
  225. conn.Close()
  226. }
  227. }()
  228. go func() {
  229. <-exit
  230. l.Close()
  231. os.Remove(sockAddr)
  232. exit <- struct{}{}
  233. }()
  234. return l, nil
  235. }