2014-03-10 20:16:58 -04:00
|
|
|
package registry
|
2013-03-14 20:43:59 -04:00
|
|
|
|
|
|
|
import (
|
2013-07-24 23:25:16 -04:00
|
|
|
"io/ioutil"
|
2013-05-10 09:34:19 -04:00
|
|
|
"os"
|
2013-03-14 20:43:59 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncodeAuth(t *testing.T) {
|
2013-03-22 05:19:39 -04:00
|
|
|
newAuthConfig := &AuthConfig{Username: "ken", Password: "test", Email: "test@example.com"}
|
2013-06-17 14:29:02 -04:00
|
|
|
authStr := encodeAuth(newAuthConfig)
|
2013-07-24 08:28:22 -04:00
|
|
|
decAuthConfig := &AuthConfig{}
|
|
|
|
var err error
|
|
|
|
decAuthConfig.Username, decAuthConfig.Password, err = decodeAuth(authStr)
|
2013-03-14 20:43:59 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if newAuthConfig.Username != decAuthConfig.Username {
|
|
|
|
t.Fatal("Encode Username doesn't match decoded Username")
|
|
|
|
}
|
|
|
|
if newAuthConfig.Password != decAuthConfig.Password {
|
|
|
|
t.Fatal("Encode Password doesn't match decoded Password")
|
|
|
|
}
|
|
|
|
if authStr != "a2VuOnRlc3Q=" {
|
|
|
|
t.Fatal("AuthString encoding isn't correct.")
|
|
|
|
}
|
|
|
|
}
|
2013-05-10 09:34:19 -04:00
|
|
|
|
2013-09-24 12:26:17 -04:00
|
|
|
func setupTempConfigFile() (*ConfigFile, error) {
|
2013-10-16 16:44:15 -04:00
|
|
|
root, err := ioutil.TempDir("", "docker-test-auth")
|
2013-07-24 23:25:16 -04:00
|
|
|
if err != nil {
|
2013-09-24 12:26:17 -04:00
|
|
|
return nil, err
|
2013-07-24 23:25:16 -04:00
|
|
|
}
|
|
|
|
configFile := &ConfigFile{
|
|
|
|
rootPath: root,
|
2013-09-24 12:26:17 -04:00
|
|
|
Configs: make(map[string]AuthConfig),
|
2013-07-24 23:25:16 -04:00
|
|
|
}
|
|
|
|
|
2013-09-24 12:26:17 -04:00
|
|
|
for _, registry := range []string{"testIndex", IndexServerAddress()} {
|
|
|
|
configFile.Configs[registry] = AuthConfig{
|
|
|
|
Username: "docker-user",
|
|
|
|
Password: "docker-pass",
|
|
|
|
Email: "docker@docker.io",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return configFile, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSameAuthDataPostSave(t *testing.T) {
|
|
|
|
configFile, err := setupTempConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2013-07-24 23:25:16 -04:00
|
|
|
}
|
2013-10-16 16:44:15 -04:00
|
|
|
defer os.RemoveAll(configFile.rootPath)
|
2013-07-24 23:25:16 -04:00
|
|
|
|
|
|
|
err = SaveConfig(configFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
authConfig := configFile.Configs["testIndex"]
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 12:26:17 -04:00
|
|
|
|
|
|
|
func TestResolveAuthConfigIndexServer(t *testing.T) {
|
|
|
|
configFile, err := setupTempConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-16 16:44:15 -04:00
|
|
|
defer os.RemoveAll(configFile.rootPath)
|
2013-09-24 12:26:17 -04:00
|
|
|
|
2014-10-06 21:54:52 -04:00
|
|
|
indexConfig := configFile.Configs[IndexServerAddress()]
|
|
|
|
|
|
|
|
officialIndex := &IndexInfo{
|
|
|
|
Official: true,
|
|
|
|
}
|
|
|
|
privateIndex := &IndexInfo{
|
|
|
|
Official: false,
|
2013-09-24 12:26:17 -04:00
|
|
|
}
|
2014-10-06 21:54:52 -04:00
|
|
|
|
|
|
|
resolved := configFile.ResolveAuthConfig(officialIndex)
|
|
|
|
assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServerAddress()")
|
|
|
|
|
|
|
|
resolved = configFile.ResolveAuthConfig(privateIndex)
|
|
|
|
assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServerAddress()")
|
2013-09-24 12:26:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveAuthConfigFullURL(t *testing.T) {
|
|
|
|
configFile, err := setupTempConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-16 16:44:15 -04:00
|
|
|
defer os.RemoveAll(configFile.rootPath)
|
2013-09-24 12:26:17 -04:00
|
|
|
|
|
|
|
registryAuth := AuthConfig{
|
|
|
|
Username: "foo-user",
|
|
|
|
Password: "foo-pass",
|
|
|
|
Email: "foo@example.com",
|
|
|
|
}
|
|
|
|
localAuth := AuthConfig{
|
|
|
|
Username: "bar-user",
|
|
|
|
Password: "bar-pass",
|
|
|
|
Email: "bar@example.com",
|
|
|
|
}
|
2014-10-06 21:54:52 -04:00
|
|
|
officialAuth := AuthConfig{
|
|
|
|
Username: "baz-user",
|
|
|
|
Password: "baz-pass",
|
|
|
|
Email: "baz@example.com",
|
|
|
|
}
|
|
|
|
configFile.Configs[IndexServerAddress()] = officialAuth
|
|
|
|
|
|
|
|
expectedAuths := map[string]AuthConfig{
|
|
|
|
"registry.example.com": registryAuth,
|
|
|
|
"localhost:8000": localAuth,
|
|
|
|
"registry.com": localAuth,
|
|
|
|
}
|
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/",
|
|
|
|
},
|
2014-02-20 17:57:58 -05:00
|
|
|
"registry.com": {
|
|
|
|
"https://registry.com/v1/",
|
|
|
|
"http://registry.com/v1/",
|
|
|
|
"registry.com",
|
|
|
|
"registry.com/v1/",
|
|
|
|
},
|
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]
|
|
|
|
if !ok || configured.Email == "" {
|
2015-01-14 17:12:03 -05:00
|
|
|
t.Fail()
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
|
|
|
index := &IndexInfo{
|
|
|
|
Name: configKey,
|
|
|
|
}
|
2013-09-24 12:26:17 -04:00
|
|
|
for _, registry := range registries {
|
2014-10-06 21:54:52 -04:00
|
|
|
configFile.Configs[registry] = configured
|
|
|
|
resolved := configFile.ResolveAuthConfig(index)
|
2013-09-24 12:26:17 -04:00
|
|
|
if resolved.Email != configured.Email {
|
|
|
|
t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email)
|
|
|
|
}
|
2014-10-06 21:54:52 -04:00
|
|
|
delete(configFile.Configs, registry)
|
|
|
|
resolved = configFile.ResolveAuthConfig(index)
|
|
|
|
if resolved.Email == configured.Email {
|
|
|
|
t.Errorf("%s -> %q == %q\n", registry, resolved.Email, configured.Email)
|
|
|
|
}
|
2013-09-24 12:26:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|