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