diff --git a/repoutils/repoutils.go b/repoutils/repoutils.go index fd50be7e..8ca2ee24 100644 --- a/repoutils/repoutils.go +++ b/repoutils/repoutils.go @@ -30,7 +30,7 @@ func GetAuthConfig(username, password, registry string) (types.AuthConfig, error dcfg, err := config.Load(config.Dir()) if err != nil { - return types.AuthConfig{}, fmt.Errorf("Loading config file from %s failed: %v", config.Dir(), err) + return types.AuthConfig{}, fmt.Errorf("Loading config file failed: %v", err) } // return error early if there are no auths saved @@ -79,7 +79,8 @@ func GetAuthConfig(username, password, registry string) (types.AuthConfig, error return creds, nil } } - fmt.Printf("Using registry '%s' with no authentication\n", registry) + + fmt.Printf("Using registry %q with no authentication\n", registry) // Otherwise just use the registry with no auth. return setDefaultRegistry(types.AuthConfig{ @@ -90,10 +91,12 @@ func GetAuthConfig(username, password, registry string) (types.AuthConfig, error // Just set the auth config as the first registryURL, username and password // found in the auth config. for _, creds := range authConfigs { + fmt.Printf("No registry passed. Using registry %q\n", creds.ServerAddress) return creds, nil } // Don't use any authentication. + // We should never get here. fmt.Println("Not using any authentication") return types.AuthConfig{}, nil } diff --git a/repoutils/repoutils_test.go b/repoutils/repoutils_test.go index c974498d..3b57db63 100644 --- a/repoutils/repoutils_test.go +++ b/repoutils/repoutils_test.go @@ -1,9 +1,14 @@ package repoutils import ( + "errors" + "os" + "path/filepath" + "strings" "testing" "github.com/docker/distribution/reference" + "github.com/docker/docker-ce/components/cli/cli/config" "github.com/docker/docker/api/types" "github.com/google/go-cmp/cmp" ) @@ -12,6 +17,7 @@ func TestGetAuthConfig(t *testing.T) { configTestcases := []struct { name string username, password, registry string + configdir string err error config types.AuthConfig }{ @@ -26,16 +32,110 @@ func TestGetAuthConfig(t *testing.T) { ServerAddress: "r.j3ss.co", }, }, + { + name: "invalid config dir", + configdir: "testdata/invalid", + err: errors.New("Loading config file failed: "), + config: types.AuthConfig{}, + }, + { + name: "empty config", + configdir: "testdata/empty", + config: types.AuthConfig{}, + }, + { + name: "empty config with registry", + registry: "r.j3ss.co", + configdir: "testdata/empty", + config: types.AuthConfig{ + ServerAddress: "r.j3ss.co", + }, + }, + { + name: "valid with multiple", + registry: "r.j3ss.co", + configdir: "testdata/valid", + config: types.AuthConfig{ + ServerAddress: "r.j3ss.co", + Username: "user", + Password: "blah\n", + }, + }, + { + name: "valid with multiple and https:// prefix", + registry: "https://r.j3ss.co", + configdir: "testdata/valid", + config: types.AuthConfig{ + ServerAddress: "r.j3ss.co", + Username: "user", + Password: "blah\n", + }, + }, + { + name: "valid with multiple and http:// prefix", + registry: "http://r.j3ss.co", + configdir: "testdata/valid", + config: types.AuthConfig{ + ServerAddress: "r.j3ss.co", + Username: "user", + Password: "blah\n", + }, + }, + { + name: "valid with multiple and no https:// prefix", + registry: "reg.j3ss.co", + configdir: "testdata/valid", + config: types.AuthConfig{ + ServerAddress: "https://reg.j3ss.co", + Username: "joe", + Password: "otherthing\n", + }, + }, + { + name: "valid with multiple and but registry not found", + registry: "otherreg.j3ss.co", + configdir: "testdata/valid", + config: types.AuthConfig{ + ServerAddress: "otherreg.j3ss.co", + }, + }, + { + name: "valid and no registry passed", + configdir: "testdata/singlevalid", + config: types.AuthConfig{ + ServerAddress: "https://index.docker.io/v1/", + Username: "user", + Password: "thing\n", + }, + }, + { + name: "no authentication", + configdir: "testdata/empty", + config: types.AuthConfig{}, + }, } for _, testcase := range configTestcases { + if testcase.configdir != "" { + // Set the config directory. + wd, err := os.Getwd() + if err != nil { + t.Fatalf("get working directory failed: %v", err) + } + config.SetDir(filepath.Join(wd, testcase.configdir)) + } + cfg, err := GetAuthConfig(testcase.username, testcase.password, testcase.registry) - if err != nil { - if err.Error() != testcase.err.Error() { + if err != nil || testcase.err != nil { + if err == nil || testcase.err == nil { + t.Fatalf("%q: expected err (%v), got err (%v)", testcase.name, testcase.err, err) + } + if !strings.Contains(err.Error(), testcase.err.Error()) { t.Fatalf("%q: expected err (%v), got err (%v)", testcase.name, testcase.err, err) } continue } + if diff := cmp.Diff(testcase.config, cfg); diff != "" { t.Errorf("%s: authconfig differs: (-got +want)\n%s", testcase.name, diff) } @@ -76,7 +176,10 @@ func TestGetRepoAndRef(t *testing.T) { for _, testcase := range imageTestcases { repo, ref, err := GetRepoAndRef(testcase.input) - if err != nil { + if err != nil || testcase.err != nil { + if err == nil || testcase.err == nil { + t.Fatalf("%q: expected err (%v), got err (%v)", testcase.input, testcase.err, err) + } if err.Error() != testcase.err.Error() { t.Fatalf("%q: expected err (%v), got err (%v)", testcase.input, testcase.err, err) } diff --git a/repoutils/testdata/empty/config.json b/repoutils/testdata/empty/config.json new file mode 100644 index 00000000..0db3279e --- /dev/null +++ b/repoutils/testdata/empty/config.json @@ -0,0 +1,3 @@ +{ + +} diff --git a/repoutils/testdata/invalid/config.json b/repoutils/testdata/invalid/config.json new file mode 100644 index 00000000..42df1ce6 --- /dev/null +++ b/repoutils/testdata/invalid/config.json @@ -0,0 +1,3 @@ +{ + "auths": "thing" +} diff --git a/repoutils/testdata/singlevalid/config.json b/repoutils/testdata/singlevalid/config.json new file mode 100644 index 00000000..2a21edd9 --- /dev/null +++ b/repoutils/testdata/singlevalid/config.json @@ -0,0 +1,7 @@ +{ + "auths": { + "https://index.docker.io/v1/": { + "auth": "dXNlcjp0aGluZwo=" + } + } +} diff --git a/repoutils/testdata/valid/config.json b/repoutils/testdata/valid/config.json new file mode 100644 index 00000000..8a09c11c --- /dev/null +++ b/repoutils/testdata/valid/config.json @@ -0,0 +1,13 @@ +{ + "auths": { + "https://index.docker.io/v1/": { + "auth": "dXNlcjp0aGluZwo=" + }, + "https://reg.j3ss.co": { + "auth": "am9lOm90aGVydGhpbmcK" + }, + "r.j3ss.co": { + "auth": "dXNlcjpibGFoCg==" + } + } +}