forked from TrueCloudLab/distribution
7b8b61bda1
This PR does the following: - migrated ~/.dockerfg to ~/.docker/config.json. The data is migrated but the old file remains in case its needed - moves the auth json in that fie into an "auth" property so we can add new top-level properties w/o messing with the auth stuff - adds support for an HttpHeaders property in ~/.docker/config.json which adds these http headers to all msgs from the cli In a follow-on PR I'll move the config file process out from under "registry" since it not specific to that any more. I didn't do it here because I wanted the diff to be smaller so people can make sure I didn't break/miss any auth code during my edits. Signed-off-by: Doug Davis <dug@us.ibm.com>
135 lines
3.5 KiB
Go
135 lines
3.5 KiB
Go
package registry
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/pkg/homedir"
|
|
)
|
|
|
|
func TestMissingFile(t *testing.T) {
|
|
tmpHome, _ := ioutil.TempDir("", "config-test")
|
|
|
|
config, err := LoadConfig(tmpHome)
|
|
if err != nil {
|
|
t.Fatalf("Failed loading on missing file: %q", err)
|
|
}
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
err = config.Save()
|
|
if err != nil {
|
|
t.Fatalf("Failed to save: %q", err)
|
|
}
|
|
|
|
buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
|
|
if !strings.Contains(string(buf), `"auths":`) {
|
|
t.Fatalf("Should have save in new form: %s", string(buf))
|
|
}
|
|
}
|
|
|
|
func TestEmptyFile(t *testing.T) {
|
|
tmpHome, _ := ioutil.TempDir("", "config-test")
|
|
fn := filepath.Join(tmpHome, CONFIGFILE)
|
|
ioutil.WriteFile(fn, []byte(""), 0600)
|
|
|
|
_, err := LoadConfig(tmpHome)
|
|
if err == nil {
|
|
t.Fatalf("Was supposed to fail")
|
|
}
|
|
}
|
|
|
|
func TestEmptyJson(t *testing.T) {
|
|
tmpHome, _ := ioutil.TempDir("", "config-test")
|
|
fn := filepath.Join(tmpHome, CONFIGFILE)
|
|
ioutil.WriteFile(fn, []byte("{}"), 0600)
|
|
|
|
config, err := LoadConfig(tmpHome)
|
|
if err != nil {
|
|
t.Fatalf("Failed loading on empty json file: %q", err)
|
|
}
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
err = config.Save()
|
|
if err != nil {
|
|
t.Fatalf("Failed to save: %q", err)
|
|
}
|
|
|
|
buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
|
|
if !strings.Contains(string(buf), `"auths":`) {
|
|
t.Fatalf("Should have save in new form: %s", string(buf))
|
|
}
|
|
}
|
|
|
|
func TestOldJson(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
return
|
|
}
|
|
|
|
tmpHome, _ := ioutil.TempDir("", "config-test")
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
homeKey := homedir.Key()
|
|
homeVal := homedir.Get()
|
|
|
|
defer func() { os.Setenv(homeKey, homeVal) }()
|
|
os.Setenv(homeKey, tmpHome)
|
|
|
|
fn := filepath.Join(tmpHome, OLD_CONFIGFILE)
|
|
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
|
|
ioutil.WriteFile(fn, []byte(js), 0600)
|
|
|
|
config, err := LoadConfig(tmpHome)
|
|
if err != nil {
|
|
t.Fatalf("Failed loading on empty json file: %q", err)
|
|
}
|
|
|
|
ac := config.AuthConfigs["https://index.docker.io/v1/"]
|
|
if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" {
|
|
t.Fatalf("Missing data from parsing:\n%q", config)
|
|
}
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
err = config.Save()
|
|
if err != nil {
|
|
t.Fatalf("Failed to save: %q", err)
|
|
}
|
|
|
|
buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
|
|
if !strings.Contains(string(buf), `"auths":`) ||
|
|
!strings.Contains(string(buf), "user@example.com") {
|
|
t.Fatalf("Should have save in new form: %s", string(buf))
|
|
}
|
|
}
|
|
|
|
func TestNewJson(t *testing.T) {
|
|
tmpHome, _ := ioutil.TempDir("", "config-test")
|
|
fn := filepath.Join(tmpHome, CONFIGFILE)
|
|
js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } } }`
|
|
ioutil.WriteFile(fn, []byte(js), 0600)
|
|
|
|
config, err := LoadConfig(tmpHome)
|
|
if err != nil {
|
|
t.Fatalf("Failed loading on empty json file: %q", err)
|
|
}
|
|
|
|
ac := config.AuthConfigs["https://index.docker.io/v1/"]
|
|
if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" {
|
|
t.Fatalf("Missing data from parsing:\n%q", config)
|
|
}
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
err = config.Save()
|
|
if err != nil {
|
|
t.Fatalf("Failed to save: %q", err)
|
|
}
|
|
|
|
buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
|
|
if !strings.Contains(string(buf), `"auths":`) ||
|
|
!strings.Contains(string(buf), "user@example.com") {
|
|
t.Fatalf("Should have save in new form: %s", string(buf))
|
|
}
|
|
}
|