Return empty string if image createdAt is zero

Signed-off-by: Jonatas Baldin <jonatas.baldin@gmail.com>
This commit is contained in:
Jonatas Baldin 2019-10-29 17:15:13 -03:00
parent 7543883244
commit 56729bc0a4
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("")