2020-01-14 18:32:20 +00:00
|
|
|
package env
|
2019-12-09 17:56:02 +00:00
|
|
|
|
|
|
|
import (
|
2019-12-31 18:03:53 +00:00
|
|
|
"fmt"
|
2019-12-23 22:44:59 +00:00
|
|
|
"os"
|
2019-12-24 19:08:09 +00:00
|
|
|
"strings"
|
2019-12-09 17:56:02 +00:00
|
|
|
"testing"
|
2020-03-31 22:07:28 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
2019-12-09 17:56:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ConfigTestSuite struct {
|
|
|
|
suite.Suite
|
2019-12-23 22:44:59 +00:00
|
|
|
// These tests need to mutate the environment, so the suite.setenv and .unsetenv functions store the original contents of the
|
|
|
|
// relevant variable in this map. Its use of *string is so they can distinguish between "not set" and "set to empty string"
|
|
|
|
envBackup map[string]*string
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ConfigTestSuite))
|
|
|
|
}
|
2019-12-23 22:44:59 +00:00
|
|
|
|
2019-12-24 17:34:38 +00:00
|
|
|
func (suite *ConfigTestSuite) TestNewConfigWithPluginPrefix() {
|
2020-01-08 18:45:53 +00:00
|
|
|
suite.unsetenv("MODE")
|
2019-12-23 23:31:40 +00:00
|
|
|
suite.unsetenv("UPDATE_DEPENDENCIES")
|
|
|
|
suite.unsetenv("DEBUG")
|
|
|
|
|
2020-01-08 18:45:53 +00:00
|
|
|
suite.setenv("PLUGIN_MODE", "iambic")
|
2019-12-23 22:44:59 +00:00
|
|
|
suite.setenv("PLUGIN_UPDATE_DEPENDENCIES", "true")
|
|
|
|
suite.setenv("PLUGIN_DEBUG", "true")
|
|
|
|
|
2019-12-24 19:08:09 +00:00
|
|
|
cfg, err := NewConfig(&strings.Builder{}, &strings.Builder{})
|
2019-12-24 17:34:38 +00:00
|
|
|
suite.Require().NoError(err)
|
2019-12-23 22:44:59 +00:00
|
|
|
|
2020-01-08 18:45:53 +00:00
|
|
|
suite.Equal("iambic", cfg.Command)
|
2019-12-23 22:44:59 +00:00
|
|
|
suite.True(cfg.UpdateDependencies)
|
|
|
|
suite.True(cfg.Debug)
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:34:38 +00:00
|
|
|
func (suite *ConfigTestSuite) TestNewConfigWithNoPrefix() {
|
2020-01-08 18:45:53 +00:00
|
|
|
suite.unsetenv("PLUGIN_MODE")
|
2019-12-23 23:31:40 +00:00
|
|
|
suite.unsetenv("PLUGIN_UPDATE_DEPENDENCIES")
|
|
|
|
suite.unsetenv("PLUGIN_DEBUG")
|
|
|
|
|
2020-01-08 18:45:53 +00:00
|
|
|
suite.setenv("MODE", "iambic")
|
2019-12-23 23:31:40 +00:00
|
|
|
suite.setenv("UPDATE_DEPENDENCIES", "true")
|
|
|
|
suite.setenv("DEBUG", "true")
|
|
|
|
|
2019-12-24 19:08:09 +00:00
|
|
|
cfg, err := NewConfig(&strings.Builder{}, &strings.Builder{})
|
2019-12-24 17:34:38 +00:00
|
|
|
suite.Require().NoError(err)
|
2019-12-23 23:31:40 +00:00
|
|
|
|
2020-01-08 18:45:53 +00:00
|
|
|
suite.Equal("iambic", cfg.Command)
|
2019-12-23 23:31:40 +00:00
|
|
|
suite.True(cfg.UpdateDependencies)
|
|
|
|
suite.True(cfg.Debug)
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:34:38 +00:00
|
|
|
func (suite *ConfigTestSuite) TestNewConfigWithConflictingVariables() {
|
2020-01-08 18:45:53 +00:00
|
|
|
suite.setenv("PLUGIN_MODE", "iambic")
|
|
|
|
suite.setenv("MODE", "haiku") // values from the `environment` block override those from `settings`
|
2019-12-24 00:07:49 +00:00
|
|
|
|
2019-12-24 19:08:09 +00:00
|
|
|
cfg, err := NewConfig(&strings.Builder{}, &strings.Builder{})
|
2019-12-24 17:34:38 +00:00
|
|
|
suite.Require().NoError(err)
|
2019-12-23 23:31:40 +00:00
|
|
|
|
2020-01-08 18:45:53 +00:00
|
|
|
suite.Equal("haiku", cfg.Command)
|
2019-12-23 23:31:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 00:18:10 +00:00
|
|
|
func (suite *ConfigTestSuite) TestNewConfigInfersNumbersAreSeconds() {
|
|
|
|
suite.setenv("PLUGIN_TIMEOUT", "42")
|
|
|
|
cfg, err := NewConfig(&strings.Builder{}, &strings.Builder{})
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Equal("42s", cfg.Timeout)
|
|
|
|
}
|
|
|
|
|
2020-01-07 23:25:54 +00:00
|
|
|
func (suite *ConfigTestSuite) TestNewConfigWithAliases() {
|
|
|
|
for _, varname := range []string{
|
2020-01-08 18:45:53 +00:00
|
|
|
"MODE",
|
|
|
|
"ADD_REPOS",
|
|
|
|
"KUBE_API_SERVER",
|
|
|
|
"KUBE_SERVICE_ACCOUNT",
|
|
|
|
"WAIT_FOR_UPGRADE",
|
|
|
|
"FORCE_UPGRADE",
|
|
|
|
"KUBE_TOKEN",
|
|
|
|
"KUBE_CERTIFICATE",
|
2020-01-07 23:25:54 +00:00
|
|
|
} {
|
|
|
|
suite.unsetenv(varname)
|
|
|
|
suite.unsetenv("PLUGIN_" + varname)
|
|
|
|
}
|
2020-01-08 18:45:53 +00:00
|
|
|
suite.setenv("PLUGIN_HELM_COMMAND", "beware the jabberwock")
|
|
|
|
suite.setenv("PLUGIN_HELM_REPOS", "chortle=http://calloo.callay/frabjous/day")
|
|
|
|
suite.setenv("PLUGIN_API_SERVER", "http://tumtum.tree")
|
|
|
|
suite.setenv("PLUGIN_SERVICE_ACCOUNT", "tulgey")
|
|
|
|
suite.setenv("PLUGIN_WAIT", "true")
|
|
|
|
suite.setenv("PLUGIN_FORCE", "true")
|
|
|
|
suite.setenv("PLUGIN_KUBERNETES_TOKEN", "Y29tZSB0byBteSBhcm1z")
|
|
|
|
suite.setenv("PLUGIN_KUBERNETES_CERTIFICATE", "d2l0aCBpdHMgaGVhZA==")
|
2020-01-07 23:25:54 +00:00
|
|
|
|
|
|
|
cfg, err := NewConfig(&strings.Builder{}, &strings.Builder{})
|
|
|
|
suite.Require().NoError(err)
|
2020-01-08 18:45:53 +00:00
|
|
|
suite.Equal("beware the jabberwock", cfg.Command)
|
2020-01-07 23:25:54 +00:00
|
|
|
suite.Equal([]string{"chortle=http://calloo.callay/frabjous/day"}, cfg.AddRepos)
|
|
|
|
suite.Equal("http://tumtum.tree", cfg.APIServer)
|
|
|
|
suite.Equal("tulgey", cfg.ServiceAccount)
|
|
|
|
suite.True(cfg.Wait, "Wait should be aliased")
|
|
|
|
suite.True(cfg.Force, "Force should be aliased")
|
2020-01-07 23:39:05 +00:00
|
|
|
suite.Equal("Y29tZSB0byBteSBhcm1z", cfg.KubeToken, "KubeToken should be aliased")
|
|
|
|
suite.Equal("d2l0aCBpdHMgaGVhZA==", cfg.Certificate, "Certificate should be aliased")
|
2020-01-07 23:25:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 20:37:34 +00:00
|
|
|
func (suite *ConfigTestSuite) TestAliasedSettingWithoutPluginPrefix() {
|
|
|
|
suite.unsetenv("FORCE_UPGRADE")
|
|
|
|
suite.unsetenv("PLUGIN_FORCE_UPGRADE")
|
|
|
|
suite.unsetenv("PLUGIN_FORCE")
|
|
|
|
suite.setenv("FORCE", "true")
|
|
|
|
|
|
|
|
cfg, err := NewConfig(&strings.Builder{}, &strings.Builder{})
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.True(cfg.Force)
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:31:22 +00:00
|
|
|
func (suite *ConfigTestSuite) TestNewConfigWithAliasConflicts() {
|
2020-01-08 18:45:53 +00:00
|
|
|
suite.unsetenv("FORCE_UPGRADE")
|
|
|
|
suite.setenv("PLUGIN_FORCE", "true")
|
|
|
|
suite.setenv("PLUGIN_FORCE_UPGRADE", "false") // should override even when set to the zero value
|
2020-01-08 17:31:22 +00:00
|
|
|
|
|
|
|
cfg, err := NewConfig(&strings.Builder{}, &strings.Builder{})
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.False(cfg.Force, "official names should override alias names")
|
|
|
|
}
|
|
|
|
|
2019-12-24 19:08:09 +00:00
|
|
|
func (suite *ConfigTestSuite) TestNewConfigSetsWriters() {
|
|
|
|
stdout := &strings.Builder{}
|
|
|
|
stderr := &strings.Builder{}
|
|
|
|
cfg, err := NewConfig(stdout, stderr)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
suite.Equal(stdout, cfg.Stdout)
|
|
|
|
suite.Equal(stderr, cfg.Stderr)
|
|
|
|
}
|
|
|
|
|
2019-12-31 18:03:53 +00:00
|
|
|
func (suite *ConfigTestSuite) TestDeprecatedSettingWarnings() {
|
|
|
|
for _, varname := range deprecatedVars {
|
|
|
|
suite.setenv(varname, "deprecoat") // environment-block entries should cause warnings
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.unsetenv("PURGE")
|
|
|
|
suite.setenv("PLUGIN_PURGE", "true") // settings-block entries should cause warnings
|
|
|
|
suite.setenv("UPGRADE", "") // entries should cause warnings even when set to empty string
|
|
|
|
|
|
|
|
stderr := &strings.Builder{}
|
|
|
|
_, err := NewConfig(&strings.Builder{}, stderr)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
for _, varname := range deprecatedVars {
|
|
|
|
suite.Contains(stderr.String(), fmt.Sprintf("Warning: ignoring deprecated '%s' setting\n", strings.ToLower(varname)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-24 19:08:09 +00:00
|
|
|
func (suite *ConfigTestSuite) TestLogDebug() {
|
|
|
|
suite.setenv("DEBUG", "true")
|
2020-01-08 18:45:53 +00:00
|
|
|
suite.setenv("MODE", "upgrade")
|
2019-12-24 19:08:09 +00:00
|
|
|
|
|
|
|
stderr := strings.Builder{}
|
|
|
|
stdout := strings.Builder{}
|
|
|
|
_, err := NewConfig(&stdout, &stderr)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
suite.Equal("", stdout.String())
|
|
|
|
|
|
|
|
suite.Regexp(`^Generated config: \{Command:upgrade.*\}`, stderr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ConfigTestSuite) TestLogDebugCensorsKubeToken() {
|
|
|
|
stderr := &strings.Builder{}
|
|
|
|
kubeToken := "I'm shy! Don't put me in your build logs!"
|
|
|
|
cfg := Config{
|
|
|
|
Debug: true,
|
|
|
|
KubeToken: kubeToken,
|
|
|
|
Stderr: stderr,
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.logDebug()
|
|
|
|
|
|
|
|
suite.Contains(stderr.String(), "KubeToken:(redacted)")
|
|
|
|
suite.Equal(kubeToken, cfg.KubeToken) // The actual config value should be left unchanged
|
|
|
|
}
|
|
|
|
|
2020-01-21 23:37:59 +00:00
|
|
|
func (suite *ConfigTestSuite) TestNewConfigWithValuesSecrets() {
|
|
|
|
suite.unsetenv("VALUES")
|
|
|
|
suite.unsetenv("STRING_VALUES")
|
2020-01-22 00:25:58 +00:00
|
|
|
suite.unsetenv("SECRET_WATER")
|
2020-01-21 23:37:59 +00:00
|
|
|
suite.setenv("SECRET_FIRE", "Eru_Ilúvatar")
|
|
|
|
suite.setenv("SECRET_RINGS", "1")
|
|
|
|
suite.setenv("PLUGIN_VALUES", "fire=$SECRET_FIRE,water=${SECRET_WATER}")
|
|
|
|
suite.setenv("PLUGIN_STRING_VALUES", "rings=${SECRET_RINGS}")
|
2020-03-31 22:07:28 +00:00
|
|
|
suite.setenv("PLUGIN_ADD_REPOS", "testrepo=https://user:${SECRET_FIRE}@testrepo.test")
|
2020-01-21 23:37:59 +00:00
|
|
|
|
|
|
|
cfg, err := NewConfig(&strings.Builder{}, &strings.Builder{})
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
suite.Equal("fire=Eru_Ilúvatar,water=", cfg.Values)
|
|
|
|
suite.Equal("rings=1", cfg.StringValues)
|
2020-03-31 22:07:28 +00:00
|
|
|
suite.Equal(fmt.Sprintf("testrepo=https://user:%s@testrepo.test", os.Getenv("SECRET_FIRE")), cfg.AddRepos[0])
|
2020-01-21 23:37:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 00:04:05 +00:00
|
|
|
func (suite *ConfigTestSuite) TestValuesSecretsWithDebugLogging() {
|
|
|
|
suite.unsetenv("VALUES")
|
2020-01-22 00:25:58 +00:00
|
|
|
suite.unsetenv("SECRET_WATER")
|
2020-01-22 00:04:05 +00:00
|
|
|
suite.setenv("SECRET_FIRE", "Eru_Ilúvatar")
|
|
|
|
suite.setenv("PLUGIN_DEBUG", "true")
|
|
|
|
suite.setenv("PLUGIN_STRING_VALUES", "fire=$SECRET_FIRE")
|
|
|
|
suite.setenv("PLUGIN_VALUES", "fire=$SECRET_FIRE,water=$SECRET_WATER")
|
|
|
|
stderr := strings.Builder{}
|
|
|
|
_, err := NewConfig(&strings.Builder{}, &stderr)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
2020-01-22 00:23:55 +00:00
|
|
|
suite.Contains(stderr.String(), "Values:fire=Eru_Ilúvatar,water=")
|
|
|
|
suite.Contains(stderr.String(), `$SECRET_WATER not present in environment, replaced with ""`)
|
2020-01-22 00:04:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 22:44:59 +00:00
|
|
|
func (suite *ConfigTestSuite) setenv(key, val string) {
|
|
|
|
orig, ok := os.LookupEnv(key)
|
|
|
|
if ok {
|
|
|
|
suite.envBackup[key] = &orig
|
|
|
|
} else {
|
|
|
|
suite.envBackup[key] = nil
|
|
|
|
}
|
|
|
|
os.Setenv(key, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ConfigTestSuite) unsetenv(key string) {
|
|
|
|
orig, ok := os.LookupEnv(key)
|
|
|
|
if ok {
|
|
|
|
suite.envBackup[key] = &orig
|
|
|
|
} else {
|
|
|
|
suite.envBackup[key] = nil
|
|
|
|
}
|
|
|
|
os.Unsetenv(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ConfigTestSuite) BeforeTest(_, _ string) {
|
|
|
|
suite.envBackup = make(map[string]*string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ConfigTestSuite) AfterTest(_, _ string) {
|
|
|
|
for key, val := range suite.envBackup {
|
|
|
|
if val == nil {
|
|
|
|
os.Unsetenv(key)
|
|
|
|
} else {
|
|
|
|
os.Setenv(key, *val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|