setting_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package setting
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestLoadingSettings(t *testing.T) {
  9. Convey("Testing loading settings from ini file", t, func() {
  10. skipStaticRootValidation = true
  11. Convey("Given the default ini files", func() {
  12. err := NewConfigContext(&CommandLineArgs{HomePath: "../../"})
  13. So(err, ShouldBeNil)
  14. So(AdminUser, ShouldEqual, "admin")
  15. })
  16. Convey("Should be able to override via environment variables", func() {
  17. os.Setenv("GF_SECURITY_ADMIN_USER", "superduper")
  18. NewConfigContext(&CommandLineArgs{HomePath: "../../"})
  19. So(AdminUser, ShouldEqual, "superduper")
  20. So(DataPath, ShouldEqual, filepath.Join(HomePath, "data"))
  21. So(LogsPath, ShouldEqual, filepath.Join(DataPath, "log"))
  22. })
  23. Convey("Should replace password when defined in environment", func() {
  24. os.Setenv("GF_SECURITY_ADMIN_PASSWORD", "supersecret")
  25. NewConfigContext(&CommandLineArgs{HomePath: "../../"})
  26. So(appliedEnvOverrides, ShouldContain, "GF_SECURITY_ADMIN_PASSWORD=*********")
  27. })
  28. Convey("Should replace password in URL when url environment is defined", func() {
  29. os.Setenv("GF_DATABASE_URL", "mysql://user:secret@localhost:3306/database")
  30. NewConfigContext(&CommandLineArgs{HomePath: "../../"})
  31. So(appliedEnvOverrides, ShouldContain, "GF_DATABASE_URL=mysql://user:-redacted-@localhost:3306/database")
  32. })
  33. Convey("Should get property map from command line args array", func() {
  34. props := getCommandLineProperties([]string{"cfg:test=value", "cfg:map.test=1"})
  35. So(len(props), ShouldEqual, 2)
  36. So(props["test"], ShouldEqual, "value")
  37. So(props["map.test"], ShouldEqual, "1")
  38. })
  39. Convey("Should be able to override via command line", func() {
  40. NewConfigContext(&CommandLineArgs{
  41. HomePath: "../../",
  42. Args: []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"},
  43. })
  44. So(DataPath, ShouldEqual, "/tmp/data")
  45. So(LogsPath, ShouldEqual, "/tmp/logs")
  46. })
  47. Convey("Should be able to override defaults via command line", func() {
  48. NewConfigContext(&CommandLineArgs{
  49. HomePath: "../../",
  50. Args: []string{
  51. "cfg:default.server.domain=test2",
  52. },
  53. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  54. })
  55. So(Domain, ShouldEqual, "test2")
  56. })
  57. Convey("Defaults can be overridden in specified config file", func() {
  58. NewConfigContext(&CommandLineArgs{
  59. HomePath: "../../",
  60. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  61. Args: []string{"cfg:default.paths.data=/tmp/data"},
  62. })
  63. So(DataPath, ShouldEqual, "/tmp/override")
  64. })
  65. Convey("Command line overrides specified config file", func() {
  66. NewConfigContext(&CommandLineArgs{
  67. HomePath: "../../",
  68. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  69. Args: []string{"cfg:paths.data=/tmp/data"},
  70. })
  71. So(DataPath, ShouldEqual, "/tmp/data")
  72. })
  73. Convey("Can use environment variables in config values", func() {
  74. os.Setenv("GF_DATA_PATH", "/tmp/env_override")
  75. NewConfigContext(&CommandLineArgs{
  76. HomePath: "../../",
  77. Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
  78. })
  79. So(DataPath, ShouldEqual, "/tmp/env_override")
  80. })
  81. Convey("instance_name default to hostname even if hostname env is empty", func() {
  82. NewConfigContext(&CommandLineArgs{
  83. HomePath: "../../",
  84. })
  85. hostname, _ := os.Hostname()
  86. So(InstanceName, ShouldEqual, hostname)
  87. })
  88. })
  89. }