setting_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package setting
  2. import (
  3. "os"
  4. "path"
  5. "path/filepath"
  6. "runtime"
  7. "testing"
  8. "gopkg.in/ini.v1"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. const (
  12. windows = "windows"
  13. )
  14. func TestLoadingSettings(t *testing.T) {
  15. Convey("Testing loading settings from ini file", t, func() {
  16. skipStaticRootValidation = true
  17. Convey("Given the default ini files", func() {
  18. cfg := NewCfg()
  19. err := cfg.Load(&CommandLineArgs{HomePath: "../../"})
  20. So(err, ShouldBeNil)
  21. So(AdminUser, ShouldEqual, "admin")
  22. So(cfg.RendererCallbackUrl, ShouldEqual, "http://localhost:3000/")
  23. })
  24. Convey("Should be able to override via environment variables", func() {
  25. os.Setenv("GF_SECURITY_ADMIN_USER", "superduper")
  26. cfg := NewCfg()
  27. cfg.Load(&CommandLineArgs{HomePath: "../../"})
  28. So(AdminUser, ShouldEqual, "superduper")
  29. So(cfg.DataPath, ShouldEqual, filepath.Join(HomePath, "data"))
  30. So(cfg.LogsPath, ShouldEqual, filepath.Join(cfg.DataPath, "log"))
  31. })
  32. Convey("Should replace password when defined in environment", func() {
  33. os.Setenv("GF_SECURITY_ADMIN_PASSWORD", "supersecret")
  34. cfg := NewCfg()
  35. cfg.Load(&CommandLineArgs{HomePath: "../../"})
  36. So(appliedEnvOverrides, ShouldContain, "GF_SECURITY_ADMIN_PASSWORD=*********")
  37. })
  38. Convey("Should return an error when url is invalid", func() {
  39. os.Setenv("GF_DATABASE_URL", "postgres.%31://grafana:secret@postgres:5432/grafana")
  40. cfg := NewCfg()
  41. err := cfg.Load(&CommandLineArgs{HomePath: "../../"})
  42. So(err, ShouldNotBeNil)
  43. })
  44. Convey("Should replace password in URL when url environment is defined", func() {
  45. os.Setenv("GF_DATABASE_URL", "mysql://user:secret@localhost:3306/database")
  46. cfg := NewCfg()
  47. cfg.Load(&CommandLineArgs{HomePath: "../../"})
  48. So(appliedEnvOverrides, ShouldContain, "GF_DATABASE_URL=mysql://user:-redacted-@localhost:3306/database")
  49. })
  50. Convey("Should get property map from command line args array", func() {
  51. props := getCommandLineProperties([]string{"cfg:test=value", "cfg:map.test=1"})
  52. So(len(props), ShouldEqual, 2)
  53. So(props["test"], ShouldEqual, "value")
  54. So(props["map.test"], ShouldEqual, "1")
  55. })
  56. Convey("Should be able to override via command line", func() {
  57. if runtime.GOOS == windows {
  58. cfg := NewCfg()
  59. cfg.Load(&CommandLineArgs{
  60. HomePath: "../../",
  61. Args: []string{`cfg:paths.data=c:\tmp\data`, `cfg:paths.logs=c:\tmp\logs`},
  62. })
  63. So(cfg.DataPath, ShouldEqual, `c:\tmp\data`)
  64. So(cfg.LogsPath, ShouldEqual, `c:\tmp\logs`)
  65. } else {
  66. cfg := NewCfg()
  67. cfg.Load(&CommandLineArgs{
  68. HomePath: "../../",
  69. Args: []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"},
  70. })
  71. So(cfg.DataPath, ShouldEqual, "/tmp/data")
  72. So(cfg.LogsPath, ShouldEqual, "/tmp/logs")
  73. }
  74. })
  75. Convey("Should be able to override defaults via command line", func() {
  76. cfg := NewCfg()
  77. cfg.Load(&CommandLineArgs{
  78. HomePath: "../../",
  79. Args: []string{
  80. "cfg:default.server.domain=test2",
  81. },
  82. Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"),
  83. })
  84. So(Domain, ShouldEqual, "test2")
  85. })
  86. Convey("Defaults can be overridden in specified config file", func() {
  87. if runtime.GOOS == windows {
  88. cfg := NewCfg()
  89. cfg.Load(&CommandLineArgs{
  90. HomePath: "../../",
  91. Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"),
  92. Args: []string{`cfg:default.paths.data=c:\tmp\data`},
  93. })
  94. So(cfg.DataPath, ShouldEqual, `c:\tmp\override`)
  95. } else {
  96. cfg := NewCfg()
  97. cfg.Load(&CommandLineArgs{
  98. HomePath: "../../",
  99. Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"),
  100. Args: []string{"cfg:default.paths.data=/tmp/data"},
  101. })
  102. So(cfg.DataPath, ShouldEqual, "/tmp/override")
  103. }
  104. })
  105. Convey("Command line overrides specified config file", func() {
  106. if runtime.GOOS == windows {
  107. cfg := NewCfg()
  108. cfg.Load(&CommandLineArgs{
  109. HomePath: "../../",
  110. Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"),
  111. Args: []string{`cfg:paths.data=c:\tmp\data`},
  112. })
  113. So(cfg.DataPath, ShouldEqual, `c:\tmp\data`)
  114. } else {
  115. cfg := NewCfg()
  116. cfg.Load(&CommandLineArgs{
  117. HomePath: "../../",
  118. Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"),
  119. Args: []string{"cfg:paths.data=/tmp/data"},
  120. })
  121. So(cfg.DataPath, ShouldEqual, "/tmp/data")
  122. }
  123. })
  124. Convey("Can use environment variables in config values", func() {
  125. if runtime.GOOS == windows {
  126. os.Setenv("GF_DATA_PATH", `c:\tmp\env_override`)
  127. cfg := NewCfg()
  128. cfg.Load(&CommandLineArgs{
  129. HomePath: "../../",
  130. Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
  131. })
  132. So(cfg.DataPath, ShouldEqual, `c:\tmp\env_override`)
  133. } else {
  134. os.Setenv("GF_DATA_PATH", "/tmp/env_override")
  135. cfg := NewCfg()
  136. cfg.Load(&CommandLineArgs{
  137. HomePath: "../../",
  138. Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
  139. })
  140. So(cfg.DataPath, ShouldEqual, "/tmp/env_override")
  141. }
  142. })
  143. Convey("instance_name default to hostname even if hostname env is empty", func() {
  144. cfg := NewCfg()
  145. cfg.Load(&CommandLineArgs{
  146. HomePath: "../../",
  147. })
  148. hostname, _ := os.Hostname()
  149. So(InstanceName, ShouldEqual, hostname)
  150. })
  151. Convey("Reading callback_url should add trailing slash", func() {
  152. cfg := NewCfg()
  153. cfg.Load(&CommandLineArgs{
  154. HomePath: "../../",
  155. Args: []string{"cfg:rendering.callback_url=http://myserver/renderer"},
  156. })
  157. So(cfg.RendererCallbackUrl, ShouldEqual, "http://myserver/renderer/")
  158. })
  159. })
  160. Convey("Test reading string values from .ini file", t, func() {
  161. iniFile, err := ini.Load(path.Join(HomePath, "pkg/setting/testdata/invalid.ini"))
  162. So(err, ShouldBeNil)
  163. Convey("If key is found - should return value from ini file", func() {
  164. value, err := valueAsString(iniFile.Section("server"), "alt_url", "")
  165. So(err, ShouldBeNil)
  166. So(value, ShouldEqual, "https://grafana.com/")
  167. })
  168. Convey("If key is not found - should return default value", func() {
  169. value, err := valueAsString(iniFile.Section("server"), "extra_url", "default_url_val")
  170. So(err, ShouldBeNil)
  171. So(value, ShouldEqual, "default_url_val")
  172. })
  173. Convey("In case of panic - should return user-friendly error", func() {
  174. value, err := valueAsString(iniFile.Section("server"), "root_url", "")
  175. So(err.Error(), ShouldEqual, "Invalid value for key 'root_url' in configuration file")
  176. So(value, ShouldEqual, "")
  177. })
  178. })
  179. }