setting_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. So(cfg.RendererCallbackUrl, ShouldEqual, "http://localhost:3000/")
  18. })
  19. Convey("Should be able to override via environment variables", func() {
  20. os.Setenv("GF_SECURITY_ADMIN_USER", "superduper")
  21. cfg := NewCfg()
  22. cfg.Load(&CommandLineArgs{HomePath: "../../"})
  23. So(AdminUser, ShouldEqual, "superduper")
  24. So(DataPath, ShouldEqual, filepath.Join(HomePath, "data"))
  25. So(LogsPath, ShouldEqual, filepath.Join(DataPath, "log"))
  26. })
  27. Convey("Should replace password when defined in environment", func() {
  28. os.Setenv("GF_SECURITY_ADMIN_PASSWORD", "supersecret")
  29. cfg := NewCfg()
  30. cfg.Load(&CommandLineArgs{HomePath: "../../"})
  31. So(appliedEnvOverrides, ShouldContain, "GF_SECURITY_ADMIN_PASSWORD=*********")
  32. })
  33. Convey("Should return an error when url is invalid", func() {
  34. os.Setenv("GF_DATABASE_URL", "postgres.%31://grafana:secret@postgres:5432/grafana")
  35. cfg := NewCfg()
  36. err := cfg.Load(&CommandLineArgs{HomePath: "../../"})
  37. So(err, ShouldNotBeNil)
  38. })
  39. Convey("Should replace password in URL when url environment is defined", func() {
  40. os.Setenv("GF_DATABASE_URL", "mysql://user:secret@localhost:3306/database")
  41. cfg := NewCfg()
  42. cfg.Load(&CommandLineArgs{HomePath: "../../"})
  43. So(appliedEnvOverrides, ShouldContain, "GF_DATABASE_URL=mysql://user:-redacted-@localhost:3306/database")
  44. })
  45. Convey("Should get property map from command line args array", func() {
  46. props := getCommandLineProperties([]string{"cfg:test=value", "cfg:map.test=1"})
  47. So(len(props), ShouldEqual, 2)
  48. So(props["test"], ShouldEqual, "value")
  49. So(props["map.test"], ShouldEqual, "1")
  50. })
  51. Convey("Should be able to override via command line", func() {
  52. if runtime.GOOS == "windows" {
  53. cfg := NewCfg()
  54. cfg.Load(&CommandLineArgs{
  55. HomePath: "../../",
  56. Args: []string{`cfg:paths.data=c:\tmp\data`, `cfg:paths.logs=c:\tmp\logs`},
  57. })
  58. So(DataPath, ShouldEqual, `c:\tmp\data`)
  59. So(LogsPath, ShouldEqual, `c:\tmp\logs`)
  60. } else {
  61. cfg := NewCfg()
  62. cfg.Load(&CommandLineArgs{
  63. HomePath: "../../",
  64. Args: []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"},
  65. })
  66. So(DataPath, ShouldEqual, "/tmp/data")
  67. So(LogsPath, ShouldEqual, "/tmp/logs")
  68. }
  69. })
  70. Convey("Should be able to override defaults via command line", func() {
  71. cfg := NewCfg()
  72. cfg.Load(&CommandLineArgs{
  73. HomePath: "../../",
  74. Args: []string{
  75. "cfg:default.server.domain=test2",
  76. },
  77. Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"),
  78. })
  79. So(Domain, ShouldEqual, "test2")
  80. })
  81. Convey("Defaults can be overridden in specified config file", func() {
  82. if runtime.GOOS == "windows" {
  83. cfg := NewCfg()
  84. cfg.Load(&CommandLineArgs{
  85. HomePath: "../../",
  86. Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"),
  87. Args: []string{`cfg:default.paths.data=c:\tmp\data`},
  88. })
  89. So(DataPath, ShouldEqual, `c:\tmp\override`)
  90. } else {
  91. cfg := NewCfg()
  92. cfg.Load(&CommandLineArgs{
  93. HomePath: "../../",
  94. Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"),
  95. Args: []string{"cfg:default.paths.data=/tmp/data"},
  96. })
  97. So(DataPath, ShouldEqual, "/tmp/override")
  98. }
  99. })
  100. Convey("Command line overrides specified config file", func() {
  101. if runtime.GOOS == "windows" {
  102. cfg := NewCfg()
  103. cfg.Load(&CommandLineArgs{
  104. HomePath: "../../",
  105. Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"),
  106. Args: []string{`cfg:paths.data=c:\tmp\data`},
  107. })
  108. So(DataPath, ShouldEqual, `c:\tmp\data`)
  109. } else {
  110. cfg := NewCfg()
  111. cfg.Load(&CommandLineArgs{
  112. HomePath: "../../",
  113. Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"),
  114. Args: []string{"cfg:paths.data=/tmp/data"},
  115. })
  116. So(DataPath, ShouldEqual, "/tmp/data")
  117. }
  118. })
  119. Convey("Can use environment variables in config values", func() {
  120. if runtime.GOOS == "windows" {
  121. os.Setenv("GF_DATA_PATH", `c:\tmp\env_override`)
  122. cfg := NewCfg()
  123. cfg.Load(&CommandLineArgs{
  124. HomePath: "../../",
  125. Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
  126. })
  127. So(DataPath, ShouldEqual, `c:\tmp\env_override`)
  128. } else {
  129. os.Setenv("GF_DATA_PATH", "/tmp/env_override")
  130. cfg := NewCfg()
  131. cfg.Load(&CommandLineArgs{
  132. HomePath: "../../",
  133. Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
  134. })
  135. So(DataPath, ShouldEqual, "/tmp/env_override")
  136. }
  137. })
  138. Convey("instance_name default to hostname even if hostname env is empty", func() {
  139. cfg := NewCfg()
  140. cfg.Load(&CommandLineArgs{
  141. HomePath: "../../",
  142. })
  143. hostname, _ := os.Hostname()
  144. So(InstanceName, ShouldEqual, hostname)
  145. })
  146. Convey("Reading callback_url should add trailing slash", func() {
  147. cfg := NewCfg()
  148. cfg.Load(&CommandLineArgs{
  149. HomePath: "../../",
  150. Args: []string{"cfg:rendering.callback_url=http://myserver/renderer"},
  151. })
  152. So(cfg.RendererCallbackUrl, ShouldEqual, "http://myserver/renderer/")
  153. })
  154. })
  155. }