grafana/pkg/setting/setting_test.go

194 lines
5.5 KiB
Go
Raw Normal View History

package setting
import (
"os"
"path/filepath"
2017-06-19 08:36:08 -05:00
"runtime"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestLoadingSettings(t *testing.T) {
Convey("Testing loading settings from ini file", t, func() {
skipStaticRootValidation = true
Convey("Given the default ini files", func() {
cfg := NewCfg()
err := cfg.Load(&CommandLineArgs{HomePath: "../../"})
So(err, ShouldBeNil)
So(AdminUser, ShouldEqual, "admin")
improve remote image rendering (#13102) * improve remote image rendering - determine "domain" during Init() so we are not re-parsing settings on every request - if using http-mode via a rednererUrl, then use the AppUrl for the page that the renderer loads. When in http-mode the renderer is likely running on another server so trying to use the localhost or even the specific IP:PORT grafana is listening on wont work. - apply the request timeout via a context rather then directly on the http client. - use a global http client so we can take advantage of connection re-use - log and handle errors better. * ensure imagesDir exists * allow users to define callback_url for remote rendering - allow users to define the url that a remote rendering service should use for connecting back to the grafana instance. By default the "root_url" is used. * improve remote image rendering - determine "domain" during Init() so we are not re-parsing settings on every request - if using http-mode via a rednererUrl, then use the AppUrl for the page that the renderer loads. When in http-mode the renderer is likely running on another server so trying to use the localhost or even the specific IP:PORT grafana is listening on wont work. - apply the request timeout via a context rather then directly on the http client. - use a global http client so we can take advantage of connection re-use - log and handle errors better. * ensure imagesDir exists * allow users to define callback_url for remote rendering - allow users to define the url that a remote rendering service should use for connecting back to the grafana instance. By default the "root_url" is used. * rendering: fixed issue with renderKey where userId and orgId was in mixed up, added test for RenderCallbackUrl reading logic
2018-09-04 06:42:55 -05:00
So(cfg.RendererCallbackUrl, ShouldEqual, "http://localhost:3000/")
})
Convey("Should be able to override via environment variables", func() {
os.Setenv("GF_SECURITY_ADMIN_USER", "superduper")
cfg := NewCfg()
cfg.Load(&CommandLineArgs{HomePath: "../../"})
So(AdminUser, ShouldEqual, "superduper")
2018-10-12 00:55:36 -05:00
So(cfg.DataPath, ShouldEqual, filepath.Join(HomePath, "data"))
So(cfg.LogsPath, ShouldEqual, filepath.Join(cfg.DataPath, "log"))
})
Convey("Should replace password when defined in environment", func() {
os.Setenv("GF_SECURITY_ADMIN_PASSWORD", "supersecret")
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")
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")
cfg := NewCfg()
cfg.Load(&CommandLineArgs{HomePath: "../../"})
So(appliedEnvOverrides, ShouldContain, "GF_DATABASE_URL=mysql://user:-redacted-@localhost:3306/database")
})
Convey("Should get property map from command line args array", func() {
props := getCommandLineProperties([]string{"cfg:test=value", "cfg:map.test=1"})
So(len(props), ShouldEqual, 2)
So(props["test"], ShouldEqual, "value")
So(props["map.test"], ShouldEqual, "1")
})
Convey("Should be able to override via command line", func() {
2017-06-19 08:36:08 -05:00
if runtime.GOOS == "windows" {
cfg := NewCfg()
cfg.Load(&CommandLineArgs{
2017-06-19 08:36:08 -05:00
HomePath: "../../",
Args: []string{`cfg:paths.data=c:\tmp\data`, `cfg:paths.logs=c:\tmp\logs`},
})
2018-10-12 00:55:36 -05:00
So(cfg.DataPath, ShouldEqual, `c:\tmp\data`)
So(cfg.LogsPath, ShouldEqual, `c:\tmp\logs`)
2017-06-19 08:36:08 -05:00
} else {
cfg := NewCfg()
cfg.Load(&CommandLineArgs{
2017-06-19 08:36:08 -05:00
HomePath: "../../",
Args: []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"},
})
2018-10-12 00:55:36 -05:00
So(cfg.DataPath, ShouldEqual, "/tmp/data")
So(cfg.LogsPath, ShouldEqual, "/tmp/logs")
2017-06-19 08:36:08 -05:00
}
})
Convey("Should be able to override defaults via command line", func() {
cfg := NewCfg()
cfg.Load(&CommandLineArgs{
HomePath: "../../",
Args: []string{
"cfg:default.server.domain=test2",
},
Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"),
})
So(Domain, ShouldEqual, "test2")
})
Convey("Defaults can be overridden in specified config file", func() {
2017-06-19 08:36:08 -05:00
if runtime.GOOS == "windows" {
cfg := NewCfg()
cfg.Load(&CommandLineArgs{
2017-06-19 08:36:08 -05:00
HomePath: "../../",
Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"),
2017-06-19 08:36:08 -05:00
Args: []string{`cfg:default.paths.data=c:\tmp\data`},
})
2018-10-12 00:55:36 -05:00
So(cfg.DataPath, ShouldEqual, `c:\tmp\override`)
2017-06-19 08:36:08 -05:00
} else {
cfg := NewCfg()
cfg.Load(&CommandLineArgs{
2017-06-19 08:36:08 -05:00
HomePath: "../../",
Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"),
2017-06-19 08:36:08 -05:00
Args: []string{"cfg:default.paths.data=/tmp/data"},
})
2018-10-12 00:55:36 -05:00
So(cfg.DataPath, ShouldEqual, "/tmp/override")
2017-06-19 08:36:08 -05:00
}
})
Convey("Command line overrides specified config file", func() {
2017-06-19 08:36:08 -05:00
if runtime.GOOS == "windows" {
cfg := NewCfg()
cfg.Load(&CommandLineArgs{
2017-06-19 08:36:08 -05:00
HomePath: "../../",
2018-09-17 10:56:52 -05:00
Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"),
2017-06-19 08:36:08 -05:00
Args: []string{`cfg:paths.data=c:\tmp\data`},
})
2018-10-12 00:55:36 -05:00
So(cfg.DataPath, ShouldEqual, `c:\tmp\data`)
2017-06-19 08:36:08 -05:00
} else {
cfg := NewCfg()
cfg.Load(&CommandLineArgs{
2017-06-19 08:36:08 -05:00
HomePath: "../../",
Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"),
2017-06-19 08:36:08 -05:00
Args: []string{"cfg:paths.data=/tmp/data"},
})
2018-10-12 00:55:36 -05:00
So(cfg.DataPath, ShouldEqual, "/tmp/data")
2017-06-19 08:36:08 -05:00
}
})
Convey("Can use environment variables in config values", func() {
2017-06-19 08:36:08 -05:00
if runtime.GOOS == "windows" {
os.Setenv("GF_DATA_PATH", `c:\tmp\env_override`)
cfg := NewCfg()
cfg.Load(&CommandLineArgs{
2017-06-19 08:36:08 -05:00
HomePath: "../../",
Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
})
2018-10-12 00:55:36 -05:00
So(cfg.DataPath, ShouldEqual, `c:\tmp\env_override`)
2017-06-19 08:36:08 -05:00
} else {
os.Setenv("GF_DATA_PATH", "/tmp/env_override")
cfg := NewCfg()
cfg.Load(&CommandLineArgs{
2017-06-19 08:36:08 -05:00
HomePath: "../../",
Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
})
2018-10-12 00:55:36 -05:00
So(cfg.DataPath, ShouldEqual, "/tmp/env_override")
2017-06-19 08:36:08 -05:00
}
})
Convey("instance_name default to hostname even if hostname env is empty", func() {
cfg := NewCfg()
cfg.Load(&CommandLineArgs{
HomePath: "../../",
})
hostname, _ := os.Hostname()
So(InstanceName, ShouldEqual, hostname)
})
improve remote image rendering (#13102) * improve remote image rendering - determine "domain" during Init() so we are not re-parsing settings on every request - if using http-mode via a rednererUrl, then use the AppUrl for the page that the renderer loads. When in http-mode the renderer is likely running on another server so trying to use the localhost or even the specific IP:PORT grafana is listening on wont work. - apply the request timeout via a context rather then directly on the http client. - use a global http client so we can take advantage of connection re-use - log and handle errors better. * ensure imagesDir exists * allow users to define callback_url for remote rendering - allow users to define the url that a remote rendering service should use for connecting back to the grafana instance. By default the "root_url" is used. * improve remote image rendering - determine "domain" during Init() so we are not re-parsing settings on every request - if using http-mode via a rednererUrl, then use the AppUrl for the page that the renderer loads. When in http-mode the renderer is likely running on another server so trying to use the localhost or even the specific IP:PORT grafana is listening on wont work. - apply the request timeout via a context rather then directly on the http client. - use a global http client so we can take advantage of connection re-use - log and handle errors better. * ensure imagesDir exists * allow users to define callback_url for remote rendering - allow users to define the url that a remote rendering service should use for connecting back to the grafana instance. By default the "root_url" is used. * rendering: fixed issue with renderKey where userId and orgId was in mixed up, added test for RenderCallbackUrl reading logic
2018-09-04 06:42:55 -05:00
Convey("Reading callback_url should add trailing slash", func() {
cfg := NewCfg()
cfg.Load(&CommandLineArgs{
HomePath: "../../",
Args: []string{"cfg:rendering.callback_url=http://myserver/renderer"},
})
So(cfg.RendererCallbackUrl, ShouldEqual, "http://myserver/renderer/")
})
})
}