github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/integration-cli/docker_cli_plugins_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  	"path"
     9  	"path/filepath"
    10  	"strings"
    11  	"time"
    12  
    13  	"github.com/docker/docker/api/types"
    14  	"github.com/docker/docker/integration-cli/checker"
    15  	"github.com/docker/docker/integration-cli/cli"
    16  	"github.com/docker/docker/integration-cli/daemon"
    17  	"github.com/docker/docker/integration-cli/fixtures/plugin"
    18  	"github.com/docker/docker/integration-cli/request"
    19  	"github.com/go-check/check"
    20  	"github.com/gotestyourself/gotestyourself/icmd"
    21  	"golang.org/x/net/context"
    22  )
    23  
    24  var (
    25  	pluginProcessName = "sample-volume-plugin"
    26  	pName             = "tiborvass/sample-volume-plugin"
    27  	npName            = "tiborvass/test-docker-netplugin"
    28  	pTag              = "latest"
    29  	pNameWithTag      = pName + ":" + pTag
    30  	npNameWithTag     = npName + ":" + pTag
    31  )
    32  
    33  func (ps *DockerPluginSuite) TestPluginBasicOps(c *check.C) {
    34  	plugin := ps.getPluginRepoWithTag()
    35  	_, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", plugin)
    36  	c.Assert(err, checker.IsNil)
    37  
    38  	out, _, err := dockerCmdWithError("plugin", "ls")
    39  	c.Assert(err, checker.IsNil)
    40  	c.Assert(out, checker.Contains, plugin)
    41  	c.Assert(out, checker.Contains, "true")
    42  
    43  	id, _, err := dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", plugin)
    44  	id = strings.TrimSpace(id)
    45  	c.Assert(err, checker.IsNil)
    46  
    47  	out, _, err = dockerCmdWithError("plugin", "remove", plugin)
    48  	c.Assert(err, checker.NotNil)
    49  	c.Assert(out, checker.Contains, "is enabled")
    50  
    51  	_, _, err = dockerCmdWithError("plugin", "disable", plugin)
    52  	c.Assert(err, checker.IsNil)
    53  
    54  	out, _, err = dockerCmdWithError("plugin", "remove", plugin)
    55  	c.Assert(err, checker.IsNil)
    56  	c.Assert(out, checker.Contains, plugin)
    57  
    58  	_, err = os.Stat(filepath.Join(testEnv.DockerBasePath(), "plugins", id))
    59  	if !os.IsNotExist(err) {
    60  		c.Fatal(err)
    61  	}
    62  }
    63  
    64  func (ps *DockerPluginSuite) TestPluginForceRemove(c *check.C) {
    65  	pNameWithTag := ps.getPluginRepoWithTag()
    66  
    67  	out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
    68  	c.Assert(err, checker.IsNil)
    69  
    70  	out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
    71  	c.Assert(out, checker.Contains, "is enabled")
    72  
    73  	out, _, err = dockerCmdWithError("plugin", "remove", "--force", pNameWithTag)
    74  	c.Assert(err, checker.IsNil)
    75  	c.Assert(out, checker.Contains, pNameWithTag)
    76  }
    77  
    78  func (s *DockerSuite) TestPluginActive(c *check.C) {
    79  	testRequires(c, DaemonIsLinux, IsAmd64, Network)
    80  
    81  	_, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
    82  	c.Assert(err, checker.IsNil)
    83  
    84  	_, _, err = dockerCmdWithError("volume", "create", "-d", pNameWithTag, "--name", "testvol1")
    85  	c.Assert(err, checker.IsNil)
    86  
    87  	out, _, err := dockerCmdWithError("plugin", "disable", pNameWithTag)
    88  	c.Assert(out, checker.Contains, "in use")
    89  
    90  	_, _, err = dockerCmdWithError("volume", "rm", "testvol1")
    91  	c.Assert(err, checker.IsNil)
    92  
    93  	_, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag)
    94  	c.Assert(err, checker.IsNil)
    95  
    96  	out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
    97  	c.Assert(err, checker.IsNil)
    98  	c.Assert(out, checker.Contains, pNameWithTag)
    99  }
   100  
   101  func (s *DockerSuite) TestPluginActiveNetwork(c *check.C) {
   102  	testRequires(c, DaemonIsLinux, IsAmd64, Network)
   103  	out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", npNameWithTag)
   104  	c.Assert(err, checker.IsNil)
   105  
   106  	out, _, err = dockerCmdWithError("network", "create", "-d", npNameWithTag, "test")
   107  	c.Assert(err, checker.IsNil)
   108  
   109  	nID := strings.TrimSpace(out)
   110  
   111  	out, _, err = dockerCmdWithError("plugin", "remove", npNameWithTag)
   112  	c.Assert(out, checker.Contains, "is in use")
   113  
   114  	_, _, err = dockerCmdWithError("network", "rm", nID)
   115  	c.Assert(err, checker.IsNil)
   116  
   117  	out, _, err = dockerCmdWithError("plugin", "remove", npNameWithTag)
   118  	c.Assert(out, checker.Contains, "is enabled")
   119  
   120  	_, _, err = dockerCmdWithError("plugin", "disable", npNameWithTag)
   121  	c.Assert(err, checker.IsNil)
   122  
   123  	out, _, err = dockerCmdWithError("plugin", "remove", npNameWithTag)
   124  	c.Assert(err, checker.IsNil)
   125  	c.Assert(out, checker.Contains, npNameWithTag)
   126  }
   127  
   128  func (ps *DockerPluginSuite) TestPluginInstallDisable(c *check.C) {
   129  	pName := ps.getPluginRepoWithTag()
   130  
   131  	out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", "--disable", pName)
   132  	c.Assert(err, checker.IsNil)
   133  	c.Assert(strings.TrimSpace(out), checker.Contains, pName)
   134  
   135  	out, _, err = dockerCmdWithError("plugin", "ls")
   136  	c.Assert(err, checker.IsNil)
   137  	c.Assert(out, checker.Contains, "false")
   138  
   139  	out, _, err = dockerCmdWithError("plugin", "enable", pName)
   140  	c.Assert(err, checker.IsNil)
   141  	c.Assert(strings.TrimSpace(out), checker.Contains, pName)
   142  
   143  	out, _, err = dockerCmdWithError("plugin", "disable", pName)
   144  	c.Assert(err, checker.IsNil)
   145  	c.Assert(strings.TrimSpace(out), checker.Contains, pName)
   146  
   147  	out, _, err = dockerCmdWithError("plugin", "remove", pName)
   148  	c.Assert(err, checker.IsNil)
   149  	c.Assert(strings.TrimSpace(out), checker.Contains, pName)
   150  }
   151  
   152  func (s *DockerSuite) TestPluginInstallDisableVolumeLs(c *check.C) {
   153  	testRequires(c, DaemonIsLinux, IsAmd64, Network)
   154  	out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", "--disable", pName)
   155  	c.Assert(err, checker.IsNil)
   156  	c.Assert(strings.TrimSpace(out), checker.Contains, pName)
   157  
   158  	dockerCmd(c, "volume", "ls")
   159  }
   160  
   161  func (ps *DockerPluginSuite) TestPluginSet(c *check.C) {
   162  	// Create a new plugin with extra settings
   163  	client, err := request.NewClient()
   164  	c.Assert(err, checker.IsNil, check.Commentf("failed to create test client"))
   165  
   166  	name := "test"
   167  	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
   168  	defer cancel()
   169  
   170  	initialValue := "0"
   171  	mntSrc := "foo"
   172  	devPath := "/dev/bar"
   173  
   174  	err = plugin.Create(ctx, client, name, func(cfg *plugin.Config) {
   175  		cfg.Env = []types.PluginEnv{{Name: "DEBUG", Value: &initialValue, Settable: []string{"value"}}}
   176  		cfg.Mounts = []types.PluginMount{
   177  			{Name: "pmount1", Settable: []string{"source"}, Type: "none", Source: &mntSrc},
   178  			{Name: "pmount2", Settable: []string{"source"}, Type: "none"}, // Mount without source is invalid.
   179  		}
   180  		cfg.Linux.Devices = []types.PluginDevice{
   181  			{Name: "pdev1", Path: &devPath, Settable: []string{"path"}},
   182  			{Name: "pdev2", Settable: []string{"path"}}, // Device without Path is invalid.
   183  		}
   184  	})
   185  	c.Assert(err, checker.IsNil, check.Commentf("failed to create test plugin"))
   186  
   187  	env, _ := dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", name)
   188  	c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=0]")
   189  
   190  	dockerCmd(c, "plugin", "set", name, "DEBUG=1")
   191  
   192  	env, _ = dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", name)
   193  	c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=1]")
   194  
   195  	env, _ = dockerCmd(c, "plugin", "inspect", "-f", "{{with $mount := index .Settings.Mounts 0}}{{$mount.Source}}{{end}}", name)
   196  	c.Assert(strings.TrimSpace(env), checker.Contains, mntSrc)
   197  
   198  	dockerCmd(c, "plugin", "set", name, "pmount1.source=bar")
   199  
   200  	env, _ = dockerCmd(c, "plugin", "inspect", "-f", "{{with $mount := index .Settings.Mounts 0}}{{$mount.Source}}{{end}}", name)
   201  	c.Assert(strings.TrimSpace(env), checker.Contains, "bar")
   202  
   203  	out, _, err := dockerCmdWithError("plugin", "set", name, "pmount2.source=bar2")
   204  	c.Assert(err, checker.NotNil)
   205  	c.Assert(out, checker.Contains, "Plugin config has no mount source")
   206  
   207  	out, _, err = dockerCmdWithError("plugin", "set", name, "pdev2.path=/dev/bar2")
   208  	c.Assert(err, checker.NotNil)
   209  	c.Assert(out, checker.Contains, "Plugin config has no device path")
   210  
   211  }
   212  
   213  func (ps *DockerPluginSuite) TestPluginInstallArgs(c *check.C) {
   214  	pName := path.Join(ps.registryHost(), "plugin", "testplugininstallwithargs")
   215  	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
   216  	defer cancel()
   217  
   218  	plugin.CreateInRegistry(ctx, pName, nil, func(cfg *plugin.Config) {
   219  		cfg.Env = []types.PluginEnv{{Name: "DEBUG", Settable: []string{"value"}}}
   220  	})
   221  
   222  	out, _ := dockerCmd(c, "plugin", "install", "--grant-all-permissions", "--disable", pName, "DEBUG=1")
   223  	c.Assert(strings.TrimSpace(out), checker.Contains, pName)
   224  
   225  	env, _ := dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", pName)
   226  	c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=1]")
   227  }
   228  
   229  func (ps *DockerPluginSuite) TestPluginInstallImage(c *check.C) {
   230  	testRequires(c, IsAmd64)
   231  
   232  	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
   233  	// tag the image to upload it to the private registry
   234  	dockerCmd(c, "tag", "busybox", repoName)
   235  	// push the image to the registry
   236  	dockerCmd(c, "push", repoName)
   237  
   238  	out, _, err := dockerCmdWithError("plugin", "install", repoName)
   239  	c.Assert(err, checker.NotNil)
   240  	c.Assert(out, checker.Contains, `Encountered remote "application/vnd.docker.container.image.v1+json"(image) when fetching`)
   241  }
   242  
   243  func (ps *DockerPluginSuite) TestPluginEnableDisableNegative(c *check.C) {
   244  	pName := ps.getPluginRepoWithTag()
   245  
   246  	out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pName)
   247  	c.Assert(err, checker.IsNil)
   248  	c.Assert(strings.TrimSpace(out), checker.Contains, pName)
   249  
   250  	out, _, err = dockerCmdWithError("plugin", "enable", pName)
   251  	c.Assert(err, checker.NotNil)
   252  	c.Assert(strings.TrimSpace(out), checker.Contains, "already enabled")
   253  
   254  	_, _, err = dockerCmdWithError("plugin", "disable", pName)
   255  	c.Assert(err, checker.IsNil)
   256  
   257  	out, _, err = dockerCmdWithError("plugin", "disable", pName)
   258  	c.Assert(err, checker.NotNil)
   259  	c.Assert(strings.TrimSpace(out), checker.Contains, "already disabled")
   260  
   261  	_, _, err = dockerCmdWithError("plugin", "remove", pName)
   262  	c.Assert(err, checker.IsNil)
   263  }
   264  
   265  func (ps *DockerPluginSuite) TestPluginCreate(c *check.C) {
   266  	name := "foo/bar-driver"
   267  	temp, err := ioutil.TempDir("", "foo")
   268  	c.Assert(err, checker.IsNil)
   269  	defer os.RemoveAll(temp)
   270  
   271  	data := `{"description": "foo plugin"}`
   272  	err = ioutil.WriteFile(filepath.Join(temp, "config.json"), []byte(data), 0644)
   273  	c.Assert(err, checker.IsNil)
   274  
   275  	err = os.MkdirAll(filepath.Join(temp, "rootfs"), 0700)
   276  	c.Assert(err, checker.IsNil)
   277  
   278  	out, _, err := dockerCmdWithError("plugin", "create", name, temp)
   279  	c.Assert(err, checker.IsNil)
   280  	c.Assert(out, checker.Contains, name)
   281  
   282  	out, _, err = dockerCmdWithError("plugin", "ls")
   283  	c.Assert(err, checker.IsNil)
   284  	c.Assert(out, checker.Contains, name)
   285  
   286  	out, _, err = dockerCmdWithError("plugin", "create", name, temp)
   287  	c.Assert(err, checker.NotNil)
   288  	c.Assert(out, checker.Contains, "already exist")
   289  
   290  	out, _, err = dockerCmdWithError("plugin", "ls")
   291  	c.Assert(err, checker.IsNil)
   292  	c.Assert(out, checker.Contains, name)
   293  	// The output will consists of one HEADER line and one line of foo/bar-driver
   294  	c.Assert(len(strings.Split(strings.TrimSpace(out), "\n")), checker.Equals, 2)
   295  }
   296  
   297  func (ps *DockerPluginSuite) TestPluginInspect(c *check.C) {
   298  	pNameWithTag := ps.getPluginRepoWithTag()
   299  
   300  	_, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag)
   301  	c.Assert(err, checker.IsNil)
   302  
   303  	out, _, err := dockerCmdWithError("plugin", "ls")
   304  	c.Assert(err, checker.IsNil)
   305  	c.Assert(out, checker.Contains, pNameWithTag)
   306  	c.Assert(out, checker.Contains, "true")
   307  
   308  	// Find the ID first
   309  	out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag)
   310  	c.Assert(err, checker.IsNil)
   311  	id := strings.TrimSpace(out)
   312  	c.Assert(id, checker.Not(checker.Equals), "")
   313  
   314  	// Long form
   315  	out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id)
   316  	c.Assert(err, checker.IsNil)
   317  	c.Assert(strings.TrimSpace(out), checker.Equals, id)
   318  
   319  	// Short form
   320  	out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id[:5])
   321  	c.Assert(err, checker.IsNil)
   322  	c.Assert(strings.TrimSpace(out), checker.Equals, id)
   323  
   324  	// Name with tag form
   325  	out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag)
   326  	c.Assert(err, checker.IsNil)
   327  	c.Assert(strings.TrimSpace(out), checker.Equals, id)
   328  
   329  	// Name without tag form
   330  	out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", ps.getPluginRepo())
   331  	c.Assert(err, checker.IsNil)
   332  	c.Assert(strings.TrimSpace(out), checker.Equals, id)
   333  
   334  	_, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag)
   335  	c.Assert(err, checker.IsNil)
   336  
   337  	out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag)
   338  	c.Assert(err, checker.IsNil)
   339  	c.Assert(out, checker.Contains, pNameWithTag)
   340  
   341  	// After remove nothing should be found
   342  	_, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id[:5])
   343  	c.Assert(err, checker.NotNil)
   344  }
   345  
   346  // Test case for https://github.com/docker/docker/pull/29186#discussion_r91277345
   347  func (s *DockerSuite) TestPluginInspectOnWindows(c *check.C) {
   348  	// This test should work on Windows only
   349  	testRequires(c, DaemonIsWindows)
   350  
   351  	out, _, err := dockerCmdWithError("plugin", "inspect", "foobar")
   352  	c.Assert(err, checker.NotNil)
   353  	c.Assert(out, checker.Contains, "plugins are not supported on this platform")
   354  	c.Assert(err.Error(), checker.Contains, "plugins are not supported on this platform")
   355  }
   356  
   357  func (s *DockerTrustSuite) TestPluginTrustedInstall(c *check.C) {
   358  	testRequires(c, DaemonIsLinux, IsAmd64, Network)
   359  
   360  	trustedName := s.setupTrustedplugin(c, pNameWithTag, "trusted-plugin-install")
   361  
   362  	cli.Docker(cli.Args("plugin", "install", "--grant-all-permissions", trustedName), trustedCmd).Assert(c, icmd.Expected{
   363  		Out: trustedName,
   364  	})
   365  
   366  	out := cli.DockerCmd(c, "plugin", "ls").Combined()
   367  	c.Assert(out, checker.Contains, "true")
   368  
   369  	out = cli.DockerCmd(c, "plugin", "disable", trustedName).Combined()
   370  	c.Assert(strings.TrimSpace(out), checker.Contains, trustedName)
   371  
   372  	out = cli.DockerCmd(c, "plugin", "enable", trustedName).Combined()
   373  	c.Assert(strings.TrimSpace(out), checker.Contains, trustedName)
   374  
   375  	out = cli.DockerCmd(c, "plugin", "rm", "-f", trustedName).Combined()
   376  	c.Assert(strings.TrimSpace(out), checker.Contains, trustedName)
   377  
   378  	// Try untrusted pull to ensure we pushed the tag to the registry
   379  	cli.Docker(cli.Args("plugin", "install", "--disable-content-trust=true", "--grant-all-permissions", trustedName), trustedCmd).Assert(c, SuccessDownloaded)
   380  
   381  	out = cli.DockerCmd(c, "plugin", "ls").Combined()
   382  	c.Assert(out, checker.Contains, "true")
   383  
   384  }
   385  
   386  func (s *DockerTrustSuite) TestPluginUntrustedInstall(c *check.C) {
   387  	testRequires(c, DaemonIsLinux, IsAmd64, Network)
   388  
   389  	pluginName := fmt.Sprintf("%v/dockercliuntrusted/plugintest:latest", privateRegistryURL)
   390  	// install locally and push to private registry
   391  	cli.DockerCmd(c, "plugin", "install", "--grant-all-permissions", "--alias", pluginName, pNameWithTag)
   392  	cli.DockerCmd(c, "plugin", "push", pluginName)
   393  	cli.DockerCmd(c, "plugin", "rm", "-f", pluginName)
   394  
   395  	// Try trusted install on untrusted plugin
   396  	cli.Docker(cli.Args("plugin", "install", "--grant-all-permissions", pluginName), trustedCmd).Assert(c, icmd.Expected{
   397  		ExitCode: 1,
   398  		Err:      "Error: remote trust data does not exist",
   399  	})
   400  }
   401  
   402  func (ps *DockerPluginSuite) TestPluginIDPrefix(c *check.C) {
   403  	name := "test"
   404  	client, err := request.NewClient()
   405  	c.Assert(err, checker.IsNil, check.Commentf("error creating test client"))
   406  
   407  	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
   408  	initialValue := "0"
   409  	err = plugin.Create(ctx, client, name, func(cfg *plugin.Config) {
   410  		cfg.Env = []types.PluginEnv{{Name: "DEBUG", Value: &initialValue, Settable: []string{"value"}}}
   411  	})
   412  	cancel()
   413  
   414  	c.Assert(err, checker.IsNil, check.Commentf("failed to create test plugin"))
   415  
   416  	// Find ID first
   417  	id, _, err := dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", name)
   418  	id = strings.TrimSpace(id)
   419  	c.Assert(err, checker.IsNil)
   420  
   421  	// List current state
   422  	out, _, err := dockerCmdWithError("plugin", "ls")
   423  	c.Assert(err, checker.IsNil)
   424  	c.Assert(out, checker.Contains, name)
   425  	c.Assert(out, checker.Contains, "false")
   426  
   427  	env, _ := dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", id[:5])
   428  	c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=0]")
   429  
   430  	dockerCmd(c, "plugin", "set", id[:5], "DEBUG=1")
   431  
   432  	env, _ = dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", id[:5])
   433  	c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=1]")
   434  
   435  	// Enable
   436  	_, _, err = dockerCmdWithError("plugin", "enable", id[:5])
   437  	c.Assert(err, checker.IsNil)
   438  	out, _, err = dockerCmdWithError("plugin", "ls")
   439  	c.Assert(err, checker.IsNil)
   440  	c.Assert(out, checker.Contains, name)
   441  	c.Assert(out, checker.Contains, "true")
   442  
   443  	// Disable
   444  	_, _, err = dockerCmdWithError("plugin", "disable", id[:5])
   445  	c.Assert(err, checker.IsNil)
   446  	out, _, err = dockerCmdWithError("plugin", "ls")
   447  	c.Assert(err, checker.IsNil)
   448  	c.Assert(out, checker.Contains, name)
   449  	c.Assert(out, checker.Contains, "false")
   450  
   451  	// Remove
   452  	out, _, err = dockerCmdWithError("plugin", "remove", id[:5])
   453  	c.Assert(err, checker.IsNil)
   454  	// List returns none
   455  	out, _, err = dockerCmdWithError("plugin", "ls")
   456  	c.Assert(err, checker.IsNil)
   457  	c.Assert(out, checker.Not(checker.Contains), name)
   458  }
   459  
   460  func (ps *DockerPluginSuite) TestPluginListDefaultFormat(c *check.C) {
   461  	config, err := ioutil.TempDir("", "config-file-")
   462  	c.Assert(err, check.IsNil)
   463  	defer os.RemoveAll(config)
   464  
   465  	err = ioutil.WriteFile(filepath.Join(config, "config.json"), []byte(`{"pluginsFormat": "raw"}`), 0644)
   466  	c.Assert(err, check.IsNil)
   467  
   468  	name := "test:latest"
   469  	client, err := request.NewClient()
   470  	c.Assert(err, checker.IsNil, check.Commentf("error creating test client"))
   471  
   472  	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
   473  	defer cancel()
   474  	err = plugin.Create(ctx, client, name, func(cfg *plugin.Config) {
   475  		cfg.Description = "test plugin"
   476  	})
   477  	c.Assert(err, checker.IsNil, check.Commentf("failed to create test plugin"))
   478  
   479  	out, _ := dockerCmd(c, "plugin", "inspect", "--format", "{{.ID}}", name)
   480  	id := strings.TrimSpace(out)
   481  
   482  	// We expect the format to be in `raw + --no-trunc`
   483  	expectedOutput := fmt.Sprintf(`plugin_id: %s
   484  name: %s
   485  description: test plugin
   486  enabled: false`, id, name)
   487  
   488  	out, _ = dockerCmd(c, "--config", config, "plugin", "ls", "--no-trunc")
   489  	c.Assert(strings.TrimSpace(out), checker.Contains, expectedOutput)
   490  }
   491  
   492  func (s *DockerSuite) TestPluginUpgrade(c *check.C) {
   493  	testRequires(c, DaemonIsLinux, Network, SameHostDaemon, IsAmd64, NotUserNamespace)
   494  	plugin := "cpuguy83/docker-volume-driver-plugin-local:latest"
   495  	pluginV2 := "cpuguy83/docker-volume-driver-plugin-local:v2"
   496  
   497  	dockerCmd(c, "plugin", "install", "--grant-all-permissions", plugin)
   498  	dockerCmd(c, "volume", "create", "--driver", plugin, "bananas")
   499  	dockerCmd(c, "run", "--rm", "-v", "bananas:/apple", "busybox", "sh", "-c", "touch /apple/core")
   500  
   501  	out, _, err := dockerCmdWithError("plugin", "upgrade", "--grant-all-permissions", plugin, pluginV2)
   502  	c.Assert(err, checker.NotNil, check.Commentf(out))
   503  	c.Assert(out, checker.Contains, "disabled before upgrading")
   504  
   505  	out, _ = dockerCmd(c, "plugin", "inspect", "--format={{.ID}}", plugin)
   506  	id := strings.TrimSpace(out)
   507  
   508  	// make sure "v2" does not exists
   509  	_, err = os.Stat(filepath.Join(testEnv.DockerBasePath(), "plugins", id, "rootfs", "v2"))
   510  	c.Assert(os.IsNotExist(err), checker.True, check.Commentf(out))
   511  
   512  	dockerCmd(c, "plugin", "disable", "-f", plugin)
   513  	dockerCmd(c, "plugin", "upgrade", "--grant-all-permissions", "--skip-remote-check", plugin, pluginV2)
   514  
   515  	// make sure "v2" file exists
   516  	_, err = os.Stat(filepath.Join(testEnv.DockerBasePath(), "plugins", id, "rootfs", "v2"))
   517  	c.Assert(err, checker.IsNil)
   518  
   519  	dockerCmd(c, "plugin", "enable", plugin)
   520  	dockerCmd(c, "volume", "inspect", "bananas")
   521  	dockerCmd(c, "run", "--rm", "-v", "bananas:/apple", "busybox", "sh", "-c", "ls -lh /apple/core")
   522  }
   523  
   524  func (s *DockerSuite) TestPluginMetricsCollector(c *check.C) {
   525  	testRequires(c, DaemonIsLinux, Network, SameHostDaemon, IsAmd64)
   526  	d := daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{})
   527  	d.Start(c)
   528  	defer d.Stop(c)
   529  
   530  	name := "cpuguy83/docker-metrics-plugin-test:latest"
   531  	r := cli.Docker(cli.Args("plugin", "install", "--grant-all-permissions", name), cli.Daemon(d))
   532  	c.Assert(r.Error, checker.IsNil, check.Commentf(r.Combined()))
   533  
   534  	// plugin lisens on localhost:19393 and proxies the metrics
   535  	resp, err := http.Get("http://localhost:19393/metrics")
   536  	c.Assert(err, checker.IsNil)
   537  	defer resp.Body.Close()
   538  
   539  	b, err := ioutil.ReadAll(resp.Body)
   540  	c.Assert(err, checker.IsNil)
   541  	// check that a known metric is there... don't expect this metric to change over time.. probably safe
   542  	c.Assert(string(b), checker.Contains, "container_actions")
   543  }