setting_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 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. HomePath: "../../",
  32. Args: []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"},
  33. })
  34. So(DataPath, ShouldEqual, "/tmp/data")
  35. So(LogsPath, ShouldEqual, "/tmp/logs")
  36. })
  37. Convey("Should be able to override defaults via command line", func() {
  38. NewConfigContext(&CommandLineArgs{
  39. HomePath: "../../",
  40. Args: []string{
  41. "cfg:default.server.domain=test2",
  42. },
  43. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  44. })
  45. So(Domain, ShouldEqual, "test2")
  46. })
  47. Convey("Defaults can be overriden in specified config file", func() {
  48. NewConfigContext(&CommandLineArgs{
  49. HomePath: "../../",
  50. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  51. Args: []string{"cfg:default.paths.data=/tmp/data"},
  52. })
  53. So(DataPath, ShouldEqual, "/tmp/override")
  54. })
  55. Convey("Command line overrides specified config file", func() {
  56. NewConfigContext(&CommandLineArgs{
  57. HomePath: "../../",
  58. Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
  59. Args: []string{"cfg:paths.data=/tmp/data"},
  60. })
  61. So(DataPath, ShouldEqual, "/tmp/data")
  62. })
  63. Convey("Can use environment variables in config values", func() {
  64. os.Setenv("GF_DATA_PATH", "/tmp/env_override")
  65. NewConfigContext(&CommandLineArgs{
  66. HomePath: "../../",
  67. Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
  68. })
  69. So(DataPath, ShouldEqual, "/tmp/env_override")
  70. })
  71. Convey("instance_name default to hostname even if hostname env is emtpy", func() {
  72. NewConfigContext(&CommandLineArgs{
  73. HomePath: "../../",
  74. })
  75. hostname, _ := os.Hostname()
  76. So(InstanceName, ShouldEqual, hostname)
  77. })
  78. })
  79. }