docker_cli_commit_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 TestCommitNewFile(t *testing.T) {
  28. cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
  29. if _, err := runCommand(cmd); err != nil {
  30. t.Fatal(err)
  31. }
  32. cmd = exec.Command(dockerBinary, "commit", "commiter")
  33. imageId, _, err := runCommandWithOutput(cmd)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. imageId = strings.Trim(imageId, "\r\n")
  38. cmd = exec.Command(dockerBinary, "run", imageId, "cat", "/foo")
  39. out, _, err := runCommandWithOutput(cmd)
  40. if err != nil {
  41. t.Fatal(err, out)
  42. }
  43. if actual := strings.Trim(out, "\r\n"); actual != "koye" {
  44. t.Fatalf("expected output koye received %s", actual)
  45. }
  46. deleteAllContainers()
  47. deleteImages(imageId)
  48. logDone("commit - commit file and read")
  49. }
  50. func TestCommitTTY(t *testing.T) {
  51. cmd := exec.Command(dockerBinary, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
  52. if _, err := runCommand(cmd); err != nil {
  53. t.Fatal(err)
  54. }
  55. cmd = exec.Command(dockerBinary, "commit", "tty", "ttytest")
  56. imageId, _, err := runCommandWithOutput(cmd)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. imageId = strings.Trim(imageId, "\r\n")
  61. cmd = exec.Command(dockerBinary, "run", "ttytest", "/bin/ls")
  62. if _, err := runCommand(cmd); err != nil {
  63. t.Fatal(err)
  64. }
  65. }