integration-cli/docker_cli_commit_test.go
6db32fde
 package main
 
 import (
 	"os/exec"
6beb858f
 	"strings"
dc944ea7
 
 	"github.com/go-check/check"
6db32fde
 )
 
dc944ea7
 func (s *DockerSuite) TestCommitAfterContainerIsDone(c *check.C) {
6db32fde
 	runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
3182ee5c
 	if err != nil {
dc944ea7
 		c.Fatalf("failed to run container: %s, %v", out, err)
3182ee5c
 	}
6db32fde
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
6db32fde
 
 	waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
3182ee5c
 	if _, _, err = runCommandWithOutput(waitCmd); err != nil {
dc944ea7
 		c.Fatalf("error thrown while waiting for container: %s, %v", out, err)
3182ee5c
 	}
6db32fde
 
 	commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
 	out, _, err = runCommandWithOutput(commitCmd)
3182ee5c
 	if err != nil {
dc944ea7
 		c.Fatalf("failed to commit container to image: %s, %v", out, err)
3182ee5c
 	}
6db32fde
 
475c6531
 	cleanedImageID := strings.TrimSpace(out)
6db32fde
 
 	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
3182ee5c
 	if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
dc944ea7
 		c.Fatalf("failed to inspect image: %s, %v", out, err)
3182ee5c
 	}
17d870be
 }
 
dc944ea7
 func (s *DockerSuite) TestCommitWithoutPause(c *check.C) {
17d870be
 	runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
3182ee5c
 	if err != nil {
dc944ea7
 		c.Fatalf("failed to run container: %s, %v", out, err)
3182ee5c
 	}
17d870be
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
17d870be
 
 	waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
3182ee5c
 	if _, _, err = runCommandWithOutput(waitCmd); err != nil {
dc944ea7
 		c.Fatalf("error thrown while waiting for container: %s, %v", out, err)
3182ee5c
 	}
17d870be
 
957c510d
 	commitCmd := exec.Command(dockerBinary, "commit", "-p=false", cleanedContainerID)
17d870be
 	out, _, err = runCommandWithOutput(commitCmd)
3182ee5c
 	if err != nil {
dc944ea7
 		c.Fatalf("failed to commit container to image: %s, %v", out, err)
3182ee5c
 	}
17d870be
 
475c6531
 	cleanedImageID := strings.TrimSpace(out)
17d870be
 
 	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
3182ee5c
 	if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
dc944ea7
 		c.Fatalf("failed to inspect image: %s, %v", out, err)
3182ee5c
 	}
6db32fde
 }
6beb858f
 
7c7c7f84
 //test commit a paused container should not unpause it after commit
dc944ea7
 func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
7c7c7f84
 	defer unpauseAllContainers()
 	cmd := exec.Command(dockerBinary, "run", "-i", "-d", "busybox")
 	out, _, _, err := runCommandWithStdoutStderr(cmd)
 	if err != nil {
dc944ea7
 		c.Fatalf("failed to run container: %v, output: %q", err, out)
7c7c7f84
 	}
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
7c7c7f84
 	cmd = exec.Command(dockerBinary, "pause", cleanedContainerID)
 	out, _, _, err = runCommandWithStdoutStderr(cmd)
 	if err != nil {
dc944ea7
 		c.Fatalf("failed to pause container: %v, output: %q", err, out)
7c7c7f84
 	}
 
 	commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
 	out, _, err = runCommandWithOutput(commitCmd)
 	if err != nil {
dc944ea7
 		c.Fatalf("failed to commit container to image: %s, %v", out, err)
7c7c7f84
 	}
 
74f8a4ec
 	out, err = inspectField(cleanedContainerID, "State.Paused")
 	c.Assert(err, check.IsNil)
7c7c7f84
 	if !strings.Contains(out, "true") {
dc944ea7
 		c.Fatalf("commit should not unpause a paused container")
7c7c7f84
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestCommitNewFile(c *check.C) {
70407ce4
 
6beb858f
 	cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
 	if _, err := runCommand(cmd); err != nil {
dc944ea7
 		c.Fatal(err)
6beb858f
 	}
 
 	cmd = exec.Command(dockerBinary, "commit", "commiter")
c0e63224
 	imageID, _, err := runCommandWithOutput(cmd)
6beb858f
 	if err != nil {
dc944ea7
 		c.Fatal(err)
6beb858f
 	}
c0e63224
 	imageID = strings.Trim(imageID, "\r\n")
6beb858f
 
c0e63224
 	cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo")
6beb858f
 
 	out, _, err := runCommandWithOutput(cmd)
 	if err != nil {
dc944ea7
 		c.Fatal(err, out)
6beb858f
 	}
 	if actual := strings.Trim(out, "\r\n"); actual != "koye" {
dc944ea7
 		c.Fatalf("expected output koye received %q", actual)
6beb858f
 	}
 
 }
2c8b63cb
 
dc944ea7
 func (s *DockerSuite) TestCommitHardlink(c *check.C) {
70407ce4
 
f9f80443
 	cmd := exec.Command(dockerBinary, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
 	firstOuput, _, err := runCommandWithOutput(cmd)
 	if err != nil {
dc944ea7
 		c.Fatal(err)
f9f80443
 	}
 
 	chunks := strings.Split(strings.TrimSpace(firstOuput), " ")
 	inode := chunks[0]
 	found := false
 	for _, chunk := range chunks[1:] {
 		if chunk == inode {
 			found = true
 			break
 		}
 	}
 	if !found {
dc944ea7
 		c.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
f9f80443
 	}
 
 	cmd = exec.Command(dockerBinary, "commit", "hardlinks", "hardlinks")
 	imageID, _, err := runCommandWithOutput(cmd)
 	if err != nil {
dc944ea7
 		c.Fatal(imageID, err)
f9f80443
 	}
 	imageID = strings.Trim(imageID, "\r\n")
 
 	cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
 	secondOuput, _, err := runCommandWithOutput(cmd)
 	if err != nil {
dc944ea7
 		c.Fatal(err)
f9f80443
 	}
 
 	chunks = strings.Split(strings.TrimSpace(secondOuput), " ")
 	inode = chunks[0]
 	found = false
 	for _, chunk := range chunks[1:] {
 		if chunk == inode {
 			found = true
 			break
 		}
 	}
 	if !found {
dc944ea7
 		c.Fatalf("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) {
da3d3b97
 
2c8b63cb
 	cmd := exec.Command(dockerBinary, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
 	if _, err := runCommand(cmd); err != nil {
dc944ea7
 		c.Fatal(err)
2c8b63cb
 	}
 
 	cmd = exec.Command(dockerBinary, "commit", "tty", "ttytest")
c0e63224
 	imageID, _, err := runCommandWithOutput(cmd)
2c8b63cb
 	if err != nil {
dc944ea7
 		c.Fatal(err)
2c8b63cb
 	}
c0e63224
 	imageID = strings.Trim(imageID, "\r\n")
2c8b63cb
 
 	cmd = exec.Command(dockerBinary, "run", "ttytest", "/bin/ls")
 	if _, err := runCommand(cmd); err != nil {
dc944ea7
 		c.Fatal(err)
2c8b63cb
 	}
3c984a6d
 
2c8b63cb
 }
d31c37fc
 
dc944ea7
 func (s *DockerSuite) TestCommitWithHostBindMount(c *check.C) {
70407ce4
 
d31c37fc
 	cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
 	if _, err := runCommand(cmd); err != nil {
dc944ea7
 		c.Fatal(err)
d31c37fc
 	}
 
 	cmd = exec.Command(dockerBinary, "commit", "bind-commit", "bindtest")
c0e63224
 	imageID, _, err := runCommandWithOutput(cmd)
d31c37fc
 	if err != nil {
dc944ea7
 		c.Fatal(imageID, err)
d31c37fc
 	}
3182ee5c
 
c0e63224
 	imageID = strings.Trim(imageID, "\r\n")
d31c37fc
 
 	cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
 
 	if _, err := runCommand(cmd); err != nil {
dc944ea7
 		c.Fatal(err)
d31c37fc
 	}
 
 }
b30257cc
 
dc944ea7
 func (s *DockerSuite) TestCommitChange(c *check.C) {
b30257cc
 
17abfc3d
 	cmd := exec.Command(dockerBinary, "run", "--name", "test", "busybox", "true")
 	if _, err := runCommand(cmd); err != nil {
dc944ea7
 		c.Fatal(err)
17abfc3d
 	}
 
 	cmd = exec.Command(dockerBinary, "commit",
b30257cc
 		"--change", "EXPOSE 8080",
 		"--change", "ENV DEBUG true",
4a9fa965
 		"--change", "ENV test 1",
c1b77921
 		"--change", "ENV PATH /foo",
b30257cc
 		"test", "test-commit")
 	imageId, _, err := runCommandWithOutput(cmd)
 	if err != nil {
dc944ea7
 		c.Fatal(imageId, err)
b30257cc
 	}
 	imageId = strings.Trim(imageId, "\r\n")
 
 	expected := map[string]string{
231d362d
 		"Config.ExposedPorts": "map[8080/tcp:{}]",
c1b77921
 		"Config.Env":          "[DEBUG=true test=1 PATH=/foo]",
b30257cc
 	}
 
 	for conf, value := range expected {
 		res, err := inspectField(imageId, conf)
74f8a4ec
 		c.Assert(err, check.IsNil)
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) {
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")
ed6074ea
 	if strings.TrimSpace(out) != "testing" {
3941623f
 		c.Fatal("run config in committed container was not merged")
ed6074ea
 	}
 
 	type cfg struct {
 		Env []string
 		Cmd []string
 	}
 	config1 := cfg{}
 	if err := inspectFieldAndMarshall(id, "Config", &config1); err != nil {
dc944ea7
 		c.Fatal(err)
ed6074ea
 	}
 	config2 := cfg{}
 	if err := inspectFieldAndMarshall(name, "Config", &config2); err != nil {
dc944ea7
 		c.Fatal(err)
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
 	}
 
 }