Do envconfig-loading in config.go (and test it!) [#9]

This commit is contained in:
Erin Call 2019-12-23 14:44:59 -08:00
parent ef4db923cd
commit c4c136b021
No known key found for this signature in database
GPG key ID: 4071FF6C15B8DAD1
3 changed files with 60 additions and 3 deletions

View file

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"github.com/kelseyhightower/envconfig"
"os" "os"
"github.com/pelotech/drone-helm3/internal/helm" "github.com/pelotech/drone-helm3/internal/helm"
@ -11,7 +10,7 @@ import (
func main() { func main() {
var c helm.Config var c helm.Config
if err := envconfig.Process("plugin", &c); err != nil { if err := c.Populate(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error()) fmt.Fprintf(os.Stderr, "%s\n", err.Error())
return return
} }

View file

@ -1,6 +1,8 @@
package helm package helm
import () import (
"github.com/kelseyhightower/envconfig"
)
// The Config struct captures the `settings` and `environment` blocks in the application's drone // The Config struct captures the `settings` and `environment` blocks in the application's drone
// config. Configuration in drone's `settings` block arrives as uppercase env vars matching the // config. Configuration in drone's `settings` block arrives as uppercase env vars matching the
@ -33,3 +35,8 @@ type Config struct {
Release string `` // Release argument to use in applicable helm commands Release string `` // Release argument to use in applicable helm commands
Force bool `` // Pass --force to applicable helm commands Force bool `` // Pass --force to applicable helm commands
} }
// Populate reads environment variables into the Config.
func (cfg *Config) Populate() error {
return envconfig.Process("plugin", cfg)
}

View file

@ -2,13 +2,64 @@ package helm
import ( import (
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"os"
"testing" "testing"
) )
type ConfigTestSuite struct { type ConfigTestSuite struct {
suite.Suite suite.Suite
// 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
} }
func TestConfigTestSuite(t *testing.T) { func TestConfigTestSuite(t *testing.T) {
suite.Run(t, new(ConfigTestSuite)) suite.Run(t, new(ConfigTestSuite))
} }
func (suite *ConfigTestSuite) TestPopulateWithPluginPrefix() {
suite.setenv("PLUGIN_HELM_COMMAND", "execute order 66")
suite.setenv("PLUGIN_UPDATE_DEPENDENCIES", "true")
suite.setenv("PLUGIN_DEBUG", "true")
cfg := Config{}
cfg.Populate()
suite.Equal("execute order 66", cfg.Command)
suite.True(cfg.UpdateDependencies)
suite.True(cfg.Debug)
}
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)
}
}
}