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