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