docker_cli_history_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. "github.com/docker/docker/integration-cli/checker"
  9. "github.com/docker/docker/integration-cli/cli/build"
  10. "github.com/go-check/check"
  11. "gotest.tools/assert"
  12. "gotest.tools/assert/cmp"
  13. )
  14. // This is a heisen-test. Because the created timestamp of images and the behavior of
  15. // sort is not predictable it doesn't always fail.
  16. func (s *DockerSuite) TestBuildHistory(c *testing.T) {
  17. name := "testbuildhistory"
  18. buildImageSuccessfully(c, name, build.WithDockerfile(`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. out, _ := dockerCmd(c, "history", name)
  46. actualValues := strings.Split(out, "\n")[1:27]
  47. 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"}
  48. for i := 0; i < 26; i++ {
  49. echoValue := fmt.Sprintf("LABEL label.%s=%s", expectedValues[i], expectedValues[i])
  50. actualValue := actualValues[i]
  51. assert.Assert(c, actualValue, checker.Contains, echoValue)
  52. }
  53. }
  54. func (s *DockerSuite) TestHistoryExistentImage(c *testing.T) {
  55. dockerCmd(c, "history", "busybox")
  56. }
  57. func (s *DockerSuite) TestHistoryNonExistentImage(c *testing.T) {
  58. _, _, err := dockerCmdWithError("history", "testHistoryNonExistentImage")
  59. assert.Assert(c, err != nil, check.Commentf("history on a non-existent image should fail."))
  60. }
  61. func (s *DockerSuite) TestHistoryImageWithComment(c *testing.T) {
  62. name := "testhistoryimagewithcomment"
  63. // make an image through docker commit <container id> [ -m messages ]
  64. dockerCmd(c, "run", "--name", name, "busybox", "true")
  65. dockerCmd(c, "wait", name)
  66. comment := "This_is_a_comment"
  67. dockerCmd(c, "commit", "-m="+comment, name, name)
  68. // test docker history <image id> to check comment messages
  69. out, _ := dockerCmd(c, "history", name)
  70. outputTabs := strings.Fields(strings.Split(out, "\n")[1])
  71. actualValue := outputTabs[len(outputTabs)-1]
  72. assert.Assert(c, actualValue, checker.Contains, comment)
  73. }
  74. func (s *DockerSuite) TestHistoryHumanOptionFalse(c *testing.T) {
  75. out, _ := dockerCmd(c, "history", "--human=false", "busybox")
  76. lines := strings.Split(out, "\n")
  77. sizeColumnRegex, _ := regexp.Compile("SIZE +")
  78. indices := sizeColumnRegex.FindStringIndex(lines[0])
  79. startIndex := indices[0]
  80. endIndex := indices[1]
  81. for i := 1; i < len(lines)-1; i++ {
  82. if endIndex > len(lines[i]) {
  83. endIndex = len(lines[i])
  84. }
  85. sizeString := lines[i][startIndex:endIndex]
  86. _, err := strconv.Atoi(strings.TrimSpace(sizeString))
  87. assert.Assert(c, err == nil, check.Commentf("The size '%s' was not an Integer", sizeString))
  88. }
  89. }
  90. func (s *DockerSuite) TestHistoryHumanOptionTrue(c *testing.T) {
  91. out, _ := dockerCmd(c, "history", "--human=true", "busybox")
  92. lines := strings.Split(out, "\n")
  93. sizeColumnRegex, _ := regexp.Compile("SIZE +")
  94. humanSizeRegexRaw := "\\d+.*B" // Matches human sizes like 10 MB, 3.2 KB, etc
  95. indices := sizeColumnRegex.FindStringIndex(lines[0])
  96. startIndex := indices[0]
  97. endIndex := indices[1]
  98. for i := 1; i < len(lines)-1; i++ {
  99. if endIndex > len(lines[i]) {
  100. endIndex = len(lines[i])
  101. }
  102. sizeString := lines[i][startIndex:endIndex]
  103. assert.Assert(c, cmp.Regexp("^"+
  104. humanSizeRegexRaw+
  105. "$",
  106. strings.TrimSpace(sizeString)), check.Commentf("The size '%s' was not in human format", sizeString))
  107. }
  108. }