manager_linux_test.go 6.7 KB

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