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
|
|
|
|
2022-02-26 13:13:43 -05:00
|
|
|
"github.com/docker/docker/api/types/registry"
|
2022-02-26 16:53:06 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2013-03-14 20:43:59 -04:00
|
|
|
)
|
|
|
|
|
2022-03-03 04:32:29 -05:00
|
|
|
func buildAuthConfigs() map[string]registry.AuthConfig {
|
|
|
|
authConfigs := map[string]registry.AuthConfig{}
|
2013-07-24 23:25:16 -04:00
|
|
|
|
2022-02-26 13:13:43 -05:00
|
|
|
for _, reg := range []string{"testIndex", IndexServer} {
|
2022-03-03 04:32:29 -05:00
|
|
|
authConfigs[reg] = registry.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
|
|
|
|
2022-02-26 13:13:43 -05:00
|
|
|
officialIndex := ®istry.IndexInfo{
|
2014-10-06 21:54:52 -04:00
|
|
|
Official: true,
|
|
|
|
}
|
2022-02-26 13:13:43 -05:00
|
|
|
privateIndex := ®istry.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)
|
2022-02-26 16:53:06 -05:00
|
|
|
assert.Equal(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)
|
2022-02-26 16:53:06 -05:00
|
|
|
assert.Check(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
|
|
|
|
2022-03-03 04:32:29 -05:00
|
|
|
registryAuth := registry.AuthConfig{
|
2013-09-24 12:26:17 -04:00
|
|
|
Username: "foo-user",
|
|
|
|
Password: "foo-pass",
|
|
|
|
}
|
2022-03-03 04:32:29 -05:00
|
|
|
localAuth := registry.AuthConfig{
|
2013-09-24 12:26:17 -04:00
|
|
|
Username: "bar-user",
|
|
|
|
Password: "bar-pass",
|
|
|
|
}
|
2022-03-03 04:32:29 -05:00
|
|
|
officialAuth := registry.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
|
|
|
|
2022-03-03 04:32:29 -05:00
|
|
|
expectedAuths := map[string]registry.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
|
|
|
}
|
2022-02-26 13:13:43 -05:00
|
|
|
index := ®istry.IndexInfo{
|
2014-10-06 21:54:52 -04:00
|
|
|
Name: configKey,
|
|
|
|
}
|
2022-02-26 13:13:43 -05:00
|
|
|
for _, reg := range registries {
|
|
|
|
authConfigs[reg] = configured
|
2015-12-11 22:11:20 -05:00
|
|
|
resolved := ResolveAuthConfig(authConfigs, index)
|
2016-02-29 20:51:36 -05:00
|
|
|
if resolved.Username != configured.Username || resolved.Password != configured.Password {
|
2022-02-26 13:13:43 -05:00
|
|
|
t.Errorf("%s -> %v != %v\n", reg, resolved, configured)
|
2013-09-24 12:26:17 -04:00
|
|
|
}
|
2022-02-26 13:13:43 -05:00
|
|
|
delete(authConfigs, reg)
|
2015-12-11 22:11:20 -05:00
|
|
|
resolved = ResolveAuthConfig(authConfigs, index)
|
2016-02-29 20:51:36 -05:00
|
|
|
if resolved.Username == configured.Username || resolved.Password == configured.Password {
|
2022-02-26 13:13:43 -05:00
|
|
|
t.Errorf("%s -> %v == %v\n", reg, resolved, configured)
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
2013-09-24 12:26:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|