github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/integration-cli/docker_cli_create_test.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "reflect" 9 "strings" 10 "time" 11 12 "github.com/docker/docker/integration-cli/checker" 13 "github.com/docker/docker/pkg/stringid" 14 "github.com/docker/docker/pkg/testutil" 15 icmd "github.com/docker/docker/pkg/testutil/cmd" 16 "github.com/docker/go-connections/nat" 17 "github.com/go-check/check" 18 ) 19 20 // Make sure we can create a simple container with some args 21 func (s *DockerSuite) TestCreateArgs(c *check.C) { 22 // Intentionally clear entrypoint, as the Windows busybox image needs an entrypoint, which breaks this test 23 out, _ := dockerCmd(c, "create", "--entrypoint=", "busybox", "command", "arg1", "arg2", "arg with space", "-c", "flags") 24 25 cleanedContainerID := strings.TrimSpace(out) 26 27 out, _ = dockerCmd(c, "inspect", cleanedContainerID) 28 29 containers := []struct { 30 ID string 31 Created time.Time 32 Path string 33 Args []string 34 Image string 35 }{} 36 37 err := json.Unmarshal([]byte(out), &containers) 38 c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err)) 39 c.Assert(containers, checker.HasLen, 1) 40 41 cont := containers[0] 42 c.Assert(string(cont.Path), checker.Equals, "command", check.Commentf("Unexpected container path. Expected command, received: %s", cont.Path)) 43 44 b := false 45 expected := []string{"arg1", "arg2", "arg with space", "-c", "flags"} 46 for i, arg := range expected { 47 if arg != cont.Args[i] { 48 b = true 49 break 50 } 51 } 52 if len(cont.Args) != len(expected) || b { 53 c.Fatalf("Unexpected args. Expected %v, received: %v", expected, cont.Args) 54 } 55 56 } 57 58 // Make sure we can grow the container's rootfs at creation time. 59 func (s *DockerSuite) TestCreateGrowRootfs(c *check.C) { 60 // Windows and Devicemapper support growing the rootfs 61 if testEnv.DaemonPlatform() != "windows" { 62 testRequires(c, Devicemapper) 63 } 64 out, _ := dockerCmd(c, "create", "--storage-opt", "size=120G", "busybox") 65 66 cleanedContainerID := strings.TrimSpace(out) 67 68 inspectOut := inspectField(c, cleanedContainerID, "HostConfig.StorageOpt") 69 c.Assert(inspectOut, checker.Equals, "map[size:120G]") 70 } 71 72 // Make sure we cannot shrink the container's rootfs at creation time. 73 func (s *DockerSuite) TestCreateShrinkRootfs(c *check.C) { 74 testRequires(c, Devicemapper) 75 76 // Ensure this fails because of the defaultBaseFsSize is 10G 77 out, _, err := dockerCmdWithError("create", "--storage-opt", "size=5G", "busybox") 78 c.Assert(err, check.NotNil, check.Commentf(out)) 79 c.Assert(out, checker.Contains, "Container size cannot be smaller than") 80 } 81 82 // Make sure we can set hostconfig options too 83 func (s *DockerSuite) TestCreateHostConfig(c *check.C) { 84 out, _ := dockerCmd(c, "create", "-P", "busybox", "echo") 85 86 cleanedContainerID := strings.TrimSpace(out) 87 88 out, _ = dockerCmd(c, "inspect", cleanedContainerID) 89 90 containers := []struct { 91 HostConfig *struct { 92 PublishAllPorts bool 93 } 94 }{} 95 96 err := json.Unmarshal([]byte(out), &containers) 97 c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err)) 98 c.Assert(containers, checker.HasLen, 1) 99 100 cont := containers[0] 101 c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none")) 102 c.Assert(cont.HostConfig.PublishAllPorts, check.NotNil, check.Commentf("Expected PublishAllPorts, got false")) 103 } 104 105 func (s *DockerSuite) TestCreateWithPortRange(c *check.C) { 106 out, _ := dockerCmd(c, "create", "-p", "3300-3303:3300-3303/tcp", "busybox", "echo") 107 108 cleanedContainerID := strings.TrimSpace(out) 109 110 out, _ = dockerCmd(c, "inspect", cleanedContainerID) 111 112 containers := []struct { 113 HostConfig *struct { 114 PortBindings map[nat.Port][]nat.PortBinding 115 } 116 }{} 117 err := json.Unmarshal([]byte(out), &containers) 118 c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err)) 119 c.Assert(containers, checker.HasLen, 1) 120 121 cont := containers[0] 122 123 c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none")) 124 c.Assert(cont.HostConfig.PortBindings, checker.HasLen, 4, check.Commentf("Expected 4 ports bindings, got %d", len(cont.HostConfig.PortBindings))) 125 126 for k, v := range cont.HostConfig.PortBindings { 127 c.Assert(v, checker.HasLen, 1, check.Commentf("Expected 1 ports binding, for the port %s but found %s", k, v)) 128 c.Assert(k.Port(), checker.Equals, v[0].HostPort, check.Commentf("Expected host port %s to match published port %s", k.Port(), v[0].HostPort)) 129 130 } 131 132 } 133 134 func (s *DockerSuite) TestCreateWithLargePortRange(c *check.C) { 135 out, _ := dockerCmd(c, "create", "-p", "1-65535:1-65535/tcp", "busybox", "echo") 136 137 cleanedContainerID := strings.TrimSpace(out) 138 139 out, _ = dockerCmd(c, "inspect", cleanedContainerID) 140 141 containers := []struct { 142 HostConfig *struct { 143 PortBindings map[nat.Port][]nat.PortBinding 144 } 145 }{} 146 147 err := json.Unmarshal([]byte(out), &containers) 148 c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err)) 149 c.Assert(containers, checker.HasLen, 1) 150 151 cont := containers[0] 152 c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none")) 153 c.Assert(cont.HostConfig.PortBindings, checker.HasLen, 65535) 154 155 for k, v := range cont.HostConfig.PortBindings { 156 c.Assert(v, checker.HasLen, 1) 157 c.Assert(k.Port(), checker.Equals, v[0].HostPort, check.Commentf("Expected host port %s to match published port %s", k.Port(), v[0].HostPort)) 158 } 159 160 } 161 162 // "test123" should be printed by docker create + start 163 func (s *DockerSuite) TestCreateEchoStdout(c *check.C) { 164 out, _ := dockerCmd(c, "create", "busybox", "echo", "test123") 165 166 cleanedContainerID := strings.TrimSpace(out) 167 168 out, _ = dockerCmd(c, "start", "-ai", cleanedContainerID) 169 c.Assert(out, checker.Equals, "test123\n", check.Commentf("container should've printed 'test123', got %q", out)) 170 171 } 172 173 func (s *DockerSuite) TestCreateVolumesCreated(c *check.C) { 174 testRequires(c, SameHostDaemon) 175 prefix, slash := getPrefixAndSlashFromDaemonPlatform() 176 177 name := "test_create_volume" 178 dockerCmd(c, "create", "--name", name, "-v", prefix+slash+"foo", "busybox") 179 180 dir, err := inspectMountSourceField(name, prefix+slash+"foo") 181 c.Assert(err, check.IsNil, check.Commentf("Error getting volume host path: %q", err)) 182 183 if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) { 184 c.Fatalf("Volume was not created") 185 } 186 if err != nil { 187 c.Fatalf("Error statting volume host path: %q", err) 188 } 189 190 } 191 192 func (s *DockerSuite) TestCreateLabels(c *check.C) { 193 name := "test_create_labels" 194 expected := map[string]string{"k1": "v1", "k2": "v2"} 195 dockerCmd(c, "create", "--name", name, "-l", "k1=v1", "--label", "k2=v2", "busybox") 196 197 actual := make(map[string]string) 198 inspectFieldAndUnmarshall(c, name, "Config.Labels", &actual) 199 200 if !reflect.DeepEqual(expected, actual) { 201 c.Fatalf("Expected %s got %s", expected, actual) 202 } 203 } 204 205 func (s *DockerSuite) TestCreateLabelFromImage(c *check.C) { 206 imageName := "testcreatebuildlabel" 207 _, err := buildImage(imageName, 208 `FROM busybox 209 LABEL k1=v1 k2=v2`, 210 true) 211 212 c.Assert(err, check.IsNil) 213 214 name := "test_create_labels_from_image" 215 expected := map[string]string{"k2": "x", "k3": "v3", "k1": "v1"} 216 dockerCmd(c, "create", "--name", name, "-l", "k2=x", "--label", "k3=v3", imageName) 217 218 actual := make(map[string]string) 219 inspectFieldAndUnmarshall(c, name, "Config.Labels", &actual) 220 221 if !reflect.DeepEqual(expected, actual) { 222 c.Fatalf("Expected %s got %s", expected, actual) 223 } 224 } 225 226 func (s *DockerSuite) TestCreateHostnameWithNumber(c *check.C) { 227 image := "busybox" 228 // Busybox on Windows does not implement hostname command 229 if testEnv.DaemonPlatform() == "windows" { 230 image = testEnv.MinimalBaseImage() 231 } 232 out, _ := dockerCmd(c, "run", "-h", "web.0", image, "hostname") 233 c.Assert(strings.TrimSpace(out), checker.Equals, "web.0", check.Commentf("hostname not set, expected `web.0`, got: %s", out)) 234 235 } 236 237 func (s *DockerSuite) TestCreateRM(c *check.C) { 238 // Test to make sure we can 'rm' a new container that is in 239 // "Created" state, and has ever been run. Test "rm -f" too. 240 241 // create a container 242 out, _ := dockerCmd(c, "create", "busybox") 243 cID := strings.TrimSpace(out) 244 245 dockerCmd(c, "rm", cID) 246 247 // Now do it again so we can "rm -f" this time 248 out, _ = dockerCmd(c, "create", "busybox") 249 250 cID = strings.TrimSpace(out) 251 dockerCmd(c, "rm", "-f", cID) 252 } 253 254 func (s *DockerSuite) TestCreateModeIpcContainer(c *check.C) { 255 // Uses Linux specific functionality (--ipc) 256 testRequires(c, DaemonIsLinux, SameHostDaemon) 257 258 out, _ := dockerCmd(c, "create", "busybox") 259 id := strings.TrimSpace(out) 260 261 dockerCmd(c, "create", fmt.Sprintf("--ipc=container:%s", id), "busybox") 262 } 263 264 func (s *DockerSuite) TestCreateByImageID(c *check.C) { 265 imageName := "testcreatebyimageid" 266 imageID, err := buildImage(imageName, 267 `FROM busybox 268 MAINTAINER dockerio`, 269 true) 270 if err != nil { 271 c.Fatal(err) 272 } 273 truncatedImageID := stringid.TruncateID(imageID) 274 275 dockerCmd(c, "create", imageID) 276 dockerCmd(c, "create", truncatedImageID) 277 dockerCmd(c, "create", fmt.Sprintf("%s:%s", imageName, truncatedImageID)) 278 279 // Ensure this fails 280 out, exit, _ := dockerCmdWithError("create", fmt.Sprintf("%s:%s", imageName, imageID)) 281 if exit == 0 { 282 c.Fatalf("expected non-zero exit code; received %d", exit) 283 } 284 285 if expected := "Error parsing reference"; !strings.Contains(out, expected) { 286 c.Fatalf(`Expected %q in output; got: %s`, expected, out) 287 } 288 289 out, exit, _ = dockerCmdWithError("create", fmt.Sprintf("%s:%s", "wrongimage", truncatedImageID)) 290 if exit == 0 { 291 c.Fatalf("expected non-zero exit code; received %d", exit) 292 } 293 294 if expected := "Unable to find image"; !strings.Contains(out, expected) { 295 c.Fatalf(`Expected %q in output; got: %s`, expected, out) 296 } 297 } 298 299 func (s *DockerTrustSuite) TestTrustedCreate(c *check.C) { 300 repoName := s.setupTrustedImage(c, "trusted-create") 301 302 // Try create 303 icmd.RunCmd(icmd.Command(dockerBinary, "create", repoName), trustedCmd).Assert(c, SuccessTagging) 304 305 dockerCmd(c, "rmi", repoName) 306 307 // Try untrusted create to ensure we pushed the tag to the registry 308 icmd.RunCmd(icmd.Command(dockerBinary, "create", "--disable-content-trust=true", repoName), trustedCmd).Assert(c, SuccessDownloadedOnStderr) 309 } 310 311 func (s *DockerTrustSuite) TestUntrustedCreate(c *check.C) { 312 repoName := fmt.Sprintf("%v/dockercliuntrusted/createtest", privateRegistryURL) 313 withTagName := fmt.Sprintf("%s:latest", repoName) 314 // tag the image and upload it to the private registry 315 dockerCmd(c, "tag", "busybox", withTagName) 316 dockerCmd(c, "push", withTagName) 317 dockerCmd(c, "rmi", withTagName) 318 319 // Try trusted create on untrusted tag 320 icmd.RunCmd(icmd.Command(dockerBinary, "create", withTagName), trustedCmd).Assert(c, icmd.Expected{ 321 ExitCode: 1, 322 Err: fmt.Sprintf("does not have trust data for %s", repoName), 323 }) 324 } 325 326 func (s *DockerTrustSuite) TestTrustedIsolatedCreate(c *check.C) { 327 repoName := s.setupTrustedImage(c, "trusted-isolated-create") 328 329 // Try create 330 icmd.RunCmd(icmd.Command(dockerBinary, "--config", "/tmp/docker-isolated-create", "create", repoName), trustedCmd).Assert(c, SuccessTagging) 331 332 dockerCmd(c, "rmi", repoName) 333 } 334 335 func (s *DockerTrustSuite) TestCreateWhenCertExpired(c *check.C) { 336 c.Skip("Currently changes system time, causing instability") 337 repoName := s.setupTrustedImage(c, "trusted-create-expired") 338 339 // Certificates have 10 years of expiration 340 elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11) 341 342 testutil.RunAtDifferentDate(elevenYearsFromNow, func() { 343 // Try create 344 icmd.RunCmd(icmd.Cmd{ 345 Command: []string{dockerBinary, "create", repoName}, 346 }, trustedCmd).Assert(c, icmd.Expected{ 347 ExitCode: 1, 348 Err: "could not validate the path to a trusted root", 349 }) 350 }) 351 352 testutil.RunAtDifferentDate(elevenYearsFromNow, func() { 353 // Try create 354 result := icmd.RunCmd(icmd.Command(dockerBinary, "create", "--disable-content-trust", repoName), trustedCmd) 355 c.Assert(result.Error, check.Not(check.IsNil)) 356 c.Assert(string(result.Combined()), checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted create in the distant future:\n%s", result.Combined())) 357 358 }) 359 } 360 361 func (s *DockerTrustSuite) TestTrustedCreateFromBadTrustServer(c *check.C) { 362 repoName := fmt.Sprintf("%v/dockerclievilcreate/trusted:latest", privateRegistryURL) 363 evilLocalConfigDir, err := ioutil.TempDir("", "evilcreate-local-config-dir") 364 c.Assert(err, check.IsNil) 365 366 // tag the image and upload it to the private registry 367 dockerCmd(c, "tag", "busybox", repoName) 368 369 icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing) 370 371 dockerCmd(c, "rmi", repoName) 372 373 // Try create 374 icmd.RunCmd(icmd.Command(dockerBinary, "create", repoName), trustedCmd).Assert(c, SuccessTagging) 375 376 dockerCmd(c, "rmi", repoName) 377 378 // Kill the notary server, start a new "evil" one. 379 s.not.Close() 380 s.not, err = newTestNotary(c) 381 c.Assert(err, check.IsNil) 382 383 // In order to make an evil server, lets re-init a client (with a different trust dir) and push new data. 384 // tag an image and upload it to the private registry 385 dockerCmd(c, "--config", evilLocalConfigDir, "tag", "busybox", repoName) 386 387 // Push up to the new server 388 icmd.RunCmd(icmd.Command(dockerBinary, "--config", evilLocalConfigDir, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing) 389 390 // Now, try creating with the original client from this new trust server. This should fail because the new root is invalid. 391 icmd.RunCmd(icmd.Command(dockerBinary, "create", repoName), trustedCmd).Assert(c, icmd.Expected{ 392 ExitCode: 1, 393 Err: "could not rotate trust to a new trusted root", 394 }) 395 } 396 397 func (s *DockerSuite) TestCreateStopSignal(c *check.C) { 398 name := "test_create_stop_signal" 399 dockerCmd(c, "create", "--name", name, "--stop-signal", "9", "busybox") 400 401 res := inspectFieldJSON(c, name, "Config.StopSignal") 402 c.Assert(res, checker.Contains, "9") 403 404 } 405 406 func (s *DockerSuite) TestCreateWithWorkdir(c *check.C) { 407 name := "foo" 408 409 prefix, slash := getPrefixAndSlashFromDaemonPlatform() 410 dir := prefix + slash + "home" + slash + "foo" + slash + "bar" 411 412 dockerCmd(c, "create", "--name", name, "-w", dir, "busybox") 413 // Windows does not create the workdir until the container is started 414 if testEnv.DaemonPlatform() == "windows" { 415 dockerCmd(c, "start", name) 416 } 417 dockerCmd(c, "cp", fmt.Sprintf("%s:%s", name, dir), prefix+slash+"tmp") 418 } 419 420 func (s *DockerSuite) TestCreateWithInvalidLogOpts(c *check.C) { 421 name := "test-invalidate-log-opts" 422 out, _, err := dockerCmdWithError("create", "--name", name, "--log-opt", "invalid=true", "busybox") 423 c.Assert(err, checker.NotNil) 424 c.Assert(out, checker.Contains, "unknown log opt") 425 426 out, _ = dockerCmd(c, "ps", "-a") 427 c.Assert(out, checker.Not(checker.Contains), name) 428 } 429 430 // #20972 431 func (s *DockerSuite) TestCreate64ByteHexID(c *check.C) { 432 out := inspectField(c, "busybox", "Id") 433 imageID := strings.TrimPrefix(strings.TrimSpace(string(out)), "sha256:") 434 435 dockerCmd(c, "create", imageID) 436 } 437 438 // Test case for #23498 439 func (s *DockerSuite) TestCreateUnsetEntrypoint(c *check.C) { 440 name := "test-entrypoint" 441 dockerfile := `FROM busybox 442 ADD entrypoint.sh /entrypoint.sh 443 RUN chmod 755 /entrypoint.sh 444 ENTRYPOINT ["/entrypoint.sh"] 445 CMD echo foobar` 446 447 ctx, err := fakeContext(dockerfile, map[string]string{ 448 "entrypoint.sh": `#!/bin/sh 449 echo "I am an entrypoint" 450 exec "$@"`, 451 }) 452 c.Assert(err, check.IsNil) 453 defer ctx.Close() 454 455 _, err = buildImageFromContext(name, ctx, true) 456 c.Assert(err, check.IsNil) 457 458 out, _ := dockerCmd(c, "create", "--entrypoint=", name, "echo", "foo") 459 id := strings.TrimSpace(out) 460 c.Assert(id, check.Not(check.Equals), "") 461 out, _ = dockerCmd(c, "start", "-a", id) 462 c.Assert(strings.TrimSpace(out), check.Equals, "foo") 463 } 464 465 // #22471 466 func (s *DockerSuite) TestCreateStopTimeout(c *check.C) { 467 name1 := "test_create_stop_timeout_1" 468 dockerCmd(c, "create", "--name", name1, "--stop-timeout", "15", "busybox") 469 470 res := inspectFieldJSON(c, name1, "Config.StopTimeout") 471 c.Assert(res, checker.Contains, "15") 472 473 name2 := "test_create_stop_timeout_2" 474 dockerCmd(c, "create", "--name", name2, "busybox") 475 476 res = inspectFieldJSON(c, name2, "Config.StopTimeout") 477 c.Assert(res, checker.Contains, "null") 478 }