Chore: Refactor GoConvey in setting package (#40861)

This commit is contained in:
Serge Zaitsev 2021-10-26 13:19:07 +02:00 committed by GitHub
parent 16046f6d43
commit 22b428836e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 279 additions and 284 deletions

View File

@ -5,7 +5,8 @@ import (
"testing" "testing"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/require"
) )
type testLogger struct { type testLogger struct {
@ -24,10 +25,9 @@ func (stub *testLogger) Info(testMessage string, ctx ...interface{}) {
} }
func TestSessionSettings(t *testing.T) { func TestSessionSettings(t *testing.T) {
Convey("session config", t, func() {
skipStaticRootValidation = true skipStaticRootValidation = true
Convey("Reading session should log error ", func() { t.Run("Reading session should log error ", func(t *testing.T) {
cfg := NewCfg() cfg := NewCfg()
homePath := "../../" homePath := "../../"
@ -38,10 +38,9 @@ func TestSessionSettings(t *testing.T) {
HomePath: homePath, HomePath: homePath,
Config: filepath.Join(homePath, "pkg/setting/testdata/session.ini"), Config: filepath.Join(homePath, "pkg/setting/testdata/session.ini"),
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(stub.warnCalled, ShouldEqual, true) require.Equal(t, true, stub.warnCalled)
So(len(stub.warnMessage), ShouldBeGreaterThan, 0) require.Greater(t, len(stub.warnMessage), 0)
})
}) })
} }

View File

@ -15,8 +15,6 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
. "github.com/smartystreets/goconvey/convey"
) )
const ( const (
@ -24,26 +22,25 @@ const (
) )
func TestLoadingSettings(t *testing.T) { 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() { t.Run("Given the default ini files", func(t *testing.T) {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{HomePath: "../../", Config: "../../conf/defaults.ini"}) err := cfg.Load(CommandLineArgs{HomePath: "../../", Config: "../../conf/defaults.ini"})
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.AdminUser, ShouldEqual, "admin") require.Equal(t, "admin", cfg.AdminUser)
So(cfg.RendererCallbackUrl, ShouldEqual, "http://localhost:3000/") require.Equal(t, "http://localhost:3000/", cfg.RendererCallbackUrl)
}) })
Convey("default.ini should have no semi-colon commented entries", func() { t.Run("default.ini should have no semi-colon commented entries", func(t *testing.T) {
file, err := os.Open("../../conf/defaults.ini") file, err := os.Open("../../conf/defaults.ini")
if err != nil { if err != nil {
t.Errorf("failed to load defaults.ini file: %v", err) t.Errorf("failed to load defaults.ini file: %v", err)
} }
defer func() { defer func() {
err := file.Close() err := file.Close()
So(err, ShouldBeNil) require.Nil(t, err)
}() }()
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
@ -55,83 +52,83 @@ func TestLoadingSettings(t *testing.T) {
} }
}) })
Convey("sample.ini should load successfully", func() { t.Run("sample.ini should load successfully", func(t *testing.T) {
customInitPath := CustomInitPath customInitPath := CustomInitPath
CustomInitPath = "conf/sample.ini" CustomInitPath = "conf/sample.ini"
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{HomePath: "../../"}) err := cfg.Load(CommandLineArgs{HomePath: "../../"})
So(err, ShouldBeNil) require.Nil(t, err)
// Restore CustomInitPath to avoid side effects. // Restore CustomInitPath to avoid side effects.
CustomInitPath = customInitPath CustomInitPath = customInitPath
}) })
Convey("Should be able to override via environment variables", func() { t.Run("Should be able to override via environment variables", func(t *testing.T) {
err := os.Setenv("GF_SECURITY_ADMIN_USER", "superduper") err := os.Setenv("GF_SECURITY_ADMIN_USER", "superduper")
require.NoError(t, err) require.NoError(t, err)
cfg := NewCfg() cfg := NewCfg()
err = cfg.Load(CommandLineArgs{HomePath: "../../"}) err = cfg.Load(CommandLineArgs{HomePath: "../../"})
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.AdminUser, ShouldEqual, "superduper") require.Equal(t, "superduper", cfg.AdminUser)
So(cfg.DataPath, ShouldEqual, filepath.Join(HomePath, "data")) require.Equal(t, filepath.Join(HomePath, "data"), cfg.DataPath)
So(cfg.LogsPath, ShouldEqual, filepath.Join(cfg.DataPath, "log")) require.Equal(t, filepath.Join(cfg.DataPath, "log"), cfg.LogsPath)
}) })
Convey("Should replace password when defined in environment", func() { t.Run("Should replace password when defined in environment", func(t *testing.T) {
err := os.Setenv("GF_SECURITY_ADMIN_PASSWORD", "supersecret") err := os.Setenv("GF_SECURITY_ADMIN_PASSWORD", "supersecret")
require.NoError(t, err) require.NoError(t, err)
cfg := NewCfg() cfg := NewCfg()
err = cfg.Load(CommandLineArgs{HomePath: "../../"}) err = cfg.Load(CommandLineArgs{HomePath: "../../"})
So(err, ShouldBeNil) require.Nil(t, err)
So(appliedEnvOverrides, ShouldContain, "GF_SECURITY_ADMIN_PASSWORD=*********") require.Contains(t, appliedEnvOverrides, "GF_SECURITY_ADMIN_PASSWORD=*********")
}) })
Convey("Should replace password in URL when url environment is defined", func() { 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") err := os.Setenv("GF_DATABASE_URL", "mysql://user:secret@localhost:3306/database")
require.NoError(t, err) require.NoError(t, err)
cfg := NewCfg() cfg := NewCfg()
err = cfg.Load(CommandLineArgs{HomePath: "../../"}) err = cfg.Load(CommandLineArgs{HomePath: "../../"})
So(err, ShouldBeNil) require.Nil(t, err)
So(appliedEnvOverrides, ShouldContain, "GF_DATABASE_URL=mysql://user:xxxxx@localhost:3306/database") require.Contains(t, appliedEnvOverrides, "GF_DATABASE_URL=mysql://user:xxxxx@localhost:3306/database")
}) })
Convey("Should get property map from command line args array", func() { 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"}) props := getCommandLineProperties([]string{"cfg:test=value", "cfg:map.test=1"})
So(len(props), ShouldEqual, 2) require.Equal(t, 2, len(props))
So(props["test"], ShouldEqual, "value") require.Equal(t, "value", props["test"])
So(props["map.test"], ShouldEqual, "1") require.Equal(t, "1", props["map.test"])
}) })
Convey("Should be able to override via command line", func() { t.Run("Should be able to override via command line", func(t *testing.T) {
if runtime.GOOS == windows { if runtime.GOOS == windows {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{ err := cfg.Load(CommandLineArgs{
HomePath: "../../", HomePath: "../../",
Args: []string{`cfg:paths.data=c:\tmp\data`, `cfg:paths.logs=c:\tmp\logs`}, Args: []string{`cfg:paths.data=c:\tmp\data`, `cfg:paths.logs=c:\tmp\logs`},
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.DataPath, ShouldEqual, `c:\tmp\data`) require.Equal(t, `c:\tmp\data`, cfg.DataPath)
So(cfg.LogsPath, ShouldEqual, `c:\tmp\logs`) require.Equal(t, `c:\tmp\logs`, cfg.LogsPath)
} else { } else {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{ err := cfg.Load(CommandLineArgs{
HomePath: "../../", HomePath: "../../",
Args: []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"}, Args: []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"},
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.DataPath, ShouldEqual, "/tmp/data") require.Equal(t, "/tmp/data", cfg.DataPath)
So(cfg.LogsPath, ShouldEqual, "/tmp/logs") require.Equal(t, "/tmp/logs", cfg.LogsPath)
} }
}) })
Convey("Should be able to override defaults via command line", func() { t.Run("Should be able to override defaults via command line", func(t *testing.T) {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{ err := cfg.Load(CommandLineArgs{
HomePath: "../../", HomePath: "../../",
@ -140,12 +137,12 @@ func TestLoadingSettings(t *testing.T) {
}, },
Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"), Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"),
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.Domain, ShouldEqual, "test2") require.Equal(t, "test2", cfg.Domain)
}) })
Convey("Defaults can be overridden in specified config file", func() { t.Run("Defaults can be overridden in specified config file", func(t *testing.T) {
if runtime.GOOS == windows { if runtime.GOOS == windows {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{ err := cfg.Load(CommandLineArgs{
@ -153,9 +150,9 @@ func TestLoadingSettings(t *testing.T) {
Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"), Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"),
Args: []string{`cfg:default.paths.data=c:\tmp\data`}, Args: []string{`cfg:default.paths.data=c:\tmp\data`},
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.DataPath, ShouldEqual, `c:\tmp\override`) require.Equal(t, `c:\tmp\override`, cfg.DataPath)
} else { } else {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{ err := cfg.Load(CommandLineArgs{
@ -163,13 +160,13 @@ func TestLoadingSettings(t *testing.T) {
Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"), Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"),
Args: []string{"cfg:default.paths.data=/tmp/data"}, Args: []string{"cfg:default.paths.data=/tmp/data"},
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.DataPath, ShouldEqual, "/tmp/override") require.Equal(t, "/tmp/override", cfg.DataPath)
} }
}) })
Convey("Command line overrides specified config file", func() { t.Run("Command line overrides specified config file", func(t *testing.T) {
if runtime.GOOS == windows { if runtime.GOOS == windows {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{ err := cfg.Load(CommandLineArgs{
@ -177,9 +174,9 @@ func TestLoadingSettings(t *testing.T) {
Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"), Config: filepath.Join(HomePath, "pkg/setting/testdata/override_windows.ini"),
Args: []string{`cfg:paths.data=c:\tmp\data`}, Args: []string{`cfg:paths.data=c:\tmp\data`},
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.DataPath, ShouldEqual, `c:\tmp\data`) require.Equal(t, `c:\tmp\data`, cfg.DataPath)
} else { } else {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{ err := cfg.Load(CommandLineArgs{
@ -187,13 +184,13 @@ func TestLoadingSettings(t *testing.T) {
Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"), Config: filepath.Join(HomePath, "pkg/setting/testdata/override.ini"),
Args: []string{"cfg:paths.data=/tmp/data"}, Args: []string{"cfg:paths.data=/tmp/data"},
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.DataPath, ShouldEqual, "/tmp/data") require.Equal(t, "/tmp/data", cfg.DataPath)
} }
}) })
Convey("Can use environment variables in config values", func() { t.Run("Can use environment variables in config values", func(t *testing.T) {
if runtime.GOOS == windows { if runtime.GOOS == windows {
err := os.Setenv("GF_DATA_PATH", `c:\tmp\env_override`) err := os.Setenv("GF_DATA_PATH", `c:\tmp\env_override`)
require.NoError(t, err) require.NoError(t, err)
@ -202,9 +199,9 @@ func TestLoadingSettings(t *testing.T) {
HomePath: "../../", HomePath: "../../",
Args: []string{"cfg:paths.data=${GF_DATA_PATH}"}, Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.DataPath, ShouldEqual, `c:\tmp\env_override`) require.Equal(t, `c:\tmp\env_override`, cfg.DataPath)
} else { } else {
err := os.Setenv("GF_DATA_PATH", "/tmp/env_override") err := os.Setenv("GF_DATA_PATH", "/tmp/env_override")
require.NoError(t, err) require.NoError(t, err)
@ -213,92 +210,91 @@ func TestLoadingSettings(t *testing.T) {
HomePath: "../../", HomePath: "../../",
Args: []string{"cfg:paths.data=${GF_DATA_PATH}"}, Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.DataPath, ShouldEqual, "/tmp/env_override") require.Equal(t, "/tmp/env_override", cfg.DataPath)
} }
}) })
Convey("instance_name default to hostname even if hostname env is empty", func() { t.Run("instance_name default to hostname even if hostname env is empty", func(t *testing.T) {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{ err := cfg.Load(CommandLineArgs{
HomePath: "../../", HomePath: "../../",
}) })
So(err, ShouldBeNil) require.Nil(t, err)
hostname, err := os.Hostname() hostname, err := os.Hostname()
So(err, ShouldBeNil) require.Nil(t, err)
So(InstanceName, ShouldEqual, hostname) require.Equal(t, hostname, InstanceName)
}) })
Convey("Reading callback_url should add trailing slash", func() { t.Run("Reading callback_url should add trailing slash", func(t *testing.T) {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{ err := cfg.Load(CommandLineArgs{
HomePath: "../../", HomePath: "../../",
Args: []string{"cfg:rendering.callback_url=http://myserver/renderer"}, Args: []string{"cfg:rendering.callback_url=http://myserver/renderer"},
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.RendererCallbackUrl, ShouldEqual, "http://myserver/renderer/") require.Equal(t, "http://myserver/renderer/", cfg.RendererCallbackUrl)
}) })
Convey("Only sync_ttl should return the value sync_ttl", func() { t.Run("Only sync_ttl should return the value sync_ttl", func(t *testing.T) {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{ err := cfg.Load(CommandLineArgs{
HomePath: "../../", HomePath: "../../",
Args: []string{"cfg:auth.proxy.sync_ttl=2"}, Args: []string{"cfg:auth.proxy.sync_ttl=2"},
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.AuthProxySyncTTL, ShouldEqual, 2) require.Equal(t, 2, cfg.AuthProxySyncTTL)
}) })
Convey("Only ldap_sync_ttl should return the value ldap_sync_ttl", func() { t.Run("Only ldap_sync_ttl should return the value ldap_sync_ttl", func(t *testing.T) {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{ err := cfg.Load(CommandLineArgs{
HomePath: "../../", HomePath: "../../",
Args: []string{"cfg:auth.proxy.ldap_sync_ttl=5"}, Args: []string{"cfg:auth.proxy.ldap_sync_ttl=5"},
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.AuthProxySyncTTL, ShouldEqual, 5) require.Equal(t, 5, cfg.AuthProxySyncTTL)
}) })
Convey("ldap_sync should override ldap_sync_ttl that is default value", func() { t.Run("ldap_sync should override ldap_sync_ttl that is default value", func(t *testing.T) {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{ err := cfg.Load(CommandLineArgs{
HomePath: "../../", HomePath: "../../",
Args: []string{"cfg:auth.proxy.sync_ttl=5"}, Args: []string{"cfg:auth.proxy.sync_ttl=5"},
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.AuthProxySyncTTL, ShouldEqual, 5) require.Equal(t, 5, cfg.AuthProxySyncTTL)
}) })
Convey("ldap_sync should not override ldap_sync_ttl that is different from default value", func() { t.Run("ldap_sync should not override ldap_sync_ttl that is different from default value", func(t *testing.T) {
cfg := NewCfg() cfg := NewCfg()
err := cfg.Load(CommandLineArgs{ err := cfg.Load(CommandLineArgs{
HomePath: "../../", HomePath: "../../",
Args: []string{"cfg:auth.proxy.ldap_sync_ttl=12", "cfg:auth.proxy.sync_ttl=5"}, Args: []string{"cfg:auth.proxy.ldap_sync_ttl=12", "cfg:auth.proxy.sync_ttl=5"},
}) })
So(err, ShouldBeNil) require.Nil(t, err)
So(cfg.AuthProxySyncTTL, ShouldEqual, 12) 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")) 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", "") 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") value := valueAsString(iniFile.Section("server"), "extra_url", "default_url_val")
So(value, ShouldEqual, "default_url_val") require.Equal(t, "default_url_val", value)
}) })
}) })
} }