2016-12-25 19:31:52 +00:00
|
|
|
package config
|
2015-04-01 22:39:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2016-12-25 19:31:52 +00:00
|
|
|
"github.com/docker/docker/cli/config/configfile"
|
2015-04-01 22:39:37 +00:00
|
|
|
"github.com/docker/docker/pkg/homedir"
|
|
|
|
)
|
|
|
|
|
2015-08-06 16:02:20 +00: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 22:39:37 +00:00
|
|
|
|
2016-12-25 19:31:52 +00:00
|
|
|
SetDir(tmpHome)
|
2015-08-06 16:02:20 +00:00
|
|
|
|
|
|
|
config, err := Load("")
|
2015-04-01 22:39:37 +00:00
|
|
|
if err != nil {
|
2015-08-06 16:02:20 +00:00
|
|
|
t.Fatalf("Failed loading on empty config dir: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedConfigFilename := filepath.Join(tmpHome, ConfigFileName)
|
2016-04-21 22:37:08 +00:00
|
|
|
if config.Filename != expectedConfigFilename {
|
|
|
|
t.Fatalf("Expected config filename %s, got %s", expectedConfigFilename, config.Filename)
|
2015-04-01 22:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
2015-08-06 16:02:20 +00:00
|
|
|
saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMissingFile(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2015-04-01 22:39:37 +00:00
|
|
|
if err != nil {
|
2015-08-06 16:02:20 +00:00
|
|
|
t.Fatal(err)
|
2015-04-01 22:39:37 +00:00
|
|
|
}
|
2015-08-06 16:02:20 +00:00
|
|
|
defer os.RemoveAll(tmpHome)
|
2015-04-01 22:39:37 +00:00
|
|
|
|
2015-08-06 16:02:20 +00:00
|
|
|
config, err := Load(tmpHome)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed loading on missing file: %q", err)
|
2015-04-20 21:09:41 +00:00
|
|
|
}
|
2015-08-06 16:02:20 +00:00
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
saveConfigAndValidateNewFormat(t, config, tmpHome)
|
2015-04-20 21:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveFileToDirs(t *testing.T) {
|
2015-08-06 16:02:20 +00:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
2015-04-20 21:09:41 +00:00
|
|
|
|
|
|
|
tmpHome += "/.docker"
|
|
|
|
|
2015-04-22 12:06:58 +00:00
|
|
|
config, err := Load(tmpHome)
|
2015-04-20 21:09:41 +00: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 16:02:20 +00:00
|
|
|
saveConfigAndValidateNewFormat(t, config, tmpHome)
|
2015-04-01 22:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyFile(t *testing.T) {
|
2015-08-06 16:02:20 +00:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
2015-07-20 22:39:07 +00:00
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
2015-08-06 16:02:20 +00:00
|
|
|
if err := ioutil.WriteFile(fn, []byte(""), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-01 22:39:37 +00:00
|
|
|
|
2015-08-06 16:02:20 +00:00
|
|
|
_, err = Load(tmpHome)
|
2015-04-01 22:39:37 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Was supposed to fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:23:10 +00:00
|
|
|
func TestEmptyJSON(t *testing.T) {
|
2015-08-06 16:02:20 +00:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
2015-07-20 22:39:07 +00:00
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
2015-08-06 16:02:20 +00:00
|
|
|
if err := ioutil.WriteFile(fn, []byte("{}"), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-01 22:39:37 +00:00
|
|
|
|
2015-04-22 12:06:58 +00:00
|
|
|
config, err := Load(tmpHome)
|
2015-04-01 22:39:37 +00: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 16:02:20 +00:00
|
|
|
saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOldInvalidsAuth(t *testing.T) {
|
|
|
|
invalids := map[string]string{
|
|
|
|
`username = test`: "The Auth config file is empty",
|
|
|
|
`username
|
2016-03-01 01:51:36 +00:00
|
|
|
password`: "Invalid Auth config file",
|
2015-08-06 16:02:20 +00:00
|
|
|
`username = test
|
|
|
|
email`: "Invalid auth configuration file",
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
2015-04-01 22:39:37 +00:00
|
|
|
if err != nil {
|
2015-08-06 16:02:20 +00:00
|
|
|
t.Fatal(err)
|
2015-04-01 22:39:37 +00:00
|
|
|
}
|
2015-08-06 16:02:20 +00: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-16 02:24:10 +00: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 16:02:20 +00:00
|
|
|
}
|
2015-04-01 22:39:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 16:02:20 +00: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
|
2016-03-01 01:51:36 +00:00
|
|
|
email = user@example.com`
|
2015-08-06 16:02:20 +00:00
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-04-01 22:39:37 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 16:02:20 +00: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/"]
|
2016-03-01 01:51:36 +00:00
|
|
|
if ac.Username != "joejoe" || ac.Password != "hello" {
|
2015-08-06 16:02:20 +00:00
|
|
|
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)
|
|
|
|
|
2016-03-01 01:51:36 +00:00
|
|
|
expConfStr := `{
|
|
|
|
"auths": {
|
|
|
|
"https://index.docker.io/v1/": {
|
|
|
|
"auth": "am9lam9lOmhlbGxv"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
if configStr != expConfStr {
|
|
|
|
t.Fatalf("Should have save in new form: \n%s\n not \n%s", configStr, expConfStr)
|
2015-08-06 16:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:23:10 +00:00
|
|
|
func TestOldJSONInvalid(t *testing.T) {
|
2015-08-06 16:02:20 +00:00
|
|
|
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-16 02:24:10 +00: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 16:02:20 +00:00
|
|
|
t.Fatalf("Expected an error got : %v, %v", config, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:23:10 +00:00
|
|
|
func TestOldJSON(t *testing.T) {
|
2015-08-06 16:02:20 +00:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-01 22:39:37 +00:00
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
homeKey := homedir.Key()
|
|
|
|
homeVal := homedir.Get()
|
|
|
|
|
|
|
|
defer func() { os.Setenv(homeKey, homeVal) }()
|
|
|
|
os.Setenv(homeKey, tmpHome)
|
|
|
|
|
2015-07-20 22:39:07 +00:00
|
|
|
fn := filepath.Join(tmpHome, oldConfigfile)
|
2015-04-01 22:39:37 +00:00
|
|
|
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
|
2015-08-06 16:02:20 +00:00
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-01 22:39:37 +00:00
|
|
|
|
2015-04-22 12:06:58 +00:00
|
|
|
config, err := Load(tmpHome)
|
2015-04-01 22:39:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed loading on empty json file: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ac := config.AuthConfigs["https://index.docker.io/v1/"]
|
2016-03-01 01:51:36 +00:00
|
|
|
if ac.Username != "joejoe" || ac.Password != "hello" {
|
2015-04-01 22:39:37 +00:00
|
|
|
t.Fatalf("Missing data from parsing:\n%q", config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
2015-08-06 16:02:20 +00:00
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
2015-04-01 22:39:37 +00:00
|
|
|
|
2016-03-01 01:51:36 +00:00
|
|
|
expConfStr := `{
|
|
|
|
"auths": {
|
|
|
|
"https://index.docker.io/v1/": {
|
|
|
|
"auth": "am9lam9lOmhlbGxv",
|
|
|
|
"email": "user@example.com"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
if configStr != expConfStr {
|
|
|
|
t.Fatalf("Should have save in new form: \n'%s'\n not \n'%s'\n", configStr, expConfStr)
|
2015-04-01 22:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:23:10 +00:00
|
|
|
func TestNewJSON(t *testing.T) {
|
2015-08-06 16:02:20 +00:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
2015-07-20 22:39:07 +00:00
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
2016-03-01 01:51:36 +00:00
|
|
|
js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } }`
|
2015-08-06 16:02:20 +00:00
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-01 22:39:37 +00:00
|
|
|
|
2015-04-22 12:06:58 +00:00
|
|
|
config, err := Load(tmpHome)
|
2015-04-01 22:39:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed loading on empty json file: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ac := config.AuthConfigs["https://index.docker.io/v1/"]
|
2016-03-01 01:51:36 +00:00
|
|
|
if ac.Username != "joejoe" || ac.Password != "hello" {
|
2015-04-01 22:39:37 +00:00
|
|
|
t.Fatalf("Missing data from parsing:\n%q", config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
2015-08-06 16:02:20 +00:00
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
2015-04-01 22:39:37 +00:00
|
|
|
|
2016-03-01 01:51:36 +00:00
|
|
|
expConfStr := `{
|
|
|
|
"auths": {
|
|
|
|
"https://index.docker.io/v1/": {
|
|
|
|
"auth": "am9lam9lOmhlbGxv"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
if configStr != expConfStr {
|
|
|
|
t.Fatalf("Should have save in new form: \n%s\n not \n%s", configStr, expConfStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:23:10 +00:00
|
|
|
func TestNewJSONNoEmail(t *testing.T) {
|
2016-03-01 01:51:36 +00:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } }`
|
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed loading on empty json file: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ac := config.AuthConfigs["https://index.docker.io/v1/"]
|
|
|
|
if 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)
|
|
|
|
|
|
|
|
expConfStr := `{
|
|
|
|
"auths": {
|
|
|
|
"https://index.docker.io/v1/": {
|
|
|
|
"auth": "am9lam9lOmhlbGxv"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
if configStr != expConfStr {
|
|
|
|
t.Fatalf("Should have save in new form: \n%s\n not \n%s", configStr, expConfStr)
|
2015-04-01 22:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-01 21:23:27 +00:00
|
|
|
|
2016-08-18 21:23:10 +00:00
|
|
|
func TestJSONWithPsFormat(t *testing.T) {
|
2015-08-06 16:02:20 +00:00
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
2015-07-17 04:03:16 +00:00
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
2015-05-01 21:23:27 +00: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 16:02:20 +00:00
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-01 21:23:27 +00: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 16:02:20 +00: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:23:10 +00:00
|
|
|
func TestJSONWithCredentialStore(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
js := `{
|
|
|
|
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } },
|
|
|
|
"credsStore": "crazy-secure-storage"
|
|
|
|
}`
|
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed loading on empty json file: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.CredentialsStore != "crazy-secure-storage" {
|
|
|
|
t.Fatalf("Unknown credential store: %s\n", config.CredentialsStore)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
if !strings.Contains(configStr, `"credsStore":`) ||
|
|
|
|
!strings.Contains(configStr, "crazy-secure-storage") {
|
|
|
|
t.Fatalf("Should have save in new form: %s", configStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONWithCredentialHelpers(t *testing.T) {
|
|
|
|
tmpHome, err := ioutil.TempDir("", "config-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpHome)
|
|
|
|
|
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
js := `{
|
|
|
|
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } },
|
|
|
|
"credHelpers": { "images.io": "images-io", "containers.com": "crazy-secure-storage" }
|
|
|
|
}`
|
|
|
|
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := Load(tmpHome)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed loading on empty json file: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.CredentialHelpers == nil {
|
|
|
|
t.Fatal("config.CredentialHelpers was nil")
|
|
|
|
} else if config.CredentialHelpers["images.io"] != "images-io" ||
|
|
|
|
config.CredentialHelpers["containers.com"] != "crazy-secure-storage" {
|
|
|
|
t.Fatalf("Credential helpers not deserialized properly: %v\n", config.CredentialHelpers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now save it and make sure it shows up in new form
|
|
|
|
configStr := saveConfigAndValidateNewFormat(t, config, tmpHome)
|
|
|
|
if !strings.Contains(configStr, `"credHelpers":`) ||
|
|
|
|
!strings.Contains(configStr, "images.io") ||
|
|
|
|
!strings.Contains(configStr, "images-io") ||
|
|
|
|
!strings.Contains(configStr, "containers.com") ||
|
|
|
|
!strings.Contains(configStr, "crazy-secure-storage") {
|
|
|
|
t.Fatalf("Should have save in new form: %s", configStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 16:02:20 +00:00
|
|
|
// Save it and make sure it shows up in new form
|
2016-04-21 22:37:08 +00:00
|
|
|
func saveConfigAndValidateNewFormat(t *testing.T, config *configfile.ConfigFile, homeFolder string) string {
|
2016-03-17 02:43:26 +00:00
|
|
|
if err := config.Save(); err != nil {
|
2015-05-01 21:23:27 +00:00
|
|
|
t.Fatalf("Failed to save: %q", err)
|
|
|
|
}
|
|
|
|
|
2015-08-06 16:02:20 +00:00
|
|
|
buf, err := ioutil.ReadFile(filepath.Join(homeFolder, ConfigFileName))
|
2016-03-17 02:43:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-08-06 16:02:20 +00:00
|
|
|
if !strings.Contains(string(buf), `"auths":`) {
|
2015-05-01 21:23:27 +00:00
|
|
|
t.Fatalf("Should have save in new form: %s", string(buf))
|
|
|
|
}
|
2015-08-06 16:02:20 +00: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)
|
|
|
|
|
2016-12-25 19:31:52 +00:00
|
|
|
if Dir() == tmpHome {
|
2015-08-06 16:02:20 +00:00
|
|
|
t.Fatalf("Expected ConfigDir to be different than %s by default, but was the same", tmpHome)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update configDir
|
2016-12-25 19:31:52 +00:00
|
|
|
SetDir(tmpHome)
|
2015-08-06 16:02:20 +00:00
|
|
|
|
2016-12-25 19:31:52 +00:00
|
|
|
if Dir() != tmpHome {
|
|
|
|
t.Fatalf("Expected ConfigDir to %s, but was %s", tmpHome, Dir())
|
2015-08-06 16:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigFile(t *testing.T) {
|
|
|
|
configFilename := "configFilename"
|
|
|
|
configFile := NewConfigFile(configFilename)
|
|
|
|
|
2016-04-21 22:37:08 +00:00
|
|
|
if configFile.Filename != configFilename {
|
|
|
|
t.Fatalf("Expected %s, got %s", configFilename, configFile.Filename)
|
2015-08-06 16:02:20 +00:00
|
|
|
}
|
2015-05-01 21:23:27 +00:00
|
|
|
}
|
2015-07-29 01:28:14 +00:00
|
|
|
|
2016-08-18 21:23:10 +00:00
|
|
|
func TestJSONReaderNoFile(t *testing.T) {
|
2015-07-29 01:28:14 +00:00
|
|
|
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/"]
|
2016-03-01 01:51:36 +00:00
|
|
|
if ac.Username != "joejoe" || ac.Password != "hello" {
|
2015-07-29 01:28:14 +00:00
|
|
|
t.Fatalf("Missing data from parsing:\n%q", config)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:23:10 +00:00
|
|
|
func TestOldJSONReaderNoFile(t *testing.T) {
|
2015-07-29 01:28:14 +00:00
|
|
|
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/"]
|
2016-03-01 01:51:36 +00:00
|
|
|
if ac.Username != "joejoe" || ac.Password != "hello" {
|
2015-07-29 01:28:14 +00:00
|
|
|
t.Fatalf("Missing data from parsing:\n%q", config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:23:10 +00:00
|
|
|
func TestJSONWithPsFormatNoFile(t *testing.T) {
|
2015-07-29 01:28:14 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:23:10 +00:00
|
|
|
func TestJSONSaveWithNoFile(t *testing.T) {
|
2015-07-29 01:28:14 +00:00
|
|
|
js := `{
|
2016-03-01 01:51:36 +00:00
|
|
|
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } },
|
2015-07-29 01:28:14 +00:00
|
|
|
"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 14:00:40 +00: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-29 01:28:14 +00:00
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
2016-06-25 03:57:21 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
2015-07-29 01:28:14 +00:00
|
|
|
err = config.SaveToWriter(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed saving to file: %q", err)
|
|
|
|
}
|
|
|
|
buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName))
|
2016-03-17 02:43:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-03-01 01:51:36 +00:00
|
|
|
expConfStr := `{
|
|
|
|
"auths": {
|
|
|
|
"https://index.docker.io/v1/": {
|
|
|
|
"auth": "am9lam9lOmhlbGxv"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}"
|
|
|
|
}`
|
|
|
|
if string(buf) != expConfStr {
|
|
|
|
t.Fatalf("Should have save in new form: \n%s\nnot \n%s", string(buf), expConfStr)
|
2015-07-29 01:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 18:36:47 +00:00
|
|
|
|
2016-08-18 21:23:10 +00:00
|
|
|
func TestLegacyJSONSaveWithNoFile(t *testing.T) {
|
2015-07-29 01:28:14 +00:00
|
|
|
|
|
|
|
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 14:00:40 +00: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-29 01:28:14 +00:00
|
|
|
fn := filepath.Join(tmpHome, ConfigFileName)
|
|
|
|
f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
2016-06-25 03:57:21 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
2016-03-17 02:43:26 +00:00
|
|
|
if err = config.SaveToWriter(f); err != nil {
|
2015-07-29 01:28:14 +00:00
|
|
|
t.Fatalf("Failed saving to file: %q", err)
|
|
|
|
}
|
|
|
|
buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName))
|
2016-03-17 02:43:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-03-01 01:51:36 +00:00
|
|
|
|
|
|
|
expConfStr := `{
|
|
|
|
"auths": {
|
|
|
|
"https://index.docker.io/v1/": {
|
|
|
|
"auth": "am9lam9lOmhlbGxv",
|
|
|
|
"email": "user@example.com"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
if string(buf) != expConfStr {
|
|
|
|
t.Fatalf("Should have save in new form: \n%s\n not \n%s", string(buf), expConfStr)
|
2015-07-29 01:28:14 +00:00
|
|
|
}
|
|
|
|
}
|