github.com/kinvolk/docker@v1.13.1/integration-cli/docker_cli_commit_test.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/docker/pkg/integration/checker"
     7  	"github.com/go-check/check"
     8  )
     9  
    10  func (s *DockerSuite) TestCommitAfterContainerIsDone(c *check.C) {
    11  	out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
    12  
    13  	cleanedContainerID := strings.TrimSpace(out)
    14  
    15  	dockerCmd(c, "wait", cleanedContainerID)
    16  
    17  	out, _ = dockerCmd(c, "commit", cleanedContainerID)
    18  
    19  	cleanedImageID := strings.TrimSpace(out)
    20  
    21  	dockerCmd(c, "inspect", cleanedImageID)
    22  }
    23  
    24  func (s *DockerSuite) TestCommitWithoutPause(c *check.C) {
    25  	testRequires(c, DaemonIsLinux)
    26  	out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
    27  
    28  	cleanedContainerID := strings.TrimSpace(out)
    29  
    30  	dockerCmd(c, "wait", cleanedContainerID)
    31  
    32  	out, _ = dockerCmd(c, "commit", "-p=false", cleanedContainerID)
    33  
    34  	cleanedImageID := strings.TrimSpace(out)
    35  
    36  	dockerCmd(c, "inspect", cleanedImageID)
    37  }
    38  
    39  //test commit a paused container should not unpause it after commit
    40  func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
    41  	testRequires(c, DaemonIsLinux)
    42  	defer unpauseAllContainers()
    43  	out, _ := dockerCmd(c, "run", "-i", "-d", "busybox")
    44  
    45  	cleanedContainerID := strings.TrimSpace(out)
    46  
    47  	dockerCmd(c, "pause", cleanedContainerID)
    48  
    49  	out, _ = dockerCmd(c, "commit", cleanedContainerID)
    50  
    51  	out = inspectField(c, cleanedContainerID, "State.Paused")
    52  	// commit should not unpause a paused container
    53  	c.Assert(out, checker.Contains, "true")
    54  }
    55  
    56  func (s *DockerSuite) TestCommitNewFile(c *check.C) {
    57  	dockerCmd(c, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
    58  
    59  	imageID, _ := dockerCmd(c, "commit", "commiter")
    60  	imageID = strings.TrimSpace(imageID)
    61  
    62  	out, _ := dockerCmd(c, "run", imageID, "cat", "/foo")
    63  	actual := strings.TrimSpace(out)
    64  	c.Assert(actual, checker.Equals, "koye")
    65  }
    66  
    67  func (s *DockerSuite) TestCommitHardlink(c *check.C) {
    68  	testRequires(c, DaemonIsLinux)
    69  	firstOutput, _ := dockerCmd(c, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
    70  
    71  	chunks := strings.Split(strings.TrimSpace(firstOutput), " ")
    72  	inode := chunks[0]
    73  	chunks = strings.SplitAfterN(strings.TrimSpace(firstOutput), " ", 2)
    74  	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:]))
    75  
    76  	imageID, _ := dockerCmd(c, "commit", "hardlinks", "hardlinks")
    77  	imageID = strings.TrimSpace(imageID)
    78  
    79  	secondOutput, _ := dockerCmd(c, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
    80  
    81  	chunks = strings.Split(strings.TrimSpace(secondOutput), " ")
    82  	inode = chunks[0]
    83  	chunks = strings.SplitAfterN(strings.TrimSpace(secondOutput), " ", 2)
    84  	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:]))
    85  }
    86  
    87  func (s *DockerSuite) TestCommitTTY(c *check.C) {
    88  	dockerCmd(c, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
    89  
    90  	imageID, _ := dockerCmd(c, "commit", "tty", "ttytest")
    91  	imageID = strings.TrimSpace(imageID)
    92  
    93  	dockerCmd(c, "run", "ttytest", "/bin/ls")
    94  }
    95  
    96  func (s *DockerSuite) TestCommitWithHostBindMount(c *check.C) {
    97  	testRequires(c, DaemonIsLinux)
    98  	dockerCmd(c, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
    99  
   100  	imageID, _ := dockerCmd(c, "commit", "bind-commit", "bindtest")
   101  	imageID = strings.TrimSpace(imageID)
   102  
   103  	dockerCmd(c, "run", "bindtest", "true")
   104  }
   105  
   106  func (s *DockerSuite) TestCommitChange(c *check.C) {
   107  	dockerCmd(c, "run", "--name", "test", "busybox", "true")
   108  
   109  	imageID, _ := dockerCmd(c, "commit",
   110  		"--change", "EXPOSE 8080",
   111  		"--change", "ENV DEBUG true",
   112  		"--change", "ENV test 1",
   113  		"--change", "ENV PATH /foo",
   114  		"--change", "LABEL foo bar",
   115  		"--change", "CMD [\"/bin/sh\"]",
   116  		"--change", "WORKDIR /opt",
   117  		"--change", "ENTRYPOINT [\"/bin/sh\"]",
   118  		"--change", "USER testuser",
   119  		"--change", "VOLUME /var/lib/docker",
   120  		"--change", "ONBUILD /usr/local/bin/python-build --dir /app/src",
   121  		"test", "test-commit")
   122  	imageID = strings.TrimSpace(imageID)
   123  
   124  	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
   125  	prefix = strings.ToUpper(prefix) // Force C: as that's how WORKDIR is normalised on Windows
   126  	expected := map[string]string{
   127  		"Config.ExposedPorts": "map[8080/tcp:{}]",
   128  		"Config.Env":          "[DEBUG=true test=1 PATH=/foo]",
   129  		"Config.Labels":       "map[foo:bar]",
   130  		"Config.Cmd":          "[/bin/sh]",
   131  		"Config.WorkingDir":   prefix + slash + "opt",
   132  		"Config.Entrypoint":   "[/bin/sh]",
   133  		"Config.User":         "testuser",
   134  		"Config.Volumes":      "map[/var/lib/docker:{}]",
   135  		"Config.OnBuild":      "[/usr/local/bin/python-build --dir /app/src]",
   136  	}
   137  
   138  	for conf, value := range expected {
   139  		res := inspectField(c, imageID, conf)
   140  		if res != value {
   141  			c.Errorf("%s('%s'), expected %s", conf, res, value)
   142  		}
   143  	}
   144  }
   145  
   146  func (s *DockerSuite) TestCommitChangeLabels(c *check.C) {
   147  	dockerCmd(c, "run", "--name", "test", "--label", "some=label", "busybox", "true")
   148  
   149  	imageID, _ := dockerCmd(c, "commit",
   150  		"--change", "LABEL some=label2",
   151  		"test", "test-commit")
   152  	imageID = strings.TrimSpace(imageID)
   153  
   154  	c.Assert(inspectField(c, imageID, "Config.Labels"), checker.Equals, "map[some:label2]")
   155  	// check that container labels didn't change
   156  	c.Assert(inspectField(c, "test", "Config.Labels"), checker.Equals, "map[some:label]")
   157  }