docker_cli_history_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "github.com/go-check/check"
  7. )
  8. // This is a heisen-test. Because the created timestamp of images and the behavior of
  9. // sort is not predictable it doesn't always fail.
  10. func (s *DockerSuite) TestBuildHistory(c *check.C) {
  11. name := "testbuildhistory"
  12. defer deleteImages(name)
  13. _, err := buildImage(name, `FROM busybox
  14. RUN echo "A"
  15. RUN echo "B"
  16. RUN echo "C"
  17. RUN echo "D"
  18. RUN echo "E"
  19. RUN echo "F"
  20. RUN echo "G"
  21. RUN echo "H"
  22. RUN echo "I"
  23. RUN echo "J"
  24. RUN echo "K"
  25. RUN echo "L"
  26. RUN echo "M"
  27. RUN echo "N"
  28. RUN echo "O"
  29. RUN echo "P"
  30. RUN echo "Q"
  31. RUN echo "R"
  32. RUN echo "S"
  33. RUN echo "T"
  34. RUN echo "U"
  35. RUN echo "V"
  36. RUN echo "W"
  37. RUN echo "X"
  38. RUN echo "Y"
  39. RUN echo "Z"`,
  40. true)
  41. if err != nil {
  42. c.Fatal(err)
  43. }
  44. out, exitCode, err := runCommandWithOutput(exec.Command(dockerBinary, "history", "testbuildhistory"))
  45. if err != nil || exitCode != 0 {
  46. c.Fatalf("failed to get image history: %s, %v", out, err)
  47. }
  48. actualValues := strings.Split(out, "\n")[1:27]
  49. expectedValues := [26]string{"Z", "Y", "X", "W", "V", "U", "T", "S", "R", "Q", "P", "O", "N", "M", "L", "K", "J", "I", "H", "G", "F", "E", "D", "C", "B", "A"}
  50. for i := 0; i < 26; i++ {
  51. echoValue := fmt.Sprintf("echo \"%s\"", expectedValues[i])
  52. actualValue := actualValues[i]
  53. if !strings.Contains(actualValue, echoValue) {
  54. c.Fatalf("Expected layer \"%s\", but was: %s", expectedValues[i], actualValue)
  55. }
  56. }
  57. }
  58. func (s *DockerSuite) TestHistoryExistentImage(c *check.C) {
  59. historyCmd := exec.Command(dockerBinary, "history", "busybox")
  60. _, exitCode, err := runCommandWithOutput(historyCmd)
  61. if err != nil || exitCode != 0 {
  62. c.Fatal("failed to get image history")
  63. }
  64. }
  65. func (s *DockerSuite) TestHistoryNonExistentImage(c *check.C) {
  66. historyCmd := exec.Command(dockerBinary, "history", "testHistoryNonExistentImage")
  67. _, exitCode, err := runCommandWithOutput(historyCmd)
  68. if err == nil || exitCode == 0 {
  69. c.Fatal("history on a non-existent image didn't result in a non-zero exit status")
  70. }
  71. }
  72. func (s *DockerSuite) TestHistoryImageWithComment(c *check.C) {
  73. name := "testhistoryimagewithcomment"
  74. defer deleteContainer(name)
  75. defer deleteImages(name)
  76. // make a image through docker commit <container id> [ -m messages ]
  77. //runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
  78. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "true")
  79. out, _, err := runCommandWithOutput(runCmd)
  80. if err != nil {
  81. c.Fatalf("failed to run container: %s, %v", out, err)
  82. }
  83. waitCmd := exec.Command(dockerBinary, "wait", name)
  84. if out, _, err := runCommandWithOutput(waitCmd); err != nil {
  85. c.Fatalf("error thrown while waiting for container: %s, %v", out, err)
  86. }
  87. comment := "This_is_a_comment"
  88. commitCmd := exec.Command(dockerBinary, "commit", "-m="+comment, name, name)
  89. if out, _, err := runCommandWithOutput(commitCmd); err != nil {
  90. c.Fatalf("failed to commit container to image: %s, %v", out, err)
  91. }
  92. // test docker history <image id> to check comment messages
  93. historyCmd := exec.Command(dockerBinary, "history", name)
  94. out, exitCode, err := runCommandWithOutput(historyCmd)
  95. if err != nil || exitCode != 0 {
  96. c.Fatalf("failed to get image history: %s, %v", out, err)
  97. }
  98. outputTabs := strings.Fields(strings.Split(out, "\n")[1])
  99. //outputTabs := regexp.MustCompile(" +").Split(outputLine, -1)
  100. actualValue := outputTabs[len(outputTabs)-1]
  101. if !strings.Contains(actualValue, comment) {
  102. c.Fatalf("Expected comments %q, but found %q", comment, actualValue)
  103. }
  104. }