config_file_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package registry
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "strings"
  8. "testing"
  9. "github.com/docker/docker/pkg/homedir"
  10. )
  11. func TestMissingFile(t *testing.T) {
  12. tmpHome, _ := ioutil.TempDir("", "config-test")
  13. config, err := LoadConfig(tmpHome)
  14. if err != nil {
  15. t.Fatalf("Failed loading on missing file: %q", err)
  16. }
  17. // Now save it and make sure it shows up in new form
  18. err = config.Save()
  19. if err != nil {
  20. t.Fatalf("Failed to save: %q", err)
  21. }
  22. buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
  23. if !strings.Contains(string(buf), `"auths":`) {
  24. t.Fatalf("Should have save in new form: %s", string(buf))
  25. }
  26. }
  27. func TestEmptyFile(t *testing.T) {
  28. tmpHome, _ := ioutil.TempDir("", "config-test")
  29. fn := filepath.Join(tmpHome, CONFIGFILE)
  30. ioutil.WriteFile(fn, []byte(""), 0600)
  31. _, err := LoadConfig(tmpHome)
  32. if err == nil {
  33. t.Fatalf("Was supposed to fail")
  34. }
  35. }
  36. func TestEmptyJson(t *testing.T) {
  37. tmpHome, _ := ioutil.TempDir("", "config-test")
  38. fn := filepath.Join(tmpHome, CONFIGFILE)
  39. ioutil.WriteFile(fn, []byte("{}"), 0600)
  40. config, err := LoadConfig(tmpHome)
  41. if err != nil {
  42. t.Fatalf("Failed loading on empty json file: %q", err)
  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. buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
  50. if !strings.Contains(string(buf), `"auths":`) {
  51. t.Fatalf("Should have save in new form: %s", string(buf))
  52. }
  53. }
  54. func TestOldJson(t *testing.T) {
  55. if runtime.GOOS == "windows" {
  56. return
  57. }
  58. tmpHome, _ := ioutil.TempDir("", "config-test")
  59. defer os.RemoveAll(tmpHome)
  60. homeKey := homedir.Key()
  61. homeVal := homedir.Get()
  62. defer func() { os.Setenv(homeKey, homeVal) }()
  63. os.Setenv(homeKey, tmpHome)
  64. fn := filepath.Join(tmpHome, OLD_CONFIGFILE)
  65. js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
  66. ioutil.WriteFile(fn, []byte(js), 0600)
  67. config, err := LoadConfig(tmpHome)
  68. if err != nil {
  69. t.Fatalf("Failed loading on empty json file: %q", err)
  70. }
  71. ac := config.AuthConfigs["https://index.docker.io/v1/"]
  72. if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" {
  73. t.Fatalf("Missing data from parsing:\n%q", config)
  74. }
  75. // Now save it and make sure it shows up in new form
  76. err = config.Save()
  77. if err != nil {
  78. t.Fatalf("Failed to save: %q", err)
  79. }
  80. buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
  81. if !strings.Contains(string(buf), `"auths":`) ||
  82. !strings.Contains(string(buf), "user@example.com") {
  83. t.Fatalf("Should have save in new form: %s", string(buf))
  84. }
  85. }
  86. func TestNewJson(t *testing.T) {
  87. tmpHome, _ := ioutil.TempDir("", "config-test")
  88. fn := filepath.Join(tmpHome, CONFIGFILE)
  89. js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } } }`
  90. ioutil.WriteFile(fn, []byte(js), 0600)
  91. config, err := LoadConfig(tmpHome)
  92. if err != nil {
  93. t.Fatalf("Failed loading on empty json file: %q", err)
  94. }
  95. ac := config.AuthConfigs["https://index.docker.io/v1/"]
  96. if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" {
  97. t.Fatalf("Missing data from parsing:\n%q", config)
  98. }
  99. // Now save it and make sure it shows up in new form
  100. err = config.Save()
  101. if err != nil {
  102. t.Fatalf("Failed to save: %q", err)
  103. }
  104. buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
  105. if !strings.Contains(string(buf), `"auths":`) ||
  106. !strings.Contains(string(buf), "user@example.com") {
  107. t.Fatalf("Should have save in new form: %s", string(buf))
  108. }
  109. }