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