setting_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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{"cfg:default.paths.data=/tmp/data"},
  39. })
  40. So(DataPath, ShouldEqual, "/tmp/data")
  41. })
  42. Convey("Defaults can be overriden in specified config file", func() {
  43. NewConfigContext(&CommandLineArgs{
  44. Args: []string{"cfg:default.paths.data=/tmp/data"},
  45. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  46. })
  47. So(DataPath, ShouldEqual, "/tmp/override")
  48. })
  49. Convey("Command line overrides specified config file", func() {
  50. NewConfigContext(&CommandLineArgs{
  51. Args: []string{"cfg:paths.data=/tmp/data"},
  52. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  53. })
  54. So(DataPath, ShouldEqual, "/tmp/data")
  55. })
  56. Convey("Can use environment variables in config values", func() {
  57. os.Setenv("GF_DATA_PATH", "/tmp/env_override")
  58. NewConfigContext(&CommandLineArgs{
  59. Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
  60. })
  61. So(DataPath, ShouldEqual, "/tmp/env_override")
  62. })
  63. })
  64. }