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