Skip to content

Commit

Permalink
Fix tags loading from config file (distribworks#315)
Browse files Browse the repository at this point in the history
* Fix tags loading from config file

Fixed tests reseting viper
  • Loading branch information
Victor Castell authored Nov 15, 2017
1 parent 90294af commit 4b9bdc2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
14 changes: 11 additions & 3 deletions dkron/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,23 @@ func NewConfig(args []string, agent *AgentCommand) *Config {
return ReadConfig(agent)
}

// ReadConfig from file and create the actual config object.
func ReadConfig(agent *AgentCommand) *Config {
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
logrus.WithError(err).Info("No valid config found: Applying default values.")
}

tags, err := UnmarshalTags(viper.GetStringSlice("tag"))
if err != nil {
log.Fatal(err)
cliTags := viper.GetStringSlice("tag")
var tags map[string]string

if len(cliTags) > 0 {
tags, err = UnmarshalTags(cliTags)
if err != nil {
logrus.Fatal("config: Error unmarshaling cli tags")
}
} else {
tags = viper.GetStringMapString("tags")
}

server := viper.GetBool("server")
Expand Down
40 changes: 40 additions & 0 deletions dkron/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dkron

import (
"bytes"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"

"github.com/mitchellh/cli"
)

func TestReadConfigTags(t *testing.T) {
shutdownCh := make(chan struct{})
defer close(shutdownCh)

ui := new(cli.MockUi)
a := &AgentCommand{
Ui: ui,
ShutdownCh: shutdownCh,
}

viper.Reset()
viper.SetConfigType("json")
var jsonConfig = []byte(`{
"tags": {
"foo": "bar"
}
}`)
viper.ReadConfig(bytes.NewBuffer(jsonConfig))
config := ReadConfig(a)
t.Log(config.Tags)
assert.Equal(t, "bar", config.Tags["foo"])

viper.Set("tag", []string{"monthy=python"})
config = ReadConfig(a)
assert.NotContains(t, config.Tags, "foo")
assert.Contains(t, config.Tags, "monthy")
assert.Equal(t, "python", config.Tags["monthy"])
}
4 changes: 3 additions & 1 deletion dkron/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (

"github.com/hashicorp/serf/testutil"
"github.com/mitchellh/cli"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func TestRPCExecutionDone(t *testing.T) {
store := NewStore("etcd", []string{etcdAddr}, nil, "dkron")
viper.Reset()

// Cleanup everything
err := store.Client.DeleteTree("dkron")
Expand All @@ -31,7 +33,7 @@ func TestRPCExecutionDone(t *testing.T) {

args := []string{
"-bind-addr", aAddr,
"-advertise-addr", aAddr,
"-backend-machine", etcdAddr,
"-node-name", "test1",
"-server",
"-keyspace", "dkron",
Expand Down

0 comments on commit 4b9bdc2

Please sign in to comment.