123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545 |
- package cliconfig
- import (
- "io/ioutil"
- "os"
- "path/filepath"
- "strings"
- "testing"
- "github.com/docker/docker/cliconfig/configfile"
- "github.com/docker/docker/pkg/homedir"
- )
- func TestEmptyConfigDir(t *testing.T) {
- tmpHome, err := ioutil.TempDir("", "config-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpHome)
- SetConfigDir(tmpHome)
- config, err := Load("")
- if err != nil {
- 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)
- }
- // Now save it and make sure it shows up in new form
- saveConfigAndValidateNewFormat(t, config, tmpHome)
- }
- func TestMissingFile(t *testing.T) {
- tmpHome, err := ioutil.TempDir("", "config-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpHome)
- 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
- saveConfigAndValidateNewFormat(t, config, tmpHome)
- }
- func TestSaveFileToDirs(t *testing.T) {
- tmpHome, err := ioutil.TempDir("", "config-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpHome)
- 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
- saveConfigAndValidateNewFormat(t, config, tmpHome)
- }
- func TestEmptyFile(t *testing.T) {
- tmpHome, err := ioutil.TempDir("", "config-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpHome)
- fn := filepath.Join(tmpHome, ConfigFileName)
- if err := ioutil.WriteFile(fn, []byte(""), 0600); err != nil {
- t.Fatal(err)
- }
- _, err = Load(tmpHome)
- if err == nil {
- t.Fatalf("Was supposed to fail")
- }
- }
- func TestEmptyJson(t *testing.T) {
- tmpHome, err := ioutil.TempDir("", "config-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpHome)
- fn := filepath.Join(tmpHome, ConfigFileName)
- if err := ioutil.WriteFile(fn, []byte("{}"), 0600); err != nil {
- t.Fatal(err)
- }
- 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
- saveConfigAndValidateNewFormat(t, config, tmpHome)
- }
- func TestOldInvalidsAuth(t *testing.T) {
- invalids := map[string]string{
- `username = test`: "The Auth config file is empty",
- `username
- password`: "Invalid Auth config file",
- `username = test
- email`: "Invalid auth configuration file",
- }
- 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)
- 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)
- // 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)
- }
- }
- }
- 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)
- }
- 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.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)
- }
- }
- 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)
- // Use Contains instead of == since the file name will change each time
- if err == nil || !strings.Contains(err.Error(), "Invalid auth configuration file") {
- 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)
- }
- 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":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
- 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",
- "email": "user@example.com"
- }
- }
- }`
- if configStr != expConfStr {
- t.Fatalf("Should have save in new form: \n'%s'\n not \n'%s'\n", configStr, expConfStr)
- }
- }
- func TestNewJson(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" } } }`
- 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)
- }
- }
- func TestNewJsonNoEmail(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" } } }`
- 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)
- }
- }
- func TestJsonWithPsFormat(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" } },
- "psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}"
- }`
- 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.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
- 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.ConfigFile, homeFolder string) string {
- if err := config.Save(); err != nil {
- t.Fatalf("Failed to save: %q", err)
- }
- buf, err := ioutil.ReadFile(filepath.Join(homeFolder, ConfigFileName))
- if err != nil {
- t.Fatal(err)
- }
- if !strings.Contains(string(buf), `"auths":`) {
- t.Fatalf("Should have save in new form: %s", string(buf))
- }
- 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)
- }
- }
- 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.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.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" } },
- "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.")
- }
- tmpHome, err := ioutil.TempDir("", "config-test")
- if err != nil {
- t.Fatalf("Failed to create a temp dir: %q", err)
- }
- defer os.RemoveAll(tmpHome)
- 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 err != nil {
- t.Fatal(err)
- }
- 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)
- }
- }
- 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.")
- }
- tmpHome, err := ioutil.TempDir("", "config-test")
- if err != nil {
- t.Fatalf("Failed to create a temp dir: %q", err)
- }
- defer os.RemoveAll(tmpHome)
- fn := filepath.Join(tmpHome, ConfigFileName)
- f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
- if err = config.SaveToWriter(f); err != nil {
- t.Fatalf("Failed saving to file: %q", err)
- }
- buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName))
- if err != nil {
- t.Fatal(err)
- }
- 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)
- }
- }
|