This repository has been archived by the owner on Oct 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from mlaventure/chp-cli-126
Handle a Docker daemon without registry info
- Loading branch information
Showing
2 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package command_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/net/context" | ||
|
||
// Prevents a circular import with "github.com/docker/cli/cli/internal/test" | ||
. "github.com/docker/cli/cli/command" | ||
"github.com/docker/cli/cli/internal/test" | ||
"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 { | ||
buf := new(bytes.Buffer) | ||
cli := test.NewFakeCli(&fakeClient{infoFunc: tc.infoFunc}, buf) | ||
errBuf := new(bytes.Buffer) | ||
cli.SetErr(errBuf) | ||
server := ElectAuthServer(context.Background(), cli) | ||
assert.Equal(t, tc.expectedAuthServer, server) | ||
actual := errBuf.String() | ||
if tc.expectedWarning == "" { | ||
assert.Empty(t, actual) | ||
} else { | ||
assert.Contains(t, actual, tc.expectedWarning) | ||
} | ||
} | ||
} |