2017-05-24 00:45:38 -04:00
|
|
|
package command_test
|
|
|
|
|
|
|
|
import (
|
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"
|
2023-07-10 11:24:07 -04:00
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-10-15 15:39:56 -04:00
|
|
|
configtypes "github.com/docker/cli/cli/config/types"
|
2023-02-07 20:31:59 -05:00
|
|
|
"github.com/docker/docker/api/types/registry"
|
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
|
|
|
)
|
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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],
|
|
|
|
},
|
|
|
|
}
|
2023-07-10 11:24:07 -04:00
|
|
|
cfg := configfile.New("filename")
|
2017-05-30 17:36:15 -04:00
|
|
|
for _, authconfig := range testAuthConfigs {
|
2023-07-10 11:24:07 -04:00
|
|
|
cfg.GetCredentialsStore(authconfig.ServerAddress).Store(configtypes.AuthConfig(authconfig))
|
2017-05-30 17:36:15 -04:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
serverAddress := tc.inputServerAddress
|
2023-07-10 11:24:07 -04:00
|
|
|
authconfig, err := GetDefaultAuthConfig(cfg, tc.checkCredStore, serverAddress, serverAddress == "https://index.docker.io/v1/")
|
2017-05-30 17:36:15 -04:00
|
|
|
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) {
|
2023-07-10 11:24:07 -04:00
|
|
|
cfg := configfile.New("filename")
|
|
|
|
cfg.CredentialsStore = "fake-does-not-exist"
|
|
|
|
|
|
|
|
const 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,
|
|
|
|
}
|
2023-07-10 11:24:07 -04:00
|
|
|
authconfig, err := GetDefaultAuthConfig(cfg, true, serverAddress, serverAddress == "https://index.docker.io/v1/")
|
2021-07-21 20:59:42 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expectedAuthConfig, authconfig))
|
|
|
|
assert.Check(t, is.ErrorContains(err, "docker-credential-fake-does-not-exist"))
|
|
|
|
}
|