docker_cli_history_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. "github.com/docker/docker/pkg/integration/checker"
  8. "github.com/go-check/check"
  9. )
  10. // This is a heisen-test. Because the created timestamp of images and the behavior of
  11. // sort is not predictable it doesn't always fail.
  12. func (s *DockerSuite) TestBuildHistory(c *check.C) {
  13. testRequires(c, DaemonIsLinux)
  14. name := "testbuildhistory"
  15. _, err := buildImage(name, `FROM busybox
  16. RUN echo "A"
  17. RUN echo "B"
  18. RUN echo "C"
  19. RUN echo "D"
  20. RUN echo "E"
  21. RUN echo "F"
  22. RUN echo "G"
  23. RUN echo "H"
  24. RUN echo "I"
  25. RUN echo "J"
  26. RUN echo "K"
  27. RUN echo "L"
  28. RUN echo "M"
  29. RUN echo "N"
  30. RUN echo "O"
  31. RUN echo "P"
  32. RUN echo "Q"
  33. RUN echo "R"
  34. RUN echo "S"
  35. RUN echo "T"
  36. RUN echo "U"
  37. RUN echo "V"
  38. RUN echo "W"
  39. RUN echo "X"
  40. RUN echo "Y"
  41. RUN echo "Z"`,
  42. true)
  43. c.Assert(err, checker.IsNil)
  44. out, _ := dockerCmd(c, "history", "testbuildhistory")
  45. actualValues := strings.Split(out, "\n")[1:27]
  46. 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"}
  47. for i := 0; i < 26; i++ {
  48. echoValue := fmt.Sprintf("echo \"%s\"", expectedValues[i])
  49. actualValue := actualValues[i]
  50. c.Assert(actualValue, checker.Contains, echoValue)
  51. }
  52. }
  53. func (s *DockerSuite) TestHistoryExistentImage(c *check.C) {
  54. testRequires(c, DaemonIsLinux)
  55. dockerCmd(c, "history", "busybox")
  56. }
  57. func (s *DockerSuite) TestHistoryNonExistentImage(c *check.C) {
  58. _, _, err := dockerCmdWithError("history", "testHistoryNonExistentImage")
  59. c.Assert(err, checker.NotNil, check.Commentf("history on a non-existent image should fail."))
  60. }
  61. func (s *DockerSuite) TestHistoryImageWithComment(c *check.C) {
  62. testRequires(c, DaemonIsLinux)
  63. name := "testhistoryimagewithcomment"
  64. // make a image through docker commit <container id> [ -m messages ]
  65. dockerCmd(c, "run", "--name", name, "busybox", "true")
  66. dockerCmd(c, "wait", name)
  67. comment := "This_is_a_comment"
  68. dockerCmd(c, "commit", "-m="+comment, name, name)
  69. // test docker history <image id> to check comment messages
  70. out, _ := dockerCmd(c, "history", name)
  71. outputTabs := strings.Fields(strings.Split(out, "\n")[1])
  72. actualValue := outputTabs[len(outputTabs)-1]
  73. c.Assert(actualValue, checker.Contains, comment)
  74. }
  75. func (s *DockerSuite) TestHistoryHumanOptionFalse(c *check.C) {
  76. testRequires(c, DaemonIsLinux)
  77. out, _ := dockerCmd(c, "history", "--human=false", "busybox")
  78. lines := strings.Split(out, "\n")
  79. sizeColumnRegex, _ := regexp.Compile("SIZE +")
  80. indices := sizeColumnRegex.FindStringIndex(lines[0])
  81. startIndex := indices[0]
  82. endIndex := indices[1]
  83. for i := 1; i < len(lines)-1; i++ {
  84. if endIndex > len(lines[i]) {
  85. endIndex = len(lines[i])
  86. }
  87. sizeString := lines[i][startIndex:endIndex]
  88. _, err := strconv.Atoi(strings.TrimSpace(sizeString))
  89. c.Assert(err, checker.IsNil, check.Commentf("The size '%s' was not an Integer", sizeString))
  90. }
  91. }
  92. func (s *DockerSuite) TestHistoryHumanOptionTrue(c *check.C) {
  93. testRequires(c, DaemonIsLinux)
  94. out, _ := dockerCmd(c, "history", "--human=true", "busybox")
  95. lines := strings.Split(out, "\n")
  96. sizeColumnRegex, _ := regexp.Compile("SIZE +")
  97. humanSizeRegexRaw := "\\d+.*B" // Matches human sizes like 10 MB, 3.2 KB, etc
  98. indices := sizeColumnRegex.FindStringIndex(lines[0])
  99. startIndex := indices[0]
  100. endIndex := indices[1]
  101. for i := 1; i < len(lines)-1; i++ {
  102. if endIndex > len(lines[i]) {
  103. endIndex = len(lines[i])
  104. }
  105. sizeString := lines[i][startIndex:endIndex]
  106. c.Assert(strings.TrimSpace(sizeString), checker.Matches, humanSizeRegexRaw, check.Commentf("The size '%s' was not in human format", sizeString))
  107. }
  108. }