docker_cli_commit_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. func TestCommitNewFile(t *testing.T) {
  58. defer deleteAllContainers()
  59. cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
  60. if _, err := runCommand(cmd); err != nil {
  61. t.Fatal(err)
  62. }
  63. cmd = exec.Command(dockerBinary, "commit", "commiter")
  64. imageID, _, err := runCommandWithOutput(cmd)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. imageID = strings.Trim(imageID, "\r\n")
  69. defer deleteImages(imageID)
  70. cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo")
  71. out, _, err := runCommandWithOutput(cmd)
  72. if err != nil {
  73. t.Fatal(err, out)
  74. }
  75. if actual := strings.Trim(out, "\r\n"); actual != "koye" {
  76. t.Fatalf("expected output koye received %q", actual)
  77. }
  78. logDone("commit - commit file and read")
  79. }
  80. func TestCommitHardlink(t *testing.T) {
  81. defer deleteAllContainers()
  82. cmd := exec.Command(dockerBinary, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
  83. firstOuput, _, err := runCommandWithOutput(cmd)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. chunks := strings.Split(strings.TrimSpace(firstOuput), " ")
  88. inode := chunks[0]
  89. found := false
  90. for _, chunk := range chunks[1:] {
  91. if chunk == inode {
  92. found = true
  93. break
  94. }
  95. }
  96. if !found {
  97. t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  98. }
  99. cmd = exec.Command(dockerBinary, "commit", "hardlinks", "hardlinks")
  100. imageID, _, err := runCommandWithOutput(cmd)
  101. if err != nil {
  102. t.Fatal(imageID, err)
  103. }
  104. imageID = strings.Trim(imageID, "\r\n")
  105. defer deleteImages(imageID)
  106. cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
  107. secondOuput, _, err := runCommandWithOutput(cmd)
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. chunks = strings.Split(strings.TrimSpace(secondOuput), " ")
  112. inode = chunks[0]
  113. found = false
  114. for _, chunk := range chunks[1:] {
  115. if chunk == inode {
  116. found = true
  117. break
  118. }
  119. }
  120. if !found {
  121. t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  122. }
  123. logDone("commit - commit hardlinks")
  124. }
  125. func TestCommitTTY(t *testing.T) {
  126. defer deleteImages("ttytest")
  127. defer deleteAllContainers()
  128. cmd := exec.Command(dockerBinary, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
  129. if _, err := runCommand(cmd); err != nil {
  130. t.Fatal(err)
  131. }
  132. cmd = exec.Command(dockerBinary, "commit", "tty", "ttytest")
  133. imageID, _, err := runCommandWithOutput(cmd)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. imageID = strings.Trim(imageID, "\r\n")
  138. cmd = exec.Command(dockerBinary, "run", "ttytest", "/bin/ls")
  139. if _, err := runCommand(cmd); err != nil {
  140. t.Fatal(err)
  141. }
  142. logDone("commit - commit tty")
  143. }
  144. func TestCommitWithHostBindMount(t *testing.T) {
  145. defer deleteAllContainers()
  146. cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
  147. if _, err := runCommand(cmd); err != nil {
  148. t.Fatal(err)
  149. }
  150. cmd = exec.Command(dockerBinary, "commit", "bind-commit", "bindtest")
  151. imageID, _, err := runCommandWithOutput(cmd)
  152. if err != nil {
  153. t.Fatal(imageID, err)
  154. }
  155. imageID = strings.Trim(imageID, "\r\n")
  156. defer deleteImages(imageID)
  157. cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
  158. if _, err := runCommand(cmd); err != nil {
  159. t.Fatal(err)
  160. }
  161. logDone("commit - commit bind mounted file")
  162. }
  163. func TestCommitChange(t *testing.T) {
  164. defer deleteAllContainers()
  165. cmd(t, "run", "--name", "test", "busybox", "true")
  166. cmd := exec.Command(dockerBinary, "commit",
  167. "--change", "EXPOSE 8080",
  168. "--change", "ENV DEBUG true",
  169. "test", "test-commit")
  170. imageId, _, err := runCommandWithOutput(cmd)
  171. if err != nil {
  172. t.Fatal(imageId, err)
  173. }
  174. imageId = strings.Trim(imageId, "\r\n")
  175. defer deleteImages(imageId)
  176. expected := map[string]string{
  177. "Config.ExposedPorts": "map[8080/tcp:map[]]",
  178. "Config.Env": "[DEBUG=true PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]",
  179. }
  180. for conf, value := range expected {
  181. res, err := inspectField(imageId, conf)
  182. if err != nil {
  183. t.Errorf("failed to get value %s, error: %s", conf, err)
  184. }
  185. if res != value {
  186. t.Errorf("%s('%s'), expected %s", conf, res, value)
  187. }
  188. }
  189. logDone("commit - commit --change")
  190. }