github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/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 busybox
    22  RUN echo "A"
    23  RUN echo "B"
    24  RUN echo "C"
    25  RUN echo "D"
    26  RUN echo "E"
    27  RUN echo "F"
    28  RUN echo "G"
    29  RUN echo "H"
    30  RUN echo "I"
    31  RUN echo "J"
    32  RUN echo "K"
    33  RUN echo "L"
    34  RUN echo "M"
    35  RUN echo "N"
    36  RUN echo "O"
    37  RUN echo "P"
    38  RUN echo "Q"
    39  RUN echo "R"
    40  RUN echo "S"
    41  RUN echo "T"
    42  RUN echo "U"
    43  RUN echo "V"
    44  RUN echo "W"
    45  RUN echo "X"
    46  RUN echo "Y"
    47  RUN echo "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("echo \"%s\"", 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  }