setting_test.go 4.6 KB

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