github.com/akerouanton/docker@v1.11.0-rc3/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) // TODO Windows: This test passes on Windows,
    17  	// but currently adds a disproportionate amount of time for the value it has.
    18  	// Removing it from Windows CI for now, but this will be revisited in the
    19  	// TP5 timeframe when perf is better.
    20  	name := "testbuildhistory"
    21  	_, err := buildImage(name, `FROM `+minimalBaseImage()+`
    22  LABEL label.A="A"
    23  LABEL label.B="B"
    24  LABEL label.C="C"
    25  LABEL label.D="D"
    26  LABEL label.E="E"
    27  LABEL label.F="F"
    28  LABEL label.G="G"
    29  LABEL label.H="H"
    30  LABEL label.I="I"
    31  LABEL label.J="J"
    32  LABEL label.K="K"
    33  LABEL label.L="L"
    34  LABEL label.M="M"
    35  LABEL label.N="N"
    36  LABEL label.O="O"
    37  LABEL label.P="P"
    38  LABEL label.Q="Q"
    39  LABEL label.R="R"
    40  LABEL label.S="S"
    41  LABEL label.T="T"
    42  LABEL label.U="U"
    43  LABEL label.V="V"
    44  LABEL label.W="W"
    45  LABEL label.X="X"
    46  LABEL label.Y="Y"
    47  LABEL label.Z="Z"`,
    48  		true)
    49  
    50  	c.Assert(err, checker.IsNil)
    51  
    52  	out, _ := dockerCmd(c, "history", "testbuildhistory")
    53  	actualValues := strings.Split(out, "\n")[1:27]
    54  	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"}
    55  
    56  	for i := 0; i < 26; i++ {
    57  		echoValue := fmt.Sprintf("LABEL label.%s=%s", expectedValues[i], expectedValues[i])
    58  		actualValue := actualValues[i]
    59  		c.Assert(actualValue, checker.Contains, echoValue)
    60  	}
    61  
    62  }
    63  
    64  func (s *DockerSuite) TestHistoryExistentImage(c *check.C) {
    65  	dockerCmd(c, "history", "busybox")
    66  }
    67  
    68  func (s *DockerSuite) TestHistoryNonExistentImage(c *check.C) {
    69  	_, _, err := dockerCmdWithError("history", "testHistoryNonExistentImage")
    70  	c.Assert(err, checker.NotNil, check.Commentf("history on a non-existent image should fail."))
    71  }
    72  
    73  func (s *DockerSuite) TestHistoryImageWithComment(c *check.C) {
    74  	name := "testhistoryimagewithcomment"
    75  
    76  	// make a image through docker commit <container id> [ -m messages ]
    77  
    78  	dockerCmd(c, "run", "--name", name, "busybox", "true")
    79  	dockerCmd(c, "wait", name)
    80  
    81  	comment := "This_is_a_comment"
    82  	dockerCmd(c, "commit", "-m="+comment, name, name)
    83  
    84  	// test docker history <image id> to check comment messages
    85  
    86  	out, _ := dockerCmd(c, "history", name)
    87  	outputTabs := strings.Fields(strings.Split(out, "\n")[1])
    88  	actualValue := outputTabs[len(outputTabs)-1]
    89  	c.Assert(actualValue, checker.Contains, comment)
    90  }
    91  
    92  func (s *DockerSuite) TestHistoryHumanOptionFalse(c *check.C) {
    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  	out, _ := dockerCmd(c, "history", "--human=true", "busybox")
   112  	lines := strings.Split(out, "\n")
   113  	sizeColumnRegex, _ := regexp.Compile("SIZE +")
   114  	humanSizeRegexRaw := "\\d+.*B" // Matches human sizes like 10 MB, 3.2 KB, etc
   115  	indices := sizeColumnRegex.FindStringIndex(lines[0])
   116  	startIndex := indices[0]
   117  	endIndex := indices[1]
   118  	for i := 1; i < len(lines)-1; i++ {
   119  		if endIndex > len(lines[i]) {
   120  			endIndex = len(lines[i])
   121  		}
   122  		sizeString := lines[i][startIndex:endIndex]
   123  		c.Assert(strings.TrimSpace(sizeString), checker.Matches, humanSizeRegexRaw, check.Commentf("The size '%s' was not in human format", sizeString))
   124  	}
   125  }