setting_test.go 2.5 KB

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