docker_cli_commit_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
  59. if _, err := runCommand(cmd); err != nil {
  60. t.Fatal(err)
  61. }
  62. cmd = exec.Command(dockerBinary, "commit", "commiter")
  63. imageID, _, err := runCommandWithOutput(cmd)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. imageID = strings.Trim(imageID, "\r\n")
  68. cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo")
  69. out, _, err := runCommandWithOutput(cmd)
  70. if err != nil {
  71. t.Fatal(err, out)
  72. }
  73. if actual := strings.Trim(out, "\r\n"); actual != "koye" {
  74. t.Fatalf("expected output koye received %q", actual)
  75. }
  76. deleteAllContainers()
  77. deleteImages(imageID)
  78. logDone("commit - commit file and read")
  79. }
  80. func TestCommitHardlink(t *testing.T) {
  81. cmd := exec.Command(dockerBinary, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
  82. firstOuput, _, err := runCommandWithOutput(cmd)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. chunks := strings.Split(strings.TrimSpace(firstOuput), " ")
  87. inode := chunks[0]
  88. found := false
  89. for _, chunk := range chunks[1:] {
  90. if chunk == inode {
  91. found = true
  92. break
  93. }
  94. }
  95. if !found {
  96. t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  97. }
  98. cmd = exec.Command(dockerBinary, "commit", "hardlinks", "hardlinks")
  99. imageID, _, err := runCommandWithOutput(cmd)
  100. if err != nil {
  101. t.Fatal(imageID, err)
  102. }
  103. imageID = strings.Trim(imageID, "\r\n")
  104. cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
  105. secondOuput, _, err := runCommandWithOutput(cmd)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. chunks = strings.Split(strings.TrimSpace(secondOuput), " ")
  110. inode = chunks[0]
  111. found = false
  112. for _, chunk := range chunks[1:] {
  113. if chunk == inode {
  114. found = true
  115. break
  116. }
  117. }
  118. if !found {
  119. t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
  120. }
  121. deleteAllContainers()
  122. deleteImages(imageID)
  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. cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
  146. if _, err := runCommand(cmd); err != nil {
  147. t.Fatal(err)
  148. }
  149. cmd = exec.Command(dockerBinary, "commit", "bind-commit", "bindtest")
  150. imageID, _, err := runCommandWithOutput(cmd)
  151. if err != nil {
  152. t.Fatal(imageID, err)
  153. }
  154. imageID = strings.Trim(imageID, "\r\n")
  155. cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
  156. if _, err := runCommand(cmd); err != nil {
  157. t.Fatal(err)
  158. }
  159. deleteAllContainers()
  160. deleteImages(imageID)
  161. logDone("commit - commit bind mounted file")
  162. }