Skip to content

Commit

Permalink
Remove multiple duplicate erases
Browse files Browse the repository at this point in the history
Signed-off-by: jrpalma <[email protected]>
  • Loading branch information
jrpalma committed Feb 19, 2020
1 parent 75299fd commit 98b7215
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cli/command/registry/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ func runLogout(dockerCli command.Cli, serverAddress string) error {
hostnameAddress = serverAddress
regsToTry = []string{serverAddress}
)

if !isDefaultRegistry {
hostnameAddress = registry.ConvertToHostname(serverAddress)
// the tries below are kept for backward compatibility where a user could have
// saved the registry in one of the following format.
regsToTry = append(regsToTry, hostnameAddress, "http://"+hostnameAddress, "https://"+hostnameAddress)
regsToTry = append(regsToTry, "http://"+hostnameAddress, "https://"+hostnameAddress)
}

// check if we're logged in based on the records in the config file
Expand Down
91 changes: 91 additions & 0 deletions cli/command/registry/logout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package registry

import (
"bytes"
"context"
"fmt"
"strings"
"testing"

"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
registrytypes "github.com/docker/docker/api/types/registry"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)

func (c fakeClient) RegistryLogout(ctx context.Context, auth types.AuthConfig) (registrytypes.AuthenticateOKBody, error) {
if auth.Password == expiredPassword {
return registrytypes.AuthenticateOKBody{}, fmt.Errorf("Invalid Username or Password")
}
if auth.Password == useToken {
return registrytypes.AuthenticateOKBody{
IdentityToken: auth.Password,
}, nil
}
err := testAuthErrors[auth.Username]
return registrytypes.AuthenticateOKBody{}, err
}

func TestLogoutWithCredStoreCreds(t *testing.T) {
testCases := []struct {
warningCount int
serverURL string
}{
{
serverURL: "registry",
warningCount: 1,
},
{
serverURL: "badregistry",
warningCount: 0,
},
}
for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{})

configStr := `
{
"auths": {
"registry": {}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/18.09.7 (linux)"
}
}
`

errBuf := new(bytes.Buffer)
cli.SetErr(errBuf)

configReader := bytes.NewReader([]byte(configStr))
cli.ConfigFile().LoadFromReader(configReader)

runLogout(cli, tc.serverURL)
errorString := errBuf.String()

//We will fail since the file store will fail to delete
//the file. We only only want one warning to ensure we
//only logout once and not twice.
warningCount := wordCount(errorString, "WARNING:")
assert.Check(t, is.Equal(tc.warningCount, warningCount), "Unexpected number of warnings")
}
}

func wordCount(s string, w string) int {
var count, index, wlen int

wlen = len(w)
index = strings.Index(s, w)

for {
if index == -1 {
break
}
index += wlen
s = s[index:]
index = strings.Index(s, w)
count++
}
return count
}

0 comments on commit 98b7215

Please sign in to comment.