config_file_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package cliconfig
  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 := Load(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 TestSaveFileToDirs(t *testing.T) {
  28. tmpHome, _ := ioutil.TempDir("", "config-test")
  29. tmpHome += "/.docker"
  30. config, err := Load(tmpHome)
  31. if err != nil {
  32. t.Fatalf("Failed loading on missing file: %q", err)
  33. }
  34. // Now save it and make sure it shows up in new form
  35. err = config.Save()
  36. if err != nil {
  37. t.Fatalf("Failed to save: %q", err)
  38. }
  39. buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
  40. if !strings.Contains(string(buf), `"auths":`) {
  41. t.Fatalf("Should have save in new form: %s", string(buf))
  42. }
  43. }
  44. func TestEmptyFile(t *testing.T) {
  45. tmpHome, _ := ioutil.TempDir("", "config-test")
  46. fn := filepath.Join(tmpHome, CONFIGFILE)
  47. ioutil.WriteFile(fn, []byte(""), 0600)
  48. _, err := Load(tmpHome)
  49. if err == nil {
  50. t.Fatalf("Was supposed to fail")
  51. }
  52. }
  53. func TestEmptyJson(t *testing.T) {
  54. tmpHome, _ := ioutil.TempDir("", "config-test")
  55. fn := filepath.Join(tmpHome, CONFIGFILE)
  56. ioutil.WriteFile(fn, []byte("{}"), 0600)
  57. config, err := Load(tmpHome)
  58. if err != nil {
  59. t.Fatalf("Failed loading on empty json file: %q", err)
  60. }
  61. // Now save it and make sure it shows up in new form
  62. err = config.Save()
  63. if err != nil {
  64. t.Fatalf("Failed to save: %q", err)
  65. }
  66. buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
  67. if !strings.Contains(string(buf), `"auths":`) {
  68. t.Fatalf("Should have save in new form: %s", string(buf))
  69. }
  70. }
  71. func TestOldJson(t *testing.T) {
  72. if runtime.GOOS == "windows" {
  73. return
  74. }
  75. tmpHome, _ := ioutil.TempDir("", "config-test")
  76. defer os.RemoveAll(tmpHome)
  77. homeKey := homedir.Key()
  78. homeVal := homedir.Get()
  79. defer func() { os.Setenv(homeKey, homeVal) }()
  80. os.Setenv(homeKey, tmpHome)
  81. fn := filepath.Join(tmpHome, OLD_CONFIGFILE)
  82. js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
  83. ioutil.WriteFile(fn, []byte(js), 0600)
  84. config, err := Load(tmpHome)
  85. if err != nil {
  86. t.Fatalf("Failed loading on empty json file: %q", err)
  87. }
  88. ac := config.AuthConfigs["https://index.docker.io/v1/"]
  89. if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" {
  90. t.Fatalf("Missing data from parsing:\n%q", config)
  91. }
  92. // Now save it and make sure it shows up in new form
  93. err = config.Save()
  94. if err != nil {
  95. t.Fatalf("Failed to save: %q", err)
  96. }
  97. buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
  98. if !strings.Contains(string(buf), `"auths":`) ||
  99. !strings.Contains(string(buf), "user@example.com") {
  100. t.Fatalf("Should have save in new form: %s", string(buf))
  101. }
  102. }
  103. func TestNewJson(t *testing.T) {
  104. tmpHome, _ := ioutil.TempDir("", "config-test")
  105. fn := filepath.Join(tmpHome, CONFIGFILE)
  106. js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } } }`
  107. ioutil.WriteFile(fn, []byte(js), 0600)
  108. config, err := Load(tmpHome)
  109. if err != nil {
  110. t.Fatalf("Failed loading on empty json file: %q", err)
  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. // Now save it and make sure it shows up in new form
  117. err = config.Save()
  118. if err != nil {
  119. t.Fatalf("Failed to save: %q", err)
  120. }
  121. buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
  122. if !strings.Contains(string(buf), `"auths":`) ||
  123. !strings.Contains(string(buf), "user@example.com") {
  124. t.Fatalf("Should have save in new form: %s", string(buf))
  125. }
  126. }