docker_cli_history_test.go 3.5 KB

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