From 39d60e491b6ae74ac3a9cb5d336dd282801bf4d9 Mon Sep 17 00:00:00 2001 From: Christian Kotzbauer Date: Sun, 8 May 2022 15:49:46 +0200 Subject: [PATCH] test: add configfile test Signed-off-by: Christian Kotzbauer --- internal/registry/configfile.go | 2 +- internal/registry/configfile_test.go | 65 ++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 internal/registry/configfile_test.go diff --git a/internal/registry/configfile.go b/internal/registry/configfile.go index 4a8421e2..268faa57 100644 --- a/internal/registry/configfile.go +++ b/internal/registry/configfile.go @@ -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. diff --git a/internal/registry/configfile_test.go b/internal/registry/configfile_test.go new file mode 100644 index 00000000..8c17302b --- /dev/null +++ b/internal/registry/configfile_test.go @@ -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) + } +}