docker_cli_commit_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. )
  7. func TestCommitAfterContainerIsDone(t *testing.T) {
  8. runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
  9. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  10. if err != nil {
  11. t.Fatalf("failed to run container: %s, %v", out, err)
  12. }
  13. cleanedContainerID := stripTrailingCharacters(out)
  14. waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
  15. if _, _, err = runCommandWithOutput(waitCmd); err != nil {
  16. t.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. t.Fatalf("failed to commit container to image: %s, %v", out, err)
  22. }
  23. cleanedImageID := stripTrailingCharacters(out)
  24. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
  25. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  26. t.Fatalf("failed to inspect image: %s, %v", out, err)
  27. }
  28. deleteContainer(cleanedContainerID)
  29. deleteImages(cleanedImageID)
  30. logDone("commit - echo foo and commit the image")
  31. }
  32. func TestCommitWithoutPause(t *testing.T) {
  33. runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
  34. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  35. if err != nil {
  36. t.Fatalf("failed to run container: %s, %v", out, err)
  37. }
  38. cleanedContainerID := stripTrailingCharacters(out)
  39. waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
  40. if _, _, err = runCommandWithOutput(waitCmd); err != nil {
  41. t.Fatalf("error thrown while waiting for container: %s, %v", out, err)
  42. }
  43. commitCmd := exec.Command(dockerBinary, "commit", "-p=false", cleanedContainerID)
  44. out, _, err = runCommandWithOutput(commitCmd)
  45. if err != nil {
  46. t.Fatalf("failed to commit container to image: %s, %v", out, err)
  47. }
  48. cleanedImageID := stripTrailingCharacters(out)
  49. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
  50. if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
  51. t.Fatalf("failed to inspect image: %s, %v", out, err)
  52. }
  53. deleteContainer(cleanedContainerID)
  54. deleteImages(cleanedImageID)
  55. logDone("commit - echo foo and commit the image with --pause=false")
  56. }
  57. //test commit a paused container should not unpause it after commit
  58. func TestCommitPausedContainer(t *testing.T) {
  59. defer deleteAllContainers()
  60. defer unpauseAllContainers()
  61. cmd := exec.Command(dockerBinary, "run", "-i", "-d", "busybox")
  62. out, _, _, err := runCommandWithStdoutStderr(cmd)
  63. if err != nil {
  64. t.Fatalf("failed to run container: %v, output: %q", err, out)
  65. }
  66. cleanedContainerID := stripTrailingCharacters(out)
  67. cmd = exec.Command(dockerBinary, "pause", cleanedContainerID)
  68. out, _, _, err = runCommandWithStdoutStderr(cmd)
  69. if err != nil {
  70. t.Fatalf("failed to pause container: %v, output: %q", err, out)
  71. }
  72. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
  73. out, _, err = runCommandWithOutput(commitCmd)
  74. if err != nil {
  75. t.Fatalf("failed to commit container to image: %s, %v", out, err)
  76. }
  77. cleanedImageID := stripTrailingCharacters(out)
  78. defer deleteImages(cleanedImageID)
  79. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Paused}}", cleanedContainerID)
  80. out, _, _, err = runCommandWithStdoutStderr(cmd)
  81. if err != nil {
  82. t.Fatalf("failed to inspect container: %v, output: %q", err, out)
  83. }
  84. if !strings.Contains(out, "true") {
  85. t.Fatalf("commit should not unpause a paused container")
  86. }
  87. logDone("commit - commit a paused container will not unpause it")
  88. }
  89. func TestCommitNewFile(t *testing.T) {
  90. defer deleteAllContainers()
  91. cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
  92. if _, err := runCommand(cmd); err != nil {
  93. t.Fatal(err)
  94. }
  95. cmd = exec.Command(dockerBinary, "commit", "commiter")
  96. imageID, _, err := runCommandWithOutput(cmd)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. imageID = strings.Trim(imageID, "\r\n")
  101. defer deleteImages(imageID)
  102. cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo")
  103. out, _, err := runCommandWithOutput(cmd)
  104. if err != nil {
  105. t.Fatal(err, out)
  106. }
  107. if actual := strings.Trim(out, "\r\n"); actual != "koye" {
  108. t.Fatalf("expected output koye received %q", actual)
  109. }
  110. logDone("commit - commit file and read")
  111. }
  112. func TestCommitHardlink(t *testing.T) {
  113. defer deleteAllContainers()
  114. cmd := exec.Command(dockerBinary, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
  115. firstOuput, _, err := runCommandWithOutput(cmd)
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. chunks := strings.Split(strings.TrimSpace(firstOuput), " ")
  120. inode := chunks[0]
  121. found := false
  122. for _, chunk := range chunks[1:] {
  123. if chunk == inode {
  124. found = true
  125. break
  126. }
  127. }
  128. if !found {
  129. t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  130. }
  131. cmd = exec.Command(dockerBinary, "commit", "hardlinks", "hardlinks")
  132. imageID, _, err := runCommandWithOutput(cmd)
  133. if err != nil {
  134. t.Fatal(imageID, err)
  135. }
  136. imageID = strings.Trim(imageID, "\r\n")
  137. defer deleteImages(imageID)
  138. cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
  139. secondOuput, _, err := runCommandWithOutput(cmd)
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. chunks = strings.Split(strings.TrimSpace(secondOuput), " ")
  144. inode = chunks[0]
  145. found = false
  146. for _, chunk := range chunks[1:] {
  147. if chunk == inode {
  148. found = true
  149. break
  150. }
  151. }
  152. if !found {
  153. t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  154. }
  155. logDone("commit - commit hardlinks")
  156. }
  157. func TestCommitTTY(t *testing.T) {
  158. defer deleteImages("ttytest")
  159. defer deleteAllContainers()
  160. cmd := exec.Command(dockerBinary, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
  161. if _, err := runCommand(cmd); err != nil {
  162. t.Fatal(err)
  163. }
  164. cmd = exec.Command(dockerBinary, "commit", "tty", "ttytest")
  165. imageID, _, err := runCommandWithOutput(cmd)
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. imageID = strings.Trim(imageID, "\r\n")
  170. cmd = exec.Command(dockerBinary, "run", "ttytest", "/bin/ls")
  171. if _, err := runCommand(cmd); err != nil {
  172. t.Fatal(err)
  173. }
  174. logDone("commit - commit tty")
  175. }
  176. func TestCommitWithHostBindMount(t *testing.T) {
  177. defer deleteAllContainers()
  178. cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
  179. if _, err := runCommand(cmd); err != nil {
  180. t.Fatal(err)
  181. }
  182. cmd = exec.Command(dockerBinary, "commit", "bind-commit", "bindtest")
  183. imageID, _, err := runCommandWithOutput(cmd)
  184. if err != nil {
  185. t.Fatal(imageID, err)
  186. }
  187. imageID = strings.Trim(imageID, "\r\n")
  188. defer deleteImages(imageID)
  189. cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
  190. if _, err := runCommand(cmd); err != nil {
  191. t.Fatal(err)
  192. }
  193. logDone("commit - commit bind mounted file")
  194. }