github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/cli/command/image/pull_test.go (about)

     1  package image
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"io/ioutil"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/docker/cli/internal/test"
    11  	"github.com/docker/cli/internal/test/notary"
    12  	"github.com/docker/docker/api/types"
    13  	"gotest.tools/assert"
    14  	is "gotest.tools/assert/cmp"
    15  	"gotest.tools/golden"
    16  )
    17  
    18  func TestNewPullCommandErrors(t *testing.T) {
    19  	testCases := []struct {
    20  		name          string
    21  		args          []string
    22  		expectedError string
    23  	}{
    24  		{
    25  			name:          "wrong-args",
    26  			expectedError: "requires exactly 1 argument.",
    27  			args:          []string{},
    28  		},
    29  		{
    30  			name:          "invalid-name",
    31  			expectedError: "invalid reference format: repository name must be lowercase",
    32  			args:          []string{"UPPERCASE_REPO"},
    33  		},
    34  		{
    35  			name:          "all-tags-with-tag",
    36  			expectedError: "tag can't be used with --all-tags/-a",
    37  			args:          []string{"--all-tags", "image:tag"},
    38  		},
    39  	}
    40  	for _, tc := range testCases {
    41  		cli := test.NewFakeCli(&fakeClient{})
    42  		cmd := NewPullCommand(cli)
    43  		cmd.SetOutput(ioutil.Discard)
    44  		cmd.SetArgs(tc.args)
    45  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    46  	}
    47  }
    48  
    49  func TestNewPullCommandSuccess(t *testing.T) {
    50  	testCases := []struct {
    51  		name        string
    52  		args        []string
    53  		expectedTag string
    54  	}{
    55  		{
    56  			name:        "simple",
    57  			args:        []string{"image:tag"},
    58  			expectedTag: "image:tag",
    59  		},
    60  		{
    61  			name:        "simple-no-tag",
    62  			args:        []string{"image"},
    63  			expectedTag: "image:latest",
    64  		},
    65  	}
    66  	for _, tc := range testCases {
    67  		cli := test.NewFakeCli(&fakeClient{
    68  			imagePullFunc: func(ref string, options types.ImagePullOptions) (io.ReadCloser, error) {
    69  				assert.Check(t, is.Equal(tc.expectedTag, ref), tc.name)
    70  				return ioutil.NopCloser(strings.NewReader("")), nil
    71  			},
    72  		})
    73  		cmd := NewPullCommand(cli)
    74  		cmd.SetOutput(ioutil.Discard)
    75  		cmd.SetArgs(tc.args)
    76  		err := cmd.Execute()
    77  		assert.NilError(t, err)
    78  		golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("pull-command-success.%s.golden", tc.name))
    79  	}
    80  }
    81  
    82  func TestNewPullCommandWithContentTrustErrors(t *testing.T) {
    83  	testCases := []struct {
    84  		name          string
    85  		args          []string
    86  		expectedError string
    87  		notaryFunc    test.NotaryClientFuncType
    88  	}{
    89  		{
    90  			name:          "offline-notary-server",
    91  			notaryFunc:    notary.GetOfflineNotaryRepository,
    92  			expectedError: "client is offline",
    93  			args:          []string{"image:tag"},
    94  		},
    95  		{
    96  			name:          "uninitialized-notary-server",
    97  			notaryFunc:    notary.GetUninitializedNotaryRepository,
    98  			expectedError: "remote trust data does not exist",
    99  			args:          []string{"image:tag"},
   100  		},
   101  		{
   102  			name:          "empty-notary-server",
   103  			notaryFunc:    notary.GetEmptyTargetsNotaryRepository,
   104  			expectedError: "No valid trust data for tag",
   105  			args:          []string{"image:tag"},
   106  		},
   107  	}
   108  	for _, tc := range testCases {
   109  		cli := test.NewFakeCli(&fakeClient{
   110  			imagePullFunc: func(ref string, options types.ImagePullOptions) (io.ReadCloser, error) {
   111  				return ioutil.NopCloser(strings.NewReader("")), fmt.Errorf("shouldn't try to pull image")
   112  			},
   113  		}, test.EnableContentTrust)
   114  		cli.SetNotaryClient(tc.notaryFunc)
   115  		cmd := NewPullCommand(cli)
   116  		cmd.SetOutput(ioutil.Discard)
   117  		cmd.SetArgs(tc.args)
   118  		err := cmd.Execute()
   119  		assert.ErrorContains(t, err, tc.expectedError)
   120  	}
   121  }