2017-02-12 14:22:01 -05:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
2018-02-27 10:54:36 -05:00
|
|
|
"bytes"
|
2017-02-12 14:22:01 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/image"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2018-06-08 12:24:26 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2017-02-12 14:22:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type historyCase struct {
|
|
|
|
historyCtx historyContext
|
|
|
|
expValue string
|
|
|
|
call func() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistoryContext_ID(t *testing.T) {
|
|
|
|
id := stringid.GenerateRandomID()
|
|
|
|
|
|
|
|
var ctx historyContext
|
|
|
|
cases := []historyCase{
|
|
|
|
{
|
|
|
|
historyContext{
|
|
|
|
h: image.HistoryResponseItem{ID: id},
|
|
|
|
trunc: false,
|
|
|
|
}, id, ctx.ID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
historyContext{
|
|
|
|
h: image.HistoryResponseItem{ID: id},
|
|
|
|
trunc: true,
|
|
|
|
}, stringid.TruncateID(id), ctx.ID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = c.historyCtx
|
|
|
|
v := c.call()
|
|
|
|
if strings.Contains(v, ",") {
|
|
|
|
compareMultipleValues(t, v, c.expValue)
|
|
|
|
} else if v != c.expValue {
|
|
|
|
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistoryContext_CreatedSince(t *testing.T) {
|
2018-02-27 10:54:36 -05:00
|
|
|
dateStr := "2009-11-10T23:00:00Z"
|
2017-02-12 14:22:01 -05:00
|
|
|
var ctx historyContext
|
|
|
|
cases := []historyCase{
|
|
|
|
{
|
|
|
|
historyContext{
|
2017-08-12 14:38:01 -04:00
|
|
|
h: image.HistoryResponseItem{Created: time.Now().AddDate(0, 0, -7).Unix()},
|
2017-02-12 14:22:01 -05:00
|
|
|
trunc: false,
|
|
|
|
human: true,
|
2017-08-12 14:38:01 -04:00
|
|
|
}, "7 days ago", ctx.CreatedSince,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
historyContext{
|
|
|
|
h: image.HistoryResponseItem{Created: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()},
|
|
|
|
trunc: false,
|
|
|
|
human: false,
|
2018-02-27 10:54:36 -05:00
|
|
|
}, dateStr, ctx.CreatedSince,
|
2017-02-12 14:22:01 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = c.historyCtx
|
|
|
|
v := c.call()
|
|
|
|
if strings.Contains(v, ",") {
|
|
|
|
compareMultipleValues(t, v, c.expValue)
|
|
|
|
} else if v != c.expValue {
|
|
|
|
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistoryContext_CreatedBy(t *testing.T) {
|
2017-05-03 18:14:30 -04:00
|
|
|
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
|
|
|
|
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
|
2017-02-12 14:22:01 -05:00
|
|
|
|
|
|
|
var ctx historyContext
|
|
|
|
cases := []historyCase{
|
|
|
|
{
|
|
|
|
historyContext{
|
|
|
|
h: image.HistoryResponseItem{CreatedBy: withTabs},
|
|
|
|
trunc: false,
|
|
|
|
}, expected, ctx.CreatedBy,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
historyContext{
|
|
|
|
h: image.HistoryResponseItem{CreatedBy: withTabs},
|
|
|
|
trunc: true,
|
2017-10-13 05:01:32 -04:00
|
|
|
}, Ellipsis(expected, 45), ctx.CreatedBy,
|
2017-02-12 14:22:01 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = c.historyCtx
|
|
|
|
v := c.call()
|
|
|
|
if strings.Contains(v, ",") {
|
|
|
|
compareMultipleValues(t, v, c.expValue)
|
|
|
|
} else if v != c.expValue {
|
|
|
|
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistoryContext_Size(t *testing.T) {
|
|
|
|
size := int64(182964289)
|
|
|
|
expected := "183MB"
|
|
|
|
|
|
|
|
var ctx historyContext
|
|
|
|
cases := []historyCase{
|
|
|
|
{
|
|
|
|
historyContext{
|
|
|
|
h: image.HistoryResponseItem{Size: size},
|
|
|
|
trunc: false,
|
|
|
|
human: true,
|
|
|
|
}, expected, ctx.Size,
|
|
|
|
}, {
|
|
|
|
historyContext{
|
|
|
|
h: image.HistoryResponseItem{Size: size},
|
|
|
|
trunc: false,
|
|
|
|
human: false,
|
|
|
|
}, strconv.Itoa(182964289), ctx.Size,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = c.historyCtx
|
|
|
|
v := c.call()
|
|
|
|
if strings.Contains(v, ",") {
|
|
|
|
compareMultipleValues(t, v, c.expValue)
|
|
|
|
} else if v != c.expValue {
|
|
|
|
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistoryContext_Comment(t *testing.T) {
|
|
|
|
comment := "Some comment"
|
|
|
|
|
|
|
|
var ctx historyContext
|
|
|
|
cases := []historyCase{
|
|
|
|
{
|
|
|
|
historyContext{
|
|
|
|
h: image.HistoryResponseItem{Comment: comment},
|
|
|
|
trunc: false,
|
|
|
|
}, comment, ctx.Comment,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = c.historyCtx
|
|
|
|
v := c.call()
|
|
|
|
if strings.Contains(v, ",") {
|
|
|
|
compareMultipleValues(t, v, c.expValue)
|
|
|
|
} else if v != c.expValue {
|
|
|
|
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistoryContext_Table(t *testing.T) {
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
unixTime := time.Now().AddDate(0, 0, -1).Unix()
|
|
|
|
histories := []image.HistoryResponseItem{
|
2017-05-03 18:14:30 -04:00
|
|
|
{
|
|
|
|
ID: "imageID1",
|
|
|
|
Created: unixTime,
|
|
|
|
CreatedBy: "/bin/bash ls && npm i && npm run test && karma -c karma.conf.js start && npm start && more commands here && the list goes on",
|
|
|
|
Size: int64(182964289),
|
|
|
|
Comment: "Hi",
|
|
|
|
Tags: []string{"image:tag2"},
|
|
|
|
},
|
2017-02-12 14:22:01 -05:00
|
|
|
{ID: "imageID2", Created: unixTime, CreatedBy: "/bin/bash echo", Size: int64(182964289), Comment: "Hi", Tags: []string{"image:tag2"}},
|
|
|
|
{ID: "imageID3", Created: unixTime, CreatedBy: "/bin/bash ls", Size: int64(182964289), Comment: "Hi", Tags: []string{"image:tag2"}},
|
|
|
|
{ID: "imageID4", Created: unixTime, CreatedBy: "/bin/bash grep", Size: int64(182964289), Comment: "Hi", Tags: []string{"image:tag2"}},
|
|
|
|
}
|
2017-05-03 18:14:30 -04:00
|
|
|
// nolint: lll
|
2017-02-12 14:22:01 -05:00
|
|
|
expectedNoTrunc := `IMAGE CREATED CREATED BY SIZE COMMENT
|
|
|
|
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
|
|
|
|
imageID2 24 hours ago /bin/bash echo 183MB Hi
|
|
|
|
imageID3 24 hours ago /bin/bash ls 183MB Hi
|
|
|
|
imageID4 24 hours ago /bin/bash grep 183MB Hi
|
|
|
|
`
|
|
|
|
expectedTrunc := `IMAGE CREATED CREATED BY SIZE COMMENT
|
2017-10-13 05:01:32 -04:00
|
|
|
imageID1 24 hours ago /bin/bash ls && npm i && npm run test && kar… 183MB Hi
|
2017-02-12 14:22:01 -05:00
|
|
|
imageID2 24 hours ago /bin/bash echo 183MB Hi
|
|
|
|
imageID3 24 hours ago /bin/bash ls 183MB Hi
|
|
|
|
imageID4 24 hours ago /bin/bash grep 183MB Hi
|
|
|
|
`
|
|
|
|
|
|
|
|
contexts := []struct {
|
|
|
|
context Context
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{Context{
|
|
|
|
Format: NewHistoryFormat("table", false, true),
|
|
|
|
Trunc: true,
|
|
|
|
Output: out,
|
|
|
|
},
|
|
|
|
expectedTrunc,
|
|
|
|
},
|
|
|
|
{Context{
|
|
|
|
Format: NewHistoryFormat("table", false, true),
|
|
|
|
Trunc: false,
|
|
|
|
Output: out,
|
|
|
|
},
|
|
|
|
expectedNoTrunc,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, context := range contexts {
|
|
|
|
HistoryWrite(context.context, true, histories)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(context.expected, out.String()))
|
2017-02-12 14:22:01 -05:00
|
|
|
// Clean buffer
|
|
|
|
out.Reset()
|
|
|
|
}
|
|
|
|
}
|