diff --git a/management/src/clusterm/main.go b/management/src/clusterm/main.go index 7e8470f..10bfd19 100644 --- a/management/src/clusterm/main.go +++ b/management/src/clusterm/main.go @@ -56,6 +56,19 @@ func main() { app.Run(os.Args) } +func mergeConfig(dst *manager.Config, srcJSON []byte) (*manager.Config, error) { + src := &manager.Config{} + if err := json.Unmarshal(srcJSON, src); err != nil { + return nil, fmt.Errorf("failed to parse configuration. Error: %s", err) + } + + if err := mergo.MergeWithOverwrite(dst, src); err != nil { + return nil, fmt.Errorf("failed to merge configuration. Error: %s", err) + } + + return dst, nil +} + func readConfig(c *cli.Context) (*manager.Config, error) { mgrConfig := manager.DefaultConfig() if !c.GlobalIsSet("config") { @@ -84,16 +97,7 @@ func readConfig(c *cli.Context) (*manager.Config, error) { return nil, err } - userConfig := &manager.Config{} - if err := json.Unmarshal(config, userConfig); err != nil { - return nil, fmt.Errorf("failed to parse configuration. Error: %s", err) - } - - if err := mergo.MergeWithOverwrite(mgrConfig, userConfig); err != nil { - return nil, fmt.Errorf("failed to merge configuration. Error: %s", err) - } - - return mgrConfig, nil + return mergeConfig(mgrConfig, config) } func startDaemon(c *cli.Context) { diff --git a/management/src/clusterm/main_test.go b/management/src/clusterm/main_test.go new file mode 100644 index 0000000..d4aeb42 --- /dev/null +++ b/management/src/clusterm/main_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "testing" + + "github.com/contiv/cluster/management/src/clusterm/manager" + . "gopkg.in/check.v1" +) + +// Hook up gocheck into the "go test" runner. +func Test(t *testing.T) { TestingT(t) } + +type mainSuite struct { +} + +var _ = Suite(&mainSuite{}) + +func (s *mainSuite) TestMergeConfigSuccess(c *C) { + dst := manager.DefaultConfig() + srcBytes := []byte(`{ + "ansible" : { + "playbook-location" : "override-location" + } + }`) + exptdDst := manager.DefaultConfig() + exptdDst.Ansible.PlaybookLocation = "override-location" + + _, err := mergeConfig(dst, srcBytes) + c.Assert(err, IsNil) + c.Assert(dst, DeepEquals, exptdDst) +} + +func (s *mainSuite) TestMergeConfigInvalidJSON(c *C) { + dst := manager.DefaultConfig() + srcBytes := []byte(`{ + "ansible" : { + "playbook-location" : "extra-comma-error", + } + }`) + + _, err := mergeConfig(dst, srcBytes) + c.Assert(err, ErrorMatches, "failed to parse configuration. Error:.*") +}