123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package cliconfig
- 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 := Load(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 TestSaveFileToDirs(t *testing.T) {
- tmpHome, _ := ioutil.TempDir("", "config-test")
- tmpHome += "/.docker"
- config, err := Load(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 := Load(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 := Load(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 := 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.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 := 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.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))
- }
- }
|