github.com/moby/docker@v26.1.3+incompatible/integration-cli/docker_cli_commit_test.go (about) 1 package main 2 3 import ( 4 "context" 5 "strings" 6 "testing" 7 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(ctx context.Context, c *testing.T) { 18 s.ds.TearDownTest(ctx, 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 := cli.DockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo").Combined() 43 44 cleanedContainerID := strings.TrimSpace(out) 45 46 cli.DockerCmd(c, "wait", cleanedContainerID) 47 48 out = cli.DockerCmd(c, "commit", "-p=false", cleanedContainerID).Combined() 49 50 cleanedImageID := strings.TrimSpace(out) 51 52 cli.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 containerID := cli.DockerCmd(c, "run", "-i", "-d", "busybox").Stdout() 59 containerID = strings.TrimSpace(containerID) 60 61 cli.DockerCmd(c, "pause", containerID) 62 cli.DockerCmd(c, "commit", containerID) 63 64 out := inspectField(c, containerID, "State.Paused") 65 // commit should not unpause a paused container 66 assert.Assert(c, strings.Contains(out, "true")) 67 } 68 69 func (s *DockerCLICommitSuite) TestCommitNewFile(c *testing.T) { 70 cli.DockerCmd(c, "run", "--name", "committer", "busybox", "/bin/sh", "-c", "echo koye > /foo") 71 72 imageID := cli.DockerCmd(c, "commit", "committer").Stdout() 73 imageID = strings.TrimSpace(imageID) 74 75 out := cli.DockerCmd(c, "run", imageID, "cat", "/foo").Combined() 76 actual := strings.TrimSpace(out) 77 assert.Equal(c, actual, "koye") 78 } 79 80 func (s *DockerCLICommitSuite) TestCommitHardlink(c *testing.T) { 81 testRequires(c, DaemonIsLinux) 82 firstOutput := cli.DockerCmd(c, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2").Combined() 83 84 chunks := strings.Split(strings.TrimSpace(firstOutput), " ") 85 inode := chunks[0] 86 chunks = strings.SplitAfterN(strings.TrimSpace(firstOutput), " ", 2) 87 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:]) 88 imageID := cli.DockerCmd(c, "commit", "hardlinks", "hardlinks").Stdout() 89 imageID = strings.TrimSpace(imageID) 90 91 secondOutput := cli.DockerCmd(c, "run", "-t", imageID, "ls", "-di", "file1", "file2").Combined() 92 93 chunks = strings.Split(strings.TrimSpace(secondOutput), " ") 94 inode = chunks[0] 95 chunks = strings.SplitAfterN(strings.TrimSpace(secondOutput), " ", 2) 96 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:]) 97 } 98 99 func (s *DockerCLICommitSuite) TestCommitTTY(c *testing.T) { 100 cli.DockerCmd(c, "run", "-t", "--name", "tty", "busybox", "/bin/ls") 101 102 imageID := cli.DockerCmd(c, "commit", "tty", "ttytest").Stdout() 103 imageID = strings.TrimSpace(imageID) 104 105 cli.DockerCmd(c, "run", imageID, "/bin/ls") 106 } 107 108 func (s *DockerCLICommitSuite) TestCommitWithHostBindMount(c *testing.T) { 109 testRequires(c, DaemonIsLinux) 110 cli.DockerCmd(c, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true") 111 112 imageID := cli.DockerCmd(c, "commit", "bind-commit", "bindtest").Stdout() 113 imageID = strings.TrimSpace(imageID) 114 115 cli.DockerCmd(c, "run", imageID, "true") 116 } 117 118 func (s *DockerCLICommitSuite) TestCommitChange(c *testing.T) { 119 cli.DockerCmd(c, "run", "--name", "test", "busybox", "true") 120 121 imageID := cli.DockerCmd(c, "commit", 122 "--change", `EXPOSE 8080`, 123 "--change", `ENV DEBUG true`, 124 "--change", `ENV test 1`, 125 "--change", `ENV PATH /foo`, 126 "--change", `LABEL foo bar`, 127 "--change", `CMD ["/bin/sh"]`, 128 "--change", `WORKDIR /opt`, 129 "--change", `ENTRYPOINT ["/bin/sh"]`, 130 "--change", `USER testuser`, 131 "--change", `VOLUME /var/lib/docker`, 132 "--change", `ONBUILD /usr/local/bin/python-build --dir /app/src`, 133 "test", "test-commit", 134 ).Stdout() 135 imageID = strings.TrimSpace(imageID) 136 137 expectedEnv := "[DEBUG=true test=1 PATH=/foo]" 138 if testEnv.DaemonInfo.OSType != "windows" { 139 // The ordering here is due to `PATH` being overridden from the container's 140 // ENV. On windows, the container doesn't have a `PATH` ENV variable so 141 // the ordering is the same as the cli. 142 expectedEnv = "[PATH=/foo DEBUG=true test=1]" 143 } 144 145 prefix, slash := getPrefixAndSlashFromDaemonPlatform() 146 prefix = strings.ToUpper(prefix) // Force C: as that's how WORKDIR is normalized on Windows 147 expected := map[string]string{ 148 "Config.ExposedPorts": "map[8080/tcp:{}]", 149 "Config.Env": expectedEnv, 150 "Config.Labels": "map[foo:bar]", 151 "Config.Cmd": "[/bin/sh]", 152 "Config.WorkingDir": prefix + slash + "opt", 153 "Config.Entrypoint": "[/bin/sh]", 154 "Config.User": "testuser", 155 "Config.Volumes": "map[/var/lib/docker:{}]", 156 "Config.OnBuild": "[/usr/local/bin/python-build --dir /app/src]", 157 } 158 159 for conf, value := range expected { 160 res := inspectField(c, imageID, conf) 161 if res != value { 162 c.Errorf("%s('%s'), expected %s", conf, res, value) 163 } 164 } 165 } 166 167 func (s *DockerCLICommitSuite) TestCommitChangeLabels(c *testing.T) { 168 cli.DockerCmd(c, "run", "--name", "test", "--label", "some=label", "busybox", "true") 169 170 imageID := cli.DockerCmd(c, "commit", "--change", "LABEL some=label2", "test", "test-commit").Stdout() 171 imageID = strings.TrimSpace(imageID) 172 173 assert.Equal(c, inspectField(c, imageID, "Config.Labels"), "map[some:label2]") 174 // check that container labels didn't change 175 assert.Equal(c, inspectField(c, "test", "Config.Labels"), "map[some:label]") 176 }