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