github.com/hms58/moby@v1.13.1/integration-cli/docker_cli_history_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "regexp" 6 "strconv" 7 "strings" 8 9 "github.com/docker/docker/pkg/integration/checker" 10 "github.com/go-check/check" 11 ) 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 *check.C) { 16 name := "testbuildhistory" 17 _, err := buildImage(name, `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 true) 45 46 c.Assert(err, checker.IsNil) 47 48 out, _ := dockerCmd(c, "history", "testbuildhistory") 49 actualValues := strings.Split(out, "\n")[1:27] 50 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"} 51 52 for i := 0; i < 26; i++ { 53 echoValue := fmt.Sprintf("LABEL label.%s=%s", expectedValues[i], expectedValues[i]) 54 actualValue := actualValues[i] 55 c.Assert(actualValue, checker.Contains, echoValue) 56 } 57 58 } 59 60 func (s *DockerSuite) TestHistoryExistentImage(c *check.C) { 61 dockerCmd(c, "history", "busybox") 62 } 63 64 func (s *DockerSuite) TestHistoryNonExistentImage(c *check.C) { 65 _, _, err := dockerCmdWithError("history", "testHistoryNonExistentImage") 66 c.Assert(err, checker.NotNil, check.Commentf("history on a non-existent image should fail.")) 67 } 68 69 func (s *DockerSuite) TestHistoryImageWithComment(c *check.C) { 70 name := "testhistoryimagewithcomment" 71 72 // make an image through docker commit <container id> [ -m messages ] 73 74 dockerCmd(c, "run", "--name", name, "busybox", "true") 75 dockerCmd(c, "wait", name) 76 77 comment := "This_is_a_comment" 78 dockerCmd(c, "commit", "-m="+comment, name, name) 79 80 // test docker history <image id> to check comment messages 81 82 out, _ := dockerCmd(c, "history", name) 83 outputTabs := strings.Fields(strings.Split(out, "\n")[1]) 84 actualValue := outputTabs[len(outputTabs)-1] 85 c.Assert(actualValue, checker.Contains, comment) 86 } 87 88 func (s *DockerSuite) TestHistoryHumanOptionFalse(c *check.C) { 89 out, _ := dockerCmd(c, "history", "--human=false", "busybox") 90 lines := strings.Split(out, "\n") 91 sizeColumnRegex, _ := regexp.Compile("SIZE +") 92 indices := sizeColumnRegex.FindStringIndex(lines[0]) 93 startIndex := indices[0] 94 endIndex := indices[1] 95 for i := 1; i < len(lines)-1; i++ { 96 if endIndex > len(lines[i]) { 97 endIndex = len(lines[i]) 98 } 99 sizeString := lines[i][startIndex:endIndex] 100 101 _, err := strconv.Atoi(strings.TrimSpace(sizeString)) 102 c.Assert(err, checker.IsNil, check.Commentf("The size '%s' was not an Integer", sizeString)) 103 } 104 } 105 106 func (s *DockerSuite) TestHistoryHumanOptionTrue(c *check.C) { 107 out, _ := dockerCmd(c, "history", "--human=true", "busybox") 108 lines := strings.Split(out, "\n") 109 sizeColumnRegex, _ := regexp.Compile("SIZE +") 110 humanSizeRegexRaw := "\\d+.*B" // Matches human sizes like 10 MB, 3.2 KB, etc 111 indices := sizeColumnRegex.FindStringIndex(lines[0]) 112 startIndex := indices[0] 113 endIndex := indices[1] 114 for i := 1; i < len(lines)-1; i++ { 115 if endIndex > len(lines[i]) { 116 endIndex = len(lines[i]) 117 } 118 sizeString := lines[i][startIndex:endIndex] 119 c.Assert(strings.TrimSpace(sizeString), checker.Matches, humanSizeRegexRaw, check.Commentf("The size '%s' was not in human format", sizeString)) 120 } 121 }