Initial Baby Step to refactoring settings from global vars to instance (#11777)
* wip: start on refactoring settings * settings: progress on settings refactor * refactor: progress on settings refactoring * fix: fixed failing test * settings: moved smtp settings from global to instance
This commit is contained in:
+34
-15
@@ -15,7 +15,8 @@ func TestLoadingSettings(t *testing.T) {
|
||||
skipStaticRootValidation = true
|
||||
|
||||
Convey("Given the default ini files", func() {
|
||||
err := NewConfigContext(&CommandLineArgs{HomePath: "../../"})
|
||||
cfg := NewCfg()
|
||||
err := cfg.Load(&CommandLineArgs{HomePath: "../../"})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(AdminUser, ShouldEqual, "admin")
|
||||
@@ -23,7 +24,9 @@ func TestLoadingSettings(t *testing.T) {
|
||||
|
||||
Convey("Should be able to override via environment variables", func() {
|
||||
os.Setenv("GF_SECURITY_ADMIN_USER", "superduper")
|
||||
NewConfigContext(&CommandLineArgs{HomePath: "../../"})
|
||||
|
||||
cfg := NewCfg()
|
||||
cfg.Load(&CommandLineArgs{HomePath: "../../"})
|
||||
|
||||
So(AdminUser, ShouldEqual, "superduper")
|
||||
So(DataPath, ShouldEqual, filepath.Join(HomePath, "data"))
|
||||
@@ -32,21 +35,27 @@ func TestLoadingSettings(t *testing.T) {
|
||||
|
||||
Convey("Should replace password when defined in environment", func() {
|
||||
os.Setenv("GF_SECURITY_ADMIN_PASSWORD", "supersecret")
|
||||
NewConfigContext(&CommandLineArgs{HomePath: "../../"})
|
||||
|
||||
cfg := NewCfg()
|
||||
cfg.Load(&CommandLineArgs{HomePath: "../../"})
|
||||
|
||||
So(appliedEnvOverrides, ShouldContain, "GF_SECURITY_ADMIN_PASSWORD=*********")
|
||||
})
|
||||
|
||||
Convey("Should return an error when url is invalid", func() {
|
||||
os.Setenv("GF_DATABASE_URL", "postgres.%31://grafana:secret@postgres:5432/grafana")
|
||||
err := NewConfigContext(&CommandLineArgs{HomePath: "../../"})
|
||||
|
||||
cfg := NewCfg()
|
||||
err := cfg.Load(&CommandLineArgs{HomePath: "../../"})
|
||||
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("Should replace password in URL when url environment is defined", func() {
|
||||
os.Setenv("GF_DATABASE_URL", "mysql://user:secret@localhost:3306/database")
|
||||
NewConfigContext(&CommandLineArgs{HomePath: "../../"})
|
||||
|
||||
cfg := NewCfg()
|
||||
cfg.Load(&CommandLineArgs{HomePath: "../../"})
|
||||
|
||||
So(appliedEnvOverrides, ShouldContain, "GF_DATABASE_URL=mysql://user:-redacted-@localhost:3306/database")
|
||||
})
|
||||
@@ -61,14 +70,16 @@ func TestLoadingSettings(t *testing.T) {
|
||||
|
||||
Convey("Should be able to override via command line", func() {
|
||||
if runtime.GOOS == "windows" {
|
||||
NewConfigContext(&CommandLineArgs{
|
||||
cfg := NewCfg()
|
||||
cfg.Load(&CommandLineArgs{
|
||||
HomePath: "../../",
|
||||
Args: []string{`cfg:paths.data=c:\tmp\data`, `cfg:paths.logs=c:\tmp\logs`},
|
||||
})
|
||||
So(DataPath, ShouldEqual, `c:\tmp\data`)
|
||||
So(LogsPath, ShouldEqual, `c:\tmp\logs`)
|
||||
} else {
|
||||
NewConfigContext(&CommandLineArgs{
|
||||
cfg := NewCfg()
|
||||
cfg.Load(&CommandLineArgs{
|
||||
HomePath: "../../",
|
||||
Args: []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"},
|
||||
})
|
||||
@@ -79,7 +90,8 @@ func TestLoadingSettings(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Should be able to override defaults via command line", func() {
|
||||
NewConfigContext(&CommandLineArgs{
|
||||
cfg := NewCfg()
|
||||
cfg.Load(&CommandLineArgs{
|
||||
HomePath: "../../",
|
||||
Args: []string{
|
||||
"cfg:default.server.domain=test2",
|
||||
@@ -92,7 +104,8 @@ func TestLoadingSettings(t *testing.T) {
|
||||
|
||||
Convey("Defaults can be overridden in specified config file", func() {
|
||||
if runtime.GOOS == "windows" {
|
||||
NewConfigContext(&CommandLineArgs{
|
||||
cfg := NewCfg()
|
||||
cfg.Load(&CommandLineArgs{
|
||||
HomePath: "../../",
|
||||
Config: filepath.Join(HomePath, "tests/config-files/override_windows.ini"),
|
||||
Args: []string{`cfg:default.paths.data=c:\tmp\data`},
|
||||
@@ -100,7 +113,8 @@ func TestLoadingSettings(t *testing.T) {
|
||||
|
||||
So(DataPath, ShouldEqual, `c:\tmp\override`)
|
||||
} else {
|
||||
NewConfigContext(&CommandLineArgs{
|
||||
cfg := NewCfg()
|
||||
cfg.Load(&CommandLineArgs{
|
||||
HomePath: "../../",
|
||||
Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
|
||||
Args: []string{"cfg:default.paths.data=/tmp/data"},
|
||||
@@ -112,7 +126,8 @@ func TestLoadingSettings(t *testing.T) {
|
||||
|
||||
Convey("Command line overrides specified config file", func() {
|
||||
if runtime.GOOS == "windows" {
|
||||
NewConfigContext(&CommandLineArgs{
|
||||
cfg := NewCfg()
|
||||
cfg.Load(&CommandLineArgs{
|
||||
HomePath: "../../",
|
||||
Config: filepath.Join(HomePath, "tests/config-files/override_windows.ini"),
|
||||
Args: []string{`cfg:paths.data=c:\tmp\data`},
|
||||
@@ -120,7 +135,8 @@ func TestLoadingSettings(t *testing.T) {
|
||||
|
||||
So(DataPath, ShouldEqual, `c:\tmp\data`)
|
||||
} else {
|
||||
NewConfigContext(&CommandLineArgs{
|
||||
cfg := NewCfg()
|
||||
cfg.Load(&CommandLineArgs{
|
||||
HomePath: "../../",
|
||||
Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
|
||||
Args: []string{"cfg:paths.data=/tmp/data"},
|
||||
@@ -133,7 +149,8 @@ func TestLoadingSettings(t *testing.T) {
|
||||
Convey("Can use environment variables in config values", func() {
|
||||
if runtime.GOOS == "windows" {
|
||||
os.Setenv("GF_DATA_PATH", `c:\tmp\env_override`)
|
||||
NewConfigContext(&CommandLineArgs{
|
||||
cfg := NewCfg()
|
||||
cfg.Load(&CommandLineArgs{
|
||||
HomePath: "../../",
|
||||
Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
|
||||
})
|
||||
@@ -141,7 +158,8 @@ func TestLoadingSettings(t *testing.T) {
|
||||
So(DataPath, ShouldEqual, `c:\tmp\env_override`)
|
||||
} else {
|
||||
os.Setenv("GF_DATA_PATH", "/tmp/env_override")
|
||||
NewConfigContext(&CommandLineArgs{
|
||||
cfg := NewCfg()
|
||||
cfg.Load(&CommandLineArgs{
|
||||
HomePath: "../../",
|
||||
Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
|
||||
})
|
||||
@@ -151,7 +169,8 @@ func TestLoadingSettings(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("instance_name default to hostname even if hostname env is empty", func() {
|
||||
NewConfigContext(&CommandLineArgs{
|
||||
cfg := NewCfg()
|
||||
cfg.Load(&CommandLineArgs{
|
||||
HomePath: "../../",
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user