registry/auth_test.go
8d88ea0c
 package registry
be20f3c5
 
 import (
 	"testing"
bb9da6ba
 
907407d0
 	"github.com/docker/engine-api/types"
 	registrytypes "github.com/docker/engine-api/types/registry"
be20f3c5
 )
 
96c10098
 func buildAuthConfigs() map[string]types.AuthConfig {
 	authConfigs := map[string]types.AuthConfig{}
0fc11699
 
4fcb9ac4
 	for _, registry := range []string{"testIndex", IndexServer} {
5b321e32
 		authConfigs[registry] = types.AuthConfig{
edde4f55
 			Username: "docker-user",
 			Password: "docker-pass",
 		}
 	}
 
920ea135
 	return authConfigs
edde4f55
 }
 
 func TestSameAuthDataPostSave(t *testing.T) {
920ea135
 	authConfigs := buildAuthConfigs()
 	authConfig := authConfigs["testIndex"]
0fc11699
 	if authConfig.Username != "docker-user" {
 		t.Fail()
 	}
 	if authConfig.Password != "docker-pass" {
 		t.Fail()
 	}
 	if authConfig.Auth != "" {
 		t.Fail()
 	}
 }
edde4f55
 
 func TestResolveAuthConfigIndexServer(t *testing.T) {
920ea135
 	authConfigs := buildAuthConfigs()
 	indexConfig := authConfigs[IndexServer]
568f86eb
 
96c10098
 	officialIndex := &registrytypes.IndexInfo{
568f86eb
 		Official: true,
 	}
96c10098
 	privateIndex := &registrytypes.IndexInfo{
568f86eb
 		Official: false,
edde4f55
 	}
568f86eb
 
920ea135
 	resolved := ResolveAuthConfig(authConfigs, officialIndex)
4fcb9ac4
 	assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer")
568f86eb
 
920ea135
 	resolved = ResolveAuthConfig(authConfigs, privateIndex)
4fcb9ac4
 	assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServer")
edde4f55
 }
 
 func TestResolveAuthConfigFullURL(t *testing.T) {
920ea135
 	authConfigs := buildAuthConfigs()
edde4f55
 
5b321e32
 	registryAuth := types.AuthConfig{
edde4f55
 		Username: "foo-user",
 		Password: "foo-pass",
 	}
5b321e32
 	localAuth := types.AuthConfig{
edde4f55
 		Username: "bar-user",
 		Password: "bar-pass",
 	}
5b321e32
 	officialAuth := types.AuthConfig{
568f86eb
 		Username: "baz-user",
 		Password: "baz-pass",
 	}
920ea135
 	authConfigs[IndexServer] = officialAuth
568f86eb
 
5b321e32
 	expectedAuths := map[string]types.AuthConfig{
568f86eb
 		"registry.example.com": registryAuth,
 		"localhost:8000":       localAuth,
 		"registry.com":         localAuth,
 	}
edde4f55
 
 	validRegistries := map[string][]string{
568f86eb
 		"registry.example.com": {
edde4f55
 			"https://registry.example.com/v1/",
 			"http://registry.example.com/v1/",
 			"registry.example.com",
 			"registry.example.com/v1/",
 		},
568f86eb
 		"localhost:8000": {
edde4f55
 			"https://localhost:8000/v1/",
 			"http://localhost:8000/v1/",
 			"localhost:8000",
 			"localhost:8000/v1/",
 		},
90b0cce0
 		"registry.com": {
 			"https://registry.com/v1/",
 			"http://registry.com/v1/",
 			"registry.com",
 			"registry.com/v1/",
 		},
edde4f55
 	}
 
 	for configKey, registries := range validRegistries {
568f86eb
 		configured, ok := expectedAuths[configKey]
aee260d4
 		if !ok {
a75b02fe
 			t.Fail()
568f86eb
 		}
96c10098
 		index := &registrytypes.IndexInfo{
568f86eb
 			Name: configKey,
 		}
edde4f55
 		for _, registry := range registries {
920ea135
 			authConfigs[registry] = configured
 			resolved := ResolveAuthConfig(authConfigs, index)
aee260d4
 			if resolved.Username != configured.Username || resolved.Password != configured.Password {
 				t.Errorf("%s -> %v != %v\n", registry, resolved, configured)
edde4f55
 			}
920ea135
 			delete(authConfigs, registry)
 			resolved = ResolveAuthConfig(authConfigs, index)
aee260d4
 			if resolved.Username == configured.Username || resolved.Password == configured.Password {
 				t.Errorf("%s -> %v == %v\n", registry, resolved, configured)
568f86eb
 			}
edde4f55
 		}
 	}
 }