diff --git a/dkron/config.go b/dkron/config.go index 8ca13c555..4bcba39e8 100644 --- a/dkron/config.go +++ b/dkron/config.go @@ -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") diff --git a/dkron/config_test.go b/dkron/config_test.go new file mode 100644 index 000000000..8a47bfa14 --- /dev/null +++ b/dkron/config_test.go @@ -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"]) +} diff --git a/dkron/rpc_test.go b/dkron/rpc_test.go index 8edd14fba..2f76f9b18 100644 --- a/dkron/rpc_test.go +++ b/dkron/rpc_test.go @@ -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") @@ -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",