2017-05-24 00:45:38 -04:00
|
|
|
package command_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
// Prevents a circular import with "github.com/docker/cli/internal/test"
|
2017-05-24 00:45:38 -04:00
|
|
|
. "github.com/docker/cli/cli/command"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-05-24 00:45:38 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fakeClient struct {
|
|
|
|
client.Client
|
|
|
|
infoFunc func() (types.Info, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *fakeClient) Info(_ context.Context) (types.Info, error) {
|
|
|
|
if cli.infoFunc != nil {
|
|
|
|
return cli.infoFunc()
|
|
|
|
}
|
|
|
|
return types.Info{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestElectAuthServer(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
expectedAuthServer string
|
|
|
|
expectedWarning string
|
|
|
|
infoFunc func() (types.Info, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
expectedAuthServer: "https://index.docker.io/v1/",
|
|
|
|
expectedWarning: "",
|
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{IndexServerAddress: "https://index.docker.io/v1/"}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expectedAuthServer: "https://index.docker.io/v1/",
|
|
|
|
expectedWarning: "Empty registry endpoint from daemon",
|
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{IndexServerAddress: ""}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expectedAuthServer: "https://foo.bar",
|
|
|
|
expectedWarning: "",
|
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{IndexServerAddress: "https://foo.bar"}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expectedAuthServer: "https://index.docker.io/v1/",
|
|
|
|
expectedWarning: "failed to get default registry endpoint from daemon",
|
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{}, errors.Errorf("error getting info")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{infoFunc: tc.infoFunc})
|
2017-05-24 00:45:38 -04:00
|
|
|
server := ElectAuthServer(context.Background(), cli)
|
|
|
|
assert.Equal(t, tc.expectedAuthServer, server)
|
2017-07-05 14:43:39 -04:00
|
|
|
actual := cli.ErrBuffer().String()
|
2017-05-24 00:45:38 -04:00
|
|
|
if tc.expectedWarning == "" {
|
|
|
|
assert.Empty(t, actual)
|
|
|
|
} else {
|
|
|
|
assert.Contains(t, actual, tc.expectedWarning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|