github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/image/push_test.go (about)

     1  package image
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/docker/cli/internal/test"
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/pkg/errors"
    12  	"gotest.tools/v3/assert"
    13  )
    14  
    15  func TestNewPushCommandErrors(t *testing.T) {
    16  	testCases := []struct {
    17  		name          string
    18  		args          []string
    19  		expectedError string
    20  		imagePushFunc func(ref string, options types.ImagePushOptions) (io.ReadCloser, error)
    21  	}{
    22  		{
    23  			name:          "wrong-args",
    24  			args:          []string{},
    25  			expectedError: "requires exactly 1 argument.",
    26  		},
    27  		{
    28  			name:          "invalid-name",
    29  			args:          []string{"UPPERCASE_REPO"},
    30  			expectedError: "invalid reference format: repository name must be lowercase",
    31  		},
    32  		{
    33  			name:          "push-failed",
    34  			args:          []string{"image:repo"},
    35  			expectedError: "Failed to push",
    36  			imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) {
    37  				return ioutil.NopCloser(strings.NewReader("")), errors.Errorf("Failed to push")
    38  			},
    39  		},
    40  	}
    41  	for _, tc := range testCases {
    42  		cli := test.NewFakeCli(&fakeClient{imagePushFunc: tc.imagePushFunc})
    43  		cmd := NewPushCommand(cli)
    44  		cmd.SetOut(ioutil.Discard)
    45  		cmd.SetArgs(tc.args)
    46  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    47  	}
    48  }
    49  
    50  func TestNewPushCommandSuccess(t *testing.T) {
    51  	testCases := []struct {
    52  		name   string
    53  		args   []string
    54  		output string
    55  	}{
    56  		{
    57  			name: "push",
    58  			args: []string{"image:tag"},
    59  		},
    60  		{
    61  			name: "push quiet",
    62  			args: []string{"--quiet", "image:tag"},
    63  			output: `docker.io/library/image:tag
    64  `,
    65  		},
    66  	}
    67  	for _, tc := range testCases {
    68  		tc := tc
    69  		t.Run(tc.name, func(t *testing.T) {
    70  			cli := test.NewFakeCli(&fakeClient{
    71  				imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) {
    72  					return ioutil.NopCloser(strings.NewReader("")), nil
    73  				},
    74  			})
    75  			cmd := NewPushCommand(cli)
    76  			cmd.SetOut(cli.OutBuffer())
    77  			cmd.SetArgs(tc.args)
    78  			assert.NilError(t, cmd.Execute())
    79  			if tc.output != "" {
    80  				assert.Equal(t, tc.output, cli.OutBuffer().String())
    81  			}
    82  		})
    83  	}
    84  }