docker_cli_commit_test.go 8.2 KB

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