github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/image/formatter_history_test.go (about)

     1  package image
     2  
     3  import (
     4  	"bytes"
     5  	"strconv"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/docker/cli/cli/command/formatter"
    11  	"github.com/docker/cli/internal/test"
    12  	"github.com/docker/docker/api/types/image"
    13  	"github.com/docker/docker/pkg/stringid"
    14  	"gotest.tools/v3/assert"
    15  	"gotest.tools/v3/skip"
    16  )
    17  
    18  type historyCase struct {
    19  	historyCtx historyContext
    20  	expValue   string
    21  	call       func() string
    22  }
    23  
    24  func TestHistoryContext_ID(t *testing.T) {
    25  	id := stringid.GenerateRandomID()
    26  
    27  	var ctx historyContext
    28  	cases := []historyCase{
    29  		{
    30  			historyContext{
    31  				h:     image.HistoryResponseItem{ID: id},
    32  				trunc: false,
    33  			}, id, ctx.ID,
    34  		},
    35  		{
    36  			historyContext{
    37  				h:     image.HistoryResponseItem{ID: id},
    38  				trunc: true,
    39  			}, stringid.TruncateID(id), ctx.ID,
    40  		},
    41  	}
    42  
    43  	for _, c := range cases {
    44  		ctx = c.historyCtx
    45  		v := c.call()
    46  		if strings.Contains(v, ",") {
    47  			test.CompareMultipleValues(t, v, c.expValue)
    48  		} else if v != c.expValue {
    49  			t.Fatalf("Expected %s, was %s\n", c.expValue, v)
    50  		}
    51  	}
    52  }
    53  
    54  func TestHistoryContext_CreatedSince(t *testing.T) {
    55  	skip.If(t, notUTCTimezone, "expected output requires UTC timezone")
    56  	dateStr := "2009-11-10T23:00:00Z"
    57  	var ctx historyContext
    58  	cases := []historyCase{
    59  		{
    60  			historyContext{
    61  				h:     image.HistoryResponseItem{Created: time.Now().AddDate(0, 0, -7).Unix()},
    62  				trunc: false,
    63  				human: true,
    64  			}, "7 days ago", ctx.CreatedSince,
    65  		},
    66  		{
    67  			historyContext{
    68  				h:     image.HistoryResponseItem{Created: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()},
    69  				trunc: false,
    70  				human: false,
    71  			}, dateStr, ctx.CreatedSince,
    72  		},
    73  	}
    74  
    75  	for _, c := range cases {
    76  		ctx = c.historyCtx
    77  		v := c.call()
    78  		if strings.Contains(v, ",") {
    79  			test.CompareMultipleValues(t, v, c.expValue)
    80  		} else if v != c.expValue {
    81  			t.Fatalf("Expected %s, was %s\n", c.expValue, v)
    82  		}
    83  	}
    84  }
    85  
    86  func TestHistoryContext_CreatedBy(t *testing.T) {
    87  	withTabs := `/bin/sh -c apt-key adv --keyserver hkp://pgp.mit.edu:80	--recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62	&& echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list  && apt-get update  && apt-get install --no-install-recommends --no-install-suggests -y       ca-certificates       nginx=${NGINX_VERSION}       nginx-module-xslt       nginx-module-geoip       nginx-module-image-filter       nginx-module-perl       nginx-module-njs       gettext-base  && rm -rf /var/lib/apt/lists/*` // nolint: lll
    88  	expected := `/bin/sh -c apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 && echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list  && apt-get update  && apt-get install --no-install-recommends --no-install-suggests -y       ca-certificates       nginx=${NGINX_VERSION}       nginx-module-xslt       nginx-module-geoip       nginx-module-image-filter       nginx-module-perl       nginx-module-njs       gettext-base  && rm -rf /var/lib/apt/lists/*` // nolint: lll
    89  
    90  	var ctx historyContext
    91  	cases := []historyCase{
    92  		{
    93  			historyContext{
    94  				h:     image.HistoryResponseItem{CreatedBy: withTabs},
    95  				trunc: false,
    96  			}, expected, ctx.CreatedBy,
    97  		},
    98  		{
    99  			historyContext{
   100  				h:     image.HistoryResponseItem{CreatedBy: withTabs},
   101  				trunc: true,
   102  			}, formatter.Ellipsis(expected, 45), ctx.CreatedBy,
   103  		},
   104  	}
   105  
   106  	for _, c := range cases {
   107  		ctx = c.historyCtx
   108  		v := c.call()
   109  		if strings.Contains(v, ",") {
   110  			test.CompareMultipleValues(t, v, c.expValue)
   111  		} else if v != c.expValue {
   112  			t.Fatalf("Expected %s, was %s\n", c.expValue, v)
   113  		}
   114  	}
   115  }
   116  
   117  func TestHistoryContext_Size(t *testing.T) {
   118  	size := int64(182964289)
   119  	expected := "183MB"
   120  
   121  	var ctx historyContext
   122  	cases := []historyCase{
   123  		{
   124  			historyContext{
   125  				h:     image.HistoryResponseItem{Size: size},
   126  				trunc: false,
   127  				human: true,
   128  			}, expected, ctx.Size,
   129  		}, {
   130  			historyContext{
   131  				h:     image.HistoryResponseItem{Size: size},
   132  				trunc: false,
   133  				human: false,
   134  			}, strconv.Itoa(182964289), ctx.Size,
   135  		},
   136  	}
   137  
   138  	for _, c := range cases {
   139  		ctx = c.historyCtx
   140  		v := c.call()
   141  		if strings.Contains(v, ",") {
   142  			test.CompareMultipleValues(t, v, c.expValue)
   143  		} else if v != c.expValue {
   144  			t.Fatalf("Expected %s, was %s\n", c.expValue, v)
   145  		}
   146  	}
   147  }
   148  
   149  func TestHistoryContext_Comment(t *testing.T) {
   150  	comment := "Some comment"
   151  
   152  	var ctx historyContext
   153  	cases := []historyCase{
   154  		{
   155  			historyContext{
   156  				h:     image.HistoryResponseItem{Comment: comment},
   157  				trunc: false,
   158  			}, comment, ctx.Comment,
   159  		},
   160  	}
   161  
   162  	for _, c := range cases {
   163  		ctx = c.historyCtx
   164  		v := c.call()
   165  		if strings.Contains(v, ",") {
   166  			test.CompareMultipleValues(t, v, c.expValue)
   167  		} else if v != c.expValue {
   168  			t.Fatalf("Expected %s, was %s\n", c.expValue, v)
   169  		}
   170  	}
   171  }
   172  
   173  func TestHistoryContext_Table(t *testing.T) {
   174  	out := bytes.NewBufferString("")
   175  	unixTime := time.Now().AddDate(0, 0, -1).Unix()
   176  	histories := []image.HistoryResponseItem{
   177  		{
   178  			ID:        "imageID1",
   179  			Created:   unixTime,
   180  			CreatedBy: "/bin/bash ls && npm i && npm run test && karma -c karma.conf.js start && npm start && more commands here && the list goes on",
   181  			Size:      int64(182964289),
   182  			Comment:   "Hi",
   183  			Tags:      []string{"image:tag2"},
   184  		},
   185  		{ID: "imageID2", Created: unixTime, CreatedBy: "/bin/bash echo", Size: int64(182964289), Comment: "Hi", Tags: []string{"image:tag2"}},
   186  		{ID: "imageID3", Created: unixTime, CreatedBy: "/bin/bash ls", Size: int64(182964289), Comment: "Hi", Tags: []string{"image:tag2"}},
   187  		{ID: "imageID4", Created: unixTime, CreatedBy: "/bin/bash grep", Size: int64(182964289), Comment: "Hi", Tags: []string{"image:tag2"}},
   188  	}
   189  	// nolint: lll
   190  	expectedNoTrunc := `IMAGE      CREATED        CREATED BY                                                                                                                     SIZE      COMMENT
   191  imageID1   24 hours ago   /bin/bash ls && npm i && npm run test && karma -c karma.conf.js start && npm start && more commands here && the list goes on   183MB     Hi
   192  imageID2   24 hours ago   /bin/bash echo                                                                                                                 183MB     Hi
   193  imageID3   24 hours ago   /bin/bash ls                                                                                                                   183MB     Hi
   194  imageID4   24 hours ago   /bin/bash grep                                                                                                                 183MB     Hi
   195  `
   196  	expectedTrunc := `IMAGE      CREATED        CREATED BY                                      SIZE      COMMENT
   197  imageID1   24 hours ago   /bin/bash ls && npm i && npm run test && kar…   183MB     Hi
   198  imageID2   24 hours ago   /bin/bash echo                                  183MB     Hi
   199  imageID3   24 hours ago   /bin/bash ls                                    183MB     Hi
   200  imageID4   24 hours ago   /bin/bash grep                                  183MB     Hi
   201  `
   202  
   203  	cases := []struct {
   204  		context  formatter.Context
   205  		expected string
   206  	}{
   207  		{formatter.Context{
   208  			Format: NewHistoryFormat("table", false, true),
   209  			Trunc:  true,
   210  			Output: out,
   211  		},
   212  			expectedTrunc,
   213  		},
   214  		{formatter.Context{
   215  			Format: NewHistoryFormat("table", false, true),
   216  			Trunc:  false,
   217  			Output: out,
   218  		},
   219  			expectedNoTrunc,
   220  		},
   221  	}
   222  
   223  	for _, tc := range cases {
   224  		tc := tc
   225  		t.Run(string(tc.context.Format), func(t *testing.T) {
   226  			err := HistoryWrite(tc.context, true, histories)
   227  			assert.NilError(t, err)
   228  			assert.Equal(t, out.String(), tc.expected)
   229  			// Clean buffer
   230  			out.Reset()
   231  		})
   232  	}
   233  }