2017-05-24 00:45:38 -04:00
|
|
|
package command_test
|
|
|
|
|
|
|
|
import (
|
2017-05-30 17:36:15 -04:00
|
|
|
"bytes"
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-05-30 17:36:15 -04:00
|
|
|
"fmt"
|
2017-05-24 00:45:38 -04:00
|
|
|
"testing"
|
|
|
|
|
2023-03-21 11:55:34 -04:00
|
|
|
. "github.com/docker/cli/cli/command" // Prevents a circular import with "github.com/docker/cli/internal/test"
|
2017-10-15 15:39:56 -04:00
|
|
|
configtypes "github.com/docker/cli/cli/config/types"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2023-02-07 20:31:59 -05:00
|
|
|
"github.com/docker/docker/api/types/registry"
|
2023-07-14 17:42:40 -04:00
|
|
|
"github.com/docker/docker/api/types/system"
|
2017-05-24 00:45:38 -04:00
|
|
|
"github.com/docker/docker/client"
|
2023-03-21 11:55:34 -04:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-05-24 00:45:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type fakeClient struct {
|
|
|
|
client.Client
|
2023-07-14 17:42:40 -04:00
|
|
|
infoFunc func() (system.Info, error)
|
2017-05-24 00:45:38 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 20:31:59 -05:00
|
|
|
var testAuthConfigs = []registry.AuthConfig{
|
2017-05-30 17:36:15 -04:00
|
|
|
{
|
|
|
|
ServerAddress: "https://index.docker.io/v1/",
|
|
|
|
Username: "u0",
|
|
|
|
Password: "p0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ServerAddress: "server1.io",
|
|
|
|
Username: "u1",
|
|
|
|
Password: "p1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-14 17:42:40 -04:00
|
|
|
func (cli *fakeClient) Info(_ context.Context) (system.Info, error) {
|
2017-05-24 00:45:38 -04:00
|
|
|
if cli.infoFunc != nil {
|
|
|
|
return cli.infoFunc()
|
|
|
|
}
|
2023-07-14 17:42:40 -04:00
|
|
|
return system.Info{}, nil
|
2017-05-24 00:45:38 -04:00
|
|
|
}
|
|
|
|
|
2017-05-30 17:36:15 -04:00
|
|
|
func TestGetDefaultAuthConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
checkCredStore bool
|
|
|
|
inputServerAddress string
|
|
|
|
expectedErr string
|
2023-02-07 20:31:59 -05:00
|
|
|
expectedAuthConfig registry.AuthConfig
|
2017-05-30 17:36:15 -04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
checkCredStore: false,
|
|
|
|
inputServerAddress: "",
|
|
|
|
expectedErr: "",
|
2023-02-07 20:31:59 -05:00
|
|
|
expectedAuthConfig: registry.AuthConfig{
|
2017-05-30 17:36:15 -04:00
|
|
|
ServerAddress: "",
|
|
|
|
Username: "",
|
|
|
|
Password: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
checkCredStore: true,
|
|
|
|
inputServerAddress: testAuthConfigs[0].ServerAddress,
|
|
|
|
expectedErr: "",
|
|
|
|
expectedAuthConfig: testAuthConfigs[0],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
checkCredStore: true,
|
|
|
|
inputServerAddress: testAuthConfigs[1].ServerAddress,
|
|
|
|
expectedErr: "",
|
|
|
|
expectedAuthConfig: testAuthConfigs[1],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
checkCredStore: true,
|
|
|
|
inputServerAddress: fmt.Sprintf("https://%s", testAuthConfigs[1].ServerAddress),
|
|
|
|
expectedErr: "",
|
|
|
|
expectedAuthConfig: testAuthConfigs[1],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
|
|
|
errBuf := new(bytes.Buffer)
|
|
|
|
cli.SetErr(errBuf)
|
|
|
|
for _, authconfig := range testAuthConfigs {
|
2017-10-15 15:39:56 -04:00
|
|
|
cli.ConfigFile().GetCredentialsStore(authconfig.ServerAddress).Store(configtypes.AuthConfig(authconfig))
|
2017-05-30 17:36:15 -04:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
serverAddress := tc.inputServerAddress
|
|
|
|
authconfig, err := GetDefaultAuthConfig(cli, tc.checkCredStore, serverAddress, serverAddress == "https://index.docker.io/v1/")
|
|
|
|
if tc.expectedErr != "" {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, err != nil)
|
|
|
|
assert.Check(t, is.Equal(tc.expectedErr, err.Error()))
|
2017-05-30 17:36:15 -04:00
|
|
|
} else {
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2021-07-21 20:59:42 -04:00
|
|
|
assert.Check(t, is.DeepEqual(tc.expectedAuthConfig, authconfig))
|
2017-05-30 17:36:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-21 20:59:42 -04:00
|
|
|
|
|
|
|
func TestGetDefaultAuthConfig_HelperError(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
|
|
|
errBuf := new(bytes.Buffer)
|
|
|
|
cli.SetErr(errBuf)
|
|
|
|
cli.ConfigFile().CredentialsStore = "fake-does-not-exist"
|
|
|
|
serverAddress := "test-server-address"
|
2023-02-07 20:31:59 -05:00
|
|
|
expectedAuthConfig := registry.AuthConfig{
|
2021-07-21 20:59:42 -04:00
|
|
|
ServerAddress: serverAddress,
|
|
|
|
}
|
|
|
|
authconfig, err := GetDefaultAuthConfig(cli, true, serverAddress, serverAddress == "https://index.docker.io/v1/")
|
|
|
|
assert.Check(t, is.DeepEqual(expectedAuthConfig, authconfig))
|
|
|
|
assert.Check(t, is.ErrorContains(err, "docker-credential-fake-does-not-exist"))
|
|
|
|
}
|