github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/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/v3/assert"
    14  	is "gotest.tools/v3/assert/cmp"
    15  	"gotest.tools/v3/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.SetOut(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  			name:        "simple-quiet",
    67  			args:        []string{"--quiet", "image"},
    68  			expectedTag: "image:latest",
    69  		},
    70  	}
    71  	for _, tc := range testCases {
    72  		cli := test.NewFakeCli(&fakeClient{
    73  			imagePullFunc: func(ref string, options types.ImagePullOptions) (io.ReadCloser, error) {
    74  				assert.Check(t, is.Equal(tc.expectedTag, ref), tc.name)
    75  				return ioutil.NopCloser(strings.NewReader("")), nil
    76  			},
    77  		})
    78  		cmd := NewPullCommand(cli)
    79  		cmd.SetOut(ioutil.Discard)
    80  		cmd.SetArgs(tc.args)
    81  		err := cmd.Execute()
    82  		assert.NilError(t, err)
    83  		golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("pull-command-success.%s.golden", tc.name))
    84  	}
    85  }
    86  
    87  func TestNewPullCommandWithContentTrustErrors(t *testing.T) {
    88  	testCases := []struct {
    89  		name          string
    90  		args          []string
    91  		expectedError string
    92  		notaryFunc    test.NotaryClientFuncType
    93  	}{
    94  		{
    95  			name:          "offline-notary-server",
    96  			notaryFunc:    notary.GetOfflineNotaryRepository,
    97  			expectedError: "client is offline",
    98  			args:          []string{"image:tag"},
    99  		},
   100  		{
   101  			name:          "uninitialized-notary-server",
   102  			notaryFunc:    notary.GetUninitializedNotaryRepository,
   103  			expectedError: "remote trust data does not exist",
   104  			args:          []string{"image:tag"},
   105  		},
   106  		{
   107  			name:          "empty-notary-server",
   108  			notaryFunc:    notary.GetEmptyTargetsNotaryRepository,
   109  			expectedError: "No valid trust data for tag",
   110  			args:          []string{"image:tag"},
   111  		},
   112  	}
   113  	for _, tc := range testCases {
   114  		cli := test.NewFakeCli(&fakeClient{
   115  			imagePullFunc: func(ref string, options types.ImagePullOptions) (io.ReadCloser, error) {
   116  				return ioutil.NopCloser(strings.NewReader("")), fmt.Errorf("shouldn't try to pull image")
   117  			},
   118  		}, test.EnableContentTrust)
   119  		cli.SetNotaryClient(tc.notaryFunc)
   120  		cmd := NewPullCommand(cli)
   121  		cmd.SetOut(ioutil.Discard)
   122  		cmd.SetArgs(tc.args)
   123  		err := cmd.Execute()
   124  		assert.ErrorContains(t, err, tc.expectedError)
   125  	}
   126  }