2019-12-09 17:56:02 +00:00
|
|
|
package helm
|
|
|
|
|
|
|
|
import (
|
2019-12-31 18:03:53 +00:00
|
|
|
"fmt"
|
2019-12-09 17:56:02 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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() {
|
2019-12-24 00:07:49 +00:00
|
|
|
suite.unsetenv("PLUGIN_PREFIX")
|
2019-12-23 23:31:40 +00:00
|
|
|
suite.unsetenv("HELM_COMMAND")
|
|
|
|
suite.unsetenv("UPDATE_DEPENDENCIES")
|
|
|
|
suite.unsetenv("DEBUG")
|
|
|
|
|
2019-12-23 22:44:59 +00:00
|
|
|
suite.setenv("PLUGIN_HELM_COMMAND", "execute order 66")
|
|
|
|
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
|
|
|
|
|
|
|
suite.Equal("execute order 66", cfg.Command)
|
|
|
|
suite.True(cfg.UpdateDependencies)
|
|
|
|
suite.True(cfg.Debug)
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:34:38 +00:00
|
|
|
func (suite *ConfigTestSuite) TestNewConfigWithNoPrefix() {
|
2019-12-24 00:07:49 +00:00
|
|
|
suite.unsetenv("PLUGIN_PREFIX")
|
2019-12-23 23:31:40 +00:00
|
|
|
suite.unsetenv("PLUGIN_HELM_COMMAND")
|
|
|
|
suite.unsetenv("PLUGIN_UPDATE_DEPENDENCIES")
|
|
|
|
suite.unsetenv("PLUGIN_DEBUG")
|
|
|
|
|
|
|
|
suite.setenv("HELM_COMMAND", "execute order 66")
|
|
|
|
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
|
|
|
|
|
|
|
suite.Equal("execute order 66", cfg.Command)
|
|
|
|
suite.True(cfg.UpdateDependencies)
|
|
|
|
suite.True(cfg.Debug)
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:34:38 +00:00
|
|
|
func (suite *ConfigTestSuite) TestNewConfigWithConfigurablePrefix() {
|
2019-12-24 00:07:49 +00:00
|
|
|
suite.unsetenv("API_SERVER")
|
|
|
|
suite.unsetenv("PLUGIN_API_SERVER")
|
|
|
|
|
|
|
|
suite.setenv("PLUGIN_PREFIX", "prix_fixe")
|
|
|
|
suite.setenv("PRIX_FIXE_API_SERVER", "your waiter this evening")
|
|
|
|
|
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-24 00:07:49 +00:00
|
|
|
|
|
|
|
suite.Equal("prix_fixe", cfg.Prefix)
|
|
|
|
suite.Equal("your waiter this evening", cfg.APIServer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ConfigTestSuite) TestPrefixSettingDoesNotAffectPluginPrefix() {
|
|
|
|
suite.setenv("PLUGIN_PREFIX", "IXFREP")
|
|
|
|
suite.setenv("PLUGIN_HELM_COMMAND", "wake me up")
|
|
|
|
suite.setenv("IXFREP_PLUGIN_HELM_COMMAND", "send me to sleep inside")
|
|
|
|
|
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-24 00:07:49 +00:00
|
|
|
|
|
|
|
suite.Equal("wake me up", cfg.Command)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ConfigTestSuite) TestPrefixSettingMustHavePluginPrefix() {
|
|
|
|
suite.unsetenv("PLUGIN_PREFIX")
|
|
|
|
suite.setenv("PREFIX", "refpix")
|
|
|
|
suite.setenv("HELM_COMMAND", "gimme more")
|
|
|
|
suite.setenv("REFPIX_HELM_COMMAND", "gimme less")
|
|
|
|
|
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-24 00:07:49 +00:00
|
|
|
|
|
|
|
suite.Equal("gimme more", cfg.Command)
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:34:38 +00:00
|
|
|
func (suite *ConfigTestSuite) TestNewConfigWithConflictingVariables() {
|
2019-12-23 23:31:40 +00:00
|
|
|
suite.setenv("PLUGIN_HELM_COMMAND", "execute order 66")
|
2019-12-24 00:07:49 +00:00
|
|
|
suite.setenv("HELM_COMMAND", "defend the jedi") // values from the `environment` block override those from `settings`
|
|
|
|
|
|
|
|
suite.setenv("PLUGIN_PREFIX", "prod")
|
|
|
|
suite.setenv("TIMEOUT", "5m0s")
|
|
|
|
suite.setenv("PROD_TIMEOUT", "2m30s") // values from prefixed env vars override those from non-prefixed ones
|
2019-12-23 23:31:40 +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
|
|
|
|
|
|
|
suite.Equal("defend the jedi", cfg.Command)
|
2019-12-24 00:07:49 +00:00
|
|
|
suite.Equal("2m30s", cfg.Timeout)
|
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)
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
suite.setenv("HELM_COMMAND", "upgrade")
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|