Refactor runPull to remove second GetImageReferencesAndAuth

Fix unit tests to catch the regression.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-10-10 15:57:16 -04:00
parent 7dda6fc3c9
commit e548861481
2 changed files with 29 additions and 21 deletions

View File

@ -41,28 +41,25 @@ func NewPullCommand(dockerCli command.Cli) *cobra.Command {
} }
func runPull(cli command.Cli, opts pullOptions) error { func runPull(cli command.Cli, opts pullOptions) error {
ctx := context.Background() distributionRef, err := reference.ParseNormalizedNamed(opts.remote)
imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, AuthResolver(cli), opts.remote) switch {
if err != nil { case err != nil:
return err return err
} case opts.all && !reference.IsNameOnly(distributionRef):
distributionRef := imgRefAndAuth.Reference()
if opts.all && !reference.IsNameOnly(distributionRef) {
return errors.New("tag can't be used with --all-tags/-a") return errors.New("tag can't be used with --all-tags/-a")
} case !opts.all && reference.IsNameOnly(distributionRef):
if !opts.all && reference.IsNameOnly(distributionRef) {
distributionRef = reference.TagNameOnly(distributionRef) distributionRef = reference.TagNameOnly(distributionRef)
imgRefAndAuth, err = trust.GetImageReferencesAndAuth(ctx, AuthResolver(cli), distributionRef.String())
if err != nil {
return err
}
if tagged, ok := distributionRef.(reference.Tagged); ok { if tagged, ok := distributionRef.(reference.Tagged); ok {
fmt.Fprintf(cli.Out(), "Using default tag: %s\n", tagged.Tag()) fmt.Fprintf(cli.Out(), "Using default tag: %s\n", tagged.Tag())
} }
} }
ctx := context.Background()
imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, AuthResolver(cli), distributionRef.String())
if err != nil {
return err
}
// Check if reference has a digest // Check if reference has a digest
_, isCanonical := distributionRef.(reference.Canonical) _, isCanonical := distributionRef.(reference.Canonical)
if command.IsTrusted() && !isCanonical { if command.IsTrusted() && !isCanonical {

View File

@ -2,11 +2,14 @@ package image
import ( import (
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"strings"
"testing" "testing"
"github.com/docker/cli/internal/test" "github.com/docker/cli/internal/test"
"github.com/docker/cli/internal/test/testutil" "github.com/docker/cli/internal/test/testutil"
"github.com/docker/docker/api/types"
"github.com/gotestyourself/gotestyourself/golden" "github.com/gotestyourself/gotestyourself/golden"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -44,20 +47,28 @@ func TestNewPullCommandErrors(t *testing.T) {
func TestNewPullCommandSuccess(t *testing.T) { func TestNewPullCommandSuccess(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
args []string args []string
expectedTag string
}{ }{
{ {
name: "simple", name: "simple",
args: []string{"image:tag"}, args: []string{"image:tag"},
expectedTag: "image:tag",
}, },
{ {
name: "simple-no-tag", name: "simple-no-tag",
args: []string{"image"}, args: []string{"image"},
expectedTag: "image:latest",
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{}) cli := test.NewFakeCli(&fakeClient{
imagePullFunc: func(ref string, options types.ImagePullOptions) (io.ReadCloser, error) {
assert.Equal(t, tc.expectedTag, ref, tc.name)
return ioutil.NopCloser(strings.NewReader("")), nil
},
})
cmd := NewPullCommand(cli) cmd := NewPullCommand(cli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)