diff options
author | Harrison Healey <harrisonmhealey@gmail.com> | 2018-04-06 12:17:43 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-06 12:17:43 -0400 |
commit | ff077c6761bd4b6d170831f7f2ba474c2a9bd5e0 (patch) | |
tree | 3c3236165a8df4d273bd57ded31a8aca3c8fcd3e /utils/config_test.go | |
parent | f9015a37f3f3ffe9dac9d3c3a44c7795b8b8b8b0 (diff) | |
download | chat-ff077c6761bd4b6d170831f7f2ba474c2a9bd5e0.tar.gz chat-ff077c6761bd4b6d170831f7f2ba474c2a9bd5e0.tar.bz2 chat-ff077c6761bd4b6d170831f7f2ba474c2a9bd5e0.zip |
MM-8400 Provide default config values to viper so that it reads all environment variables (#8581)
* MM-8400 Provide default config values to viper so that it reads all environment variables
* Added unit tests
Diffstat (limited to 'utils/config_test.go')
-rw-r--r-- | utils/config_test.go | 98 |
1 files changed, 65 insertions, 33 deletions
diff --git a/utils/config_test.go b/utils/config_test.go index 84e7291b0..a998bfbc6 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -50,48 +50,80 @@ func TestFindConfigFile(t *testing.T) { } func TestConfigFromEnviroVars(t *testing.T) { - os.Setenv("MM_TEAMSETTINGS_SITENAME", "From Environment") - os.Setenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT", "Custom Brand") - os.Setenv("MM_SERVICESETTINGS_ENABLECOMMANDS", "false") - os.Setenv("MM_SERVICESETTINGS_READTIMEOUT", "400") - TranslationsPreInit() - cfg, cfgPath, err := LoadConfig("config.json") - require.Nil(t, err) - if cfg.TeamSettings.SiteName != "From Environment" { - t.Fatal("Couldn't read config from environment var") - } + config := `{ + "ServiceSettings": { + "EnableCommands": true, + "ReadTimeout": 100 + }, + "TeamSettings": { + "SiteName": "Mattermost", + "CustomBrandText": "" + } + }` - if *cfg.TeamSettings.CustomBrandText != "Custom Brand" { - t.Fatal("Couldn't read config from environment var") - } + t.Run("string settings", func(t *testing.T) { + os.Setenv("MM_TEAMSETTINGS_SITENAME", "From Environment") + os.Setenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT", "Custom Brand") - if *cfg.ServiceSettings.EnableCommands { - t.Fatal("Couldn't read config from environment var") - } + cfg, err := ReadConfig(strings.NewReader(config), true) + require.Nil(t, err) - if *cfg.ServiceSettings.ReadTimeout != 400 { - t.Fatal("Couldn't read config from environment var") - } + if cfg.TeamSettings.SiteName != "From Environment" { + t.Fatal("Couldn't read config from environment var") + } - os.Unsetenv("MM_TEAMSETTINGS_SITENAME") - os.Unsetenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT") - os.Unsetenv("MM_SERVICESETTINGS_ENABLECOMMANDS") - os.Unsetenv("MM_SERVICESETTINGS_READTIMEOUT") + if *cfg.TeamSettings.CustomBrandText != "Custom Brand" { + t.Fatal("Couldn't read config from environment var") + } - cfg.TeamSettings.SiteName = "Mattermost" - *cfg.ServiceSettings.SiteURL = "" - *cfg.ServiceSettings.EnableCommands = true - *cfg.ServiceSettings.ReadTimeout = 300 - SaveConfig(cfgPath, cfg) + os.Unsetenv("MM_TEAMSETTINGS_SITENAME") + os.Unsetenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT") - cfg, _, err = LoadConfig("config.json") - require.Nil(t, err) + cfg, err = ReadConfig(strings.NewReader(config), true) + require.Nil(t, err) - if cfg.TeamSettings.SiteName != "Mattermost" { - t.Fatal("should have been reset") - } + if cfg.TeamSettings.SiteName != "Mattermost" { + t.Fatal("should have been reset") + } + }) + + t.Run("boolean setting", func(t *testing.T) { + os.Setenv("MM_SERVICESETTINGS_ENABLECOMMANDS", "false") + defer os.Unsetenv("MM_SERVICESETTINGS_ENABLECOMMANDS") + + cfg, err := ReadConfig(strings.NewReader(config), true) + require.Nil(t, err) + + if *cfg.ServiceSettings.EnableCommands { + t.Fatal("Couldn't read config from environment var") + } + }) + + t.Run("integer setting", func(t *testing.T) { + os.Setenv("MM_SERVICESETTINGS_READTIMEOUT", "400") + defer os.Unsetenv("MM_SERVICESETTINGS_READTIMEOUT") + + cfg, err := ReadConfig(strings.NewReader(config), true) + require.Nil(t, err) + + if *cfg.ServiceSettings.ReadTimeout != 400 { + t.Fatal("Couldn't read config from environment var") + } + }) + + t.Run("setting missing from config.json", func(t *testing.T) { + os.Setenv("MM_SERVICESETTINGS_SITEURL", "https://example.com") + defer os.Unsetenv("MM_SERVICESETTINGS_SITEURL") + + cfg, err := ReadConfig(strings.NewReader(config), true) + require.Nil(t, err) + + if *cfg.ServiceSettings.SiteURL != "https://example.com" { + t.Fatal("Couldn't read config from environment var") + } + }) } func TestValidateLocales(t *testing.T) { |