setting_test.go 4.9 KB

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