setting_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. HomePath, _ = filepath.Abs("../../")
  10. Convey("Testing loading settings from ini file", t, func() {
  11. Convey("Given the default ini files", func() {
  12. NewConfigContext(&CommandLineArgs{})
  13. So(AppName, ShouldEqual, "Grafana")
  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{})
  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 get property map from command line args array", func() {
  24. props := getCommandLineProperties([]string{"cfg:test=value", "cfg:map.test=1"})
  25. So(len(props), ShouldEqual, 2)
  26. So(props["test"], ShouldEqual, "value")
  27. So(props["map.test"], ShouldEqual, "1")
  28. })
  29. Convey("Should be able to override via command line", func() {
  30. NewConfigContext(&CommandLineArgs{
  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. Args: []string{
  39. "cfg:default.server.domain=test2",
  40. },
  41. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  42. })
  43. So(Domain, ShouldEqual, "test2")
  44. })
  45. Convey("Defaults can be overriden in specified config file", func() {
  46. NewConfigContext(&CommandLineArgs{
  47. Args: []string{"cfg:default.paths.data=/tmp/data"},
  48. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  49. })
  50. So(DataPath, ShouldEqual, "/tmp/override")
  51. })
  52. Convey("Command line overrides specified config file", func() {
  53. NewConfigContext(&CommandLineArgs{
  54. Args: []string{"cfg:paths.data=/tmp/data"},
  55. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  56. })
  57. So(DataPath, ShouldEqual, "/tmp/data")
  58. })
  59. Convey("Can use environment variables in config values", func() {
  60. os.Setenv("GF_DATA_PATH", "/tmp/env_override")
  61. NewConfigContext(&CommandLineArgs{
  62. Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
  63. })
  64. So(DataPath, ShouldEqual, "/tmp/env_override")
  65. })
  66. })
  67. }