2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-02-25 07:10:34 -05:00
|
|
|
"io"
|
2017-03-30 20:21:14 -04:00
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-03-30 20:21:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewInspectCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wrong-args",
|
|
|
|
args: []string{},
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "requires at least 1 argument.",
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-08-16 12:19:53 -04:00
|
|
|
cmd := newInspectCommand(test.NewFakeCli(&fakeClient{}))
|
2022-02-25 07:10:34 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewInspectCommandSuccess(t *testing.T) {
|
|
|
|
imageInspectInvocationCount := 0
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
imageCount int
|
|
|
|
imageInspectFunc func(image string) (types.ImageInspect, []byte, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
args: []string{"image"},
|
|
|
|
imageCount: 1,
|
|
|
|
imageInspectFunc: func(image string) (types.ImageInspect, []byte, error) {
|
|
|
|
imageInspectInvocationCount++
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("image", image))
|
2017-03-30 20:21:14 -04:00
|
|
|
return types.ImageInspect{}, nil, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "format",
|
|
|
|
imageCount: 1,
|
|
|
|
args: []string{"--format='{{.ID}}'", "image"},
|
|
|
|
imageInspectFunc: func(image string) (types.ImageInspect, []byte, error) {
|
|
|
|
imageInspectInvocationCount++
|
|
|
|
return types.ImageInspect{ID: image}, nil, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple-many",
|
|
|
|
args: []string{"image1", "image2"},
|
|
|
|
imageCount: 2,
|
|
|
|
imageInspectFunc: func(image string) (types.ImageInspect, []byte, error) {
|
|
|
|
imageInspectInvocationCount++
|
|
|
|
if imageInspectInvocationCount == 1 {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("image1", image))
|
2017-03-30 20:21:14 -04:00
|
|
|
} else {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("image2", image))
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
return types.ImageInspect{}, nil, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
imageInspectInvocationCount = 0
|
2017-08-16 12:19:53 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{imageInspectFunc: tc.imageInspectFunc})
|
|
|
|
cmd := newInspectCommand(cli)
|
2022-02-25 07:10:34 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.Execute()
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-16 12:19:53 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("inspect-command-success.%s.golden", tc.name))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(imageInspectInvocationCount, tc.imageCount))
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|