2018-02-05 16:05:59 -05:00
|
|
|
package registry // import "github.com/docker/docker/registry"
|
2013-03-14 20:43:59 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-04-22 08:06:58 -04:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
2013-03-14 20:43:59 -04:00
|
|
|
)
|
|
|
|
|
2015-12-11 21:14:52 -05:00
|
|
|
func buildAuthConfigs() map[string]types.AuthConfig {
|
|
|
|
authConfigs := map[string]types.AuthConfig{}
|
2013-07-24 23:25:16 -04:00
|
|
|
|
2015-07-21 15:40:36 -04:00
|
|
|
for _, registry := range []string{"testIndex", IndexServer} {
|
2015-12-11 23:11:42 -05:00
|
|
|
authConfigs[registry] = types.AuthConfig{
|
2013-09-24 12:26:17 -04:00
|
|
|
Username: "docker-user",
|
|
|
|
Password: "docker-pass",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 22:11:20 -05:00
|
|
|
return authConfigs
|
2013-09-24 12:26:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveAuthConfigIndexServer(t *testing.T) {
|
2015-12-11 22:11:20 -05:00
|
|
|
authConfigs := buildAuthConfigs()
|
|
|
|
indexConfig := authConfigs[IndexServer]
|
2014-10-06 21:54:52 -04:00
|
|
|
|
2015-12-11 21:14:52 -05:00
|
|
|
officialIndex := ®istrytypes.IndexInfo{
|
2014-10-06 21:54:52 -04:00
|
|
|
Official: true,
|
|
|
|
}
|
2015-12-11 21:14:52 -05:00
|
|
|
privateIndex := ®istrytypes.IndexInfo{
|
2014-10-06 21:54:52 -04:00
|
|
|
Official: false,
|
2013-09-24 12:26:17 -04:00
|
|
|
}
|
2014-10-06 21:54:52 -04:00
|
|
|
|
2015-12-11 22:11:20 -05:00
|
|
|
resolved := ResolveAuthConfig(authConfigs, officialIndex)
|
2015-07-21 15:40:36 -04:00
|
|
|
assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer")
|
2014-10-06 21:54:52 -04:00
|
|
|
|
2015-12-11 22:11:20 -05:00
|
|
|
resolved = ResolveAuthConfig(authConfigs, privateIndex)
|
2015-07-21 15:40:36 -04:00
|
|
|
assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServer")
|
2013-09-24 12:26:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveAuthConfigFullURL(t *testing.T) {
|
2015-12-11 22:11:20 -05:00
|
|
|
authConfigs := buildAuthConfigs()
|
2013-09-24 12:26:17 -04:00
|
|
|
|
2015-12-11 23:11:42 -05:00
|
|
|
registryAuth := types.AuthConfig{
|
2013-09-24 12:26:17 -04:00
|
|
|
Username: "foo-user",
|
|
|
|
Password: "foo-pass",
|
|
|
|
}
|
2015-12-11 23:11:42 -05:00
|
|
|
localAuth := types.AuthConfig{
|
2013-09-24 12:26:17 -04:00
|
|
|
Username: "bar-user",
|
|
|
|
Password: "bar-pass",
|
|
|
|
}
|
2015-12-11 23:11:42 -05:00
|
|
|
officialAuth := types.AuthConfig{
|
2014-10-06 21:54:52 -04:00
|
|
|
Username: "baz-user",
|
|
|
|
Password: "baz-pass",
|
|
|
|
}
|
2015-12-11 22:11:20 -05:00
|
|
|
authConfigs[IndexServer] = officialAuth
|
2014-10-06 21:54:52 -04:00
|
|
|
|
2015-12-11 23:11:42 -05:00
|
|
|
expectedAuths := map[string]types.AuthConfig{
|
2014-10-06 21:54:52 -04:00
|
|
|
"registry.example.com": registryAuth,
|
|
|
|
"localhost:8000": localAuth,
|
2021-04-02 08:06:27 -04:00
|
|
|
"example.com": localAuth,
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
2013-09-24 12:26:17 -04:00
|
|
|
|
|
|
|
validRegistries := map[string][]string{
|
2014-10-06 21:54:52 -04:00
|
|
|
"registry.example.com": {
|
2013-09-24 12:26:17 -04:00
|
|
|
"https://registry.example.com/v1/",
|
|
|
|
"http://registry.example.com/v1/",
|
|
|
|
"registry.example.com",
|
|
|
|
"registry.example.com/v1/",
|
|
|
|
},
|
2014-10-06 21:54:52 -04:00
|
|
|
"localhost:8000": {
|
2013-09-24 12:26:17 -04:00
|
|
|
"https://localhost:8000/v1/",
|
|
|
|
"http://localhost:8000/v1/",
|
|
|
|
"localhost:8000",
|
|
|
|
"localhost:8000/v1/",
|
|
|
|
},
|
2021-04-02 08:06:27 -04:00
|
|
|
"example.com": {
|
|
|
|
"https://example.com/v1/",
|
|
|
|
"http://example.com/v1/",
|
|
|
|
"example.com",
|
|
|
|
"example.com/v1/",
|
2014-02-20 17:57:58 -05:00
|
|
|
},
|
2013-09-24 12:26:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for configKey, registries := range validRegistries {
|
2014-10-06 21:54:52 -04:00
|
|
|
configured, ok := expectedAuths[configKey]
|
2016-02-29 20:51:36 -05:00
|
|
|
if !ok {
|
2015-01-14 17:12:03 -05:00
|
|
|
t.Fail()
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
2015-12-11 21:14:52 -05:00
|
|
|
index := ®istrytypes.IndexInfo{
|
2014-10-06 21:54:52 -04:00
|
|
|
Name: configKey,
|
|
|
|
}
|
2013-09-24 12:26:17 -04:00
|
|
|
for _, registry := range registries {
|
2015-12-11 22:11:20 -05:00
|
|
|
authConfigs[registry] = configured
|
|
|
|
resolved := ResolveAuthConfig(authConfigs, index)
|
2016-02-29 20:51:36 -05:00
|
|
|
if resolved.Username != configured.Username || resolved.Password != configured.Password {
|
|
|
|
t.Errorf("%s -> %v != %v\n", registry, resolved, configured)
|
2013-09-24 12:26:17 -04:00
|
|
|
}
|
2015-12-11 22:11:20 -05:00
|
|
|
delete(authConfigs, registry)
|
|
|
|
resolved = ResolveAuthConfig(authConfigs, index)
|
2016-02-29 20:51:36 -05:00
|
|
|
if resolved.Username == configured.Username || resolved.Password == configured.Password {
|
|
|
|
t.Errorf("%s -> %v == %v\n", registry, resolved, configured)
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
2013-09-24 12:26:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|