2016-09-08 13:11:39 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-07-10 09:51:07 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestImageContext(t *testing.T) {
|
|
|
|
imageID := stringid.GenerateRandomID()
|
|
|
|
unix := time.Now().Unix()
|
2019-10-29 16:15:13 -04:00
|
|
|
zeroTime := int64(-62135596800)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
var ctx imageContext
|
|
|
|
cases := []struct {
|
2017-02-03 19:48:46 -05:00
|
|
|
imageCtx imageContext
|
|
|
|
expValue string
|
|
|
|
call func() string
|
2016-09-08 13:11:39 -04:00
|
|
|
}{
|
2020-07-10 10:00:11 -04:00
|
|
|
{
|
|
|
|
imageCtx: imageContext{i: types.ImageSummary{ID: imageID}, trunc: true},
|
|
|
|
expValue: stringid.TruncateID(imageID),
|
|
|
|
call: ctx.ID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
imageCtx: imageContext{i: types.ImageSummary{ID: imageID}, trunc: false},
|
|
|
|
expValue: imageID,
|
|
|
|
call: ctx.ID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
imageCtx: imageContext{i: types.ImageSummary{Size: 10, VirtualSize: 10}, trunc: true},
|
|
|
|
expValue: "10B",
|
|
|
|
call: ctx.Size,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
imageCtx: imageContext{i: types.ImageSummary{Created: unix}, trunc: true},
|
|
|
|
expValue: time.Unix(unix, 0).String(), call: ctx.CreatedAt,
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
// FIXME
|
|
|
|
// {imageContext{
|
2016-10-03 15:17:39 -04:00
|
|
|
// i: types.ImageSummary{Created: unix},
|
2016-09-08 13:11:39 -04:00
|
|
|
// trunc: true,
|
|
|
|
// }, units.HumanDuration(time.Unix(unix, 0)), createdSinceHeader, ctx.CreatedSince},
|
2017-05-09 17:24:40 -04:00
|
|
|
{
|
2020-07-10 10:00:11 -04:00
|
|
|
imageCtx: imageContext{i: types.ImageSummary{}, repo: "busybox"},
|
|
|
|
expValue: "busybox",
|
|
|
|
call: ctx.Repository,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
imageCtx: imageContext{i: types.ImageSummary{}, tag: "latest"},
|
|
|
|
expValue: "latest",
|
|
|
|
call: ctx.Tag,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
imageCtx: imageContext{i: types.ImageSummary{}, digest: "sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a"},
|
|
|
|
expValue: "sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a",
|
|
|
|
call: ctx.Digest,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
imageCtx: imageContext{i: types.ImageSummary{Containers: 10}},
|
|
|
|
expValue: "10",
|
|
|
|
call: ctx.Containers,
|
2017-05-09 17:24:40 -04:00
|
|
|
},
|
|
|
|
{
|
2020-07-10 10:00:11 -04:00
|
|
|
imageCtx: imageContext{i: types.ImageSummary{VirtualSize: 10000}},
|
|
|
|
expValue: "10kB",
|
|
|
|
call: ctx.VirtualSize,
|
2017-05-09 17:24:40 -04:00
|
|
|
},
|
|
|
|
{
|
2020-07-10 10:00:11 -04:00
|
|
|
imageCtx: imageContext{i: types.ImageSummary{SharedSize: 10000}},
|
|
|
|
expValue: "10kB",
|
|
|
|
call: ctx.SharedSize,
|
2017-05-09 17:24:40 -04:00
|
|
|
},
|
|
|
|
{
|
2020-07-10 10:00:11 -04:00
|
|
|
imageCtx: imageContext{i: types.ImageSummary{SharedSize: 5000, VirtualSize: 20000}},
|
|
|
|
expValue: "15kB",
|
|
|
|
call: ctx.UniqueSize,
|
2017-05-09 17:24:40 -04:00
|
|
|
},
|
2019-10-29 16:15:13 -04:00
|
|
|
{
|
2020-07-10 10:00:11 -04:00
|
|
|
imageCtx: imageContext{i: types.ImageSummary{Created: zeroTime}},
|
|
|
|
expValue: "",
|
|
|
|
call: ctx.CreatedSince,
|
2019-10-29 16:15:13 -04:00
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = c.imageCtx
|
|
|
|
v := c.call()
|
|
|
|
if strings.Contains(v, ",") {
|
2020-07-10 09:51:07 -04:00
|
|
|
test.CompareMultipleValues(t, v, c.expValue)
|
2017-05-09 17:24:40 -04:00
|
|
|
} else {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(c.expValue, v))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImageContextWrite(t *testing.T) {
|
|
|
|
unixTime := time.Now().AddDate(0, 0, -1).Unix()
|
2019-10-29 16:15:13 -04:00
|
|
|
zeroTime := int64(-62135596800)
|
2016-09-08 13:11:39 -04:00
|
|
|
expectedTime := time.Unix(unixTime, 0).String()
|
2019-10-29 16:15:13 -04:00
|
|
|
expectedZeroTime := time.Unix(zeroTime, 0).String()
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
cases := []struct {
|
2016-09-08 13:11:39 -04:00
|
|
|
context ImageContext
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
// Errors
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
|
|
|
Format: "{{InvalidFunction}}",
|
|
|
|
},
|
|
|
|
},
|
linting: fix incorrectly formatted errors (revive)
cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
^
cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n",
^
cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("error checking context: '%s'.", err)
^
cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n")
^
cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 4ab70bf61e87271ca56511172352296a202ced41)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
|
|
|
`template parsing error: template: :1: function "InvalidFunction" not defined`,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
|
|
|
Format: "{{nil}}",
|
|
|
|
},
|
|
|
|
},
|
linting: fix incorrectly formatted errors (revive)
cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
^
cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n",
^
cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("error checking context: '%s'.", err)
^
cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n")
^
cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 4ab70bf61e87271ca56511172352296a202ced41)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
|
|
|
`template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
// Table Format
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("table", false, false),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
2020-08-28 17:00:21 -04:00
|
|
|
`REPOSITORY TAG IMAGE ID CREATED SIZE
|
|
|
|
image tag1 imageID1 24 hours ago 0B
|
|
|
|
image tag2 imageID2 N/A 0B
|
|
|
|
<none> <none> imageID3 24 hours ago 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("table {{.Repository}}", false, false),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"REPOSITORY\nimage\nimage\n<none>\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("table {{.Repository}}", false, true),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
Digest: true,
|
|
|
|
},
|
2020-08-28 17:00:21 -04:00
|
|
|
`REPOSITORY DIGEST
|
|
|
|
image sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf
|
|
|
|
image <none>
|
|
|
|
<none> <none>
|
2016-09-08 13:11:39 -04:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("table {{.Repository}}", true, false),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"REPOSITORY\nimage\nimage\n<none>\n",
|
|
|
|
},
|
2017-08-14 05:06:31 -04:00
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
|
|
|
Format: NewImageFormat("table {{.Digest}}", true, false),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"DIGEST\nsha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf\n<none>\n<none>\n",
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("table", true, false),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"imageID1\nimageID2\nimageID3\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("table", false, true),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
Digest: true,
|
|
|
|
},
|
2020-08-28 17:00:21 -04:00
|
|
|
`REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
|
|
|
|
image tag1 sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf imageID1 24 hours ago 0B
|
|
|
|
image tag2 <none> imageID2 N/A 0B
|
|
|
|
<none> <none> <none> imageID3 24 hours ago 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("table", true, true),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
Digest: true,
|
|
|
|
},
|
|
|
|
"imageID1\nimageID2\nimageID3\n",
|
|
|
|
},
|
|
|
|
// Raw Format
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("raw", false, false),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
fmt.Sprintf(`repository: image
|
|
|
|
tag: tag1
|
|
|
|
image_id: imageID1
|
|
|
|
created_at: %s
|
2017-02-07 15:58:56 -05:00
|
|
|
virtual_size: 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
repository: image
|
|
|
|
tag: tag2
|
|
|
|
image_id: imageID2
|
|
|
|
created_at: %s
|
2017-02-07 15:58:56 -05:00
|
|
|
virtual_size: 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
repository: <none>
|
|
|
|
tag: <none>
|
|
|
|
image_id: imageID3
|
|
|
|
created_at: %s
|
2017-02-07 15:58:56 -05:00
|
|
|
virtual_size: 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2019-10-29 16:15:13 -04:00
|
|
|
`, expectedTime, expectedZeroTime, expectedTime),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("raw", false, true),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
Digest: true,
|
|
|
|
},
|
|
|
|
fmt.Sprintf(`repository: image
|
|
|
|
tag: tag1
|
|
|
|
digest: sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf
|
|
|
|
image_id: imageID1
|
|
|
|
created_at: %s
|
2017-02-07 15:58:56 -05:00
|
|
|
virtual_size: 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
repository: image
|
|
|
|
tag: tag2
|
|
|
|
digest: <none>
|
|
|
|
image_id: imageID2
|
|
|
|
created_at: %s
|
2017-02-07 15:58:56 -05:00
|
|
|
virtual_size: 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
repository: <none>
|
|
|
|
tag: <none>
|
|
|
|
digest: <none>
|
|
|
|
image_id: imageID3
|
|
|
|
created_at: %s
|
2017-02-07 15:58:56 -05:00
|
|
|
virtual_size: 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2019-10-29 16:15:13 -04:00
|
|
|
`, expectedTime, expectedZeroTime, expectedTime),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("raw", true, false),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
`image_id: imageID1
|
|
|
|
image_id: imageID2
|
|
|
|
image_id: imageID3
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// Custom Format
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("{{.Repository}}", false, false),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"image\nimage\n<none>\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("{{.Repository}}", false, true),
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
Digest: true,
|
|
|
|
},
|
|
|
|
"image\nimage\n<none>\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-28 17:00:21 -04:00
|
|
|
images := []types.ImageSummary{
|
|
|
|
{ID: "imageID1", RepoTags: []string{"image:tag1"}, RepoDigests: []string{"image@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf"}, Created: unixTime},
|
|
|
|
{ID: "imageID2", RepoTags: []string{"image:tag2"}, Created: zeroTime},
|
|
|
|
{ID: "imageID3", RepoTags: []string{"<none>:<none>"}, RepoDigests: []string{"<none>@<none>"}, Created: unixTime},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(string(tc.context.Format), func(t *testing.T) {
|
|
|
|
var out bytes.Buffer
|
|
|
|
tc.context.Output = &out
|
|
|
|
err := ImageWrite(tc.context, images)
|
|
|
|
if err != nil {
|
|
|
|
assert.Error(t, err, tc.expected)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, out.String(), tc.expected)
|
|
|
|
}
|
|
|
|
})
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImageContextWriteWithNoImage(t *testing.T) {
|
|
|
|
out := bytes.NewBufferString("")
|
2016-10-03 15:17:39 -04:00
|
|
|
images := []types.ImageSummary{}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2020-08-28 17:00:21 -04:00
|
|
|
cases := []struct {
|
2016-09-08 13:11:39 -04:00
|
|
|
context ImageContext
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("{{.Repository}}", false, false),
|
2016-09-08 13:11:39 -04:00
|
|
|
Output: out,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("table {{.Repository}}", false, false),
|
2016-09-08 13:11:39 -04:00
|
|
|
Output: out,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"REPOSITORY\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("{{.Repository}}", false, true),
|
2016-09-08 13:11:39 -04:00
|
|
|
Output: out,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
2016-09-12 16:59:18 -04:00
|
|
|
Format: NewImageFormat("table {{.Repository}}", false, true),
|
2016-09-08 13:11:39 -04:00
|
|
|
Output: out,
|
|
|
|
},
|
|
|
|
},
|
2020-08-28 17:00:21 -04:00
|
|
|
"REPOSITORY DIGEST\n",
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-28 17:00:21 -04:00
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(string(tc.context.Format), func(t *testing.T) {
|
|
|
|
err := ImageWrite(tc.context, images)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, out.String(), tc.expected)
|
|
|
|
// Clean buffer
|
|
|
|
out.Reset()
|
|
|
|
})
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|