docker_cli_history_test.go 3.9 KB

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