github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/integration-cli/docker_cli_commit_test.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/docker/integration-cli/checker"
     7  	"github.com/docker/docker/integration-cli/cli"
     8  	"github.com/go-check/check"
     9  )
    10  
    11  func (s *DockerSuite) TestCommitAfterContainerIsDone(c *check.C) {
    12  	out := cli.DockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo").Combined()
    13  
    14  	cleanedContainerID := strings.TrimSpace(out)
    15  
    16  	cli.DockerCmd(c, "wait", cleanedContainerID)
    17  
    18  	out = cli.DockerCmd(c, "commit", cleanedContainerID).Combined()
    19  
    20  	cleanedImageID := strings.TrimSpace(out)
    21  
    22  	cli.DockerCmd(c, "inspect", cleanedImageID)
    23  }
    24  
    25  func (s *DockerSuite) TestCommitWithoutPause(c *check.C) {
    26  	testRequires(c, DaemonIsLinux)
    27  	out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
    28  
    29  	cleanedContainerID := strings.TrimSpace(out)
    30  
    31  	dockerCmd(c, "wait", cleanedContainerID)
    32  
    33  	out, _ = dockerCmd(c, "commit", "-p=false", cleanedContainerID)
    34  
    35  	cleanedImageID := strings.TrimSpace(out)
    36  
    37  	dockerCmd(c, "inspect", cleanedImageID)
    38  }
    39  
    40  //test commit a paused container should not unpause it after commit
    41  func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
    42  	testRequires(c, DaemonIsLinux)
    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", "committer", "busybox", "/bin/sh", "-c", "echo koye > /foo")
    58  
    59  	imageID, _ := dockerCmd(c, "commit", "committer")
    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", imageID, "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", imageID, "/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", imageID, "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  	// The ordering here is due to `PATH` being overridden from the container's
   125  	// ENV.  On windows, the container doesn't have a `PATH` ENV variable so
   126  	// the ordering is the same as the cli.
   127  	expectedEnv := "[PATH=/foo DEBUG=true test=1]"
   128  	if testEnv.OSType == "windows" {
   129  		expectedEnv = "[DEBUG=true test=1 PATH=/foo]"
   130  	}
   131  
   132  	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
   133  	prefix = strings.ToUpper(prefix) // Force C: as that's how WORKDIR is normalized on Windows
   134  	expected := map[string]string{
   135  		"Config.ExposedPorts": "map[8080/tcp:{}]",
   136  		"Config.Env":          expectedEnv,
   137  		"Config.Labels":       "map[foo:bar]",
   138  		"Config.Cmd":          "[/bin/sh]",
   139  		"Config.WorkingDir":   prefix + slash + "opt",
   140  		"Config.Entrypoint":   "[/bin/sh]",
   141  		"Config.User":         "testuser",
   142  		"Config.Volumes":      "map[/var/lib/docker:{}]",
   143  		"Config.OnBuild":      "[/usr/local/bin/python-build --dir /app/src]",
   144  	}
   145  
   146  	for conf, value := range expected {
   147  		res := inspectField(c, imageID, conf)
   148  		if res != value {
   149  			c.Errorf("%s('%s'), expected %s", conf, res, value)
   150  		}
   151  	}
   152  }
   153  
   154  func (s *DockerSuite) TestCommitChangeLabels(c *check.C) {
   155  	dockerCmd(c, "run", "--name", "test", "--label", "some=label", "busybox", "true")
   156  
   157  	imageID, _ := dockerCmd(c, "commit",
   158  		"--change", "LABEL some=label2",
   159  		"test", "test-commit")
   160  	imageID = strings.TrimSpace(imageID)
   161  
   162  	c.Assert(inspectField(c, imageID, "Config.Labels"), checker.Equals, "map[some:label2]")
   163  	// check that container labels didn't change
   164  	c.Assert(inspectField(c, "test", "Config.Labels"), checker.Equals, "map[some:label]")
   165  }