Add ability to override config variables with env variables (#32554)

* Add ability to override config variables with env variables

* Inline checkForOverrides

* Update pkg/plugins/backendplugin/manager/plugin_settings.go

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Dimitris Sotirakis
2021-03-31 19:51:16 +03:00
committed by GitHub
parent 6a3faad0b0
commit d42bfedd9e
2 changed files with 59 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package manager
import (
"os"
"sort"
"testing"
@ -41,5 +42,55 @@ func TestPluginSettings(t *testing.T) {
require.Len(t, env, 3)
require.EqualValues(t, []string{"GF_PLUGIN_KEY1=value1", "GF_PLUGIN_KEY2=value2", "GF_VERSION=6.7.0"}, env)
})
t.Run("Should override config variable with environment variable ", func(t *testing.T) {
_ = os.Setenv("GF_PLUGIN_KEY1", "sth")
t.Cleanup(func() {
_ = os.Unsetenv("GF_PLUGIN_KEY1")
})
ps := getPluginSettings("plugin", cfg)
env := ps.ToEnv("GF_PLUGIN", []string{"GF_VERSION=6.7.0"})
sort.Strings(env)
require.Len(t, env, 3)
require.EqualValues(t, []string{"GF_PLUGIN_KEY1=sth", "GF_PLUGIN_KEY2=value2", "GF_VERSION=6.7.0"}, env)
})
t.Run("Config variable doesn't match env variable ", func(t *testing.T) {
_ = os.Setenv("GF_PLUGIN_KEY3", "value3")
t.Cleanup(func() {
_ = os.Unsetenv("GF_PLUGIN_KEY3")
})
ps := getPluginSettings("plugin", cfg)
env := ps.ToEnv("GF_PLUGIN", []string{"GF_VERSION=6.7.0"})
sort.Strings(env)
require.Len(t, env, 3)
require.EqualValues(t, []string{"GF_PLUGIN_KEY1=value1", "GF_PLUGIN_KEY2=value2", "GF_VERSION=6.7.0"}, env)
})
t.Run("Should override missing config variable with environment variable ", func(t *testing.T) {
cfg := &setting.Cfg{
PluginSettings: setting.PluginSettings{
"plugin": map[string]string{
"key1": "value1",
"key2": "",
},
},
}
ps := getPluginSettings("plugin", cfg)
require.Len(t, ps, 2)
_ = os.Setenv("GF_PLUGIN_KEY2", "sth")
t.Cleanup(func() {
_ = os.Unsetenv("GF_PLUGIN_KEY1")
})
env := ps.ToEnv("GF_PLUGIN", []string{"GF_VERSION=6.7.0"})
sort.Strings(env)
require.Len(t, env, 3)
require.EqualValues(t, []string{"GF_PLUGIN_KEY1=value1", "GF_PLUGIN_KEY2=sth", "GF_VERSION=6.7.0"}, env)
})
})
}