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

     1  package command_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"testing"
     8  
     9  	"github.com/pkg/errors"
    10  	"gotest.tools/assert"
    11  	is "gotest.tools/assert/cmp"
    12  
    13  	// Prevents a circular import with "github.com/docker/cli/internal/test"
    14  
    15  	. "github.com/docker/cli/cli/command"
    16  	"github.com/docker/cli/cli/debug"
    17  	"github.com/docker/cli/internal/test"
    18  	"github.com/docker/docker/api/types"
    19  	"github.com/docker/docker/client"
    20  )
    21  
    22  type fakeClient struct {
    23  	client.Client
    24  	infoFunc func() (types.Info, error)
    25  }
    26  
    27  var testAuthConfigs = []types.AuthConfig{
    28  	{
    29  		ServerAddress: "https://index.docker.io/v1/",
    30  		Username:      "u0",
    31  		Password:      "p0",
    32  	},
    33  	{
    34  		ServerAddress: "server1.io",
    35  		Username:      "u1",
    36  		Password:      "p1",
    37  	},
    38  }
    39  
    40  func (cli *fakeClient) Info(_ context.Context) (types.Info, error) {
    41  	if cli.infoFunc != nil {
    42  		return cli.infoFunc()
    43  	}
    44  	return types.Info{}, nil
    45  }
    46  
    47  func TestElectAuthServer(t *testing.T) {
    48  	testCases := []struct {
    49  		expectedAuthServer string
    50  		expectedWarning    string
    51  		infoFunc           func() (types.Info, error)
    52  	}{
    53  		{
    54  			expectedAuthServer: "https://index.docker.io/v1/",
    55  			expectedWarning:    "",
    56  			infoFunc: func() (types.Info, error) {
    57  				return types.Info{IndexServerAddress: "https://index.docker.io/v1/"}, nil
    58  			},
    59  		},
    60  		{
    61  			expectedAuthServer: "https://index.docker.io/v1/",
    62  			expectedWarning:    "Empty registry endpoint from daemon",
    63  			infoFunc: func() (types.Info, error) {
    64  				return types.Info{IndexServerAddress: ""}, nil
    65  			},
    66  		},
    67  		{
    68  			expectedAuthServer: "https://foo.bar",
    69  			expectedWarning:    "",
    70  			infoFunc: func() (types.Info, error) {
    71  				return types.Info{IndexServerAddress: "https://foo.bar"}, nil
    72  			},
    73  		},
    74  		{
    75  			expectedAuthServer: "https://index.docker.io/v1/",
    76  			expectedWarning:    "failed to get default registry endpoint from daemon",
    77  			infoFunc: func() (types.Info, error) {
    78  				return types.Info{}, errors.Errorf("error getting info")
    79  			},
    80  		},
    81  	}
    82  	// Enable debug to see warnings we're checking for
    83  	debug.Enable()
    84  	for _, tc := range testCases {
    85  		cli := test.NewFakeCli(&fakeClient{infoFunc: tc.infoFunc})
    86  		server := ElectAuthServer(context.Background(), cli)
    87  		assert.Check(t, is.Equal(tc.expectedAuthServer, server))
    88  		actual := cli.ErrBuffer().String()
    89  		if tc.expectedWarning == "" {
    90  			assert.Check(t, is.Len(actual, 0))
    91  		} else {
    92  			assert.Check(t, is.Contains(actual, tc.expectedWarning))
    93  		}
    94  	}
    95  }
    96  
    97  func TestGetDefaultAuthConfig(t *testing.T) {
    98  	testCases := []struct {
    99  		checkCredStore     bool
   100  		inputServerAddress string
   101  		expectedErr        string
   102  		expectedAuthConfig types.AuthConfig
   103  	}{
   104  		{
   105  			checkCredStore:     false,
   106  			inputServerAddress: "",
   107  			expectedErr:        "",
   108  			expectedAuthConfig: types.AuthConfig{
   109  				ServerAddress: "",
   110  				Username:      "",
   111  				Password:      "",
   112  			},
   113  		},
   114  		{
   115  			checkCredStore:     true,
   116  			inputServerAddress: testAuthConfigs[0].ServerAddress,
   117  			expectedErr:        "",
   118  			expectedAuthConfig: testAuthConfigs[0],
   119  		},
   120  		{
   121  			checkCredStore:     true,
   122  			inputServerAddress: testAuthConfigs[1].ServerAddress,
   123  			expectedErr:        "",
   124  			expectedAuthConfig: testAuthConfigs[1],
   125  		},
   126  		{
   127  			checkCredStore:     true,
   128  			inputServerAddress: fmt.Sprintf("https://%s", testAuthConfigs[1].ServerAddress),
   129  			expectedErr:        "",
   130  			expectedAuthConfig: testAuthConfigs[1],
   131  		},
   132  	}
   133  	cli := test.NewFakeCli(&fakeClient{})
   134  	errBuf := new(bytes.Buffer)
   135  	cli.SetErr(errBuf)
   136  	for _, authconfig := range testAuthConfigs {
   137  		cli.ConfigFile().GetCredentialsStore(authconfig.ServerAddress).Store(authconfig)
   138  	}
   139  	for _, tc := range testCases {
   140  		serverAddress := tc.inputServerAddress
   141  		authconfig, err := GetDefaultAuthConfig(cli, tc.checkCredStore, serverAddress, serverAddress == "https://index.docker.io/v1/")
   142  		if tc.expectedErr != "" {
   143  			assert.Check(t, err != nil)
   144  			assert.Check(t, is.Equal(tc.expectedErr, err.Error()))
   145  		} else {
   146  			assert.NilError(t, err)
   147  			assert.Check(t, is.DeepEqual(tc.expectedAuthConfig, *authconfig))
   148  		}
   149  	}
   150  }