Skip to content

Commit

Permalink
Merge pull request #141 from akutz/feature/config-test-coverage
Browse files Browse the repository at this point in the history
Config Test Coverage Enhancements
  • Loading branch information
akutz committed Oct 19, 2015
2 parents d1e4513 + 0a822eb commit 303cc6e
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 74 deletions.
47 changes: 11 additions & 36 deletions core/config/config.go
Expand Up @@ -22,11 +22,7 @@ var (
)

func init() {
var envVarRxErr error
envVarRx, envVarRxErr = regexp.Compile(`^\s*([^#=]+)=(.+)$`)
if envVarRxErr != nil {
panic(envVarRxErr)
}
envVarRx = regexp.MustCompile(`^\s*([^#=]+)=(.+)$`)
loadEtcEnvironment()
initConfigKeyMap()
}
Expand Down Expand Up @@ -142,7 +138,7 @@ func NewConfig(
c.Viper.SetConfigType(configType)

cfgFile := fmt.Sprintf("%s.%s", configName, configType)
etcRexRayFile := fmt.Sprintf("%s/%s", util.EtcDirPath(), cfgFile)
etcRexRayFile := util.EtcFilePath(cfgFile)
usrRexRayFile := fmt.Sprintf("%s/.rexray/%s", util.HomeDir(), cfgFile)

if loadGlobalConfig && util.FileExists(etcRexRayFile) {
Expand Down Expand Up @@ -184,12 +180,8 @@ func (c *Config) SetJSONMarshalStrategy(s JSONMarshalStrategy) {
// Copy creates a copy of this Config instance
func (c *Config) Copy() (*Config, error) {
newC := New()
if err := c.Viper.Unmarshal(&newC.plainTextConfig); err != nil {
return nil, err
}
if err := c.Viper.Unmarshal(&newC.secureConfig); err != nil {
return nil, err
}
c.Viper.Unmarshal(&newC.plainTextConfig)
c.Viper.Unmarshal(&newC.secureConfig)
return newC, nil
}

Expand All @@ -205,20 +197,14 @@ func FromJSON(from string) (*Config, error) {

// ToJSON exports this Config instance to a JSON string
func (c *Config) ToJSON() (string, error) {
buf, err := c.marshalJSON(JSONMarshalPlainText)
if err != nil {
return "", err
}
buf, _ := c.marshalJSON(JSONMarshalPlainText)
return string(buf), nil
}

// ToSecureJSON exports this Config instance to a JSON string omitting any of
// the secure fields
func (c *Config) ToSecureJSON() (string, error) {
buf, err := c.marshalJSON(JSONMarshalSecure)
if err != nil {
return "", err
}
buf, _ := c.marshalJSON(JSONMarshalSecure)
return string(buf), nil
}

Expand All @@ -230,14 +216,11 @@ func (c *Config) MarshalJSON() ([]byte, error) {

func (c *Config) marshalJSON(s JSONMarshalStrategy) ([]byte, error) {
switch s {
case JSONMarshalSecure:
return json.MarshalIndent(c.secureConfig, "", " ")
case JSONMarshalPlainText:
return json.MarshalIndent(c.plainTextConfig, "", " ")
default:
return json.MarshalIndent(c.secureConfig, "", " ")
}

return nil, errors.WithField(
"strategy", s, "unknown json marshalling strategy")
}

// ReadConfig reads a configuration stream into the current config instance
Expand All @@ -247,17 +230,9 @@ func (c *Config) ReadConfig(in io.Reader) error {
return errors.New("config reader is nil")
}

if err := c.Viper.ReadConfigNoNil(in); err != nil {
return err
}

if err := c.Viper.Unmarshal(&c.secureConfig); err != nil {
return err
}

if err := c.Viper.Unmarshal(&c.plainTextConfig); err != nil {
return err
}
c.Viper.ReadConfigNoNil(in)
c.Viper.Unmarshal(&c.secureConfig)
c.Viper.Unmarshal(&c.plainTextConfig)

for key := range keys {
c.updateFlag(key, c.GlobalFlags)
Expand Down
179 changes: 168 additions & 11 deletions core/config/config_test.go
Expand Up @@ -4,13 +4,107 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"

"github.com/emccode/rexray/util"
)

var (
tmpPrefixDirs []string
usrRexRayFile string
)

func TestMain(m *testing.M) {
usrRexRayDir := fmt.Sprintf("%s/.rexray", util.HomeDir())
os.MkdirAll(usrRexRayDir, 0755)
usrRexRayFile = fmt.Sprintf("%s/%s.%s", usrRexRayDir, "config", "yml")
usrRexRayFileBak := fmt.Sprintf("%s.bak", usrRexRayFile)

os.Remove(usrRexRayFileBak)
os.Rename(usrRexRayFile, usrRexRayFileBak)

exitCode := m.Run()
for _, d := range tmpPrefixDirs {
os.RemoveAll(d)
}

os.Remove(usrRexRayFile)
os.Rename(usrRexRayFileBak, usrRexRayFile)
os.Exit(exitCode)
}

func newPrefixDir(testName string, t *testing.T) string {
tmpDir, err := ioutil.TempDir(
"", fmt.Sprintf("rexray-core-config_test-%s", testName))
if err != nil {
t.Fatal(err)
}

util.Prefix(tmpDir)
os.MkdirAll(tmpDir, 0755)
tmpPrefixDirs = append(tmpPrefixDirs, tmpDir)
return tmpDir
}

func TestCopy(t *testing.T) {
newPrefixDir("TestCopy", t)

etcRexRayCfg := util.EtcFilePath("config.yml")
t.Logf("etcRexRayCfg=%s", etcRexRayCfg)
util.WriteStringToFile(string(yamlConfig1), etcRexRayCfg)

c := New()

assertLogLevel(t, c, "error")
assertStorageDrivers(t, c)
assertOsDrivers1(t, c)

cc, _ := c.Copy()

assertLogLevel(t, cc, "error")
assertStorageDrivers(t, cc)
assertOsDrivers1(t, cc)

cJSON, _ := c.ToJSON()
ccJSON, _ := cc.ToJSON()

cMap := map[string]interface{}{}
ccMap := map[string]interface{}{}
json.Unmarshal([]byte(cJSON), cMap)
json.Unmarshal([]byte(ccJSON), ccJSON)

if !reflect.DeepEqual(cMap, ccMap) {
t.Fail()
}
}

func TestEnvVars(t *testing.T) {
c := New()
c.Viper.Set("awsSecretKey", "Hello, world.")
if !util.StringInSlice("AWS_SECRET_KEY=Hello, world.", c.EnvVars()) {
t.Fail()
}

if util.StringInSlice("AWS_SECRET_KEY=Hello, world.", New().EnvVars()) {
t.Fail()
}
}

func TestJSONMarshalStrategy(t *testing.T) {
c := New()
if c.JSONMarshalStrategy() != JSONMarshalSecure {
t.Fail()
}
c.SetJSONMarshalStrategy(JSONMarshalPlainText)
if c.JSONMarshalStrategy() != JSONMarshalPlainText {
t.Fail()
}
c.SetJSONMarshalStrategy(JSONMarshalSecure)
}

func TestToJson(t *testing.T) {
c := New()

Expand Down Expand Up @@ -136,6 +230,13 @@ func TestFromJson(t *testing.T) {
assertAwsSecretKey(t, c, "MyAwsSecretKey")
}

func TestFromJsonWithErrors(t *testing.T) {
_, err := FromJSON("///*")
if err == nil {
t.Fatal("expected unmarshalling error")
}
}

func TestFromSecureJson(t *testing.T) {
c, err := FromJSON(secureJSONConfig)
if err != nil {
Expand All @@ -147,21 +248,39 @@ func TestFromSecureJson(t *testing.T) {
assertAwsSecretKey(t, c, "")
}

func TestNew(t *testing.T) {
func TestNewWithUserConfigFile(t *testing.T) {
util.WriteStringToFile(string(yamlConfig1), usrRexRayFile)
defer os.RemoveAll(usrRexRayFile)

usrRexRayDir := fmt.Sprintf("%s/.rexray", util.HomeDir())
os.MkdirAll(usrRexRayDir, 0755)
usrRexRayFile := fmt.Sprintf("%s/%s.%s", usrRexRayDir, "config", "yml")
usrRexRayFileBak := fmt.Sprintf("%s.bak", usrRexRayFile)
c := New()

os.Remove(usrRexRayFileBak)
os.Rename(usrRexRayFile, usrRexRayFileBak)
defer func() {
os.Remove(usrRexRayFile)
os.Rename(usrRexRayFileBak, usrRexRayFile)
}()
assertLogLevel(t, c, "error")
assertStorageDrivers(t, c)
assertOsDrivers1(t, c)

if err := c.ReadConfig(bytes.NewReader(yamlConfig2)); err != nil {
t.Fatal(err)
}

assertLogLevel(t, c, "debug")
assertStorageDrivers(t, c)
assertOsDrivers2(t, c)
}

func TestNewWithUserConfigFileWithErrors(t *testing.T) {
util.WriteStringToFile(string(yamlConfig1), usrRexRayFile)
defer os.RemoveAll(usrRexRayFile)

os.Chmod(usrRexRayFile, 0000)
New()
}

func TestNewWithGlobalConfigFile(t *testing.T) {
newPrefixDir("TestNewWithGlobalConfigFile", t)

etcRexRayCfg := util.EtcFilePath("config.yml")
t.Logf("etcRexRayCfg=%s", etcRexRayCfg)
util.WriteStringToFile(string(yamlConfig1), etcRexRayCfg)

c := New()

Expand All @@ -178,6 +297,38 @@ func TestNew(t *testing.T) {
assertOsDrivers2(t, c)
}

func TestNewWithGlobalConfigFileWithErrors(t *testing.T) {
newPrefixDir("TestNewWithGlobalConfigFileWithErrors", t)

etcRexRayCfg := util.EtcFilePath("config.yml")
t.Logf("etcRexRayCfg=%s", etcRexRayCfg)
util.WriteStringToFile(string(yamlConfig1), etcRexRayCfg)

os.Chmod(etcRexRayCfg, 0000)
New()
}

func TestReadConfigFile(t *testing.T) {
var err error
var tmp *os.File
if tmp, err = ioutil.TempFile("", "TestReadConfigFile"); err != nil {
t.Fatal(err)
}
defer os.Remove(tmp.Name())

if _, err := tmp.Write(yamlConfig1); err != nil {
t.Fatal(err)
}
tmp.Close()

os.Chmod(tmp.Name(), 0000)

c := New()
if err := c.ReadConfigFile(tmp.Name()); err == nil {
t.Fatal("expected error reading config file")
}
}

func TestReadConfig(t *testing.T) {
c := NewConfig(false, false, "config", "yml")
if err := c.ReadConfig(bytes.NewReader(yamlConfig1)); err != nil {
Expand All @@ -197,6 +348,12 @@ func TestReadConfig(t *testing.T) {
assertOsDrivers2(t, c)
}

func TestReadNilConfig(t *testing.T) {
if err := New().ReadConfig(nil); err == nil {
t.Fatal("expected nil config error")
}
}

func assertAwsSecretKey(t *testing.T, c *Config, expected string) {
val := c.Viper.GetString("awsSecretKey")
if val != expected {
Expand Down
8 changes: 8 additions & 0 deletions util/util.go
Expand Up @@ -88,6 +88,14 @@ func Prefix(p string) {
return
}

binDirPath = ""
binFilePath = ""
logDirPath = ""
libDirPath = ""
runDirPath = ""
etcDirPath = ""
pidFilePath = ""

prefix = p
}

Expand Down

0 comments on commit 303cc6e

Please sign in to comment.