2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-10-10 15:57:16 -04:00
|
|
|
"io"
|
2017-03-30 20:21:14 -04:00
|
|
|
"io/ioutil"
|
2017-10-10 15:57:16 -04:00
|
|
|
"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"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-08-16 12:19:53 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/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",
|
|
|
|
expectedError: "invalid reference format: repository name must be lowercase",
|
|
|
|
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)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
2017-10-10 15:57:16 -04:00
|
|
|
return ioutil.NopCloser(strings.NewReader("")), nil
|
|
|
|
},
|
|
|
|
})
|
2017-06-21 17:20:49 -04:00
|
|
|
cmd := NewPullCommand(cli)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
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) {
|
|
|
|
return ioutil.NopCloser(strings.NewReader("")), fmt.Errorf("shouldn't try to pull image")
|
|
|
|
},
|
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)
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.ErrorContains(t, err, tc.expectedError)
|
|
|
|
}
|
|
|
|
}
|