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