docker_cli_history_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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) // TODO Windows: This test passes on Windows,
  14. // but currently adds a disproportionate amount of time for the value it has.
  15. // Removing it from Windows CI for now, but this will be revisited in the
  16. // TP5 timeframe when perf is better.
  17. name := "testbuildhistory"
  18. _, err := buildImage(name, `FROM `+minimalBaseImage()+`
  19. LABEL label.A="A"
  20. LABEL label.B="B"
  21. LABEL label.C="C"
  22. LABEL label.D="D"
  23. LABEL label.E="E"
  24. LABEL label.F="F"
  25. LABEL label.G="G"
  26. LABEL label.H="H"
  27. LABEL label.I="I"
  28. LABEL label.J="J"
  29. LABEL label.K="K"
  30. LABEL label.L="L"
  31. LABEL label.M="M"
  32. LABEL label.N="N"
  33. LABEL label.O="O"
  34. LABEL label.P="P"
  35. LABEL label.Q="Q"
  36. LABEL label.R="R"
  37. LABEL label.S="S"
  38. LABEL label.T="T"
  39. LABEL label.U="U"
  40. LABEL label.V="V"
  41. LABEL label.W="W"
  42. LABEL label.X="X"
  43. LABEL label.Y="Y"
  44. LABEL label.Z="Z"`,
  45. true)
  46. c.Assert(err, checker.IsNil)
  47. out, _ := dockerCmd(c, "history", "testbuildhistory")
  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("LABEL label.%s=%s", expectedValues[i], expectedValues[i])
  52. actualValue := actualValues[i]
  53. c.Assert(actualValue, checker.Contains, echoValue)
  54. }
  55. }
  56. func (s *DockerSuite) TestHistoryExistentImage(c *check.C) {
  57. dockerCmd(c, "history", "busybox")
  58. }
  59. func (s *DockerSuite) TestHistoryNonExistentImage(c *check.C) {
  60. _, _, err := dockerCmdWithError("history", "testHistoryNonExistentImage")
  61. c.Assert(err, checker.NotNil, check.Commentf("history on a non-existent image should fail."))
  62. }
  63. func (s *DockerSuite) TestHistoryImageWithComment(c *check.C) {
  64. name := "testhistoryimagewithcomment"
  65. // make an image through docker commit <container id> [ -m messages ]
  66. dockerCmd(c, "run", "--name", name, "busybox", "true")
  67. dockerCmd(c, "wait", name)
  68. comment := "This_is_a_comment"
  69. dockerCmd(c, "commit", "-m="+comment, name, name)
  70. // test docker history <image id> to check comment messages
  71. out, _ := dockerCmd(c, "history", name)
  72. outputTabs := strings.Fields(strings.Split(out, "\n")[1])
  73. actualValue := outputTabs[len(outputTabs)-1]
  74. c.Assert(actualValue, checker.Contains, comment)
  75. }
  76. func (s *DockerSuite) TestHistoryHumanOptionFalse(c *check.C) {
  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. out, _ := dockerCmd(c, "history", "--human=true", "busybox")
  94. lines := strings.Split(out, "\n")
  95. sizeColumnRegex, _ := regexp.Compile("SIZE +")
  96. humanSizeRegexRaw := "\\d+.*B" // Matches human sizes like 10 MB, 3.2 KB, etc
  97. indices := sizeColumnRegex.FindStringIndex(lines[0])
  98. startIndex := indices[0]
  99. endIndex := indices[1]
  100. for i := 1; i < len(lines)-1; i++ {
  101. if endIndex > len(lines[i]) {
  102. endIndex = len(lines[i])
  103. }
  104. sizeString := lines[i][startIndex:endIndex]
  105. c.Assert(strings.TrimSpace(sizeString), checker.Matches, humanSizeRegexRaw, check.Commentf("The size '%s' was not in human format", sizeString))
  106. }
  107. }