2016-09-08 13:11:39 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestImageContext(t *testing.T) {
|
|
|
|
imageID := stringid.GenerateRandomID()
|
|
|
|
unix := time.Now().Unix()
|
|
|
|
|
|
|
|
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
|
|
|
}{
|
|
|
|
{imageContext{
|
2016-10-03 15:17:39 -04:00
|
|
|
i: types.ImageSummary{ID: imageID},
|
2016-09-08 13:11:39 -04:00
|
|
|
trunc: true,
|
2017-02-03 19:48:46 -05:00
|
|
|
}, stringid.TruncateID(imageID), ctx.ID},
|
2016-09-08 13:11:39 -04:00
|
|
|
{imageContext{
|
2016-10-03 15:17:39 -04:00
|
|
|
i: types.ImageSummary{ID: imageID},
|
2016-09-08 13:11:39 -04:00
|
|
|
trunc: false,
|
2017-02-03 19:48:46 -05:00
|
|
|
}, imageID, ctx.ID},
|
2016-09-08 13:11:39 -04:00
|
|
|
{imageContext{
|
2016-10-03 15:17:39 -04:00
|
|
|
i: types.ImageSummary{Size: 10, VirtualSize: 10},
|
2016-09-08 13:11:39 -04:00
|
|
|
trunc: true,
|
2017-02-03 19:48:46 -05:00
|
|
|
}, "10B", ctx.Size},
|
2016-09-08 13:11:39 -04:00
|
|
|
{imageContext{
|
2016-10-03 15:17:39 -04:00
|
|
|
i: types.ImageSummary{Created: unix},
|
2016-09-08 13:11:39 -04:00
|
|
|
trunc: true,
|
2017-02-03 19:48:46 -05:00
|
|
|
}, time.Unix(unix, 0).String(), 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},
|
|
|
|
{imageContext{
|
2016-10-03 15:17:39 -04:00
|
|
|
i: types.ImageSummary{},
|
2016-09-08 13:11:39 -04:00
|
|
|
repo: "busybox",
|
2017-02-03 19:48:46 -05:00
|
|
|
}, "busybox", ctx.Repository},
|
2016-09-08 13:11:39 -04:00
|
|
|
{imageContext{
|
2016-10-03 15:17:39 -04:00
|
|
|
i: types.ImageSummary{},
|
2016-09-08 13:11:39 -04:00
|
|
|
tag: "latest",
|
2017-02-03 19:48:46 -05:00
|
|
|
}, "latest", ctx.Tag},
|
2016-09-08 13:11:39 -04:00
|
|
|
{imageContext{
|
2016-10-03 15:17:39 -04:00
|
|
|
i: types.ImageSummary{},
|
2016-09-08 13:11:39 -04:00
|
|
|
digest: "sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a",
|
2017-02-03 19:48:46 -05:00
|
|
|
}, "sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a", ctx.Digest},
|
2017-05-09 17:24:40 -04:00
|
|
|
{
|
|
|
|
imageContext{
|
|
|
|
i: types.ImageSummary{Containers: 10},
|
|
|
|
}, "10", ctx.Containers,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
imageContext{
|
|
|
|
i: types.ImageSummary{VirtualSize: 10000},
|
|
|
|
}, "10kB", ctx.VirtualSize,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
imageContext{
|
|
|
|
i: types.ImageSummary{SharedSize: 10000},
|
|
|
|
}, "10kB", ctx.SharedSize,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
imageContext{
|
|
|
|
i: types.ImageSummary{SharedSize: 5000, VirtualSize: 20000},
|
|
|
|
}, "15kB", ctx.UniqueSize,
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = c.imageCtx
|
|
|
|
v := c.call()
|
|
|
|
if strings.Contains(v, ",") {
|
|
|
|
compareMultipleValues(t, v, c.expValue)
|
2017-05-09 17:24:40 -04:00
|
|
|
} else {
|
|
|
|
assert.Equal(t, c.expValue, v)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImageContextWrite(t *testing.T) {
|
|
|
|
unixTime := time.Now().AddDate(0, 0, -1).Unix()
|
|
|
|
expectedTime := time.Unix(unixTime, 0).String()
|
|
|
|
|
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}}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
`Template parsing error: template: :1: function "InvalidFunction" not defined
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ImageContext{
|
|
|
|
Context: Context{
|
|
|
|
Format: "{{nil}}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// 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
|
|
|
},
|
|
|
|
},
|
|
|
|
`REPOSITORY TAG IMAGE ID CREATED SIZE
|
2017-02-07 15:58:56 -05:00
|
|
|
image tag1 imageID1 24 hours ago 0B
|
|
|
|
image tag2 imageID2 24 hours ago 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,
|
|
|
|
},
|
|
|
|
`REPOSITORY DIGEST
|
|
|
|
image sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf
|
|
|
|
image <none>
|
|
|
|
<none> <none>
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
`REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
|
2017-02-07 15:58:56 -05:00
|
|
|
image tag1 sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf imageID1 24 hours ago 0B
|
|
|
|
image tag2 <none> imageID2 24 hours ago 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
|
|
|
|
|
|
|
`, expectedTime, expectedTime, expectedTime),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
`, expectedTime, expectedTime, expectedTime),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
for _, testcase := range cases {
|
2016-10-03 15:17:39 -04:00
|
|
|
images := []types.ImageSummary{
|
2016-09-08 13:11:39 -04:00
|
|
|
{ID: "imageID1", RepoTags: []string{"image:tag1"}, RepoDigests: []string{"image@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf"}, Created: unixTime},
|
|
|
|
{ID: "imageID2", RepoTags: []string{"image:tag2"}, Created: unixTime},
|
|
|
|
{ID: "imageID3", RepoTags: []string{"<none>:<none>"}, RepoDigests: []string{"<none>@<none>"}, Created: unixTime},
|
|
|
|
}
|
|
|
|
out := bytes.NewBufferString("")
|
2016-09-12 16:59:18 -04:00
|
|
|
testcase.context.Output = out
|
|
|
|
err := ImageWrite(testcase.context, images)
|
|
|
|
if err != nil {
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.EqualError(t, err, testcase.expected)
|
2016-09-12 16:59:18 -04:00
|
|
|
} else {
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Equal(t, testcase.expected, out.String())
|
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
|
|
|
|
|
|
|
contexts := []struct {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"REPOSITORY DIGEST\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, context := range contexts {
|
2016-09-12 16:59:18 -04:00
|
|
|
ImageWrite(context.context, images)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Equal(t, context.expected, out.String())
|
2016-09-08 13:11:39 -04:00
|
|
|
// Clean buffer
|
|
|
|
out.Reset()
|
|
|
|
}
|
|
|
|
}
|