2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-10-10 15:57:16 -04:00
|
|
|
"io"
|
|
|
|
"strings"
|
2017-03-30 20:21:14 -04:00
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2018-03-08 08:35:17 -05:00
|
|
|
"github.com/docker/cli/internal/test/notary"
|
2017-10-10 15:57:16 -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 TestNewPullCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2017-06-09 12:32:14 -04:00
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
2017-03-30 20:21:14 -04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wrong-args",
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "requires exactly 1 argument.",
|
2017-03-30 20:21:14 -04:00
|
|
|
args: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid-name",
|
2023-08-30 18:36:58 -04:00
|
|
|
expectedError: "invalid reference format: repository name (library/UPPERCASE_REPO) must be lowercase",
|
2017-03-30 20:21:14 -04:00
|
|
|
args: []string{"UPPERCASE_REPO"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all-tags-with-tag",
|
|
|
|
expectedError: "tag can't be used with --all-tags/-a",
|
|
|
|
args: []string{"--all-tags", "image:tag"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-07-05 14:34:16 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
2017-06-21 17:20:49 -04:00
|
|
|
cmd := NewPullCommand(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)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewPullCommandSuccess(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2017-10-10 15:57:16 -04:00
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedTag string
|
2017-03-30 20:21:14 -04:00
|
|
|
}{
|
|
|
|
{
|
2017-10-10 15:57:16 -04:00
|
|
|
name: "simple",
|
|
|
|
args: []string{"image:tag"},
|
|
|
|
expectedTag: "image:tag",
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
{
|
2017-10-10 15:57:16 -04:00
|
|
|
name: "simple-no-tag",
|
|
|
|
args: []string{"image"},
|
|
|
|
expectedTag: "image:latest",
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
2018-12-19 07:48:41 -05:00
|
|
|
{
|
2019-08-27 05:23:13 -04:00
|
|
|
name: "simple-quiet",
|
|
|
|
args: []string{"--quiet", "image"},
|
2018-12-19 07:48:41 -05:00
|
|
|
expectedTag: "image:latest",
|
|
|
|
},
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-10-10 15:57:16 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
imagePullFunc: func(ref string, options types.ImagePullOptions) (io.ReadCloser, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(tc.expectedTag, ref), tc.name)
|
2022-02-25 07:10:34 -05:00
|
|
|
return io.NopCloser(strings.NewReader("")), nil
|
2017-10-10 15:57:16 -04:00
|
|
|
},
|
|
|
|
})
|
2017-06-21 17:20:49 -04:00
|
|
|
cmd := NewPullCommand(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("pull-command-success.%s.golden", tc.name))
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|
2018-03-08 08:35:17 -05:00
|
|
|
|
|
|
|
func TestNewPullCommandWithContentTrustErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
notaryFunc test.NotaryClientFuncType
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "offline-notary-server",
|
|
|
|
notaryFunc: notary.GetOfflineNotaryRepository,
|
|
|
|
expectedError: "client is offline",
|
|
|
|
args: []string{"image:tag"},
|
|
|
|
},
|
|
|
|
{
|
2018-03-06 05:15:18 -05:00
|
|
|
name: "uninitialized-notary-server",
|
2018-03-08 08:35:17 -05:00
|
|
|
notaryFunc: notary.GetUninitializedNotaryRepository,
|
|
|
|
expectedError: "remote trust data does not exist",
|
|
|
|
args: []string{"image:tag"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty-notary-server",
|
|
|
|
notaryFunc: notary.GetEmptyTargetsNotaryRepository,
|
|
|
|
expectedError: "No valid trust data for tag",
|
|
|
|
args: []string{"image:tag"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
imagePullFunc: func(ref string, options types.ImagePullOptions) (io.ReadCloser, error) {
|
2022-02-25 07:10:34 -05:00
|
|
|
return io.NopCloser(strings.NewReader("")), fmt.Errorf("shouldn't try to pull image")
|
2018-03-08 08:35:17 -05:00
|
|
|
},
|
2018-03-08 14:56:56 -05:00
|
|
|
}, test.EnableContentTrust)
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(tc.notaryFunc)
|
|
|
|
cmd := NewPullCommand(cli)
|
2022-02-25 07:10:34 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-03-08 08:35:17 -05:00
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.ErrorContains(t, err, tc.expectedError)
|
|
|
|
}
|
|
|
|
}
|