docker_cli_commit_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "github.com/go-check/check"
  6. )
  7. func (s *DockerSuite) TestCommitAfterContainerIsDone(c *check.C) {
  8. runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
  9. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  10. if err != nil {
  11. c.Fatalf("failed to run container: %s, %v", out, err)
  12. }
  13. cleanedContainerID := strings.TrimSpace(out)
  14. waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
  15. if _, _, err = runCommandWithOutput(waitCmd); err != nil {
  16. c.Fatalf("error thrown while waiting for container: %s, %v", out, err)
  17. }
  18. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
  19. out, _, err = runCommandWithOutput(commitCmd)
  20. if err != nil {
  21. c.Fatalf("failed to commit container to image: %s, %v", out, err)
  22. }
  23. cleanedImageID := strings.TrimSpace(out)
  24. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
  25. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  26. c.Fatalf("failed to inspect image: %s, %v", out, err)
  27. }
  28. deleteContainer(cleanedContainerID)
  29. deleteImages(cleanedImageID)
  30. }
  31. func (s *DockerSuite) TestCommitWithoutPause(c *check.C) {
  32. runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
  33. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  34. if err != nil {
  35. c.Fatalf("failed to run container: %s, %v", out, err)
  36. }
  37. cleanedContainerID := strings.TrimSpace(out)
  38. waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
  39. if _, _, err = runCommandWithOutput(waitCmd); err != nil {
  40. c.Fatalf("error thrown while waiting for container: %s, %v", out, err)
  41. }
  42. commitCmd := exec.Command(dockerBinary, "commit", "-p=false", cleanedContainerID)
  43. out, _, err = runCommandWithOutput(commitCmd)
  44. if err != nil {
  45. c.Fatalf("failed to commit container to image: %s, %v", out, err)
  46. }
  47. cleanedImageID := strings.TrimSpace(out)
  48. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
  49. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  50. c.Fatalf("failed to inspect image: %s, %v", out, err)
  51. }
  52. deleteContainer(cleanedContainerID)
  53. deleteImages(cleanedImageID)
  54. }
  55. //test commit a paused container should not unpause it after commit
  56. func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
  57. defer unpauseAllContainers()
  58. cmd := exec.Command(dockerBinary, "run", "-i", "-d", "busybox")
  59. out, _, _, err := runCommandWithStdoutStderr(cmd)
  60. if err != nil {
  61. c.Fatalf("failed to run container: %v, output: %q", err, out)
  62. }
  63. cleanedContainerID := strings.TrimSpace(out)
  64. cmd = exec.Command(dockerBinary, "pause", cleanedContainerID)
  65. out, _, _, err = runCommandWithStdoutStderr(cmd)
  66. if err != nil {
  67. c.Fatalf("failed to pause container: %v, output: %q", err, out)
  68. }
  69. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
  70. out, _, err = runCommandWithOutput(commitCmd)
  71. if err != nil {
  72. c.Fatalf("failed to commit container to image: %s, %v", out, err)
  73. }
  74. cleanedImageID := strings.TrimSpace(out)
  75. defer deleteImages(cleanedImageID)
  76. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Paused}}", cleanedContainerID)
  77. out, _, _, err = runCommandWithStdoutStderr(cmd)
  78. if err != nil {
  79. c.Fatalf("failed to inspect container: %v, output: %q", err, out)
  80. }
  81. if !strings.Contains(out, "true") {
  82. c.Fatalf("commit should not unpause a paused container")
  83. }
  84. }
  85. func (s *DockerSuite) TestCommitNewFile(c *check.C) {
  86. cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
  87. if _, err := runCommand(cmd); err != nil {
  88. c.Fatal(err)
  89. }
  90. cmd = exec.Command(dockerBinary, "commit", "commiter")
  91. imageID, _, err := runCommandWithOutput(cmd)
  92. if err != nil {
  93. c.Fatal(err)
  94. }
  95. imageID = strings.Trim(imageID, "\r\n")
  96. defer deleteImages(imageID)
  97. cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo")
  98. out, _, err := runCommandWithOutput(cmd)
  99. if err != nil {
  100. c.Fatal(err, out)
  101. }
  102. if actual := strings.Trim(out, "\r\n"); actual != "koye" {
  103. c.Fatalf("expected output koye received %q", actual)
  104. }
  105. }
  106. func (s *DockerSuite) TestCommitHardlink(c *check.C) {
  107. cmd := exec.Command(dockerBinary, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
  108. firstOuput, _, err := runCommandWithOutput(cmd)
  109. if err != nil {
  110. c.Fatal(err)
  111. }
  112. chunks := strings.Split(strings.TrimSpace(firstOuput), " ")
  113. inode := chunks[0]
  114. found := false
  115. for _, chunk := range chunks[1:] {
  116. if chunk == inode {
  117. found = true
  118. break
  119. }
  120. }
  121. if !found {
  122. c.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  123. }
  124. cmd = exec.Command(dockerBinary, "commit", "hardlinks", "hardlinks")
  125. imageID, _, err := runCommandWithOutput(cmd)
  126. if err != nil {
  127. c.Fatal(imageID, err)
  128. }
  129. imageID = strings.Trim(imageID, "\r\n")
  130. defer deleteImages(imageID)
  131. cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
  132. secondOuput, _, err := runCommandWithOutput(cmd)
  133. if err != nil {
  134. c.Fatal(err)
  135. }
  136. chunks = strings.Split(strings.TrimSpace(secondOuput), " ")
  137. inode = chunks[0]
  138. found = false
  139. for _, chunk := range chunks[1:] {
  140. if chunk == inode {
  141. found = true
  142. break
  143. }
  144. }
  145. if !found {
  146. c.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  147. }
  148. }
  149. func (s *DockerSuite) TestCommitTTY(c *check.C) {
  150. defer deleteImages("ttytest")
  151. cmd := exec.Command(dockerBinary, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
  152. if _, err := runCommand(cmd); err != nil {
  153. c.Fatal(err)
  154. }
  155. cmd = exec.Command(dockerBinary, "commit", "tty", "ttytest")
  156. imageID, _, err := runCommandWithOutput(cmd)
  157. if err != nil {
  158. c.Fatal(err)
  159. }
  160. imageID = strings.Trim(imageID, "\r\n")
  161. cmd = exec.Command(dockerBinary, "run", "ttytest", "/bin/ls")
  162. if _, err := runCommand(cmd); err != nil {
  163. c.Fatal(err)
  164. }
  165. }
  166. func (s *DockerSuite) TestCommitWithHostBindMount(c *check.C) {
  167. cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
  168. if _, err := runCommand(cmd); err != nil {
  169. c.Fatal(err)
  170. }
  171. cmd = exec.Command(dockerBinary, "commit", "bind-commit", "bindtest")
  172. imageID, _, err := runCommandWithOutput(cmd)
  173. if err != nil {
  174. c.Fatal(imageID, err)
  175. }
  176. imageID = strings.Trim(imageID, "\r\n")
  177. defer deleteImages(imageID)
  178. cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
  179. if _, err := runCommand(cmd); err != nil {
  180. c.Fatal(err)
  181. }
  182. }
  183. func (s *DockerSuite) TestCommitChange(c *check.C) {
  184. cmd := exec.Command(dockerBinary, "run", "--name", "test", "busybox", "true")
  185. if _, err := runCommand(cmd); err != nil {
  186. c.Fatal(err)
  187. }
  188. cmd = exec.Command(dockerBinary, "commit",
  189. "--change", "EXPOSE 8080",
  190. "--change", "ENV DEBUG true",
  191. "--change", "ENV test 1",
  192. "--change", "ENV PATH /foo",
  193. "test", "test-commit")
  194. imageId, _, err := runCommandWithOutput(cmd)
  195. if err != nil {
  196. c.Fatal(imageId, err)
  197. }
  198. imageId = strings.Trim(imageId, "\r\n")
  199. defer deleteImages(imageId)
  200. expected := map[string]string{
  201. "Config.ExposedPorts": "map[8080/tcp:map[]]",
  202. "Config.Env": "[DEBUG=true test=1 PATH=/foo]",
  203. }
  204. for conf, value := range expected {
  205. res, err := inspectField(imageId, conf)
  206. if err != nil {
  207. c.Errorf("failed to get value %s, error: %s", conf, err)
  208. }
  209. if res != value {
  210. c.Errorf("%s('%s'), expected %s", conf, res, value)
  211. }
  212. }
  213. }
  214. // TODO: commit --run is deprecated, remove this once --run is removed
  215. func (s *DockerSuite) TestCommitMergeConfigRun(c *check.C) {
  216. name := "commit-test"
  217. out, _ := dockerCmd(c, "run", "-d", "-e=FOO=bar", "busybox", "/bin/sh", "-c", "echo testing > /tmp/foo")
  218. id := strings.TrimSpace(out)
  219. dockerCmd(c, "commit", `--run={"Cmd": ["cat", "/tmp/foo"]}`, id, "commit-test")
  220. defer deleteImages("commit-test")
  221. out, _ = dockerCmd(c, "run", "--name", name, "commit-test")
  222. if strings.TrimSpace(out) != "testing" {
  223. c.Fatal("run config in commited container was not merged")
  224. }
  225. type cfg struct {
  226. Env []string
  227. Cmd []string
  228. }
  229. config1 := cfg{}
  230. if err := inspectFieldAndMarshall(id, "Config", &config1); err != nil {
  231. c.Fatal(err)
  232. }
  233. config2 := cfg{}
  234. if err := inspectFieldAndMarshall(name, "Config", &config2); err != nil {
  235. c.Fatal(err)
  236. }
  237. // Env has at least PATH loaded as well here, so let's just grab the FOO one
  238. var env1, env2 string
  239. for _, e := range config1.Env {
  240. if strings.HasPrefix(e, "FOO") {
  241. env1 = e
  242. break
  243. }
  244. }
  245. for _, e := range config2.Env {
  246. if strings.HasPrefix(e, "FOO") {
  247. env2 = e
  248. break
  249. }
  250. }
  251. if len(config1.Env) != len(config2.Env) || env1 != env2 && env2 != "" {
  252. c.Fatalf("expected envs to match: %v - %v", config1.Env, config2.Env)
  253. }
  254. }