integration-cli/docker_cli_create_test.go
b0cb37fd
 package main
 
 import (
 	"encoding/json"
84aae5a2
 	"fmt"
7107898d
 	"os"
abb5e9a0
 	"reflect"
475c6531
 	"strings"
b0cb37fd
 	"time"
321874f3
 
eeb6d0a7
 	"os/exec"
 
3e90b12d
 	"io/ioutil"
 
91041884
 	"github.com/docker/docker/pkg/integration/checker"
4352da78
 	"github.com/docker/docker/pkg/stringid"
056e7449
 	"github.com/docker/go-connections/nat"
dc944ea7
 	"github.com/go-check/check"
b0cb37fd
 )
 
 // Make sure we can create a simple container with some args
dc944ea7
 func (s *DockerSuite) TestCreateArgs(c *check.C) {
5e3fdd3c
 	// TODO Windows. This requires further investigation for porting to
 	// Windows CI. Currently fails.
 	if daemonPlatform == "windows" {
 		c.Skip("Fails on Windows CI")
 	}
5c295460
 	out, _ := dockerCmd(c, "create", "busybox", "command", "arg1", "arg2", "arg with space")
b0cb37fd
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
b0cb37fd
 
5c295460
 	out, _ = dockerCmd(c, "inspect", cleanedContainerID)
b0cb37fd
 
 	containers := []struct {
 		ID      string
 		Created time.Time
 		Path    string
 		Args    []string
 		Image   string
 	}{}
91041884
 
 	err := json.Unmarshal([]byte(out), &containers)
 	c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
 	c.Assert(containers, checker.HasLen, 1)
b0cb37fd
 
dc944ea7
 	cont := containers[0]
91041884
 	c.Assert(string(cont.Path), checker.Equals, "command", check.Commentf("Unexpected container path. Expected command, received: %s", cont.Path))
b0cb37fd
 
 	b := false
 	expected := []string{"arg1", "arg2", "arg with space"}
 	for i, arg := range expected {
dc944ea7
 		if arg != cont.Args[i] {
b0cb37fd
 			b = true
 			break
 		}
 	}
dc944ea7
 	if len(cont.Args) != len(expected) || b {
 		c.Fatalf("Unexpected args. Expected %v, received: %v", expected, cont.Args)
b0cb37fd
 	}
 
 }
 
 // Make sure we can set hostconfig options too
dc944ea7
 func (s *DockerSuite) TestCreateHostConfig(c *check.C) {
5c295460
 	out, _ := dockerCmd(c, "create", "-P", "busybox", "echo")
b0cb37fd
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
b0cb37fd
 
5c295460
 	out, _ = dockerCmd(c, "inspect", cleanedContainerID)
b0cb37fd
 
 	containers := []struct {
 		HostConfig *struct {
 			PublishAllPorts bool
 		}
 	}{}
 
91041884
 	err := json.Unmarshal([]byte(out), &containers)
 	c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
 	c.Assert(containers, checker.HasLen, 1)
b0cb37fd
 
91041884
 	cont := containers[0]
 	c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none"))
 	c.Assert(cont.HostConfig.PublishAllPorts, check.NotNil, check.Commentf("Expected PublishAllPorts, got false"))
b0cb37fd
 }
 
dc944ea7
 func (s *DockerSuite) TestCreateWithPortRange(c *check.C) {
5e3fdd3c
 	// Windows does not currently support port ranges.
f9a3558a
 	testRequires(c, DaemonIsLinux)
5c295460
 	out, _ := dockerCmd(c, "create", "-p", "3300-3303:3300-3303/tcp", "busybox", "echo")
2338a9cf
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
2338a9cf
 
5c295460
 	out, _ = dockerCmd(c, "inspect", cleanedContainerID)
2338a9cf
 
 	containers := []struct {
 		HostConfig *struct {
 			PortBindings map[nat.Port][]nat.PortBinding
 		}
 	}{}
91041884
 	err := json.Unmarshal([]byte(out), &containers)
 	c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
 	c.Assert(containers, checker.HasLen, 1)
2338a9cf
 
dc944ea7
 	cont := containers[0]
2338a9cf
 
91041884
 	c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none"))
 	c.Assert(cont.HostConfig.PortBindings, checker.HasLen, 4, check.Commentf("Expected 4 ports bindings, got %d", len(cont.HostConfig.PortBindings)))
 
dc944ea7
 	for k, v := range cont.HostConfig.PortBindings {
91041884
 		c.Assert(v, checker.HasLen, 1, check.Commentf("Expected 1 ports binding, for the port  %s but found %s", k, v))
 		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))
 
2338a9cf
 	}
 
 }
 
5e3fdd3c
 func (s *DockerSuite) TestCreateWithLargePortRange(c *check.C) {
 	// Windows does not currently support port ranges.
f9a3558a
 	testRequires(c, DaemonIsLinux)
5c295460
 	out, _ := dockerCmd(c, "create", "-p", "1-65535:1-65535/tcp", "busybox", "echo")
2338a9cf
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
2338a9cf
 
5c295460
 	out, _ = dockerCmd(c, "inspect", cleanedContainerID)
2338a9cf
 
 	containers := []struct {
 		HostConfig *struct {
 			PortBindings map[nat.Port][]nat.PortBinding
 		}
 	}{}
91041884
 
 	err := json.Unmarshal([]byte(out), &containers)
 	c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
 	c.Assert(containers, checker.HasLen, 1)
2338a9cf
 
dc944ea7
 	cont := containers[0]
91041884
 	c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none"))
 	c.Assert(cont.HostConfig.PortBindings, checker.HasLen, 65535)
2338a9cf
 
dc944ea7
 	for k, v := range cont.HostConfig.PortBindings {
91041884
 		c.Assert(v, checker.HasLen, 1)
 		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))
2338a9cf
 	}
 
 }
 
b0cb37fd
 // "test123" should be printed by docker create + start
dc944ea7
 func (s *DockerSuite) TestCreateEchoStdout(c *check.C) {
5c295460
 	out, _ := dockerCmd(c, "create", "busybox", "echo", "test123")
b0cb37fd
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
b0cb37fd
 
5c295460
 	out, _ = dockerCmd(c, "start", "-ai", cleanedContainerID)
91041884
 	c.Assert(out, checker.Equals, "test123\n", check.Commentf("container should've printed 'test123', got %q", out))
b0cb37fd
 
 }
7107898d
 
dc944ea7
 func (s *DockerSuite) TestCreateVolumesCreated(c *check.C) {
 	testRequires(c, SameHostDaemon)
5e3fdd3c
 	prefix := "/"
 	if daemonPlatform == "windows" {
 		prefix = `c:\`
 	}
70407ce4
 
7107898d
 	name := "test_create_volume"
5e3fdd3c
 	dockerCmd(c, "create", "--name", name, "-v", prefix+"foo", "busybox")
5c295460
 
5e3fdd3c
 	dir, err := inspectMountSourceField(name, prefix+"foo")
91041884
 	c.Assert(err, check.IsNil, check.Commentf("Error getting volume host path: %q", err))
7107898d
 
 	if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
dc944ea7
 		c.Fatalf("Volume was not created")
7107898d
 	}
 	if err != nil {
dc944ea7
 		c.Fatalf("Error statting volume host path: %q", err)
7107898d
 	}
 
 }
abb5e9a0
 
dc944ea7
 func (s *DockerSuite) TestCreateLabels(c *check.C) {
abb5e9a0
 	name := "test_create_labels"
 	expected := map[string]string{"k1": "v1", "k2": "v2"}
5c295460
 	dockerCmd(c, "create", "--name", name, "-l", "k1=v1", "--label", "k2=v2", "busybox")
abb5e9a0
 
 	actual := make(map[string]string)
62a856e9
 	inspectFieldAndMarshall(c, name, "Config.Labels", &actual)
abb5e9a0
 
 	if !reflect.DeepEqual(expected, actual) {
dc944ea7
 		c.Fatalf("Expected %s got %s", expected, actual)
abb5e9a0
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestCreateLabelFromImage(c *check.C) {
abb5e9a0
 	imageName := "testcreatebuildlabel"
 	_, err := buildImage(imageName,
 		`FROM busybox
 		LABEL k1=v1 k2=v2`,
 		true)
91041884
 
 	c.Assert(err, check.IsNil)
abb5e9a0
 
 	name := "test_create_labels_from_image"
8b91b3cf
 	expected := map[string]string{"k2": "x", "k3": "v3", "k1": "v1"}
5c295460
 	dockerCmd(c, "create", "--name", name, "-l", "k2=x", "--label", "k3=v3", imageName)
abb5e9a0
 
 	actual := make(map[string]string)
62a856e9
 	inspectFieldAndMarshall(c, name, "Config.Labels", &actual)
abb5e9a0
 
 	if !reflect.DeepEqual(expected, actual) {
dc944ea7
 		c.Fatalf("Expected %s got %s", expected, actual)
abb5e9a0
 	}
 }
2c24a8a4
 
dc944ea7
 func (s *DockerSuite) TestCreateHostnameWithNumber(c *check.C) {
5e3fdd3c
 	// TODO Windows. Consider enabling this in TP5 timeframe if Windows support
 	// is fully hooked up. The hostname is passed through, but only to the
 	// environment variable "COMPUTERNAME". It is not hooked up to hostname.exe
 	// or returned in ipconfig. Needs platform support in networking.
f9a3558a
 	testRequires(c, DaemonIsLinux)
dc944ea7
 	out, _ := dockerCmd(c, "run", "-h", "web.0", "busybox", "hostname")
91041884
 	c.Assert(strings.TrimSpace(out), checker.Equals, "web.0", check.Commentf("hostname not set, expected `web.0`, got: %s", out))
 
2c24a8a4
 }
012e588a
 
 func (s *DockerSuite) TestCreateRM(c *check.C) {
 	// Test to make sure we can 'rm' a new container that is in
 	// "Created" state, and has ever been run. Test "rm -f" too.
 
 	// create a container
5c295460
 	out, _ := dockerCmd(c, "create", "busybox")
012e588a
 	cID := strings.TrimSpace(out)
 
5c295460
 	dockerCmd(c, "rm", cID)
012e588a
 
 	// Now do it again so we can "rm -f" this time
5c295460
 	out, _ = dockerCmd(c, "create", "busybox")
012e588a
 
 	cID = strings.TrimSpace(out)
5c295460
 	dockerCmd(c, "rm", "-f", cID)
012e588a
 }
84aae5a2
 
 func (s *DockerSuite) TestCreateModeIpcContainer(c *check.C) {
5e3fdd3c
 	// Uses Linux specific functionality (--ipc)
f9a3558a
 	testRequires(c, DaemonIsLinux)
ea3afdad
 	testRequires(c, SameHostDaemon, NotUserNamespace)
84aae5a2
 
5c295460
 	out, _ := dockerCmd(c, "create", "busybox")
84aae5a2
 	id := strings.TrimSpace(out)
 
5c295460
 	dockerCmd(c, "create", fmt.Sprintf("--ipc=container:%s", id), "busybox")
84aae5a2
 }
1406cb35
 
4352da78
 func (s *DockerSuite) TestCreateByImageID(c *check.C) {
 	imageName := "testcreatebyimageid"
 	imageID, err := buildImage(imageName,
 		`FROM busybox
 		MAINTAINER dockerio`,
 		true)
 	if err != nil {
 		c.Fatal(err)
 	}
 	truncatedImageID := stringid.TruncateID(imageID)
 
 	dockerCmd(c, "create", imageID)
 	dockerCmd(c, "create", truncatedImageID)
 	dockerCmd(c, "create", fmt.Sprintf("%s:%s", imageName, truncatedImageID))
 
 	// Ensure this fails
 	out, exit, _ := dockerCmdWithError("create", fmt.Sprintf("%s:%s", imageName, imageID))
 	if exit == 0 {
 		c.Fatalf("expected non-zero exit code; received %d", exit)
 	}
 
15d84a3a
 	if expected := "Error parsing reference"; !strings.Contains(out, expected) {
4352da78
 		c.Fatalf(`Expected %q in output; got: %s`, expected, out)
 	}
 
 	out, exit, _ = dockerCmdWithError("create", fmt.Sprintf("%s:%s", "wrongimage", truncatedImageID))
 	if exit == 0 {
 		c.Fatalf("expected non-zero exit code; received %d", exit)
 	}
 
 	if expected := "Unable to find image"; !strings.Contains(out, expected) {
 		c.Fatalf(`Expected %q in output; got: %s`, expected, out)
 	}
 }
 
1406cb35
 func (s *DockerTrustSuite) TestTrustedCreate(c *check.C) {
871d2b96
 	repoName := s.setupTrustedImage(c, "trusted-create")
1406cb35
 
 	// Try create
 	createCmd := exec.Command(dockerBinary, "create", repoName)
 	s.trustedCmd(createCmd)
871d2b96
 	out, _, err := runCommandWithOutput(createCmd)
91041884
 	c.Assert(err, check.IsNil)
 	c.Assert(string(out), checker.Contains, "Tagging", check.Commentf("Missing expected output on trusted push:\n%s", out))
1406cb35
 
 	dockerCmd(c, "rmi", repoName)
 
 	// Try untrusted create to ensure we pushed the tag to the registry
259cadb0
 	createCmd = exec.Command(dockerBinary, "create", "--disable-content-trust=true", repoName)
1406cb35
 	s.trustedCmd(createCmd)
 	out, _, err = runCommandWithOutput(createCmd)
91041884
 	c.Assert(err, check.IsNil)
 	c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted create with --disable-content-trust:\n%s", out))
1406cb35
 
 }
 
 func (s *DockerTrustSuite) TestUntrustedCreate(c *check.C) {
0617521b
 	repoName := fmt.Sprintf("%v/dockercliuntrusted/createtest", privateRegistryURL)
 	withTagName := fmt.Sprintf("%s:latest", repoName)
1406cb35
 	// tag the image and upload it to the private registry
0617521b
 	dockerCmd(c, "tag", "busybox", withTagName)
 	dockerCmd(c, "push", withTagName)
 	dockerCmd(c, "rmi", withTagName)
1406cb35
 
 	// Try trusted create on untrusted tag
0617521b
 	createCmd := exec.Command(dockerBinary, "create", withTagName)
1406cb35
 	s.trustedCmd(createCmd)
 	out, _, err := runCommandWithOutput(createCmd)
91041884
 	c.Assert(err, check.Not(check.IsNil))
0617521b
 	c.Assert(string(out), checker.Contains, fmt.Sprintf("does not have trust data for %s", repoName), check.Commentf("Missing expected output on trusted create:\n%s", out))
1406cb35
 
 }
 
871d2b96
 func (s *DockerTrustSuite) TestTrustedIsolatedCreate(c *check.C) {
 	repoName := s.setupTrustedImage(c, "trusted-isolated-create")
1406cb35
 
871d2b96
 	// Try create
 	createCmd := exec.Command(dockerBinary, "--config", "/tmp/docker-isolated-create", "create", repoName)
 	s.trustedCmd(createCmd)
 	out, _, err := runCommandWithOutput(createCmd)
91041884
 	c.Assert(err, check.IsNil)
 	c.Assert(string(out), checker.Contains, "Tagging", check.Commentf("Missing expected output on trusted push:\n%s", out))
1406cb35
 
 	dockerCmd(c, "rmi", repoName)
871d2b96
 }
 
 func (s *DockerTrustSuite) TestCreateWhenCertExpired(c *check.C) {
bf3c1e6a
 	c.Skip("Currently changes system time, causing instability")
871d2b96
 	repoName := s.setupTrustedImage(c, "trusted-create-expired")
1406cb35
 
 	// Certificates have 10 years of expiration
 	elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
 
 	runAtDifferentDate(elevenYearsFromNow, func() {
 		// Try create
 		createCmd := exec.Command(dockerBinary, "create", repoName)
 		s.trustedCmd(createCmd)
871d2b96
 		out, _, err := runCommandWithOutput(createCmd)
91041884
 		c.Assert(err, check.Not(check.IsNil))
 		c.Assert(string(out), checker.Contains, "could not validate the path to a trusted root", check.Commentf("Missing expected output on trusted create in the distant future:\n%s", out))
1406cb35
 	})
 
 	runAtDifferentDate(elevenYearsFromNow, func() {
 		// Try create
259cadb0
 		createCmd := exec.Command(dockerBinary, "create", "--disable-content-trust", repoName)
1406cb35
 		s.trustedCmd(createCmd)
871d2b96
 		out, _, err := runCommandWithOutput(createCmd)
91041884
 		c.Assert(err, check.Not(check.IsNil))
 		c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted create in the distant future:\n%s", out))
1406cb35
 
 	})
 }
268fa5af
 
 func (s *DockerTrustSuite) TestTrustedCreateFromBadTrustServer(c *check.C) {
 	repoName := fmt.Sprintf("%v/dockerclievilcreate/trusted:latest", privateRegistryURL)
84dc2d9e
 	evilLocalConfigDir, err := ioutil.TempDir("", "evilcreate-local-config-dir")
91041884
 	c.Assert(err, check.IsNil)
268fa5af
 
 	// tag the image and upload it to the private registry
 	dockerCmd(c, "tag", "busybox", repoName)
 
 	pushCmd := exec.Command(dockerBinary, "push", repoName)
 	s.trustedCmd(pushCmd)
 	out, _, err := runCommandWithOutput(pushCmd)
91041884
 	c.Assert(err, check.IsNil)
 	c.Assert(string(out), checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push:\n%s", out))
268fa5af
 
 	dockerCmd(c, "rmi", repoName)
 
 	// Try create
 	createCmd := exec.Command(dockerBinary, "create", repoName)
 	s.trustedCmd(createCmd)
 	out, _, err = runCommandWithOutput(createCmd)
91041884
 	c.Assert(err, check.IsNil)
 	c.Assert(string(out), checker.Contains, "Tagging", check.Commentf("Missing expected output on trusted push:\n%s", out))
268fa5af
 
 	dockerCmd(c, "rmi", repoName)
 
 	// Kill the notary server, start a new "evil" one.
 	s.not.Close()
 	s.not, err = newTestNotary(c)
91041884
 	c.Assert(err, check.IsNil)
268fa5af
 
 	// In order to make an evil server, lets re-init a client (with a different trust dir) and push new data.
 	// tag an image and upload it to the private registry
 	dockerCmd(c, "--config", evilLocalConfigDir, "tag", "busybox", repoName)
 
 	// Push up to the new server
 	pushCmd = exec.Command(dockerBinary, "--config", evilLocalConfigDir, "push", repoName)
 	s.trustedCmd(pushCmd)
 	out, _, err = runCommandWithOutput(pushCmd)
91041884
 	c.Assert(err, check.IsNil)
 	c.Assert(string(out), checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push:\n%s", out))
268fa5af
 
84dc2d9e
 	// Now, try creating with the original client from this new trust server. This should fallback to our cached timestamp and metadata.
268fa5af
 	createCmd = exec.Command(dockerBinary, "create", repoName)
 	s.trustedCmd(createCmd)
 	out, _, err = runCommandWithOutput(createCmd)
84dc2d9e
 	if err != nil {
 		c.Fatalf("Error falling back to cached trust data: %s\n%s", err, out)
 	}
 	if !strings.Contains(string(out), "Error while downloading remote metadata, using cached timestamp") {
 		c.Fatalf("Missing expected output on trusted create:\n%s", out)
 	}
268fa5af
 
 }
3781cde6
 
 func (s *DockerSuite) TestCreateStopSignal(c *check.C) {
 	name := "test_create_stop_signal"
 	dockerCmd(c, "create", "--name", name, "--stop-signal", "9", "busybox")
 
62a856e9
 	res := inspectFieldJSON(c, name, "Config.StopSignal")
91041884
 	c.Assert(res, checker.Contains, "9")
3781cde6
 
 }
cde0ed67
 
 func (s *DockerSuite) TestCreateWithWorkdir(c *check.C) {
5e3fdd3c
 	// TODO Windows. This requires further investigation for porting to
 	// Windows CI. Currently fails.
 	if daemonPlatform == "windows" {
 		c.Skip("Fails on Windows CI")
 	}
cde0ed67
 	name := "foo"
382c96ee
 
 	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
5e3fdd3c
 	dir := prefix + slash + "home" + slash + "foo" + slash + "bar"
 
cde0ed67
 	dockerCmd(c, "create", "--name", name, "-w", dir, "busybox")
5e3fdd3c
 	dockerCmd(c, "cp", fmt.Sprintf("%s:%s", name, dir), prefix+slash+"tmp")
cde0ed67
 }
7285c9a5
 
 func (s *DockerSuite) TestCreateWithInvalidLogOpts(c *check.C) {
 	name := "test-invalidate-log-opts"
06808500
 	out, _, err := dockerCmdWithError("create", "--name", name, "--log-opt", "invalid=true", "busybox")
7285c9a5
 	c.Assert(err, checker.NotNil)
06808500
 	c.Assert(out, checker.Contains, "unknown log opt")
 
 	out, _ = dockerCmd(c, "ps", "-a")
7285c9a5
 	c.Assert(out, checker.Not(checker.Contains), name)
 }
16e4c4e4
 
 // #20972
 func (s *DockerSuite) TestCreate64ByteHexID(c *check.C) {
 	out := inspectField(c, "busybox", "Id")
 	imageID := strings.TrimPrefix(strings.TrimSpace(string(out)), "sha256:")
 
 	dockerCmd(c, "create", imageID)
 }