github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/integration-cli/docker_cli_daemon_plugins_test.go (about)

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