Skip to content

Commit

Permalink
Merge pull request #30 from mapuri/unittests
Browse files Browse the repository at this point in the history
add some unit-tests for config merge functionality
  • Loading branch information
mapuri committed Dec 19, 2015
2 parents 2d0f548 + 8de35bb commit 41ae040
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
24 changes: 14 additions & 10 deletions management/src/clusterm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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) {
Expand Down
43 changes: 43 additions & 0 deletions management/src/clusterm/main_test.go
Original file line number Diff line number Diff line change
@@ -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:.*")
}

0 comments on commit 41ae040

Please sign in to comment.