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