docker_cli_commit_test.go 8.0 KB

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