manager_linux_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package plugin // import "github.com/docker/docker/plugin"
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "net"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/pkg/stringid"
  11. "github.com/docker/docker/pkg/system"
  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 := ioutil.TempDir("", "test-store-with-plugin-mounts")
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. defer system.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, 0755); 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, 0755); 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. }
  79. func (e *simpleExecutor) Create(id string, spec specs.Spec, stdout, stderr io.WriteCloser) error {
  80. return errors.New("Create failed")
  81. }
  82. func (e *simpleExecutor) Restore(id string, stdout, stderr io.WriteCloser) (bool, error) {
  83. return false, nil
  84. }
  85. func (e *simpleExecutor) IsRunning(id string) (bool, error) {
  86. return false, nil
  87. }
  88. func (e *simpleExecutor) Signal(id string, signal int) error {
  89. return nil
  90. }
  91. func TestCreateFailed(t *testing.T) {
  92. root, err := ioutil.TempDir("", "test-create-failed")
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. defer system.EnsureRemoveAll(root)
  97. s := NewStore()
  98. managerRoot := filepath.Join(root, "manager")
  99. p := newTestPlugin(t, "create", "testcreate", managerRoot)
  100. m, err := NewManager(
  101. ManagerConfig{
  102. Store: s,
  103. Root: managerRoot,
  104. ExecRoot: filepath.Join(root, "exec"),
  105. CreateExecutor: func(*Manager) (Executor, error) { return &simpleExecutor{}, nil },
  106. LogPluginEvent: func(_, _, _ string) {},
  107. })
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. if err := s.Add(p); err != nil {
  112. t.Fatal(err)
  113. }
  114. if err := m.enable(p, &controller{}, false); err == nil {
  115. t.Fatalf("expected Create failed error, got %v", err)
  116. }
  117. if err := m.Remove(p.GetID(), &types.PluginRmConfig{ForceRemove: true}); err != nil {
  118. t.Fatal(err)
  119. }
  120. }
  121. type executorWithRunning struct {
  122. m *Manager
  123. root string
  124. exitChans map[string]chan struct{}
  125. }
  126. func (e *executorWithRunning) Create(id string, spec specs.Spec, stdout, stderr io.WriteCloser) error {
  127. sockAddr := filepath.Join(e.root, id, "plugin.sock")
  128. ch := make(chan struct{})
  129. if e.exitChans == nil {
  130. e.exitChans = make(map[string]chan struct{})
  131. }
  132. e.exitChans[id] = ch
  133. listenTestPlugin(sockAddr, ch)
  134. return nil
  135. }
  136. func (e *executorWithRunning) IsRunning(id string) (bool, error) {
  137. return true, nil
  138. }
  139. func (e *executorWithRunning) Restore(id string, stdout, stderr io.WriteCloser) (bool, error) {
  140. return true, nil
  141. }
  142. func (e *executorWithRunning) Signal(id string, signal int) error {
  143. ch := e.exitChans[id]
  144. ch <- struct{}{}
  145. <-ch
  146. e.m.HandleExitEvent(id)
  147. return nil
  148. }
  149. func TestPluginAlreadyRunningOnStartup(t *testing.T) {
  150. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  151. t.Parallel()
  152. root, err := ioutil.TempDir("", t.Name())
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. defer system.EnsureRemoveAll(root)
  157. for _, test := range []struct {
  158. desc string
  159. config ManagerConfig
  160. }{
  161. {
  162. desc: "live-restore-disabled",
  163. config: ManagerConfig{
  164. LogPluginEvent: func(_, _, _ string) {},
  165. },
  166. },
  167. {
  168. desc: "live-restore-enabled",
  169. config: ManagerConfig{
  170. LogPluginEvent: func(_, _, _ string) {},
  171. LiveRestoreEnabled: true,
  172. },
  173. },
  174. } {
  175. t.Run(test.desc, func(t *testing.T) {
  176. config := test.config
  177. desc := test.desc
  178. t.Parallel()
  179. p := newTestPlugin(t, desc, desc, config.Root)
  180. p.PluginObj.Enabled = true
  181. // Need a short-ish path here so we don't run into unix socket path length issues.
  182. config.ExecRoot, err = ioutil.TempDir("", "plugintest")
  183. executor := &executorWithRunning{root: config.ExecRoot}
  184. config.CreateExecutor = func(m *Manager) (Executor, error) { executor.m = m; return executor, nil }
  185. if err := executor.Create(p.GetID(), specs.Spec{}, nil, nil); err != nil {
  186. t.Fatal(err)
  187. }
  188. root := filepath.Join(root, desc)
  189. config.Root = filepath.Join(root, "manager")
  190. if err := os.MkdirAll(filepath.Join(config.Root, p.GetID()), 0755); err != nil {
  191. t.Fatal(err)
  192. }
  193. if !p.IsEnabled() {
  194. t.Fatal("plugin should be enabled")
  195. }
  196. if err := (&Manager{config: config}).save(p); err != nil {
  197. t.Fatal(err)
  198. }
  199. s := NewStore()
  200. config.Store = s
  201. if err != nil {
  202. t.Fatal(err)
  203. }
  204. defer system.EnsureRemoveAll(config.ExecRoot)
  205. m, err := NewManager(config)
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. defer m.Shutdown()
  210. p = s.GetAll()[p.GetID()] // refresh `p` with what the manager knows
  211. if p.Client() == nil {
  212. t.Fatal("plugin client should not be nil")
  213. }
  214. })
  215. }
  216. }
  217. func listenTestPlugin(sockAddr string, exit chan struct{}) (net.Listener, error) {
  218. if err := os.MkdirAll(filepath.Dir(sockAddr), 0755); err != nil {
  219. return nil, err
  220. }
  221. l, err := net.Listen("unix", sockAddr)
  222. if err != nil {
  223. return nil, err
  224. }
  225. go func() {
  226. for {
  227. conn, err := l.Accept()
  228. if err != nil {
  229. return
  230. }
  231. conn.Close()
  232. }
  233. }()
  234. go func() {
  235. <-exit
  236. l.Close()
  237. os.Remove(sockAddr)
  238. exit <- struct{}{}
  239. }()
  240. return l, nil
  241. }