|
|
|
|
@ -15,8 +15,6 @@ import ( |
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
|
|
|
|
|
|
"gopkg.in/ini.v1" |
|
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
|
@ -24,281 +22,279 @@ const ( |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func TestLoadingSettings(t *testing.T) { |
|
|
|
|
Convey("Testing loading settings from ini file", t, func() { |
|
|
|
|
skipStaticRootValidation = true |
|
|
|
|
skipStaticRootValidation = true |
|
|
|
|
|
|
|
|
|
Convey("Given the default ini files", func() { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{HomePath: "../../", Config: "../../conf/defaults.ini"}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
t.Run("Given the default ini files", func(t *testing.T) { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{HomePath: "../../", Config: "../../conf/defaults.ini"}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
So(cfg.AdminUser, ShouldEqual, "admin") |
|
|
|
|
So(cfg.RendererCallbackUrl, ShouldEqual, "http://localhost:3000/") |
|
|
|
|
}) |
|
|
|
|
require.Equal(t, "admin", cfg.AdminUser) |
|
|
|
|
require.Equal(t, "http://localhost:3000/", cfg.RendererCallbackUrl) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("default.ini should have no semi-colon commented entries", func() { |
|
|
|
|
file, err := os.Open("../../conf/defaults.ini") |
|
|
|
|
if err != nil { |
|
|
|
|
t.Errorf("failed to load defaults.ini file: %v", err) |
|
|
|
|
} |
|
|
|
|
defer func() { |
|
|
|
|
err := file.Close() |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file) |
|
|
|
|
for scanner.Scan() { |
|
|
|
|
// This only catches values commented out with ";" and will not catch those that are commented out with "#".
|
|
|
|
|
if strings.HasPrefix(scanner.Text(), ";") { |
|
|
|
|
t.Errorf("entries in defaults.ini must not be commented or environment variables will not work: %v", scanner.Text()) |
|
|
|
|
} |
|
|
|
|
t.Run("default.ini should have no semi-colon commented entries", func(t *testing.T) { |
|
|
|
|
file, err := os.Open("../../conf/defaults.ini") |
|
|
|
|
if err != nil { |
|
|
|
|
t.Errorf("failed to load defaults.ini file: %v", err) |
|
|
|
|
} |
|
|
|
|
defer func() { |
|
|
|
|
err := file.Close() |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file) |
|
|
|
|
for scanner.Scan() { |
|
|
|
|
// This only catches values commented out with ";" and will not catch those that are commented out with "#".
|
|
|
|
|
if strings.HasPrefix(scanner.Text(), ";") { |
|
|
|
|
t.Errorf("entries in defaults.ini must not be commented or environment variables will not work: %v", scanner.Text()) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("sample.ini should load successfully", func() { |
|
|
|
|
customInitPath := CustomInitPath |
|
|
|
|
CustomInitPath = "conf/sample.ini" |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{HomePath: "../../"}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
// Restore CustomInitPath to avoid side effects.
|
|
|
|
|
CustomInitPath = customInitPath |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("Should be able to override via environment variables", func() { |
|
|
|
|
err := os.Setenv("GF_SECURITY_ADMIN_USER", "superduper") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
t.Run("sample.ini should load successfully", func(t *testing.T) { |
|
|
|
|
customInitPath := CustomInitPath |
|
|
|
|
CustomInitPath = "conf/sample.ini" |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{HomePath: "../../"}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
// Restore CustomInitPath to avoid side effects.
|
|
|
|
|
CustomInitPath = customInitPath |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err = cfg.Load(CommandLineArgs{HomePath: "../../"}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
t.Run("Should be able to override via environment variables", func(t *testing.T) { |
|
|
|
|
err := os.Setenv("GF_SECURITY_ADMIN_USER", "superduper") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
So(cfg.AdminUser, ShouldEqual, "superduper") |
|
|
|
|
So(cfg.DataPath, ShouldEqual, filepath.Join(HomePath, "data")) |
|
|
|
|
So(cfg.LogsPath, ShouldEqual, filepath.Join(cfg.DataPath, "log")) |
|
|
|
|
}) |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err = cfg.Load(CommandLineArgs{HomePath: "../../"}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
Convey("Should replace password when defined in environment", func() { |
|
|
|
|
err := os.Setenv("GF_SECURITY_ADMIN_PASSWORD", "supersecret") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Equal(t, "superduper", cfg.AdminUser) |
|
|
|
|
require.Equal(t, filepath.Join(HomePath, "data"), cfg.DataPath) |
|
|
|
|
require.Equal(t, filepath.Join(cfg.DataPath, "log"), cfg.LogsPath) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err = cfg.Load(CommandLineArgs{HomePath: "../../"}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
t.Run("Should replace password when defined in environment", func(t *testing.T) { |
|
|
|
|
err := os.Setenv("GF_SECURITY_ADMIN_PASSWORD", "supersecret") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
So(appliedEnvOverrides, ShouldContain, "GF_SECURITY_ADMIN_PASSWORD=*********") |
|
|
|
|
}) |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err = cfg.Load(CommandLineArgs{HomePath: "../../"}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
Convey("Should replace password in URL when url environment is defined", func() { |
|
|
|
|
err := os.Setenv("GF_DATABASE_URL", "mysql://user:secret@localhost:3306/database") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Contains(t, appliedEnvOverrides, "GF_SECURITY_ADMIN_PASSWORD=*********") |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err = cfg.Load(CommandLineArgs{HomePath: "../../"}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
t.Run("Should replace password in URL when url environment is defined", func(t *testing.T) { |
|
|
|
|
err := os.Setenv("GF_DATABASE_URL", "mysql://user:secret@localhost:3306/database") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
So(appliedEnvOverrides, ShouldContain, "GF_DATABASE_URL=mysql://user:xxxxx@localhost:3306/database") |
|
|
|
|
}) |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err = cfg.Load(CommandLineArgs{HomePath: "../../"}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
Convey("Should get property map from command line args array", func() { |
|
|
|
|
props := getCommandLineProperties([]string{"cfg:test=value", "cfg:map.test=1"}) |
|
|
|
|
require.Contains(t, appliedEnvOverrides, "GF_DATABASE_URL=mysql://user:xxxxx@localhost:3306/database") |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
So(len(props), ShouldEqual, 2) |
|
|
|
|
So(props["test"], ShouldEqual, "value") |
|
|
|
|
So(props["map.test"], ShouldEqual, "1") |
|
|
|
|
}) |
|
|
|
|
t.Run("Should get property map from command line args array", func(t *testing.T) { |
|
|
|
|
props := getCommandLineProperties([]string{"cfg:test=value", "cfg:map.test=1"}) |
|
|
|
|
|
|
|
|
|
Convey("Should be able to override via command line", func() { |
|
|
|
|
if runtime.GOOS == windows { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{`cfg:paths.data=c:\tmp\data`, `cfg:paths.logs=c:\tmp\logs`}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
So(cfg.DataPath, ShouldEqual, `c:\tmp\data`) |
|
|
|
|
So(cfg.LogsPath, ShouldEqual, `c:\tmp\logs`) |
|
|
|
|
} else { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
So(cfg.DataPath, ShouldEqual, "/tmp/data") |
|
|
|
|
So(cfg.LogsPath, ShouldEqual, "/tmp/logs") |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
require.Equal(t, 2, len(props)) |
|
|
|
|
require.Equal(t, "value", props["test"]) |
|
|
|
|
require.Equal(t, "1", props["map.test"]) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("Should be able to override defaults via command line", func() { |
|
|
|
|
t.Run("Should be able to override via command line", func(t *testing.T) { |
|
|
|
|
if runtime.GOOS == windows { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{ |
|
|
|
|
"cfg:default.server.domain=test2", |
|
|
|
|
}, |
|
|
|
|
Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"), |
|
|
|
|
Args: []string{`cfg:paths.data=c:\tmp\data`, `cfg:paths.logs=c:\tmp\logs`}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
So(cfg.Domain, ShouldEqual, "test2") |
|
|
|
|
}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
require.Equal(t, `c:\tmp\data`, cfg.DataPath) |
|
|
|
|
require.Equal(t, `c:\tmp\logs`, cfg.LogsPath) |
|
|
|
|
} else { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"}, |
|
|
|
|
}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
Convey("Defaults can be overridden in specified config file", func() { |
|
|
|
|
if runtime.GOOS == windows { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"), |
|
|
|
|
Args: []string{`cfg:default.paths.data=c:\tmp\data`}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
So(cfg.DataPath, ShouldEqual, `c:\tmp\override`) |
|
|
|
|
} else { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"), |
|
|
|
|
Args: []string{"cfg:default.paths.data=/tmp/data"}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
So(cfg.DataPath, ShouldEqual, "/tmp/override") |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
require.Equal(t, "/tmp/data", cfg.DataPath) |
|
|
|
|
require.Equal(t, "/tmp/logs", cfg.LogsPath) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("Command line overrides specified config file", func() { |
|
|
|
|
if runtime.GOOS == windows { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"), |
|
|
|
|
Args: []string{`cfg:paths.data=c:\tmp\data`}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
So(cfg.DataPath, ShouldEqual, `c:\tmp\data`) |
|
|
|
|
} else { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"), |
|
|
|
|
Args: []string{"cfg:paths.data=/tmp/data"}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
So(cfg.DataPath, ShouldEqual, "/tmp/data") |
|
|
|
|
} |
|
|
|
|
t.Run("Should be able to override defaults via command line", func(t *testing.T) { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{ |
|
|
|
|
"cfg:default.server.domain=test2", |
|
|
|
|
}, |
|
|
|
|
Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"), |
|
|
|
|
}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
Convey("Can use environment variables in config values", func() { |
|
|
|
|
if runtime.GOOS == windows { |
|
|
|
|
err := os.Setenv("GF_DATA_PATH", `c:\tmp\env_override`) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err = cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:paths.data=${GF_DATA_PATH}"}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
So(cfg.DataPath, ShouldEqual, `c:\tmp\env_override`) |
|
|
|
|
} else { |
|
|
|
|
err := os.Setenv("GF_DATA_PATH", "/tmp/env_override") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err = cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:paths.data=${GF_DATA_PATH}"}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
So(cfg.DataPath, ShouldEqual, "/tmp/env_override") |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
require.Equal(t, "test2", cfg.Domain) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("instance_name default to hostname even if hostname env is empty", func() { |
|
|
|
|
t.Run("Defaults can be overridden in specified config file", func(t *testing.T) { |
|
|
|
|
if runtime.GOOS == windows { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"), |
|
|
|
|
Args: []string{`cfg:default.paths.data=c:\tmp\data`}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
hostname, err := os.Hostname() |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
So(InstanceName, ShouldEqual, hostname) |
|
|
|
|
}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
Convey("Reading callback_url should add trailing slash", func() { |
|
|
|
|
require.Equal(t, `c:\tmp\override`, cfg.DataPath) |
|
|
|
|
} else { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:rendering.callback_url=http://myserver/renderer"}, |
|
|
|
|
Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"), |
|
|
|
|
Args: []string{"cfg:default.paths.data=/tmp/data"}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
So(cfg.RendererCallbackUrl, ShouldEqual, "http://myserver/renderer/") |
|
|
|
|
}) |
|
|
|
|
require.Equal(t, "/tmp/override", cfg.DataPath) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("Only sync_ttl should return the value sync_ttl", func() { |
|
|
|
|
t.Run("Command line overrides specified config file", func(t *testing.T) { |
|
|
|
|
if runtime.GOOS == windows { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:auth.proxy.sync_ttl=2"}, |
|
|
|
|
Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"), |
|
|
|
|
Args: []string{`cfg:paths.data=c:\tmp\data`}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
So(cfg.AuthProxySyncTTL, ShouldEqual, 2) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("Only ldap_sync_ttl should return the value ldap_sync_ttl", func() { |
|
|
|
|
require.Equal(t, `c:\tmp\data`, cfg.DataPath) |
|
|
|
|
} else { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:auth.proxy.ldap_sync_ttl=5"}, |
|
|
|
|
Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"), |
|
|
|
|
Args: []string{"cfg:paths.data=/tmp/data"}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
So(cfg.AuthProxySyncTTL, ShouldEqual, 5) |
|
|
|
|
}) |
|
|
|
|
require.Equal(t, "/tmp/data", cfg.DataPath) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("ldap_sync should override ldap_sync_ttl that is default value", func() { |
|
|
|
|
t.Run("Can use environment variables in config values", func(t *testing.T) { |
|
|
|
|
if runtime.GOOS == windows { |
|
|
|
|
err := os.Setenv("GF_DATA_PATH", `c:\tmp\env_override`) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
err = cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:auth.proxy.sync_ttl=5"}, |
|
|
|
|
Args: []string{"cfg:paths.data=${GF_DATA_PATH}"}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
So(cfg.AuthProxySyncTTL, ShouldEqual, 5) |
|
|
|
|
}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
Convey("ldap_sync should not override ldap_sync_ttl that is different from default value", func() { |
|
|
|
|
require.Equal(t, `c:\tmp\env_override`, cfg.DataPath) |
|
|
|
|
} else { |
|
|
|
|
err := os.Setenv("GF_DATA_PATH", "/tmp/env_override") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
err = cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:auth.proxy.ldap_sync_ttl=12", "cfg:auth.proxy.sync_ttl=5"}, |
|
|
|
|
Args: []string{"cfg:paths.data=${GF_DATA_PATH}"}, |
|
|
|
|
}) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
require.Equal(t, "/tmp/env_override", cfg.DataPath) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("instance_name default to hostname even if hostname env is empty", func(t *testing.T) { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
hostname, err := os.Hostname() |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
require.Equal(t, hostname, InstanceName) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("Reading callback_url should add trailing slash", func(t *testing.T) { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:rendering.callback_url=http://myserver/renderer"}, |
|
|
|
|
}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
require.Equal(t, "http://myserver/renderer/", cfg.RendererCallbackUrl) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("Only sync_ttl should return the value sync_ttl", func(t *testing.T) { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:auth.proxy.sync_ttl=2"}, |
|
|
|
|
}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
require.Equal(t, 2, cfg.AuthProxySyncTTL) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("Only ldap_sync_ttl should return the value ldap_sync_ttl", func(t *testing.T) { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:auth.proxy.ldap_sync_ttl=5"}, |
|
|
|
|
}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
require.Equal(t, 5, cfg.AuthProxySyncTTL) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
So(cfg.AuthProxySyncTTL, ShouldEqual, 12) |
|
|
|
|
t.Run("ldap_sync should override ldap_sync_ttl that is default value", func(t *testing.T) { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:auth.proxy.sync_ttl=5"}, |
|
|
|
|
}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
require.Equal(t, 5, cfg.AuthProxySyncTTL) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("ldap_sync should not override ldap_sync_ttl that is different from default value", func(t *testing.T) { |
|
|
|
|
cfg := NewCfg() |
|
|
|
|
err := cfg.Load(CommandLineArgs{ |
|
|
|
|
HomePath: "../../", |
|
|
|
|
Args: []string{"cfg:auth.proxy.ldap_sync_ttl=12", "cfg:auth.proxy.sync_ttl=5"}, |
|
|
|
|
}) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
require.Equal(t, 12, cfg.AuthProxySyncTTL) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("Test reading string values from .ini file", t, func() { |
|
|
|
|
t.Run("Test reading string values from .ini file", func(t *testing.T) { |
|
|
|
|
iniFile, err := ini.Load(path.Join(HomePath, "pkg/setting/testdata/invalid.ini")) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
require.Nil(t, err) |
|
|
|
|
|
|
|
|
|
Convey("If key is found - should return value from ini file", func() { |
|
|
|
|
t.Run("If key is found - should return value from ini file", func(t *testing.T) { |
|
|
|
|
value := valueAsString(iniFile.Section("server"), "alt_url", "") |
|
|
|
|
So(value, ShouldEqual, "https://grafana.com/") |
|
|
|
|
require.Equal(t, "https://grafana.com/", value) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("If key is not found - should return default value", func() { |
|
|
|
|
t.Run("If key is not found - should return default value", func(t *testing.T) { |
|
|
|
|
value := valueAsString(iniFile.Section("server"), "extra_url", "default_url_val") |
|
|
|
|
So(value, ShouldEqual, "default_url_val") |
|
|
|
|
require.Equal(t, "default_url_val", value) |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|