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