docker_cli_history_test.go 4.0 KB

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