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
|
2024-07-03 10:14:40 -04:00
|
|
|
imageInspectFunc func(img string) (types.ImageInspect, []byte, error)
|
2017-03-30 20:21:14 -04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
args: []string{"image"},
|
|
|
|
imageCount: 1,
|
2024-07-03 10:14:40 -04:00
|
|
|
imageInspectFunc: func(img string) (types.ImageInspect, []byte, error) {
|
2017-03-30 20:21:14 -04:00
|
|
|
imageInspectInvocationCount++
|
2024-07-03 10:14:40 -04:00
|
|
|
assert.Check(t, is.Equal("image", img))
|
2017-03-30 20:21:14 -04:00
|
|
|
return types.ImageInspect{}, nil, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "format",
|
|
|
|
imageCount: 1,
|
|
|
|
args: []string{"--format='{{.ID}}'", "image"},
|
2024-07-03 10:14:40 -04:00
|
|
|
imageInspectFunc: func(img string) (types.ImageInspect, []byte, error) {
|
2017-03-30 20:21:14 -04:00
|
|
|
imageInspectInvocationCount++
|
2024-07-03 10:14:40 -04:00
|
|
|
return types.ImageInspect{ID: img}, nil, nil
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple-many",
|
|
|
|
args: []string{"image1", "image2"},
|
|
|
|
imageCount: 2,
|
2024-07-03 10:14:40 -04:00
|
|
|
imageInspectFunc: func(img string) (types.ImageInspect, []byte, error) {
|
2017-03-30 20:21:14 -04:00
|
|
|
imageInspectInvocationCount++
|
|
|
|
if imageInspectInvocationCount == 1 {
|
2024-07-03 10:14:40 -04:00
|
|
|
assert.Check(t, is.Equal("image1", img))
|
2017-03-30 20:21:14 -04:00
|
|
|
} else {
|
2024-07-03 10:14:40 -04:00
|
|
|
assert.Check(t, is.Equal("image2", img))
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
return types.ImageInspect{}, nil, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2022-03-03 08:19:41 -05:00
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
imageInspectInvocationCount = 0
|
|
|
|
cli := test.NewFakeCli(&fakeClient{imageInspectFunc: tc.imageInspectFunc})
|
|
|
|
cmd := newInspectCommand(cli)
|
|
|
|
cmd.SetOut(io.Discard)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("inspect-command-success.%s.golden", tc.name))
|
|
|
|
assert.Check(t, is.Equal(imageInspectInvocationCount, tc.imageCount))
|
|
|
|
})
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|