github.com/rentongzhang/docker@v1.8.2-rc1/cliconfig/config_test.go (about) 1 package cliconfig 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "runtime" 8 "strings" 9 "testing" 10 11 "github.com/docker/docker/pkg/homedir" 12 ) 13 14 func TestMissingFile(t *testing.T) { 15 tmpHome, _ := ioutil.TempDir("", "config-test") 16 17 config, err := Load(tmpHome) 18 if err != nil { 19 t.Fatalf("Failed loading on missing file: %q", err) 20 } 21 22 // Now save it and make sure it shows up in new form 23 err = config.Save() 24 if err != nil { 25 t.Fatalf("Failed to save: %q", err) 26 } 27 28 buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName)) 29 if !strings.Contains(string(buf), `"auths":`) { 30 t.Fatalf("Should have save in new form: %s", string(buf)) 31 } 32 } 33 34 func TestSaveFileToDirs(t *testing.T) { 35 tmpHome, _ := ioutil.TempDir("", "config-test") 36 37 tmpHome += "/.docker" 38 39 config, err := Load(tmpHome) 40 if err != nil { 41 t.Fatalf("Failed loading on missing file: %q", err) 42 } 43 44 // Now save it and make sure it shows up in new form 45 err = config.Save() 46 if err != nil { 47 t.Fatalf("Failed to save: %q", err) 48 } 49 50 buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName)) 51 if !strings.Contains(string(buf), `"auths":`) { 52 t.Fatalf("Should have save in new form: %s", string(buf)) 53 } 54 } 55 56 func TestEmptyFile(t *testing.T) { 57 tmpHome, _ := ioutil.TempDir("", "config-test") 58 fn := filepath.Join(tmpHome, ConfigFileName) 59 ioutil.WriteFile(fn, []byte(""), 0600) 60 61 _, err := Load(tmpHome) 62 if err == nil { 63 t.Fatalf("Was supposed to fail") 64 } 65 } 66 67 func TestEmptyJson(t *testing.T) { 68 tmpHome, _ := ioutil.TempDir("", "config-test") 69 fn := filepath.Join(tmpHome, ConfigFileName) 70 ioutil.WriteFile(fn, []byte("{}"), 0600) 71 72 config, err := Load(tmpHome) 73 if err != nil { 74 t.Fatalf("Failed loading on empty json file: %q", err) 75 } 76 77 // Now save it and make sure it shows up in new form 78 err = config.Save() 79 if err != nil { 80 t.Fatalf("Failed to save: %q", err) 81 } 82 83 buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName)) 84 if !strings.Contains(string(buf), `"auths":`) { 85 t.Fatalf("Should have save in new form: %s", string(buf)) 86 } 87 } 88 89 func TestOldJson(t *testing.T) { 90 if runtime.GOOS == "windows" { 91 return 92 } 93 94 tmpHome, _ := ioutil.TempDir("", "config-test") 95 defer os.RemoveAll(tmpHome) 96 97 homeKey := homedir.Key() 98 homeVal := homedir.Get() 99 100 defer func() { os.Setenv(homeKey, homeVal) }() 101 os.Setenv(homeKey, tmpHome) 102 103 fn := filepath.Join(tmpHome, oldConfigfile) 104 js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}` 105 ioutil.WriteFile(fn, []byte(js), 0600) 106 107 config, err := Load(tmpHome) 108 if err != nil { 109 t.Fatalf("Failed loading on empty json file: %q", err) 110 } 111 112 ac := config.AuthConfigs["https://index.docker.io/v1/"] 113 if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" { 114 t.Fatalf("Missing data from parsing:\n%q", config) 115 } 116 117 // Now save it and make sure it shows up in new form 118 err = config.Save() 119 if err != nil { 120 t.Fatalf("Failed to save: %q", err) 121 } 122 123 buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName)) 124 if !strings.Contains(string(buf), `"auths":`) || 125 !strings.Contains(string(buf), "user@example.com") { 126 t.Fatalf("Should have save in new form: %s", string(buf)) 127 } 128 } 129 130 func TestNewJson(t *testing.T) { 131 tmpHome, _ := ioutil.TempDir("", "config-test") 132 fn := filepath.Join(tmpHome, ConfigFileName) 133 js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } } }` 134 ioutil.WriteFile(fn, []byte(js), 0600) 135 136 config, err := Load(tmpHome) 137 if err != nil { 138 t.Fatalf("Failed loading on empty json file: %q", err) 139 } 140 141 ac := config.AuthConfigs["https://index.docker.io/v1/"] 142 if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" { 143 t.Fatalf("Missing data from parsing:\n%q", config) 144 } 145 146 // Now save it and make sure it shows up in new form 147 err = config.Save() 148 if err != nil { 149 t.Fatalf("Failed to save: %q", err) 150 } 151 152 buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName)) 153 if !strings.Contains(string(buf), `"auths":`) || 154 !strings.Contains(string(buf), "user@example.com") { 155 t.Fatalf("Should have save in new form: %s", string(buf)) 156 } 157 } 158 159 func TestJsonWithPsFormat(t *testing.T) { 160 tmpHome, _ := ioutil.TempDir("", "config-test") 161 fn := filepath.Join(tmpHome, ConfigFileName) 162 js := `{ 163 "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } }, 164 "psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}" 165 }` 166 ioutil.WriteFile(fn, []byte(js), 0600) 167 168 config, err := Load(tmpHome) 169 if err != nil { 170 t.Fatalf("Failed loading on empty json file: %q", err) 171 } 172 173 if config.PsFormat != `table {{.ID}}\t{{.Label "com.docker.label.cpu"}}` { 174 t.Fatalf("Unknown ps format: %s\n", config.PsFormat) 175 } 176 177 // Now save it and make sure it shows up in new form 178 err = config.Save() 179 if err != nil { 180 t.Fatalf("Failed to save: %q", err) 181 } 182 183 buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName)) 184 if !strings.Contains(string(buf), `"psFormat":`) || 185 !strings.Contains(string(buf), "{{.ID}}") { 186 t.Fatalf("Should have save in new form: %s", string(buf)) 187 } 188 }