github.com/lacework-dev/go-moby@v20.10.12+incompatible/integration-cli/docker_cli_commit_test.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/api/types/versions"
     8  	"github.com/docker/docker/integration-cli/cli"
     9  	"gotest.tools/v3/assert"
    10  )
    11  
    12  func (s *DockerSuite) TestCommitAfterContainerIsDone(c *testing.T) {
    13  	out := cli.DockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo").Combined()
    14  
    15  	cleanedContainerID := strings.TrimSpace(out)
    16  
    17  	cli.DockerCmd(c, "wait", cleanedContainerID)
    18  
    19  	out = cli.DockerCmd(c, "commit", cleanedContainerID).Combined()
    20  
    21  	cleanedImageID := strings.TrimSpace(out)
    22  
    23  	cli.DockerCmd(c, "inspect", cleanedImageID)
    24  }
    25  
    26  func (s *DockerSuite) TestCommitWithoutPause(c *testing.T) {
    27  	testRequires(c, DaemonIsLinux)
    28  	out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
    29  
    30  	cleanedContainerID := strings.TrimSpace(out)
    31  
    32  	dockerCmd(c, "wait", cleanedContainerID)
    33  
    34  	out, _ = dockerCmd(c, "commit", "-p=false", cleanedContainerID)
    35  
    36  	cleanedImageID := strings.TrimSpace(out)
    37  
    38  	dockerCmd(c, "inspect", cleanedImageID)
    39  }
    40  
    41  // TestCommitPausedContainer tests that a paused container is not unpaused after being committed
    42  func (s *DockerSuite) TestCommitPausedContainer(c *testing.T) {
    43  	testRequires(c, DaemonIsLinux)
    44  	out, _ := dockerCmd(c, "run", "-i", "-d", "busybox")
    45  
    46  	cleanedContainerID := strings.TrimSpace(out)
    47  
    48  	dockerCmd(c, "pause", cleanedContainerID)
    49  	dockerCmd(c, "commit", cleanedContainerID)
    50  
    51  	out = inspectField(c, cleanedContainerID, "State.Paused")
    52  	// commit should not unpause a paused container
    53  	assert.Assert(c, strings.Contains(out, "true"))
    54  }
    55  
    56  func (s *DockerSuite) TestCommitNewFile(c *testing.T) {
    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  	assert.Equal(c, actual, "koye")
    65  }
    66  
    67  func (s *DockerSuite) TestCommitHardlink(c *testing.T) {
    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  	assert.Assert(c, strings.Contains(chunks[1], chunks[0]), "Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
    75  	imageID, _ := dockerCmd(c, "commit", "hardlinks", "hardlinks")
    76  	imageID = strings.TrimSpace(imageID)
    77  
    78  	secondOutput, _ := dockerCmd(c, "run", "-t", imageID, "ls", "-di", "file1", "file2")
    79  
    80  	chunks = strings.Split(strings.TrimSpace(secondOutput), " ")
    81  	inode = chunks[0]
    82  	chunks = strings.SplitAfterN(strings.TrimSpace(secondOutput), " ", 2)
    83  	assert.Assert(c, strings.Contains(chunks[1], chunks[0]), "Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
    84  }
    85  
    86  func (s *DockerSuite) TestCommitTTY(c *testing.T) {
    87  	dockerCmd(c, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
    88  
    89  	imageID, _ := dockerCmd(c, "commit", "tty", "ttytest")
    90  	imageID = strings.TrimSpace(imageID)
    91  
    92  	dockerCmd(c, "run", imageID, "/bin/ls")
    93  }
    94  
    95  func (s *DockerSuite) TestCommitWithHostBindMount(c *testing.T) {
    96  	testRequires(c, DaemonIsLinux)
    97  	dockerCmd(c, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
    98  
    99  	imageID, _ := dockerCmd(c, "commit", "bind-commit", "bindtest")
   100  	imageID = strings.TrimSpace(imageID)
   101  
   102  	dockerCmd(c, "run", imageID, "true")
   103  }
   104  
   105  func (s *DockerSuite) TestCommitChange(c *testing.T) {
   106  	dockerCmd(c, "run", "--name", "test", "busybox", "true")
   107  
   108  	imageID, _ := dockerCmd(c, "commit",
   109  		"--change", "EXPOSE 8080",
   110  		"--change", "ENV DEBUG true",
   111  		"--change", "ENV test 1",
   112  		"--change", "ENV PATH /foo",
   113  		"--change", "LABEL foo bar",
   114  		"--change", "CMD [\"/bin/sh\"]",
   115  		"--change", "WORKDIR /opt",
   116  		"--change", "ENTRYPOINT [\"/bin/sh\"]",
   117  		"--change", "USER testuser",
   118  		"--change", "VOLUME /var/lib/docker",
   119  		"--change", "ONBUILD /usr/local/bin/python-build --dir /app/src",
   120  		"test", "test-commit")
   121  	imageID = strings.TrimSpace(imageID)
   122  
   123  	expectedEnv := "[DEBUG=true test=1 PATH=/foo]"
   124  	// bug fixed in 1.36, add min APi >= 1.36 requirement
   125  	// PR record https://github.com/moby/moby/pull/35582
   126  	if versions.GreaterThan(testEnv.DaemonAPIVersion(), "1.35") && testEnv.OSType != "windows" {
   127  		// The ordering here is due to `PATH` being overridden from the container's
   128  		// ENV.  On windows, the container doesn't have a `PATH` ENV variable so
   129  		// the ordering is the same as the cli.
   130  		expectedEnv = "[PATH=/foo DEBUG=true test=1]"
   131  	}
   132  
   133  	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
   134  	prefix = strings.ToUpper(prefix) // Force C: as that's how WORKDIR is normalized on Windows
   135  	expected := map[string]string{
   136  		"Config.ExposedPorts": "map[8080/tcp:{}]",
   137  		"Config.Env":          expectedEnv,
   138  		"Config.Labels":       "map[foo:bar]",
   139  		"Config.Cmd":          "[/bin/sh]",
   140  		"Config.WorkingDir":   prefix + slash + "opt",
   141  		"Config.Entrypoint":   "[/bin/sh]",
   142  		"Config.User":         "testuser",
   143  		"Config.Volumes":      "map[/var/lib/docker:{}]",
   144  		"Config.OnBuild":      "[/usr/local/bin/python-build --dir /app/src]",
   145  	}
   146  
   147  	for conf, value := range expected {
   148  		res := inspectField(c, imageID, conf)
   149  		if res != value {
   150  			c.Errorf("%s('%s'), expected %s", conf, res, value)
   151  		}
   152  	}
   153  }
   154  
   155  func (s *DockerSuite) TestCommitChangeLabels(c *testing.T) {
   156  	dockerCmd(c, "run", "--name", "test", "--label", "some=label", "busybox", "true")
   157  
   158  	imageID, _ := dockerCmd(c, "commit",
   159  		"--change", "LABEL some=label2",
   160  		"test", "test-commit")
   161  	imageID = strings.TrimSpace(imageID)
   162  
   163  	assert.Equal(c, inspectField(c, imageID, "Config.Labels"), "map[some:label2]")
   164  	// check that container labels didn't change
   165  	assert.Equal(c, inspectField(c, "test", "Config.Labels"), "map[some:label]")
   166  }