github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/cli/command/registry_test.go (about)

     1  package command_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"testing"
     8  
     9  	"gotest.tools/v3/assert"
    10  	is "gotest.tools/v3/assert/cmp"
    11  
    12  	// Prevents a circular import with "github.com/docker/cli/internal/test"
    13  
    14  	. "github.com/docker/cli/cli/command"
    15  	configtypes "github.com/docker/cli/cli/config/types"
    16  	"github.com/docker/cli/internal/test"
    17  	"github.com/docker/docker/api/types"
    18  	"github.com/docker/docker/client"
    19  )
    20  
    21  type fakeClient struct {
    22  	client.Client
    23  	infoFunc func() (types.Info, error)
    24  }
    25  
    26  var testAuthConfigs = []types.AuthConfig{
    27  	{
    28  		ServerAddress: "https://index.docker.io/v1/",
    29  		Username:      "u0",
    30  		Password:      "p0",
    31  	},
    32  	{
    33  		ServerAddress: "server1.io",
    34  		Username:      "u1",
    35  		Password:      "p1",
    36  	},
    37  }
    38  
    39  func (cli *fakeClient) Info(_ context.Context) (types.Info, error) {
    40  	if cli.infoFunc != nil {
    41  		return cli.infoFunc()
    42  	}
    43  	return types.Info{}, nil
    44  }
    45  
    46  func TestGetDefaultAuthConfig(t *testing.T) {
    47  	testCases := []struct {
    48  		checkCredStore     bool
    49  		inputServerAddress string
    50  		expectedErr        string
    51  		expectedAuthConfig types.AuthConfig
    52  	}{
    53  		{
    54  			checkCredStore:     false,
    55  			inputServerAddress: "",
    56  			expectedErr:        "",
    57  			expectedAuthConfig: types.AuthConfig{
    58  				ServerAddress: "",
    59  				Username:      "",
    60  				Password:      "",
    61  			},
    62  		},
    63  		{
    64  			checkCredStore:     true,
    65  			inputServerAddress: testAuthConfigs[0].ServerAddress,
    66  			expectedErr:        "",
    67  			expectedAuthConfig: testAuthConfigs[0],
    68  		},
    69  		{
    70  			checkCredStore:     true,
    71  			inputServerAddress: testAuthConfigs[1].ServerAddress,
    72  			expectedErr:        "",
    73  			expectedAuthConfig: testAuthConfigs[1],
    74  		},
    75  		{
    76  			checkCredStore:     true,
    77  			inputServerAddress: fmt.Sprintf("https://%s", testAuthConfigs[1].ServerAddress),
    78  			expectedErr:        "",
    79  			expectedAuthConfig: testAuthConfigs[1],
    80  		},
    81  	}
    82  	cli := test.NewFakeCli(&fakeClient{})
    83  	errBuf := new(bytes.Buffer)
    84  	cli.SetErr(errBuf)
    85  	for _, authconfig := range testAuthConfigs {
    86  		cli.ConfigFile().GetCredentialsStore(authconfig.ServerAddress).Store(configtypes.AuthConfig(authconfig))
    87  	}
    88  	for _, tc := range testCases {
    89  		serverAddress := tc.inputServerAddress
    90  		authconfig, err := GetDefaultAuthConfig(cli, tc.checkCredStore, serverAddress, serverAddress == "https://index.docker.io/v1/")
    91  		if tc.expectedErr != "" {
    92  			assert.Check(t, err != nil)
    93  			assert.Check(t, is.Equal(tc.expectedErr, err.Error()))
    94  		} else {
    95  			assert.NilError(t, err)
    96  			assert.Check(t, is.DeepEqual(tc.expectedAuthConfig, authconfig))
    97  		}
    98  	}
    99  }
   100  
   101  func TestGetDefaultAuthConfig_HelperError(t *testing.T) {
   102  	cli := test.NewFakeCli(&fakeClient{})
   103  	errBuf := new(bytes.Buffer)
   104  	cli.SetErr(errBuf)
   105  	cli.ConfigFile().CredentialsStore = "fake-does-not-exist"
   106  	serverAddress := "test-server-address"
   107  	expectedAuthConfig := types.AuthConfig{
   108  		ServerAddress: serverAddress,
   109  	}
   110  	authconfig, err := GetDefaultAuthConfig(cli, true, serverAddress, serverAddress == "https://index.docker.io/v1/")
   111  	assert.Check(t, is.DeepEqual(expectedAuthConfig, authconfig))
   112  	assert.Check(t, is.ErrorContains(err, "docker-credential-fake-does-not-exist"))
   113  }