setting_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. Convey("Given the default ini files", func() {
  11. NewConfigContext(&CommandLineArgs{HomePath: "../../"})
  12. So(AppName, ShouldEqual, "Grafana")
  13. So(AdminUser, ShouldEqual, "admin")
  14. })
  15. Convey("Should be able to override via environment variables", func() {
  16. os.Setenv("GF_SECURITY_ADMIN_USER", "superduper")
  17. NewConfigContext(&CommandLineArgs{HomePath: "../../"})
  18. So(AdminUser, ShouldEqual, "superduper")
  19. So(DataPath, ShouldEqual, filepath.Join(HomePath, "data"))
  20. So(LogsPath, ShouldEqual, filepath.Join(DataPath, "log"))
  21. })
  22. Convey("Should get property map from command line args array", func() {
  23. props := getCommandLineProperties([]string{"cfg:test=value", "cfg:map.test=1"})
  24. So(len(props), ShouldEqual, 2)
  25. So(props["test"], ShouldEqual, "value")
  26. So(props["map.test"], ShouldEqual, "1")
  27. })
  28. Convey("Should be able to override via command line", func() {
  29. NewConfigContext(&CommandLineArgs{
  30. HomePath: "../../",
  31. Args: []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"},
  32. })
  33. So(DataPath, ShouldEqual, "/tmp/data")
  34. So(LogsPath, ShouldEqual, "/tmp/logs")
  35. })
  36. Convey("Should be able to override defaults via command line", func() {
  37. NewConfigContext(&CommandLineArgs{
  38. HomePath: "../../",
  39. Args: []string{
  40. "cfg:default.server.domain=test2",
  41. },
  42. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  43. })
  44. So(Domain, ShouldEqual, "test2")
  45. })
  46. Convey("Defaults can be overriden in specified config file", func() {
  47. NewConfigContext(&CommandLineArgs{
  48. HomePath: "../../",
  49. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  50. Args: []string{"cfg:default.paths.data=/tmp/data"},
  51. })
  52. So(DataPath, ShouldEqual, "/tmp/override")
  53. })
  54. Convey("Command line overrides specified config file", func() {
  55. NewConfigContext(&CommandLineArgs{
  56. HomePath: "../../",
  57. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  58. Args: []string{"cfg:paths.data=/tmp/data"},
  59. })
  60. So(DataPath, ShouldEqual, "/tmp/data")
  61. })
  62. Convey("Can use environment variables in config values", func() {
  63. os.Setenv("GF_DATA_PATH", "/tmp/env_override")
  64. NewConfigContext(&CommandLineArgs{
  65. HomePath: "../../",
  66. Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
  67. })
  68. So(DataPath, ShouldEqual, "/tmp/env_override")
  69. })
  70. })
  71. }