manager_linux_test.go 6.5 KB

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