docker_cli_history_test.go 3.9 KB

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