integration-cli/docker_cli_commit_test.go
6db32fde
 package main
 
 import (
6beb858f
 	"strings"
dc944ea7
 
483abbb3
 	"github.com/docker/docker/pkg/integration/checker"
dc944ea7
 	"github.com/go-check/check"
6db32fde
 )
 
dc944ea7
 func (s *DockerSuite) TestCommitAfterContainerIsDone(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
5c295460
 	out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
6db32fde
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
6db32fde
 
5c295460
 	dockerCmd(c, "wait", cleanedContainerID)
6db32fde
 
5c295460
 	out, _ = dockerCmd(c, "commit", cleanedContainerID)
6db32fde
 
475c6531
 	cleanedImageID := strings.TrimSpace(out)
6db32fde
 
5c295460
 	dockerCmd(c, "inspect", cleanedImageID)
17d870be
 }
 
dc944ea7
 func (s *DockerSuite) TestCommitWithoutPause(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
5c295460
 	out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
17d870be
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
17d870be
 
5c295460
 	dockerCmd(c, "wait", cleanedContainerID)
17d870be
 
5c295460
 	out, _ = dockerCmd(c, "commit", "-p=false", cleanedContainerID)
17d870be
 
475c6531
 	cleanedImageID := strings.TrimSpace(out)
17d870be
 
5c295460
 	dockerCmd(c, "inspect", cleanedImageID)
6db32fde
 }
6beb858f
 
7c7c7f84
 //test commit a paused container should not unpause it after commit
dc944ea7
 func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
7c7c7f84
 	defer unpauseAllContainers()
5c295460
 	out, _ := dockerCmd(c, "run", "-i", "-d", "busybox")
7c7c7f84
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
7c7c7f84
 
5c295460
 	dockerCmd(c, "pause", cleanedContainerID)
 
 	out, _ = dockerCmd(c, "commit", cleanedContainerID)
7c7c7f84
 
62a856e9
 	out = inspectField(c, cleanedContainerID, "State.Paused")
483abbb3
 	// commit should not unpause a paused container
 	c.Assert(out, checker.Contains, "true")
7c7c7f84
 }
 
dc944ea7
 func (s *DockerSuite) TestCommitNewFile(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
5c295460
 	dockerCmd(c, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
6beb858f
 
5c295460
 	imageID, _ := dockerCmd(c, "commit", "commiter")
483abbb3
 	imageID = strings.TrimSpace(imageID)
6beb858f
 
5c295460
 	out, _ := dockerCmd(c, "run", imageID, "cat", "/foo")
483abbb3
 	actual := strings.TrimSpace(out)
 	c.Assert(actual, checker.Equals, "koye")
6beb858f
 }
2c8b63cb
 
dc944ea7
 func (s *DockerSuite) TestCommitHardlink(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
5c295460
 	firstOutput, _ := dockerCmd(c, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
f9f80443
 
5c295460
 	chunks := strings.Split(strings.TrimSpace(firstOutput), " ")
f9f80443
 	inode := chunks[0]
483abbb3
 	chunks = strings.SplitAfterN(strings.TrimSpace(firstOutput), " ", 2)
 	c.Assert(chunks[1], checker.Contains, chunks[0], check.Commentf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:]))
f9f80443
 
5c295460
 	imageID, _ := dockerCmd(c, "commit", "hardlinks", "hardlinks")
483abbb3
 	imageID = strings.TrimSpace(imageID)
f9f80443
 
5c295460
 	secondOutput, _ := dockerCmd(c, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
f9f80443
 
5c295460
 	chunks = strings.Split(strings.TrimSpace(secondOutput), " ")
f9f80443
 	inode = chunks[0]
483abbb3
 	chunks = strings.SplitAfterN(strings.TrimSpace(secondOutput), " ", 2)
 	c.Assert(chunks[1], checker.Contains, chunks[0], check.Commentf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:]))
f9f80443
 }
 
dc944ea7
 func (s *DockerSuite) TestCommitTTY(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
5c295460
 	dockerCmd(c, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
2c8b63cb
 
5c295460
 	imageID, _ := dockerCmd(c, "commit", "tty", "ttytest")
483abbb3
 	imageID = strings.TrimSpace(imageID)
2c8b63cb
 
5c295460
 	dockerCmd(c, "run", "ttytest", "/bin/ls")
2c8b63cb
 }
d31c37fc
 
dc944ea7
 func (s *DockerSuite) TestCommitWithHostBindMount(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
5c295460
 	dockerCmd(c, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
3182ee5c
 
5c295460
 	imageID, _ := dockerCmd(c, "commit", "bind-commit", "bindtest")
483abbb3
 	imageID = strings.TrimSpace(imageID)
d31c37fc
 
5c295460
 	dockerCmd(c, "run", "bindtest", "true")
d31c37fc
 }
b30257cc
 
dc944ea7
 func (s *DockerSuite) TestCommitChange(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
5c295460
 	dockerCmd(c, "run", "--name", "test", "busybox", "true")
17abfc3d
 
5c295460
 	imageID, _ := dockerCmd(c, "commit",
b30257cc
 		"--change", "EXPOSE 8080",
 		"--change", "ENV DEBUG true",
4a9fa965
 		"--change", "ENV test 1",
c1b77921
 		"--change", "ENV PATH /foo",
6e2662b3
 		"--change", "LABEL foo bar",
 		"--change", "CMD [\"/bin/sh\"]",
 		"--change", "WORKDIR /opt",
 		"--change", "ENTRYPOINT [\"/bin/sh\"]",
 		"--change", "USER testuser",
 		"--change", "VOLUME /var/lib/docker",
 		"--change", "ONBUILD /usr/local/bin/python-build --dir /app/src",
b30257cc
 		"test", "test-commit")
483abbb3
 	imageID = strings.TrimSpace(imageID)
b30257cc
 
 	expected := map[string]string{
231d362d
 		"Config.ExposedPorts": "map[8080/tcp:{}]",
c1b77921
 		"Config.Env":          "[DEBUG=true test=1 PATH=/foo]",
6e2662b3
 		"Config.Labels":       "map[foo:bar]",
53b0d626
 		"Config.Cmd":          "[/bin/sh]",
6e2662b3
 		"Config.WorkingDir":   "/opt",
53b0d626
 		"Config.Entrypoint":   "[/bin/sh]",
6e2662b3
 		"Config.User":         "testuser",
 		"Config.Volumes":      "map[/var/lib/docker:{}]",
 		"Config.OnBuild":      "[/usr/local/bin/python-build --dir /app/src]",
b30257cc
 	}
 
 	for conf, value := range expected {
62a856e9
 		res := inspectField(c, imageID, conf)
b30257cc
 		if res != value {
dc944ea7
 			c.Errorf("%s('%s'), expected %s", conf, res, value)
b30257cc
 		}
 	}
 }
ed6074ea
 
 // TODO: commit --run is deprecated, remove this once --run is removed
dc944ea7
 func (s *DockerSuite) TestCommitMergeConfigRun(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
ed6074ea
 	name := "commit-test"
dc944ea7
 	out, _ := dockerCmd(c, "run", "-d", "-e=FOO=bar", "busybox", "/bin/sh", "-c", "echo testing > /tmp/foo")
ed6074ea
 	id := strings.TrimSpace(out)
 
dc944ea7
 	dockerCmd(c, "commit", `--run={"Cmd": ["cat", "/tmp/foo"]}`, id, "commit-test")
ed6074ea
 
dc944ea7
 	out, _ = dockerCmd(c, "run", "--name", name, "commit-test")
483abbb3
 	//run config in committed container was not merged
 	c.Assert(strings.TrimSpace(out), checker.Equals, "testing")
ed6074ea
 
 	type cfg struct {
 		Env []string
 		Cmd []string
 	}
 	config1 := cfg{}
62a856e9
 	inspectFieldAndMarshall(c, id, "Config", &config1)
483abbb3
 
ed6074ea
 	config2 := cfg{}
62a856e9
 	inspectFieldAndMarshall(c, name, "Config", &config2)
ed6074ea
 
 	// Env has at least PATH loaded as well here, so let's just grab the FOO one
 	var env1, env2 string
 	for _, e := range config1.Env {
 		if strings.HasPrefix(e, "FOO") {
 			env1 = e
 			break
 		}
 	}
 	for _, e := range config2.Env {
 		if strings.HasPrefix(e, "FOO") {
 			env2 = e
 			break
 		}
 	}
 
 	if len(config1.Env) != len(config2.Env) || env1 != env2 && env2 != "" {
dc944ea7
 		c.Fatalf("expected envs to match: %v - %v", config1.Env, config2.Env)
ed6074ea
 	}
 }