github.com/yamamoto-febc/docker@v1.9.0/cliconfig/config_test.go (about) 1 package cliconfig 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/docker/docker/pkg/homedir" 11 ) 12 13 func TestEmptyConfigDir(t *testing.T) { 14 tmpHome, err := ioutil.TempDir("", "config-test") 15 if err != nil { 16 t.Fatal(err) 17 } 18 defer os.RemoveAll(tmpHome) 19 20 SetConfigDir(tmpHome) 21 22 config, err := Load("") 23 if err != nil { 24 t.Fatalf("Failed loading on empty config dir: %q", err) 25 } 26 27 expectedConfigFilename := filepath.Join(tmpHome, ConfigFileName) 28 if config.Filename() != expectedConfigFilename { 29 t.Fatalf("Expected config filename %s, got %s", expectedConfigFilename, config.Filename()) 30 } 31 32 // Now save it and make sure it shows up in new form 33 saveConfigAndValidateNewFormat(t, config, tmpHome) 34 } 35 36 func TestMissingFile(t *testing.T) { 37 tmpHome, err := ioutil.TempDir("", "config-test") 38 if err != nil { 39 t.Fatal(err) 40 } 41 defer os.RemoveAll(tmpHome) 42 43 config, err := Load(tmpHome) 44 if err != nil { 45 t.Fatalf("Failed loading on missing file: %q", err) 46 } 47 48 // Now save it and make sure it shows up in new form 49 saveConfigAndValidateNewFormat(t, config, tmpHome) 50 } 51 52 func TestSaveFileToDirs(t *testing.T) { 53 tmpHome, err := ioutil.TempDir("", "config-test") 54 if err != nil { 55 t.Fatal(err) 56 } 57 defer os.RemoveAll(tmpHome) 58 59 tmpHome += "/.docker" 60 61 config, err := Load(tmpHome) 62 if err != nil { 63 t.Fatalf("Failed loading on missing file: %q", err) 64 } 65 66 // Now save it and make sure it shows up in new form 67 saveConfigAndValidateNewFormat(t, config, tmpHome) 68 } 69 70 func TestEmptyFile(t *testing.T) { 71 tmpHome, err := ioutil.TempDir("", "config-test") 72 if err != nil { 73 t.Fatal(err) 74 } 75 defer os.RemoveAll(tmpHome) 76 77 fn := filepath.Join(tmpHome, ConfigFileName) 78 if err := ioutil.WriteFile(fn, []byte(""), 0600); err != nil { 79 t.Fatal(err) 80 } 81 82 _, err = Load(tmpHome) 83 if err == nil { 84 t.Fatalf("Was supposed to fail") 85 } 86 } 87 88 func TestEmptyJson(t *testing.T) { 89 tmpHome, err := ioutil.TempDir("", "config-test") 90 if err != nil { 91 t.Fatal(err) 92 } 93 defer os.RemoveAll(tmpHome) 94 95 fn := filepath.Join(tmpHome, ConfigFileName) 96 if err := ioutil.WriteFile(fn, []byte("{}"), 0600); err != nil { 97 t.Fatal(err) 98 } 99 100 config, err := Load(tmpHome) 101 if err != nil { 102 t.Fatalf("Failed loading on empty json file: %q", err) 103 } 104 105 // Now save it and make sure it shows up in new form 106 saveConfigAndValidateNewFormat(t, config, tmpHome) 107 } 108 109 func TestOldInvalidsAuth(t *testing.T) { 110 invalids := map[string]string{ 111 `username = test`: "The Auth config file is empty", 112 `username 113 password 114 email`: "Invalid Auth config file", 115 `username = test 116 email`: "Invalid auth configuration file", 117 `username = am9lam9lOmhlbGxv 118 email`: "Invalid Auth config file", 119 } 120 121 tmpHome, err := ioutil.TempDir("", "config-test") 122 if err != nil { 123 t.Fatal(err) 124 } 125 defer os.RemoveAll(tmpHome) 126 127 homeKey := homedir.Key() 128 homeVal := homedir.Get() 129 130 defer func() { os.Setenv(homeKey, homeVal) }() 131 os.Setenv(homeKey, tmpHome) 132 133 for content, expectedError := range invalids { 134 fn := filepath.Join(tmpHome, oldConfigfile) 135 if err := ioutil.WriteFile(fn, []byte(content), 0600); err != nil { 136 t.Fatal(err) 137 } 138 139 config, err := Load(tmpHome) 140 if err == nil || err.Error() != expectedError { 141 t.Fatalf("Should have failed, got: %q, %q", config, err) 142 } 143 144 } 145 } 146 147 func TestOldValidAuth(t *testing.T) { 148 tmpHome, err := ioutil.TempDir("", "config-test") 149 if err != nil { 150 t.Fatal(err) 151 } 152 if err != nil { 153 t.Fatal(err) 154 } 155 defer os.RemoveAll(tmpHome) 156 157 homeKey := homedir.Key() 158 homeVal := homedir.Get() 159 160 defer func() { os.Setenv(homeKey, homeVal) }() 161 os.Setenv(homeKey, tmpHome) 162 163 fn := filepath.Join(tmpHome, oldConfigfile) 164 js := `username = am9lam9lOmhlbGxv 165 email = user@example.com` 166 if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil { 167 t.Fatal(err) 168 } 169 170 config, err := Load(tmpHome) 171 if err != nil { 172 t.Fatal(err) 173 } 174 175 // defaultIndexserver is https://index.docker.io/v1/ 176 ac := config.AuthConfigs["https://index.docker.io/v1/"] 177 if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" { 178 t.Fatalf("Missing data from parsing:\n%q", config) 179 } 180 181 // Now save it and make sure it shows up in new form 182 configStr := saveConfigAndValidateNewFormat(t, config, tmpHome) 183 184 if !strings.Contains(configStr, "user@example.com") { 185 t.Fatalf("Should have save in new form: %s", configStr) 186 } 187 } 188 189 func TestOldJsonInvalid(t *testing.T) { 190 tmpHome, err := ioutil.TempDir("", "config-test") 191 if err != nil { 192 t.Fatal(err) 193 } 194 defer os.RemoveAll(tmpHome) 195 196 homeKey := homedir.Key() 197 homeVal := homedir.Get() 198 199 defer func() { os.Setenv(homeKey, homeVal) }() 200 os.Setenv(homeKey, tmpHome) 201 202 fn := filepath.Join(tmpHome, oldConfigfile) 203 js := `{"https://index.docker.io/v1/":{"auth":"test","email":"user@example.com"}}` 204 if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil { 205 t.Fatal(err) 206 } 207 208 config, err := Load(tmpHome) 209 if err == nil || err.Error() != "Invalid auth configuration file" { 210 t.Fatalf("Expected an error got : %v, %v", config, err) 211 } 212 } 213 214 func TestOldJson(t *testing.T) { 215 tmpHome, err := ioutil.TempDir("", "config-test") 216 if err != nil { 217 t.Fatal(err) 218 } 219 defer os.RemoveAll(tmpHome) 220 221 homeKey := homedir.Key() 222 homeVal := homedir.Get() 223 224 defer func() { os.Setenv(homeKey, homeVal) }() 225 os.Setenv(homeKey, tmpHome) 226 227 fn := filepath.Join(tmpHome, oldConfigfile) 228 js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}` 229 if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil { 230 t.Fatal(err) 231 } 232 233 config, err := Load(tmpHome) 234 if err != nil { 235 t.Fatalf("Failed loading on empty json file: %q", err) 236 } 237 238 ac := config.AuthConfigs["https://index.docker.io/v1/"] 239 if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" { 240 t.Fatalf("Missing data from parsing:\n%q", config) 241 } 242 243 // Now save it and make sure it shows up in new form 244 configStr := saveConfigAndValidateNewFormat(t, config, tmpHome) 245 246 if !strings.Contains(configStr, "user@example.com") { 247 t.Fatalf("Should have save in new form: %s", configStr) 248 } 249 } 250 251 func TestNewJson(t *testing.T) { 252 tmpHome, err := ioutil.TempDir("", "config-test") 253 if err != nil { 254 t.Fatal(err) 255 } 256 defer os.RemoveAll(tmpHome) 257 258 fn := filepath.Join(tmpHome, ConfigFileName) 259 js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } } }` 260 if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil { 261 t.Fatal(err) 262 } 263 264 config, err := Load(tmpHome) 265 if err != nil { 266 t.Fatalf("Failed loading on empty json file: %q", err) 267 } 268 269 ac := config.AuthConfigs["https://index.docker.io/v1/"] 270 if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" { 271 t.Fatalf("Missing data from parsing:\n%q", config) 272 } 273 274 // Now save it and make sure it shows up in new form 275 configStr := saveConfigAndValidateNewFormat(t, config, tmpHome) 276 277 if !strings.Contains(configStr, "user@example.com") { 278 t.Fatalf("Should have save in new form: %s", configStr) 279 } 280 } 281 282 func TestJsonWithPsFormat(t *testing.T) { 283 tmpHome, err := ioutil.TempDir("", "config-test") 284 if err != nil { 285 t.Fatal(err) 286 } 287 defer os.RemoveAll(tmpHome) 288 289 fn := filepath.Join(tmpHome, ConfigFileName) 290 js := `{ 291 "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } }, 292 "psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}" 293 }` 294 if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil { 295 t.Fatal(err) 296 } 297 298 config, err := Load(tmpHome) 299 if err != nil { 300 t.Fatalf("Failed loading on empty json file: %q", err) 301 } 302 303 if config.PsFormat != `table {{.ID}}\t{{.Label "com.docker.label.cpu"}}` { 304 t.Fatalf("Unknown ps format: %s\n", config.PsFormat) 305 } 306 307 // Now save it and make sure it shows up in new form 308 configStr := saveConfigAndValidateNewFormat(t, config, tmpHome) 309 if !strings.Contains(configStr, `"psFormat":`) || 310 !strings.Contains(configStr, "{{.ID}}") { 311 t.Fatalf("Should have save in new form: %s", configStr) 312 } 313 } 314 315 // Save it and make sure it shows up in new form 316 func saveConfigAndValidateNewFormat(t *testing.T, config *ConfigFile, homeFolder string) string { 317 err := config.Save() 318 if err != nil { 319 t.Fatalf("Failed to save: %q", err) 320 } 321 322 buf, err := ioutil.ReadFile(filepath.Join(homeFolder, ConfigFileName)) 323 if !strings.Contains(string(buf), `"auths":`) { 324 t.Fatalf("Should have save in new form: %s", string(buf)) 325 } 326 return string(buf) 327 } 328 329 func TestConfigDir(t *testing.T) { 330 tmpHome, err := ioutil.TempDir("", "config-test") 331 if err != nil { 332 t.Fatal(err) 333 } 334 defer os.RemoveAll(tmpHome) 335 336 if ConfigDir() == tmpHome { 337 t.Fatalf("Expected ConfigDir to be different than %s by default, but was the same", tmpHome) 338 } 339 340 // Update configDir 341 SetConfigDir(tmpHome) 342 343 if ConfigDir() != tmpHome { 344 t.Fatalf("Expected ConfigDir to %s, but was %s", tmpHome, ConfigDir()) 345 } 346 } 347 348 func TestConfigFile(t *testing.T) { 349 configFilename := "configFilename" 350 configFile := NewConfigFile(configFilename) 351 352 if configFile.Filename() != configFilename { 353 t.Fatalf("Expected %s, got %s", configFilename, configFile.Filename()) 354 } 355 } 356 357 func TestJsonReaderNoFile(t *testing.T) { 358 js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } } }` 359 360 config, err := LoadFromReader(strings.NewReader(js)) 361 if err != nil { 362 t.Fatalf("Failed loading on empty json file: %q", err) 363 } 364 365 ac := config.AuthConfigs["https://index.docker.io/v1/"] 366 if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" { 367 t.Fatalf("Missing data from parsing:\n%q", config) 368 } 369 370 } 371 372 func TestOldJsonReaderNoFile(t *testing.T) { 373 js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}` 374 375 config, err := LegacyLoadFromReader(strings.NewReader(js)) 376 if err != nil { 377 t.Fatalf("Failed loading on empty json file: %q", err) 378 } 379 380 ac := config.AuthConfigs["https://index.docker.io/v1/"] 381 if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" { 382 t.Fatalf("Missing data from parsing:\n%q", config) 383 } 384 } 385 386 func TestJsonWithPsFormatNoFile(t *testing.T) { 387 js := `{ 388 "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } }, 389 "psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}" 390 }` 391 config, err := LoadFromReader(strings.NewReader(js)) 392 if err != nil { 393 t.Fatalf("Failed loading on empty json file: %q", err) 394 } 395 396 if config.PsFormat != `table {{.ID}}\t{{.Label "com.docker.label.cpu"}}` { 397 t.Fatalf("Unknown ps format: %s\n", config.PsFormat) 398 } 399 400 } 401 402 func TestJsonSaveWithNoFile(t *testing.T) { 403 js := `{ 404 "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } }, 405 "psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}" 406 }` 407 config, err := LoadFromReader(strings.NewReader(js)) 408 err = config.Save() 409 if err == nil { 410 t.Fatalf("Expected error. File should not have been able to save with no file name.") 411 } 412 413 tmpHome, err := ioutil.TempDir("", "config-test") 414 if err != nil { 415 t.Fatalf("Failed to create a temp dir: %q", err) 416 } 417 defer os.RemoveAll(tmpHome) 418 419 fn := filepath.Join(tmpHome, ConfigFileName) 420 f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) 421 err = config.SaveToWriter(f) 422 if err != nil { 423 t.Fatalf("Failed saving to file: %q", err) 424 } 425 buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName)) 426 if !strings.Contains(string(buf), `"auths":`) || 427 !strings.Contains(string(buf), "user@example.com") { 428 t.Fatalf("Should have save in new form: %s", string(buf)) 429 } 430 431 } 432 func TestLegacyJsonSaveWithNoFile(t *testing.T) { 433 434 js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}` 435 config, err := LegacyLoadFromReader(strings.NewReader(js)) 436 err = config.Save() 437 if err == nil { 438 t.Fatalf("Expected error. File should not have been able to save with no file name.") 439 } 440 441 tmpHome, err := ioutil.TempDir("", "config-test") 442 if err != nil { 443 t.Fatalf("Failed to create a temp dir: %q", err) 444 } 445 defer os.RemoveAll(tmpHome) 446 447 fn := filepath.Join(tmpHome, ConfigFileName) 448 f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) 449 err = config.SaveToWriter(f) 450 if err != nil { 451 t.Fatalf("Failed saving to file: %q", err) 452 } 453 buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName)) 454 if !strings.Contains(string(buf), `"auths":`) || 455 !strings.Contains(string(buf), "user@example.com") { 456 t.Fatalf("Should have save in new form: %s", string(buf)) 457 } 458 }