docker_cli_commit_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. )
  8. func TestCommitAfterContainerIsDone(t *testing.T) {
  9. runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
  10. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  11. errorOut(err, t, fmt.Sprintf("failed to run container: %v %v", out, err))
  12. cleanedContainerID := stripTrailingCharacters(out)
  13. waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
  14. _, _, err = runCommandWithOutput(waitCmd)
  15. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  16. commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
  17. out, _, err = runCommandWithOutput(commitCmd)
  18. errorOut(err, t, fmt.Sprintf("failed to commit container to image: %v %v", out, err))
  19. cleanedImageID := stripTrailingCharacters(out)
  20. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
  21. out, _, err = runCommandWithOutput(inspectCmd)
  22. errorOut(err, t, fmt.Sprintf("failed to inspect image: %v %v", out, err))
  23. deleteContainer(cleanedContainerID)
  24. deleteImages(cleanedImageID)
  25. logDone("commit - echo foo and commit the image")
  26. }
  27. func TestCommitWithoutPause(t *testing.T) {
  28. runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
  29. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  30. errorOut(err, t, fmt.Sprintf("failed to run container: %v %v", out, err))
  31. cleanedContainerID := stripTrailingCharacters(out)
  32. waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
  33. _, _, err = runCommandWithOutput(waitCmd)
  34. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  35. commitCmd := exec.Command(dockerBinary, "commit", "-p=false", cleanedContainerID)
  36. out, _, err = runCommandWithOutput(commitCmd)
  37. errorOut(err, t, fmt.Sprintf("failed to commit container to image: %v %v", out, err))
  38. cleanedImageID := stripTrailingCharacters(out)
  39. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
  40. out, _, err = runCommandWithOutput(inspectCmd)
  41. errorOut(err, t, fmt.Sprintf("failed to inspect image: %v %v", out, err))
  42. deleteContainer(cleanedContainerID)
  43. deleteImages(cleanedImageID)
  44. logDone("commit - echo foo and commit the image with --pause=false")
  45. }
  46. func TestCommitNewFile(t *testing.T) {
  47. cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
  48. if _, err := runCommand(cmd); err != nil {
  49. t.Fatal(err)
  50. }
  51. cmd = exec.Command(dockerBinary, "commit", "commiter")
  52. imageId, _, err := runCommandWithOutput(cmd)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. imageId = strings.Trim(imageId, "\r\n")
  57. cmd = exec.Command(dockerBinary, "run", imageId, "cat", "/foo")
  58. out, _, err := runCommandWithOutput(cmd)
  59. if err != nil {
  60. t.Fatal(err, out)
  61. }
  62. if actual := strings.Trim(out, "\r\n"); actual != "koye" {
  63. t.Fatalf("expected output koye received %s", actual)
  64. }
  65. deleteAllContainers()
  66. deleteImages(imageId)
  67. logDone("commit - commit file and read")
  68. }
  69. func TestCommitTTY(t *testing.T) {
  70. cmd := exec.Command(dockerBinary, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
  71. if _, err := runCommand(cmd); err != nil {
  72. t.Fatal(err)
  73. }
  74. cmd = exec.Command(dockerBinary, "commit", "tty", "ttytest")
  75. imageId, _, err := runCommandWithOutput(cmd)
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. imageId = strings.Trim(imageId, "\r\n")
  80. cmd = exec.Command(dockerBinary, "run", "ttytest", "/bin/ls")
  81. if _, err := runCommand(cmd); err != nil {
  82. t.Fatal(err)
  83. }
  84. logDone("commit - commit tty")
  85. }
  86. func TestCommitWithHostBindMount(t *testing.T) {
  87. cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
  88. if _, err := runCommand(cmd); err != nil {
  89. t.Fatal(err)
  90. }
  91. cmd = exec.Command(dockerBinary, "commit", "bind-commit", "bindtest")
  92. imageId, _, err := runCommandWithOutput(cmd)
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. imageId = strings.Trim(imageId, "\r\n")
  97. cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
  98. if _, err := runCommand(cmd); err != nil {
  99. t.Fatal(err)
  100. }
  101. deleteAllContainers()
  102. deleteImages(imageId)
  103. logDone("commit - commit bind mounted file")
  104. }