setting_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package setting
  2. import (
  3. "os"
  4. "path/filepath"
  5. "runtime"
  6. "testing"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestLoadingSettings(t *testing.T) {
  10. Convey("Testing loading settings from ini file", t, func() {
  11. skipStaticRootValidation = true
  12. Convey("Given the default ini files", func() {
  13. cfg := NewCfg()
  14. err := cfg.Load(&CommandLineArgs{HomePath: "../../"})
  15. So(err, ShouldBeNil)
  16. So(AdminUser, ShouldEqual, "admin")
  17. })
  18. Convey("Should be able to override via environment variables", func() {
  19. os.Setenv("GF_SECURITY_ADMIN_USER", "superduper")
  20. cfg := NewCfg()
  21. cfg.Load(&CommandLineArgs{HomePath: "../../"})
  22. So(AdminUser, ShouldEqual, "superduper")
  23. So(DataPath, ShouldEqual, filepath.Join(HomePath, "data"))
  24. So(LogsPath, ShouldEqual, filepath.Join(DataPath, "log"))
  25. })
  26. Convey("Should replace password when defined in environment", func() {
  27. os.Setenv("GF_SECURITY_ADMIN_PASSWORD", "supersecret")
  28. cfg := NewCfg()
  29. cfg.Load(&CommandLineArgs{HomePath: "../../"})
  30. So(appliedEnvOverrides, ShouldContain, "GF_SECURITY_ADMIN_PASSWORD=*********")
  31. })
  32. Convey("Should return an error when url is invalid", func() {
  33. os.Setenv("GF_DATABASE_URL", "postgres.%31://grafana:secret@postgres:5432/grafana")
  34. cfg := NewCfg()
  35. err := cfg.Load(&CommandLineArgs{HomePath: "../../"})
  36. So(err, ShouldNotBeNil)
  37. })
  38. Convey("Should replace password in URL when url environment is defined", func() {
  39. os.Setenv("GF_DATABASE_URL", "mysql://user:secret@localhost:3306/database")
  40. cfg := NewCfg()
  41. cfg.Load(&CommandLineArgs{HomePath: "../../"})
  42. So(appliedEnvOverrides, ShouldContain, "GF_DATABASE_URL=mysql://user:-redacted-@localhost:3306/database")
  43. })
  44. Convey("Should get property map from command line args array", func() {
  45. props := getCommandLineProperties([]string{"cfg:test=value", "cfg:map.test=1"})
  46. So(len(props), ShouldEqual, 2)
  47. So(props["test"], ShouldEqual, "value")
  48. So(props["map.test"], ShouldEqual, "1")
  49. })
  50. Convey("Should be able to override via command line", func() {
  51. if runtime.GOOS == "windows" {
  52. cfg := NewCfg()
  53. cfg.Load(&CommandLineArgs{
  54. HomePath: "../../",
  55. Args: []string{`cfg:paths.data=c:\tmp\data`, `cfg:paths.logs=c:\tmp\logs`},
  56. })
  57. So(DataPath, ShouldEqual, `c:\tmp\data`)
  58. So(LogsPath, ShouldEqual, `c:\tmp\logs`)
  59. } else {
  60. cfg := NewCfg()
  61. cfg.Load(&CommandLineArgs{
  62. HomePath: "../../",
  63. Args: []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"},
  64. })
  65. So(DataPath, ShouldEqual, "/tmp/data")
  66. So(LogsPath, ShouldEqual, "/tmp/logs")
  67. }
  68. })
  69. Convey("Should be able to override defaults via command line", func() {
  70. cfg := NewCfg()
  71. cfg.Load(&CommandLineArgs{
  72. HomePath: "../../",
  73. Args: []string{
  74. "cfg:default.server.domain=test2",
  75. },
  76. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  77. })
  78. So(Domain, ShouldEqual, "test2")
  79. })
  80. Convey("Defaults can be overridden in specified config file", func() {
  81. if runtime.GOOS == "windows" {
  82. cfg := NewCfg()
  83. cfg.Load(&CommandLineArgs{
  84. HomePath: "../../",
  85. Config: filepath.Join(HomePath, "tests/config-files/override_windows.ini"),
  86. Args: []string{`cfg:default.paths.data=c:\tmp\data`},
  87. })
  88. So(DataPath, ShouldEqual, `c:\tmp\override`)
  89. } else {
  90. cfg := NewCfg()
  91. cfg.Load(&CommandLineArgs{
  92. HomePath: "../../",
  93. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  94. Args: []string{"cfg:default.paths.data=/tmp/data"},
  95. })
  96. So(DataPath, ShouldEqual, "/tmp/override")
  97. }
  98. })
  99. Convey("Command line overrides specified config file", func() {
  100. if runtime.GOOS == "windows" {
  101. cfg := NewCfg()
  102. cfg.Load(&CommandLineArgs{
  103. HomePath: "../../",
  104. Config: filepath.Join(HomePath, "tests/config-files/override_windows.ini"),
  105. Args: []string{`cfg:paths.data=c:\tmp\data`},
  106. })
  107. So(DataPath, ShouldEqual, `c:\tmp\data`)
  108. } else {
  109. cfg := NewCfg()
  110. cfg.Load(&CommandLineArgs{
  111. HomePath: "../../",
  112. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  113. Args: []string{"cfg:paths.data=/tmp/data"},
  114. })
  115. So(DataPath, ShouldEqual, "/tmp/data")
  116. }
  117. })
  118. Convey("Can use environment variables in config values", func() {
  119. if runtime.GOOS == "windows" {
  120. os.Setenv("GF_DATA_PATH", `c:\tmp\env_override`)
  121. cfg := NewCfg()
  122. cfg.Load(&CommandLineArgs{
  123. HomePath: "../../",
  124. Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
  125. })
  126. So(DataPath, ShouldEqual, `c:\tmp\env_override`)
  127. } else {
  128. os.Setenv("GF_DATA_PATH", "/tmp/env_override")
  129. cfg := NewCfg()
  130. cfg.Load(&CommandLineArgs{
  131. HomePath: "../../",
  132. Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
  133. })
  134. So(DataPath, ShouldEqual, "/tmp/env_override")
  135. }
  136. })
  137. Convey("instance_name default to hostname even if hostname env is empty", func() {
  138. cfg := NewCfg()
  139. cfg.Load(&CommandLineArgs{
  140. HomePath: "../../",
  141. })
  142. hostname, _ := os.Hostname()
  143. So(InstanceName, ShouldEqual, hostname)
  144. })
  145. })
  146. }