Merge pull request #2107 from jonatasbaldin/2047-fix-image-createdsince-without-value

Fix CREATED field when listing image if date is not specified
This commit is contained in:
Silvin Lubecki 2019-11-05 09:46:14 +01:00 committed by GitHub
commit 9e041dd34c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 7 deletions

View File

@ -11,8 +11,8 @@ import (
) )
const ( const (
defaultImageTableFormat = "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.CreatedSince}}\t{{.Size}}" defaultImageTableFormat = "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{if .CreatedSince }}{{.CreatedSince}}{{else}}N/A{{end}}\t{{.Size}}"
defaultImageTableFormatWithDigest = "table {{.Repository}}\t{{.Tag}}\t{{.Digest}}\t{{.ID}}\t{{.CreatedSince}}\t{{.Size}}" defaultImageTableFormatWithDigest = "table {{.Repository}}\t{{.Tag}}\t{{.Digest}}\t{{.ID}}\t{{if .CreatedSince }}{{.CreatedSince}}{{else}}N/A{{end}}\t{{.Size}}"
imageIDHeader = "IMAGE ID" imageIDHeader = "IMAGE ID"
repositoryHeader = "REPOSITORY" repositoryHeader = "REPOSITORY"
@ -235,6 +235,11 @@ func (c *imageContext) Digest() string {
func (c *imageContext) CreatedSince() string { func (c *imageContext) CreatedSince() string {
createdAt := time.Unix(c.i.Created, 0) createdAt := time.Unix(c.i.Created, 0)
if createdAt.IsZero() {
return ""
}
return units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago" return units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago"
} }

View File

@ -16,6 +16,7 @@ import (
func TestImageContext(t *testing.T) { func TestImageContext(t *testing.T) {
imageID := stringid.GenerateRandomID() imageID := stringid.GenerateRandomID()
unix := time.Now().Unix() unix := time.Now().Unix()
zeroTime := int64(-62135596800)
var ctx imageContext var ctx imageContext
cases := []struct { cases := []struct {
@ -76,6 +77,11 @@ func TestImageContext(t *testing.T) {
i: types.ImageSummary{SharedSize: 5000, VirtualSize: 20000}, i: types.ImageSummary{SharedSize: 5000, VirtualSize: 20000},
}, "15kB", ctx.UniqueSize, }, "15kB", ctx.UniqueSize,
}, },
{
imageContext{
i: types.ImageSummary{Created: zeroTime},
}, "", ctx.CreatedSince,
},
} }
for _, c := range cases { for _, c := range cases {
@ -91,7 +97,9 @@ func TestImageContext(t *testing.T) {
func TestImageContextWrite(t *testing.T) { func TestImageContextWrite(t *testing.T) {
unixTime := time.Now().AddDate(0, 0, -1).Unix() unixTime := time.Now().AddDate(0, 0, -1).Unix()
zeroTime := int64(-62135596800)
expectedTime := time.Unix(unixTime, 0).String() expectedTime := time.Unix(unixTime, 0).String()
expectedZeroTime := time.Unix(zeroTime, 0).String()
cases := []struct { cases := []struct {
context ImageContext context ImageContext
@ -125,7 +133,7 @@ func TestImageContextWrite(t *testing.T) {
}, },
`REPOSITORY TAG IMAGE ID CREATED SIZE `REPOSITORY TAG IMAGE ID CREATED SIZE
image tag1 imageID1 24 hours ago 0B image tag1 imageID1 24 hours ago 0B
image tag2 imageID2 24 hours ago 0B image tag2 imageID2 N/A 0B
<none> <none> imageID3 24 hours ago 0B <none> <none> imageID3 24 hours ago 0B
`, `,
}, },
@ -183,7 +191,7 @@ image <none>
}, },
`REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE `REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
image tag1 sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf imageID1 24 hours ago 0B image tag1 sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf imageID1 24 hours ago 0B
image tag2 <none> imageID2 24 hours ago 0B image tag2 <none> imageID2 N/A 0B
<none> <none> <none> imageID3 24 hours ago 0B <none> <none> <none> imageID3 24 hours ago 0B
`, `,
}, },
@ -221,7 +229,7 @@ image_id: imageID3
created_at: %s created_at: %s
virtual_size: 0B virtual_size: 0B
`, expectedTime, expectedTime, expectedTime), `, expectedTime, expectedZeroTime, expectedTime),
}, },
{ {
ImageContext{ ImageContext{
@ -251,7 +259,7 @@ image_id: imageID3
created_at: %s created_at: %s
virtual_size: 0B virtual_size: 0B
`, expectedTime, expectedTime, expectedTime), `, expectedTime, expectedZeroTime, expectedTime),
}, },
{ {
ImageContext{ ImageContext{
@ -287,7 +295,7 @@ image_id: imageID3
for _, testcase := range cases { for _, testcase := range cases {
images := []types.ImageSummary{ images := []types.ImageSummary{
{ID: "imageID1", RepoTags: []string{"image:tag1"}, RepoDigests: []string{"image@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf"}, Created: unixTime}, {ID: "imageID1", RepoTags: []string{"image:tag1"}, RepoDigests: []string{"image@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf"}, Created: unixTime},
{ID: "imageID2", RepoTags: []string{"image:tag2"}, Created: unixTime}, {ID: "imageID2", RepoTags: []string{"image:tag2"}, Created: zeroTime},
{ID: "imageID3", RepoTags: []string{"<none>:<none>"}, RepoDigests: []string{"<none>@<none>"}, Created: unixTime}, {ID: "imageID3", RepoTags: []string{"<none>:<none>"}, RepoDigests: []string{"<none>@<none>"}, Created: unixTime},
} }
out := bytes.NewBufferString("") out := bytes.NewBufferString("")