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