github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration-cli/docker_cli_history_test.go (about)

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