Skip to content

Commit

Permalink
auth: add some tests for NewAuthConfigurationsFromDockerCfg
Browse files Browse the repository at this point in the history
  • Loading branch information
fsouza committed Apr 9, 2020
1 parent 4a7bf99 commit dd8b321
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
3 changes: 3 additions & 0 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ func NewAuthConfigurationsFromDockerCfg() (*AuthConfigurations, error) {
if len(pathsToTry) < 1 {
return nil, errors.New("no docker configuration found")
}
return newAuthConfigurationsFromDockerCfg(pathsToTry)
}

func newAuthConfigurationsFromDockerCfg(pathsToTry []string) (*AuthConfigurations, error) {
var result *AuthConfigurations
var auths *AuthConfigurations
var err error
Expand Down
43 changes: 42 additions & 1 deletion auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestAuthConfigurationsFromFile(t *testing.T) {
t.Parallel()
tmpDir, err := ioutil.TempDir("", "go-dockerclient-auth-test")
if err != nil {
t.Errorf("Unable to create temporary directory for TestAuthConfigurationsFromFile: %s", err)
t.Fatalf("Unable to create temporary directory for TestAuthConfigurationsFromFile: %s", err)
}
defer os.RemoveAll(tmpDir)
authString := base64.StdEncoding.EncodeToString([]byte("user:pass"))
Expand All @@ -62,6 +62,47 @@ func TestAuthConfigurationsFromFile(t *testing.T) {
}
}

func TestAuthConfigurationsFromDockerCfg(t *testing.T) {
t.Parallel()
tmpDir, err := ioutil.TempDir("", "go-dockerclient-auth-dockercfg-test")
if err != nil {
t.Fatalf("Unable to create temporary directory for TestAuthConfigurationsFromDockerCfg: %s", err)
}
defer os.RemoveAll(tmpDir)

keys := []string{
"docker.io",
"us.gcr.io",
}
pathsToTry := []string{"some/unknown/path"}
for i, key := range keys {
authString := base64.StdEncoding.EncodeToString([]byte("user:pass"))
content := fmt.Sprintf(`{"auths":{"%s": {"auth": "%s"}}}`, key, authString)
configFile := path.Join(tmpDir, fmt.Sprintf("docker_config_%d.json", i))
if err = ioutil.WriteFile(configFile, []byte(content), 0600); err != nil {
t.Errorf("Error writing auth config for TestAuthConfigurationsFromFile: %s", err)
}
pathsToTry = append(pathsToTry, configFile)
}
auths, err := newAuthConfigurationsFromDockerCfg(pathsToTry)
if err != nil {
t.Errorf("Error calling NewAuthConfigurationsFromFile: %s", err)
}

for _, key := range keys {
if _, hasKey := auths.Configs[key]; !hasKey {
t.Errorf("Returned auths did not include expected auth key %q", key)
}
}
}

func TestAuthConfigurationsFromDockerCfgError(t *testing.T) {
auths, err := newAuthConfigurationsFromDockerCfg([]string{"this/doesnt/exist.json"})
if err == nil {
t.Fatalf("unexpected <nil> error, returned auth config: %#v", auths)
}
}

func TestAuthLegacyConfig(t *testing.T) {
t.Parallel()
auth := base64.StdEncoding.EncodeToString([]byte("user:pa:ss"))
Expand Down

0 comments on commit dd8b321

Please sign in to comment.