2014-03-11 00:16:58 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-04-22 12:06:58 +00:00
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
2014-03-11 00:16:58 +00:00
|
|
|
)
|
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
func buildAuthConfigs() map[string]types.AuthConfig {
|
|
|
|
authConfigs := map[string]types.AuthConfig{}
|
2014-03-11 00:16:58 +00:00
|
|
|
|
2015-07-21 19:40:36 +00:00
|
|
|
for _, registry := range []string{"testIndex", IndexServer} {
|
2015-12-12 04:11:42 +00:00
|
|
|
authConfigs[registry] = types.AuthConfig{
|
2014-03-11 00:16:58 +00:00
|
|
|
Username: "docker-user",
|
|
|
|
Password: "docker-pass",
|
|
|
|
Email: "docker@docker.io",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-12 03:11:20 +00:00
|
|
|
return authConfigs
|
2014-03-11 00:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSameAuthDataPostSave(t *testing.T) {
|
2015-12-12 03:11:20 +00:00
|
|
|
authConfigs := buildAuthConfigs()
|
|
|
|
authConfig := authConfigs["testIndex"]
|
2014-03-11 00:16:58 +00:00
|
|
|
if authConfig.Username != "docker-user" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if authConfig.Password != "docker-pass" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if authConfig.Email != "docker@docker.io" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if authConfig.Auth != "" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveAuthConfigIndexServer(t *testing.T) {
|
2015-12-12 03:11:20 +00:00
|
|
|
authConfigs := buildAuthConfigs()
|
|
|
|
indexConfig := authConfigs[IndexServer]
|
2014-10-07 01:54:52 +00:00
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
officialIndex := ®istrytypes.IndexInfo{
|
2014-10-07 01:54:52 +00:00
|
|
|
Official: true,
|
|
|
|
}
|
2015-12-12 02:14:52 +00:00
|
|
|
privateIndex := ®istrytypes.IndexInfo{
|
2014-10-07 01:54:52 +00:00
|
|
|
Official: false,
|
2014-03-11 00:16:58 +00:00
|
|
|
}
|
2014-10-07 01:54:52 +00:00
|
|
|
|
2015-12-12 03:11:20 +00:00
|
|
|
resolved := ResolveAuthConfig(authConfigs, officialIndex)
|
2015-07-21 19:40:36 +00:00
|
|
|
assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer")
|
2014-10-07 01:54:52 +00:00
|
|
|
|
2015-12-12 03:11:20 +00:00
|
|
|
resolved = ResolveAuthConfig(authConfigs, privateIndex)
|
2015-07-21 19:40:36 +00:00
|
|
|
assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServer")
|
2014-03-11 00:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveAuthConfigFullURL(t *testing.T) {
|
2015-12-12 03:11:20 +00:00
|
|
|
authConfigs := buildAuthConfigs()
|
2014-03-11 00:16:58 +00:00
|
|
|
|
2015-12-12 04:11:42 +00:00
|
|
|
registryAuth := types.AuthConfig{
|
2014-03-11 00:16:58 +00:00
|
|
|
Username: "foo-user",
|
|
|
|
Password: "foo-pass",
|
|
|
|
Email: "foo@example.com",
|
|
|
|
}
|
2015-12-12 04:11:42 +00:00
|
|
|
localAuth := types.AuthConfig{
|
2014-03-11 00:16:58 +00:00
|
|
|
Username: "bar-user",
|
|
|
|
Password: "bar-pass",
|
|
|
|
Email: "bar@example.com",
|
|
|
|
}
|
2015-12-12 04:11:42 +00:00
|
|
|
officialAuth := types.AuthConfig{
|
2014-10-07 01:54:52 +00:00
|
|
|
Username: "baz-user",
|
|
|
|
Password: "baz-pass",
|
|
|
|
Email: "baz@example.com",
|
|
|
|
}
|
2015-12-12 03:11:20 +00:00
|
|
|
authConfigs[IndexServer] = officialAuth
|
2014-10-07 01:54:52 +00:00
|
|
|
|
2015-12-12 04:11:42 +00:00
|
|
|
expectedAuths := map[string]types.AuthConfig{
|
2014-10-07 01:54:52 +00:00
|
|
|
"registry.example.com": registryAuth,
|
|
|
|
"localhost:8000": localAuth,
|
|
|
|
"registry.com": localAuth,
|
|
|
|
}
|
2014-03-11 00:16:58 +00:00
|
|
|
|
|
|
|
validRegistries := map[string][]string{
|
2014-10-07 01:54:52 +00:00
|
|
|
"registry.example.com": {
|
2014-03-11 00:16:58 +00:00
|
|
|
"https://registry.example.com/v1/",
|
|
|
|
"http://registry.example.com/v1/",
|
|
|
|
"registry.example.com",
|
|
|
|
"registry.example.com/v1/",
|
|
|
|
},
|
2014-10-07 01:54:52 +00:00
|
|
|
"localhost:8000": {
|
2014-03-11 00:16:58 +00:00
|
|
|
"https://localhost:8000/v1/",
|
|
|
|
"http://localhost:8000/v1/",
|
|
|
|
"localhost:8000",
|
|
|
|
"localhost:8000/v1/",
|
|
|
|
},
|
|
|
|
"registry.com": {
|
|
|
|
"https://registry.com/v1/",
|
|
|
|
"http://registry.com/v1/",
|
|
|
|
"registry.com",
|
|
|
|
"registry.com/v1/",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for configKey, registries := range validRegistries {
|
2014-10-07 01:54:52 +00:00
|
|
|
configured, ok := expectedAuths[configKey]
|
|
|
|
if !ok || configured.Email == "" {
|
2015-01-14 22:12:03 +00:00
|
|
|
t.Fail()
|
2014-10-07 01:54:52 +00:00
|
|
|
}
|
2015-12-12 02:14:52 +00:00
|
|
|
index := ®istrytypes.IndexInfo{
|
2014-10-07 01:54:52 +00:00
|
|
|
Name: configKey,
|
|
|
|
}
|
2014-03-11 00:16:58 +00:00
|
|
|
for _, registry := range registries {
|
2015-12-12 03:11:20 +00:00
|
|
|
authConfigs[registry] = configured
|
|
|
|
resolved := ResolveAuthConfig(authConfigs, index)
|
2014-03-11 00:16:58 +00:00
|
|
|
if resolved.Email != configured.Email {
|
|
|
|
t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email)
|
|
|
|
}
|
2015-12-12 03:11:20 +00:00
|
|
|
delete(authConfigs, registry)
|
|
|
|
resolved = ResolveAuthConfig(authConfigs, index)
|
2014-10-07 01:54:52 +00:00
|
|
|
if resolved.Email == configured.Email {
|
|
|
|
t.Errorf("%s -> %q == %q\n", registry, resolved.Email, configured.Email)
|
|
|
|
}
|
2014-03-11 00:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|