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