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