Tizen_4.0 base
[platform/upstream/docker-engine.git] / integration-cli / docker_cli_daemon_plugins_test.go
1 // +build linux
2
3 package main
4
5 import (
6         "os"
7         "path/filepath"
8         "strings"
9         "syscall"
10
11         "github.com/docker/docker/integration-cli/checker"
12         "github.com/docker/docker/pkg/mount"
13         icmd "github.com/docker/docker/pkg/testutil/cmd"
14         "github.com/go-check/check"
15 )
16
17 // TestDaemonRestartWithPluginEnabled tests state restore for an enabled plugin
18 func (s *DockerDaemonSuite) TestDaemonRestartWithPluginEnabled(c *check.C) {
19         c.Skip("Plugins aren't supported")
20
21         testRequires(c, IsAmd64, Network)
22
23         s.d.Start(c)
24
25         if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
26                 c.Fatalf("Could not install plugin: %v %s", err, out)
27         }
28
29         defer func() {
30                 if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
31                         c.Fatalf("Could not disable plugin: %v %s", err, out)
32                 }
33                 if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
34                         c.Fatalf("Could not remove plugin: %v %s", err, out)
35                 }
36         }()
37
38         s.d.Restart(c)
39
40         out, err := s.d.Cmd("plugin", "ls")
41         if err != nil {
42                 c.Fatalf("Could not list plugins: %v %s", err, out)
43         }
44         c.Assert(out, checker.Contains, pName)
45         c.Assert(out, checker.Contains, "true")
46 }
47
48 // TestDaemonRestartWithPluginDisabled tests state restore for a disabled plugin
49 func (s *DockerDaemonSuite) TestDaemonRestartWithPluginDisabled(c *check.C) {
50         c.Skip("Plugins aren't supported")
51
52         testRequires(c, IsAmd64, Network)
53
54         s.d.Start(c)
55
56         if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName, "--disable"); err != nil {
57                 c.Fatalf("Could not install plugin: %v %s", err, out)
58         }
59
60         defer func() {
61                 if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
62                         c.Fatalf("Could not remove plugin: %v %s", err, out)
63                 }
64         }()
65
66         s.d.Restart(c)
67
68         out, err := s.d.Cmd("plugin", "ls")
69         if err != nil {
70                 c.Fatalf("Could not list plugins: %v %s", err, out)
71         }
72         c.Assert(out, checker.Contains, pName)
73         c.Assert(out, checker.Contains, "false")
74 }
75
76 // TestDaemonKillLiveRestoreWithPlugins SIGKILLs daemon started with --live-restore.
77 // Plugins should continue to run.
78 func (s *DockerDaemonSuite) TestDaemonKillLiveRestoreWithPlugins(c *check.C) {
79         c.Skip("Plugins aren't supported")
80
81         testRequires(c, IsAmd64, Network)
82
83         s.d.Start(c, "--live-restore")
84         if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
85                 c.Fatalf("Could not install plugin: %v %s", err, out)
86         }
87         defer func() {
88                 s.d.Restart(c, "--live-restore")
89                 if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
90                         c.Fatalf("Could not disable plugin: %v %s", err, out)
91                 }
92                 if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
93                         c.Fatalf("Could not remove plugin: %v %s", err, out)
94                 }
95         }()
96
97         if err := s.d.Kill(); err != nil {
98                 c.Fatalf("Could not kill daemon: %v", err)
99         }
100
101         icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Success)
102 }
103
104 // TestDaemonShutdownLiveRestoreWithPlugins SIGTERMs daemon started with --live-restore.
105 // Plugins should continue to run.
106 func (s *DockerDaemonSuite) TestDaemonShutdownLiveRestoreWithPlugins(c *check.C) {
107         c.Skip("Plugins aren't supported")
108
109         testRequires(c, IsAmd64, Network)
110
111         s.d.Start(c, "--live-restore")
112         if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
113                 c.Fatalf("Could not install plugin: %v %s", err, out)
114         }
115         defer func() {
116                 s.d.Restart(c, "--live-restore")
117                 if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
118                         c.Fatalf("Could not disable plugin: %v %s", err, out)
119                 }
120                 if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
121                         c.Fatalf("Could not remove plugin: %v %s", err, out)
122                 }
123         }()
124
125         if err := s.d.Interrupt(); err != nil {
126                 c.Fatalf("Could not kill daemon: %v", err)
127         }
128
129         icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Success)
130 }
131
132 // TestDaemonShutdownWithPlugins shuts down running plugins.
133 func (s *DockerDaemonSuite) TestDaemonShutdownWithPlugins(c *check.C) {
134         c.Skip("Plugins aren't supported")
135
136         testRequires(c, IsAmd64, Network, SameHostDaemon)
137
138         s.d.Start(c)
139         if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
140                 c.Fatalf("Could not install plugin: %v %s", err, out)
141         }
142
143         defer func() {
144                 s.d.Restart(c)
145                 if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
146                         c.Fatalf("Could not disable plugin: %v %s", err, out)
147                 }
148                 if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
149                         c.Fatalf("Could not remove plugin: %v %s", err, out)
150                 }
151         }()
152
153         if err := s.d.Interrupt(); err != nil {
154                 c.Fatalf("Could not kill daemon: %v", err)
155         }
156
157         for {
158                 if err := syscall.Kill(s.d.Pid(), 0); err == syscall.ESRCH {
159                         break
160                 }
161         }
162
163         icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Expected{
164                 ExitCode: 1,
165                 Error:    "exit status 1",
166         })
167
168         s.d.Start(c)
169         icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Success)
170 }
171
172 // TestDaemonKillWithPlugins leaves plugins running.
173 func (s *DockerDaemonSuite) TestDaemonKillWithPlugins(c *check.C) {
174         c.Skip("Plugins aren't supported")
175
176         testRequires(c, IsAmd64, Network, SameHostDaemon)
177
178         s.d.Start(c)
179         if out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName); err != nil {
180                 c.Fatalf("Could not install plugin: %v %s", err, out)
181         }
182
183         defer func() {
184                 s.d.Restart(c)
185                 if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
186                         c.Fatalf("Could not disable plugin: %v %s", err, out)
187                 }
188                 if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
189                         c.Fatalf("Could not remove plugin: %v %s", err, out)
190                 }
191         }()
192
193         if err := s.d.Kill(); err != nil {
194                 c.Fatalf("Could not kill daemon: %v", err)
195         }
196
197         // assert that plugins are running.
198         icmd.RunCommand("pgrep", "-f", pluginProcessName).Assert(c, icmd.Success)
199 }
200
201 // TestVolumePlugin tests volume creation using a plugin.
202 func (s *DockerDaemonSuite) TestVolumePlugin(c *check.C) {
203         c.Skip("Plugins aren't supported")
204
205         testRequires(c, IsAmd64, Network)
206
207         volName := "plugin-volume"
208         destDir := "/tmp/data/"
209         destFile := "foo"
210
211         s.d.Start(c)
212         out, err := s.d.Cmd("plugin", "install", pName, "--grant-all-permissions")
213         if err != nil {
214                 c.Fatalf("Could not install plugin: %v %s", err, out)
215         }
216         pluginID, err := s.d.Cmd("plugin", "inspect", "-f", "{{.Id}}", pName)
217         pluginID = strings.TrimSpace(pluginID)
218         if err != nil {
219                 c.Fatalf("Could not retrieve plugin ID: %v %s", err, pluginID)
220         }
221         mountpointPrefix := filepath.Join(s.d.RootDir(), "plugins", pluginID, "rootfs")
222         defer func() {
223                 if out, err := s.d.Cmd("plugin", "disable", pName); err != nil {
224                         c.Fatalf("Could not disable plugin: %v %s", err, out)
225                 }
226
227                 if out, err := s.d.Cmd("plugin", "remove", pName); err != nil {
228                         c.Fatalf("Could not remove plugin: %v %s", err, out)
229                 }
230
231                 exists, err := existsMountpointWithPrefix(mountpointPrefix)
232                 c.Assert(err, checker.IsNil)
233                 c.Assert(exists, checker.Equals, false)
234
235         }()
236
237         out, err = s.d.Cmd("volume", "create", "-d", pName, volName)
238         if err != nil {
239                 c.Fatalf("Could not create volume: %v %s", err, out)
240         }
241         defer func() {
242                 if out, err := s.d.Cmd("volume", "remove", volName); err != nil {
243                         c.Fatalf("Could not remove volume: %v %s", err, out)
244                 }
245         }()
246
247         out, err = s.d.Cmd("volume", "ls")
248         if err != nil {
249                 c.Fatalf("Could not list volume: %v %s", err, out)
250         }
251         c.Assert(out, checker.Contains, volName)
252         c.Assert(out, checker.Contains, pName)
253
254         mountPoint, err := s.d.Cmd("volume", "inspect", volName, "--format", "{{.Mountpoint}}")
255         if err != nil {
256                 c.Fatalf("Could not inspect volume: %v %s", err, mountPoint)
257         }
258         mountPoint = strings.TrimSpace(mountPoint)
259
260         out, err = s.d.Cmd("run", "--rm", "-v", volName+":"+destDir, "busybox", "touch", destDir+destFile)
261         c.Assert(err, checker.IsNil, check.Commentf(out))
262         path := filepath.Join(s.d.RootDir(), "plugins", pluginID, "rootfs", mountPoint, destFile)
263         _, err = os.Lstat(path)
264         c.Assert(err, checker.IsNil)
265
266         exists, err := existsMountpointWithPrefix(mountpointPrefix)
267         c.Assert(err, checker.IsNil)
268         c.Assert(exists, checker.Equals, true)
269 }
270
271 func (s *DockerDaemonSuite) TestGraphdriverPlugin(c *check.C) {
272         c.Skip("Plugins aren't supported")
273
274         testRequires(c, Network, IsAmd64, DaemonIsLinux, overlay2Supported, ExperimentalDaemon)
275
276         s.d.Start(c)
277
278         // install the plugin
279         plugin := "cpuguy83/docker-overlay2-graphdriver-plugin"
280         out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", plugin)
281         c.Assert(err, checker.IsNil, check.Commentf(out))
282
283         // restart the daemon with the plugin set as the storage driver
284         s.d.Restart(c, "-s", plugin, "--storage-opt", "overlay2.override_kernel_check=1")
285
286         // run a container
287         out, err = s.d.Cmd("run", "--rm", "busybox", "true") // this will pull busybox using the plugin
288         c.Assert(err, checker.IsNil, check.Commentf(out))
289 }
290
291 func (s *DockerDaemonSuite) TestPluginVolumeRemoveOnRestart(c *check.C) {
292         c.Skip("Plugins aren't supported")
293
294         testRequires(c, DaemonIsLinux, Network, IsAmd64)
295
296         s.d.Start(c, "--live-restore=true")
297
298         out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pName)
299         c.Assert(err, checker.IsNil, check.Commentf(out))
300         c.Assert(strings.TrimSpace(out), checker.Contains, pName)
301
302         out, err = s.d.Cmd("volume", "create", "--driver", pName, "test")
303         c.Assert(err, checker.IsNil, check.Commentf(out))
304
305         s.d.Restart(c, "--live-restore=true")
306
307         out, err = s.d.Cmd("plugin", "disable", pName)
308         c.Assert(err, checker.NotNil, check.Commentf(out))
309         c.Assert(out, checker.Contains, "in use")
310
311         out, err = s.d.Cmd("volume", "rm", "test")
312         c.Assert(err, checker.IsNil, check.Commentf(out))
313
314         out, err = s.d.Cmd("plugin", "disable", pName)
315         c.Assert(err, checker.IsNil, check.Commentf(out))
316
317         out, err = s.d.Cmd("plugin", "rm", pName)
318         c.Assert(err, checker.IsNil, check.Commentf(out))
319 }
320
321 func existsMountpointWithPrefix(mountpointPrefix string) (bool, error) {
322         mounts, err := mount.GetMounts()
323         if err != nil {
324                 return false, err
325         }
326         for _, mnt := range mounts {
327                 if strings.HasPrefix(mnt.Mountpoint, mountpointPrefix) {
328                         return true, nil
329                 }
330         }
331         return false, nil
332 }
333
334 func (s *DockerDaemonSuite) TestPluginListFilterEnabled(c *check.C) {
335         c.Skip("Plugins aren't supported")
336
337         testRequires(c, IsAmd64, Network)
338
339         s.d.Start(c)
340
341         out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pNameWithTag, "--disable")
342         c.Assert(err, check.IsNil, check.Commentf(out))
343
344         defer func() {
345                 if out, err := s.d.Cmd("plugin", "remove", pNameWithTag); err != nil {
346                         c.Fatalf("Could not remove plugin: %v %s", err, out)
347                 }
348         }()
349
350         out, err = s.d.Cmd("plugin", "ls", "--filter", "enabled=true")
351         c.Assert(err, checker.IsNil)
352         c.Assert(out, checker.Not(checker.Contains), pName)
353
354         out, err = s.d.Cmd("plugin", "ls", "--filter", "enabled=false")
355         c.Assert(err, checker.IsNil)
356         c.Assert(out, checker.Contains, pName)
357         c.Assert(out, checker.Contains, "false")
358
359         out, err = s.d.Cmd("plugin", "ls")
360         c.Assert(err, checker.IsNil)
361         c.Assert(out, checker.Contains, pName)
362 }
363
364 func (s *DockerDaemonSuite) TestPluginListFilterCapability(c *check.C) {
365         c.Skip("Plugins aren't supported")
366
367         testRequires(c, IsAmd64, Network)
368
369         s.d.Start(c)
370
371         out, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", pNameWithTag, "--disable")
372         c.Assert(err, check.IsNil, check.Commentf(out))
373
374         defer func() {
375                 if out, err := s.d.Cmd("plugin", "remove", pNameWithTag); err != nil {
376                         c.Fatalf("Could not remove plugin: %v %s", err, out)
377                 }
378         }()
379
380         out, err = s.d.Cmd("plugin", "ls", "--filter", "capability=volumedriver")
381         c.Assert(err, checker.IsNil)
382         c.Assert(out, checker.Contains, pName)
383
384         out, err = s.d.Cmd("plugin", "ls", "--filter", "capability=authz")
385         c.Assert(err, checker.IsNil)
386         c.Assert(out, checker.Not(checker.Contains), pName)
387
388         out, err = s.d.Cmd("plugin", "ls")
389         c.Assert(err, checker.IsNil)
390         c.Assert(out, checker.Contains, pName)
391 }