docker_cli_history_test.go 3.6 KB

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