github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/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/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.SetOutput(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  	}{
    55  		{
    56  			name: "simple",
    57  			args: []string{"image:tag"},
    58  		},
    59  	}
    60  	for _, tc := range testCases {
    61  		cli := test.NewFakeCli(&fakeClient{
    62  			imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) {
    63  				return ioutil.NopCloser(strings.NewReader("")), nil
    64  			},
    65  		})
    66  		cmd := NewPushCommand(cli)
    67  		cmd.SetOutput(ioutil.Discard)
    68  		cmd.SetArgs(tc.args)
    69  		assert.NilError(t, cmd.Execute())
    70  	}
    71  }