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