docker_cli_commit_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 TestCommitTTY(t *testing.T) {
  81. cmd := exec.Command(dockerBinary, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
  82. if _, err := runCommand(cmd); err != nil {
  83. t.Fatal(err)
  84. }
  85. cmd = exec.Command(dockerBinary, "commit", "tty", "ttytest")
  86. imageID, _, err := runCommandWithOutput(cmd)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. imageID = strings.Trim(imageID, "\r\n")
  91. cmd = exec.Command(dockerBinary, "run", "ttytest", "/bin/ls")
  92. if _, err := runCommand(cmd); err != nil {
  93. t.Fatal(err)
  94. }
  95. logDone("commit - commit tty")
  96. }
  97. func TestCommitWithHostBindMount(t *testing.T) {
  98. cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
  99. if _, err := runCommand(cmd); err != nil {
  100. t.Fatal(err)
  101. }
  102. cmd = exec.Command(dockerBinary, "commit", "bind-commit", "bindtest")
  103. imageID, _, err := runCommandWithOutput(cmd)
  104. if err != nil {
  105. t.Fatal(imageID, err)
  106. }
  107. imageID = strings.Trim(imageID, "\r\n")
  108. cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
  109. if _, err := runCommand(cmd); err != nil {
  110. t.Fatal(err)
  111. }
  112. deleteAllContainers()
  113. deleteImages(imageID)
  114. logDone("commit - commit bind mounted file")
  115. }