manager_linux_test.go 6.6 KB

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