2015-04-22 08:06:58 -04:00
|
|
|
package cliconfig
|
2015-04-01 18:39:37 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2015-12-15 13:36:47 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-04-01 18:39:37 -04:00
|
|
|
"github.com/docker/docker/pkg/homedir"
|
|
|
|
)
|
|
|
|
|
2015-08-06 12:02:20 -04:00
|
|
|
func TestEmptyConfigDir(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
2015-04-01 18:39:37 -04:00
|
|
|
|
2015-08-06 12:02:20 -04:00
|
|
|
SetConfigDir(tmpHome)
|
|
|
|
|
|
|
|
config, err := Load("")
|
2015-04-01 18:39:37 -04:00
|
|
|
if err != nil {
|
2015-08-06 12:02:20 -04:00
|
|
|
t.Fatalf("Failed loading on empty config dir: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedConfigFilename := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
if config.Filename() != expectedConfigFilename {
|
|
|
|
t.Fatalf("Expected config filename %s, got %s", expectedConfigFilename, config.Filename())
|
2015-04-01 18:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
2015-08-06 12:02:20 -04:00
|
|
|
saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMissingFile(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2015-04-01 18:39:37 -04:00
|
|
|
if err != nil {
|
2015-08-06 12:02:20 -04:00
|
|
|
t.Fatal(err)
|
2015-04-01 18:39:37 -04:00
|
|
|
}
|
2015-08-06 12:02:20 -04:00
|
|
|
defer os.RemoveAll(tmpHome)
|
2015-04-01 18:39:37 -04:00
|
|
|
|
2015-08-06 12:02:20 -04:00
|
|
|
config, err := Load(tmpHome)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed loading on missing file: %q", err)
|
2015-04-20 17:09:41 -04:00
|
|
|
}
|
2015-08-06 12:02:20 -04:00
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
saveConfigAndValidateNewFormat(t, config, tmpHome)
|
2015-04-20 17:09:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveFileToDirs(t *testing.T) {
|
2015-08-06 12:02:20 -04:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
2015-04-20 17:09:41 -04:00
|
|
|
|
|
|
|
tmpHome += "/.docker"
|
|
|
|
|
2015-04-22 08:06:58 -04:00
|
|
|
config, err := Load(tmpHome)
|
2015-04-20 17:09:41 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed loading on missing file: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
2015-08-06 12:02:20 -04:00
|
|
|
saveConfigAndValidateNewFormat(t, config, tmpHome)
|
2015-04-01 18:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyFile(t *testing.T) {
|
2015-08-06 12:02:20 -04:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
2015-07-20 18:39:07 -04:00
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
2015-08-06 12:02:20 -04:00
|
|
|
if err := ioutil.WriteFile(fn, []byte(""), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-01 18:39:37 -04:00
|
|
|
|
2015-08-06 12:02:20 -04:00
|
|
|
_, err = Load(tmpHome)
|
2015-04-01 18:39:37 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Was supposed to fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyJson(t *testing.T) {
|
2015-08-06 12:02:20 -04:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
2015-07-20 18:39:07 -04:00
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
2015-08-06 12:02:20 -04:00
|
|
|
if err := ioutil.WriteFile(fn, []byte("{}"), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-01 18:39:37 -04:00
|
|
|
|
2015-04-22 08:06:58 -04:00
|
|
|
config, err := Load(tmpHome)
|
2015-04-01 18:39:37 -04:00
|
|
|
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
|
2015-08-06 12:02:20 -04:00
|
|
|
saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOldInvalidsAuth(t *testing.T) {
|
|
|
|
invalids := map[string]string{
|
|
|
|
`username = test`: "The Auth config file is empty",
|
|
|
|
`username
|
|
|
|
password
|
|
|
|
email`: "Invalid Auth config file",
|
|
|
|
`username = test
|
|
|
|
email`: "Invalid auth configuration file",
|
|
|
|
`username = am9lam9lOmhlbGxv
|
|
|
|
email`: "Invalid Auth config file",
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2015-04-01 18:39:37 -04:00
|
|
|
if err != nil {
|
2015-08-06 12:02:20 -04:00
|
|
|
t.Fatal(err)
|
2015-04-01 18:39:37 -04:00
|
|
|
}
|
2015-08-06 12:02:20 -04:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
homeKey := homedir.Key()
|
|
|
|
homeVal := homedir.Get()
|
|
|
|
|
|
|
|
defer func() { os.Setenv(homeKey, homeVal) }()
|
|
|
|
os.Setenv(homeKey, tmpHome)
|
|
|
|
|
|
|
|
for content, expectedError := range invalids {
|
|
|
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
|
|
|
if err := ioutil.WriteFile(fn, []byte(content), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
2015-12-15 21:24:10 -05:00
|
|
|
// Use Contains instead of == since the file name will change each time
|
|
|
|
if err == nil || !strings.Contains(err.Error(), expectedError) {
|
|
|
|
t.Fatalf("Should have failed\nConfig: %v\nGot: %v\nExpected: %v", config, err, expectedError)
|
2015-08-06 12:02:20 -04:00
|
|
|
}
|
2015-04-01 18:39:37 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 12:02:20 -04:00
|
|
|
func TestOldValidAuth(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
homeKey := homedir.Key()
|
|
|
|
homeVal := homedir.Get()
|
|
|
|
|
|
|
|
defer func() { os.Setenv(homeKey, homeVal) }()
|
|
|
|
os.Setenv(homeKey, tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
|
|
|
js := `username = am9lam9lOmhlbGxv
|
|
|
|
email = user@example.com`
|
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-04-01 18:39:37 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 12:02:20 -04:00
|
|
|
config, err := Load(tmpHome)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultIndexserver is https://index.docker.io/v1/
|
|
|
|
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
|
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
|
|
|
|
if !strings.Contains(configStr, "user@example.com") {
|
|
|
|
t.Fatalf("Should have save in new form: %s", configStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOldJsonInvalid(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
homeKey := homedir.Key()
|
|
|
|
homeVal := homedir.Get()
|
|
|
|
|
|
|
|
defer func() { os.Setenv(homeKey, homeVal) }()
|
|
|
|
os.Setenv(homeKey, tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
|
|
|
js := `{"https://index.docker.io/v1/":{"auth":"test","email":"user@example.com"}}`
|
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
2015-12-15 21:24:10 -05:00
|
|
|
// Use Contains instead of == since the file name will change each time
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Invalid auth configuration file") {
|
2015-08-06 12:02:20 -04:00
|
|
|
t.Fatalf("Expected an error got : %v, %v", config, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOldJson(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-01 18:39:37 -04:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
homeKey := homedir.Key()
|
|
|
|
homeVal := homedir.Get()
|
|
|
|
|
|
|
|
defer func() { os.Setenv(homeKey, homeVal) }()
|
|
|
|
os.Setenv(homeKey, tmpHome)
|
|
|
|
|
2015-07-20 18:39:07 -04:00
|
|
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
2015-04-01 18:39:37 -04:00
|
|
|
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
|
2015-08-06 12:02:20 -04:00
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-01 18:39:37 -04:00
|
|
|
|
2015-04-22 08:06:58 -04:00
|
|
|
config, err := Load(tmpHome)
|
2015-04-01 18:39:37 -04:00
|
|
|
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
|
2015-08-06 12:02:20 -04:00
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
2015-04-01 18:39:37 -04:00
|
|
|
|
2015-08-06 12:02:20 -04:00
|
|
|
if !strings.Contains(configStr, "user@example.com") {
|
|
|
|
t.Fatalf("Should have save in new form: %s", configStr)
|
2015-04-01 18:39:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewJson(t *testing.T) {
|
2015-08-06 12:02:20 -04:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
2015-07-20 18:39:07 -04:00
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
2015-04-01 18:39:37 -04:00
|
|
|
js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } } }`
|
2015-08-06 12:02:20 -04:00
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-01 18:39:37 -04:00
|
|
|
|
2015-04-22 08:06:58 -04:00
|
|
|
config, err := Load(tmpHome)
|
2015-04-01 18:39:37 -04:00
|
|
|
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
|
2015-08-06 12:02:20 -04:00
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
2015-04-01 18:39:37 -04:00
|
|
|
|
2015-08-06 12:02:20 -04:00
|
|
|
if !strings.Contains(configStr, "user@example.com") {
|
|
|
|
t.Fatalf("Should have save in new form: %s", configStr)
|
2015-04-01 18:39:37 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-01 17:23:27 -04:00
|
|
|
|
|
|
|
func TestJsonWithPsFormat(t *testing.T) {
|
2015-08-06 12:02:20 -04:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
2015-07-17 00:03:16 -04:00
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
2015-05-01 17:23:27 -04:00
|
|
|
js := `{
|
|
|
|
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } },
|
|
|
|
"psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}"
|
|
|
|
}`
|
2015-08-06 12:02:20 -04:00
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-01 17:23:27 -04:00
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed loading on empty json file: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.PsFormat != `table {{.ID}}\t{{.Label "com.docker.label.cpu"}}` {
|
|
|
|
t.Fatalf("Unknown ps format: %s\n", config.PsFormat)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
2015-08-06 12:02:20 -04:00
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
if !strings.Contains(configStr, `"psFormat":`) ||
|
|
|
|
!strings.Contains(configStr, "{{.ID}}") {
|
|
|
|
t.Fatalf("Should have save in new form: %s", configStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save it and make sure it shows up in new form
|
|
|
|
func saveConfigAndValidateNewFormat(t *testing.T, config *ConfigFile, homeFolder string) string {
|
|
|
|
err := config.Save()
|
2015-05-01 17:23:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to save: %q", err)
|
|
|
|
}
|
|
|
|
|
2015-08-06 12:02:20 -04:00
|
|
|
buf, err := ioutil.ReadFile(filepath.Join(homeFolder, ConfigFileName))
|
|
|
|
if !strings.Contains(string(buf), `"auths":`) {
|
2015-05-01 17:23:27 -04:00
|
|
|
t.Fatalf("Should have save in new form: %s", string(buf))
|
|
|
|
}
|
2015-08-06 12:02:20 -04:00
|
|
|
return string(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigDir(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
if ConfigDir() == tmpHome {
|
|
|
|
t.Fatalf("Expected ConfigDir to be different than %s by default, but was the same", tmpHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update configDir
|
|
|
|
SetConfigDir(tmpHome)
|
|
|
|
|
|
|
|
if ConfigDir() != tmpHome {
|
|
|
|
t.Fatalf("Expected ConfigDir to %s, but was %s", tmpHome, ConfigDir())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigFile(t *testing.T) {
|
|
|
|
configFilename := "configFilename"
|
|
|
|
configFile := NewConfigFile(configFilename)
|
|
|
|
|
|
|
|
if configFile.Filename() != configFilename {
|
|
|
|
t.Fatalf("Expected %s, got %s", configFilename, configFile.Filename())
|
|
|
|
}
|
2015-05-01 17:23:27 -04:00
|
|
|
}
|
2015-07-28 21:28:14 -04:00
|
|
|
|
|
|
|
func TestJsonReaderNoFile(t *testing.T) {
|
|
|
|
js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } } }`
|
|
|
|
|
|
|
|
config, err := LoadFromReader(strings.NewReader(js))
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOldJsonReaderNoFile(t *testing.T) {
|
|
|
|
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
|
|
|
|
|
|
|
|
config, err := LegacyLoadFromReader(strings.NewReader(js))
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonWithPsFormatNoFile(t *testing.T) {
|
|
|
|
js := `{
|
|
|
|
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } },
|
|
|
|
"psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}"
|
|
|
|
}`
|
|
|
|
config, err := LoadFromReader(strings.NewReader(js))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed loading on empty json file: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.PsFormat != `table {{.ID}}\t{{.Label "com.docker.label.cpu"}}` {
|
|
|
|
t.Fatalf("Unknown ps format: %s\n", config.PsFormat)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonSaveWithNoFile(t *testing.T) {
|
|
|
|
js := `{
|
|
|
|
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } },
|
|
|
|
"psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}"
|
|
|
|
}`
|
|
|
|
config, err := LoadFromReader(strings.NewReader(js))
|
|
|
|
err = config.Save()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error. File should not have been able to save with no file name.")
|
|
|
|
}
|
|
|
|
|
2015-09-01 10:00:40 -04:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a temp dir: %q", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
2015-07-28 21:28:14 -04:00
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
err = config.SaveToWriter(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed saving to file: %q", err)
|
|
|
|
}
|
|
|
|
buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName))
|
|
|
|
if !strings.Contains(string(buf), `"auths":`) ||
|
|
|
|
!strings.Contains(string(buf), "user@example.com") {
|
|
|
|
t.Fatalf("Should have save in new form: %s", string(buf))
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 13:36:47 -05:00
|
|
|
|
2015-07-28 21:28:14 -04:00
|
|
|
func TestLegacyJsonSaveWithNoFile(t *testing.T) {
|
|
|
|
|
|
|
|
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
|
|
|
|
config, err := LegacyLoadFromReader(strings.NewReader(js))
|
|
|
|
err = config.Save()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error. File should not have been able to save with no file name.")
|
|
|
|
}
|
|
|
|
|
2015-09-01 10:00:40 -04:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a temp dir: %q", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
2015-07-28 21:28:14 -04:00
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
err = config.SaveToWriter(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed saving to file: %q", err)
|
|
|
|
}
|
|
|
|
buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName))
|
|
|
|
if !strings.Contains(string(buf), `"auths":`) ||
|
|
|
|
!strings.Contains(string(buf), "user@example.com") {
|
|
|
|
t.Fatalf("Should have save in new form: %s", string(buf))
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 13:36:47 -05:00
|
|
|
|
|
|
|
func TestEncodeAuth(t *testing.T) {
|
|
|
|
newAuthConfig := &types.AuthConfig{Username: "ken", Password: "test", Email: "test@example.com"}
|
|
|
|
authStr := encodeAuth(newAuthConfig)
|
|
|
|
decAuthConfig := &types.AuthConfig{}
|
|
|
|
var err error
|
|
|
|
decAuthConfig.Username, decAuthConfig.Password, err = decodeAuth(authStr)
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
}
|