Skip to content

Commit

Permalink
test: add configfile test
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Kotzbauer <git@ckotzbauer.de>
  • Loading branch information
ckotzbauer committed May 8, 2022
1 parent d076603 commit 39d60e4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/registry/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/pkg/errors"
)

// This is ported from https://github.com/docker/cli/blob/v20.10.12/cli/config/config.go
// This is ported from https://github.com/docker/cli/blob/v20.10.15/cli/config/configfile/file.go
// The only changes to the original source are the fact, that the "auth" field is not decoded
// when "username" or "password" are not blank to avoid overwrites.

Expand Down
65 changes: 65 additions & 0 deletions internal/registry/configfile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package registry

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/config/types"
"github.com/stretchr/testify/assert"
)

func TestOldJSONReaderNoFile(t *testing.T) {
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
configFile := configfile.ConfigFile{
AuthConfigs: make(map[string]types.AuthConfig),
}

err := LegacyLoadFromReader(strings.NewReader(js), &configFile)
assert.Nil(t, err)

ac := configFile.AuthConfigs["https://index.docker.io/v1/"]
assert.Equal(t, ac.Username, "joejoe")
assert.Equal(t, ac.Password, "hello")
}

func TestLegacyJSONSaveWithNoFile(t *testing.T) {
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
configFile := configfile.ConfigFile{
AuthConfigs: make(map[string]types.AuthConfig),
}

err := LegacyLoadFromReader(strings.NewReader(js), &configFile)
assert.Nil(t, err)
err = configFile.Save()
assert.ErrorContains(t, err, "with empty filename")

tmpHome, err := ioutil.TempDir("", "config-test")
assert.Nil(t, err)
defer os.RemoveAll(tmpHome)

fn := filepath.Join(tmpHome, config.ConfigFileName)
f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
defer f.Close()

assert.Nil(t, configFile.SaveToWriter(f))
buf, err := ioutil.ReadFile(filepath.Join(tmpHome, config.ConfigFileName))
assert.Nil(t, err)

expConfStr := `{
"auths": {
"https://index.docker.io/v1/": {
"auth": "am9lam9lOmhlbGxv",
"email": "user@example.com"
}
}
}`

if string(buf) != expConfStr {
t.Fatalf("Should have save in new form: \n%s\n not \n%s", string(buf), expConfStr)
}
}

0 comments on commit 39d60e4

Please sign in to comment.