github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/integration-cli/docker_cli_commit_test.go (about) 1 package main 2 3 import ( 4 "strings" 5 6 "github.com/docker/docker/api/types/versions" 7 "github.com/docker/docker/integration-cli/checker" 8 "github.com/docker/docker/integration-cli/cli" 9 "github.com/go-check/check" 10 ) 11 12 func (s *DockerSuite) TestCommitAfterContainerIsDone(c *check.C) { 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 *check.C) { 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 //test commit a paused container should not unpause it after commit 42 func (s *DockerSuite) TestCommitPausedContainer(c *check.C) { 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 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 expectedEnv := "[DEBUG=true test=1 PATH=/foo]" 125 // bug fixed in 1.36, add min APi >= 1.36 requirement 126 // PR record https://github.com/moby/moby/pull/35582 127 if versions.GreaterThan(testEnv.DaemonAPIVersion(), "1.35") && testEnv.OSType != "windows" { 128 // The ordering here is due to `PATH` being overridden from the container's 129 // ENV. On windows, the container doesn't have a `PATH` ENV variable so 130 // the ordering is the same as the cli. 131 expectedEnv = "[PATH=/foo DEBUG=true test=1]" 132 } 133 134 prefix, slash := getPrefixAndSlashFromDaemonPlatform() 135 prefix = strings.ToUpper(prefix) // Force C: as that's how WORKDIR is normalized on Windows 136 expected := map[string]string{ 137 "Config.ExposedPorts": "map[8080/tcp:{}]", 138 "Config.Env": expectedEnv, 139 "Config.Labels": "map[foo:bar]", 140 "Config.Cmd": "[/bin/sh]", 141 "Config.WorkingDir": prefix + slash + "opt", 142 "Config.Entrypoint": "[/bin/sh]", 143 "Config.User": "testuser", 144 "Config.Volumes": "map[/var/lib/docker:{}]", 145 "Config.OnBuild": "[/usr/local/bin/python-build --dir /app/src]", 146 } 147 148 for conf, value := range expected { 149 res := inspectField(c, imageID, conf) 150 if res != value { 151 c.Errorf("%s('%s'), expected %s", conf, res, value) 152 } 153 } 154 } 155 156 func (s *DockerSuite) TestCommitChangeLabels(c *check.C) { 157 dockerCmd(c, "run", "--name", "test", "--label", "some=label", "busybox", "true") 158 159 imageID, _ := dockerCmd(c, "commit", 160 "--change", "LABEL some=label2", 161 "test", "test-commit") 162 imageID = strings.TrimSpace(imageID) 163 164 c.Assert(inspectField(c, imageID, "Config.Labels"), checker.Equals, "map[some:label2]") 165 // check that container labels didn't change 166 c.Assert(inspectField(c, "test", "Config.Labels"), checker.Equals, "map[some:label]") 167 }