github.com/kinvolk/docker@v1.13.1/integration-cli/docker_cli_plugins_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os/exec" 6 7 "github.com/docker/docker/pkg/integration/checker" 8 "github.com/go-check/check" 9 10 "io/ioutil" 11 "os" 12 "path/filepath" 13 "strings" 14 ) 15 16 var ( 17 pluginProcessName = "sample-volume-plugin" 18 pName = "tiborvass/sample-volume-plugin" 19 npName = "tiborvass/test-docker-netplugin" 20 pTag = "latest" 21 pNameWithTag = pName + ":" + pTag 22 npNameWithTag = npName + ":" + pTag 23 ) 24 25 func (s *DockerSuite) TestPluginBasicOps(c *check.C) { 26 testRequires(c, DaemonIsLinux, IsAmd64, Network) 27 _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag) 28 c.Assert(err, checker.IsNil) 29 30 out, _, err := dockerCmdWithError("plugin", "ls") 31 c.Assert(err, checker.IsNil) 32 c.Assert(out, checker.Contains, pName) 33 c.Assert(out, checker.Contains, pTag) 34 c.Assert(out, checker.Contains, "true") 35 36 id, _, err := dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag) 37 id = strings.TrimSpace(id) 38 c.Assert(err, checker.IsNil) 39 40 out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag) 41 c.Assert(err, checker.NotNil) 42 c.Assert(out, checker.Contains, "is enabled") 43 44 _, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag) 45 c.Assert(err, checker.IsNil) 46 47 out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag) 48 c.Assert(err, checker.IsNil) 49 c.Assert(out, checker.Contains, pNameWithTag) 50 51 _, err = os.Stat(filepath.Join(dockerBasePath, "plugins", id)) 52 if !os.IsNotExist(err) { 53 c.Fatal(err) 54 } 55 } 56 57 func (s *DockerSuite) TestPluginForceRemove(c *check.C) { 58 testRequires(c, DaemonIsLinux, IsAmd64, Network) 59 out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag) 60 c.Assert(err, checker.IsNil) 61 62 out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag) 63 c.Assert(out, checker.Contains, "is enabled") 64 65 out, _, err = dockerCmdWithError("plugin", "remove", "--force", pNameWithTag) 66 c.Assert(err, checker.IsNil) 67 c.Assert(out, checker.Contains, pNameWithTag) 68 } 69 70 func (s *DockerSuite) TestPluginActive(c *check.C) { 71 testRequires(c, DaemonIsLinux, IsAmd64, Network) 72 _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag) 73 c.Assert(err, checker.IsNil) 74 75 _, _, err = dockerCmdWithError("volume", "create", "-d", pNameWithTag, "--name", "testvol1") 76 c.Assert(err, checker.IsNil) 77 78 out, _, err := dockerCmdWithError("plugin", "disable", pNameWithTag) 79 c.Assert(out, checker.Contains, "in use") 80 81 _, _, err = dockerCmdWithError("volume", "rm", "testvol1") 82 c.Assert(err, checker.IsNil) 83 84 _, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag) 85 c.Assert(err, checker.IsNil) 86 87 out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag) 88 c.Assert(err, checker.IsNil) 89 c.Assert(out, checker.Contains, pNameWithTag) 90 } 91 92 func (s *DockerSuite) TestPluginActiveNetwork(c *check.C) { 93 testRequires(c, DaemonIsLinux, IsAmd64, Network) 94 out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", npNameWithTag) 95 c.Assert(err, checker.IsNil) 96 97 out, _, err = dockerCmdWithError("network", "create", "-d", npNameWithTag, "test") 98 c.Assert(err, checker.IsNil) 99 100 nID := strings.TrimSpace(out) 101 102 out, _, err = dockerCmdWithError("plugin", "remove", npNameWithTag) 103 c.Assert(out, checker.Contains, "is in use") 104 105 _, _, err = dockerCmdWithError("network", "rm", nID) 106 c.Assert(err, checker.IsNil) 107 108 out, _, err = dockerCmdWithError("plugin", "remove", npNameWithTag) 109 c.Assert(out, checker.Contains, "is enabled") 110 111 _, _, err = dockerCmdWithError("plugin", "disable", npNameWithTag) 112 c.Assert(err, checker.IsNil) 113 114 out, _, err = dockerCmdWithError("plugin", "remove", npNameWithTag) 115 c.Assert(err, checker.IsNil) 116 c.Assert(out, checker.Contains, npNameWithTag) 117 } 118 119 func (s *DockerSuite) TestPluginInstallDisable(c *check.C) { 120 testRequires(c, DaemonIsLinux, IsAmd64, Network) 121 out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", "--disable", pName) 122 c.Assert(err, checker.IsNil) 123 c.Assert(strings.TrimSpace(out), checker.Contains, pName) 124 125 out, _, err = dockerCmdWithError("plugin", "ls") 126 c.Assert(err, checker.IsNil) 127 c.Assert(out, checker.Contains, "false") 128 129 out, _, err = dockerCmdWithError("plugin", "enable", pName) 130 c.Assert(err, checker.IsNil) 131 c.Assert(strings.TrimSpace(out), checker.Contains, pName) 132 133 out, _, err = dockerCmdWithError("plugin", "disable", pName) 134 c.Assert(err, checker.IsNil) 135 c.Assert(strings.TrimSpace(out), checker.Contains, pName) 136 137 out, _, err = dockerCmdWithError("plugin", "remove", pName) 138 c.Assert(err, checker.IsNil) 139 c.Assert(strings.TrimSpace(out), checker.Contains, pName) 140 } 141 142 func (s *DockerSuite) TestPluginInstallDisableVolumeLs(c *check.C) { 143 testRequires(c, DaemonIsLinux, IsAmd64, Network) 144 out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", "--disable", pName) 145 c.Assert(err, checker.IsNil) 146 c.Assert(strings.TrimSpace(out), checker.Contains, pName) 147 148 dockerCmd(c, "volume", "ls") 149 } 150 151 func (s *DockerSuite) TestPluginSet(c *check.C) { 152 testRequires(c, DaemonIsLinux, IsAmd64, Network) 153 out, _ := dockerCmd(c, "plugin", "install", "--grant-all-permissions", "--disable", pName) 154 c.Assert(strings.TrimSpace(out), checker.Contains, pName) 155 156 env, _ := dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", pName) 157 c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=0]") 158 159 dockerCmd(c, "plugin", "set", pName, "DEBUG=1") 160 161 env, _ = dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", pName) 162 c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=1]") 163 } 164 165 func (s *DockerSuite) TestPluginInstallArgs(c *check.C) { 166 testRequires(c, DaemonIsLinux, IsAmd64, Network) 167 out, _ := dockerCmd(c, "plugin", "install", "--grant-all-permissions", "--disable", pName, "DEBUG=1") 168 c.Assert(strings.TrimSpace(out), checker.Contains, pName) 169 170 env, _ := dockerCmd(c, "plugin", "inspect", "-f", "{{.Settings.Env}}", pName) 171 c.Assert(strings.TrimSpace(env), checker.Equals, "[DEBUG=1]") 172 } 173 174 func (s *DockerRegistrySuite) TestPluginInstallImage(c *check.C) { 175 testRequires(c, DaemonIsLinux, IsAmd64) 176 177 repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL) 178 // tag the image to upload it to the private registry 179 dockerCmd(c, "tag", "busybox", repoName) 180 // push the image to the registry 181 dockerCmd(c, "push", repoName) 182 183 out, _, err := dockerCmdWithError("plugin", "install", repoName) 184 c.Assert(err, checker.NotNil) 185 c.Assert(out, checker.Contains, "target is image") 186 } 187 188 func (s *DockerSuite) TestPluginEnableDisableNegative(c *check.C) { 189 testRequires(c, DaemonIsLinux, IsAmd64, Network) 190 out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pName) 191 c.Assert(err, checker.IsNil) 192 c.Assert(strings.TrimSpace(out), checker.Contains, pName) 193 194 out, _, err = dockerCmdWithError("plugin", "enable", pName) 195 c.Assert(err, checker.NotNil) 196 c.Assert(strings.TrimSpace(out), checker.Contains, "already enabled") 197 198 _, _, err = dockerCmdWithError("plugin", "disable", pName) 199 c.Assert(err, checker.IsNil) 200 201 out, _, err = dockerCmdWithError("plugin", "disable", pName) 202 c.Assert(err, checker.NotNil) 203 c.Assert(strings.TrimSpace(out), checker.Contains, "already disabled") 204 205 _, _, err = dockerCmdWithError("plugin", "remove", pName) 206 c.Assert(err, checker.IsNil) 207 } 208 209 func (s *DockerSuite) TestPluginCreate(c *check.C) { 210 testRequires(c, DaemonIsLinux, IsAmd64, Network) 211 212 name := "foo/bar-driver" 213 temp, err := ioutil.TempDir("", "foo") 214 c.Assert(err, checker.IsNil) 215 defer os.RemoveAll(temp) 216 217 data := `{"description": "foo plugin"}` 218 err = ioutil.WriteFile(filepath.Join(temp, "config.json"), []byte(data), 0644) 219 c.Assert(err, checker.IsNil) 220 221 err = os.MkdirAll(filepath.Join(temp, "rootfs"), 0700) 222 c.Assert(err, checker.IsNil) 223 224 out, _, err := dockerCmdWithError("plugin", "create", name, temp) 225 c.Assert(err, checker.IsNil) 226 c.Assert(out, checker.Contains, name) 227 228 out, _, err = dockerCmdWithError("plugin", "ls") 229 c.Assert(err, checker.IsNil) 230 c.Assert(out, checker.Contains, name) 231 232 out, _, err = dockerCmdWithError("plugin", "create", name, temp) 233 c.Assert(err, checker.NotNil) 234 c.Assert(out, checker.Contains, "already exist") 235 236 out, _, err = dockerCmdWithError("plugin", "ls") 237 c.Assert(err, checker.IsNil) 238 c.Assert(out, checker.Contains, name) 239 // The output will consists of one HEADER line and one line of foo/bar-driver 240 c.Assert(len(strings.Split(strings.TrimSpace(out), "\n")), checker.Equals, 2) 241 } 242 243 func (s *DockerSuite) TestPluginInspect(c *check.C) { 244 testRequires(c, DaemonIsLinux, IsAmd64, Network) 245 _, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pNameWithTag) 246 c.Assert(err, checker.IsNil) 247 248 out, _, err := dockerCmdWithError("plugin", "ls") 249 c.Assert(err, checker.IsNil) 250 c.Assert(out, checker.Contains, pName) 251 c.Assert(out, checker.Contains, pTag) 252 c.Assert(out, checker.Contains, "true") 253 254 // Find the ID first 255 out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag) 256 c.Assert(err, checker.IsNil) 257 id := strings.TrimSpace(out) 258 c.Assert(id, checker.Not(checker.Equals), "") 259 260 // Long form 261 out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id) 262 c.Assert(err, checker.IsNil) 263 c.Assert(strings.TrimSpace(out), checker.Equals, id) 264 265 // Short form 266 out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id[:5]) 267 c.Assert(err, checker.IsNil) 268 c.Assert(strings.TrimSpace(out), checker.Equals, id) 269 270 // Name with tag form 271 out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pNameWithTag) 272 c.Assert(err, checker.IsNil) 273 c.Assert(strings.TrimSpace(out), checker.Equals, id) 274 275 // Name without tag form 276 out, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", pName) 277 c.Assert(err, checker.IsNil) 278 c.Assert(strings.TrimSpace(out), checker.Equals, id) 279 280 _, _, err = dockerCmdWithError("plugin", "disable", pNameWithTag) 281 c.Assert(err, checker.IsNil) 282 283 out, _, err = dockerCmdWithError("plugin", "remove", pNameWithTag) 284 c.Assert(err, checker.IsNil) 285 c.Assert(out, checker.Contains, pNameWithTag) 286 287 // After remove nothing should be found 288 _, _, err = dockerCmdWithError("plugin", "inspect", "-f", "{{.Id}}", id[:5]) 289 c.Assert(err, checker.NotNil) 290 } 291 292 // Test case for https://github.com/docker/docker/pull/29186#discussion_r91277345 293 func (s *DockerSuite) TestPluginInspectOnWindows(c *check.C) { 294 // This test should work on Windows only 295 testRequires(c, DaemonIsWindows) 296 297 out, _, err := dockerCmdWithError("plugin", "inspect", "foobar") 298 c.Assert(err, checker.NotNil) 299 c.Assert(out, checker.Contains, "plugins are not supported on this platform") 300 c.Assert(err.Error(), checker.Contains, "plugins are not supported on this platform") 301 } 302 303 func (s *DockerTrustSuite) TestPluginTrustedInstall(c *check.C) { 304 testRequires(c, DaemonIsLinux, IsAmd64, Network) 305 306 trustedName := s.setupTrustedplugin(c, pNameWithTag, "trusted-plugin-install") 307 308 installCmd := exec.Command(dockerBinary, "plugin", "install", "--grant-all-permissions", trustedName) 309 s.trustedCmd(installCmd) 310 out, _, err := runCommandWithOutput(installCmd) 311 312 c.Assert(strings.TrimSpace(out), checker.Contains, trustedName) 313 c.Assert(err, checker.IsNil) 314 c.Assert(strings.TrimSpace(out), checker.Contains, trustedName) 315 316 out, _, err = dockerCmdWithError("plugin", "ls") 317 c.Assert(err, checker.IsNil) 318 c.Assert(out, checker.Contains, "true") 319 320 out, _, err = dockerCmdWithError("plugin", "disable", trustedName) 321 c.Assert(err, checker.IsNil) 322 c.Assert(strings.TrimSpace(out), checker.Contains, trustedName) 323 324 out, _, err = dockerCmdWithError("plugin", "enable", trustedName) 325 c.Assert(err, checker.IsNil) 326 c.Assert(strings.TrimSpace(out), checker.Contains, trustedName) 327 328 out, _, err = dockerCmdWithError("plugin", "rm", "-f", trustedName) 329 c.Assert(err, checker.IsNil) 330 c.Assert(strings.TrimSpace(out), checker.Contains, trustedName) 331 332 // Try untrusted pull to ensure we pushed the tag to the registry 333 installCmd = exec.Command(dockerBinary, "plugin", "install", "--disable-content-trust=true", "--grant-all-permissions", trustedName) 334 s.trustedCmd(installCmd) 335 out, _, err = runCommandWithOutput(installCmd) 336 c.Assert(err, check.IsNil, check.Commentf(out)) 337 c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out)) 338 339 out, _, err = dockerCmdWithError("plugin", "ls") 340 c.Assert(err, checker.IsNil) 341 c.Assert(out, checker.Contains, "true") 342 343 } 344 345 func (s *DockerTrustSuite) TestPluginUntrustedInstall(c *check.C) { 346 testRequires(c, DaemonIsLinux, IsAmd64, Network) 347 348 pluginName := fmt.Sprintf("%v/dockercliuntrusted/plugintest:latest", privateRegistryURL) 349 // install locally and push to private registry 350 dockerCmd(c, "plugin", "install", "--grant-all-permissions", "--alias", pluginName, pNameWithTag) 351 dockerCmd(c, "plugin", "push", pluginName) 352 dockerCmd(c, "plugin", "rm", "-f", pluginName) 353 354 // Try trusted install on untrusted plugin 355 installCmd := exec.Command(dockerBinary, "plugin", "install", "--grant-all-permissions", pluginName) 356 s.trustedCmd(installCmd) 357 out, _, err := runCommandWithOutput(installCmd) 358 359 c.Assert(err, check.NotNil, check.Commentf(out)) 360 c.Assert(string(out), checker.Contains, "Error: remote trust data does not exist", check.Commentf(out)) 361 } 362 363 func (s *DockerSuite) TestPluginUpgrade(c *check.C) { 364 testRequires(c, DaemonIsLinux, Network, SameHostDaemon, IsAmd64) 365 plugin := "cpuguy83/docker-volume-driver-plugin-local:latest" 366 pluginV2 := "cpuguy83/docker-volume-driver-plugin-local:v2" 367 368 dockerCmd(c, "plugin", "install", "--grant-all-permissions", plugin) 369 dockerCmd(c, "volume", "create", "--driver", plugin, "bananas") 370 dockerCmd(c, "run", "--rm", "-v", "bananas:/apple", "busybox", "sh", "-c", "touch /apple/core") 371 372 out, _, err := dockerCmdWithError("plugin", "upgrade", "--grant-all-permissions", plugin, pluginV2) 373 c.Assert(err, checker.NotNil, check.Commentf(out)) 374 c.Assert(out, checker.Contains, "disabled before upgrading") 375 376 out, _ = dockerCmd(c, "plugin", "inspect", "--format={{.ID}}", plugin) 377 id := strings.TrimSpace(out) 378 379 // make sure "v2" does not exists 380 _, err = os.Stat(filepath.Join(dockerBasePath, "plugins", id, "rootfs", "v2")) 381 c.Assert(os.IsNotExist(err), checker.True, check.Commentf(out)) 382 383 dockerCmd(c, "plugin", "disable", "-f", plugin) 384 dockerCmd(c, "plugin", "upgrade", "--grant-all-permissions", "--skip-remote-check", plugin, pluginV2) 385 386 // make sure "v2" file exists 387 _, err = os.Stat(filepath.Join(dockerBasePath, "plugins", id, "rootfs", "v2")) 388 c.Assert(err, checker.IsNil) 389 390 dockerCmd(c, "plugin", "enable", plugin) 391 dockerCmd(c, "volume", "inspect", "bananas") 392 dockerCmd(c, "run", "--rm", "-v", "bananas:/apple", "busybox", "sh", "-c", "ls -lh /apple/core") 393 }