github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/get_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"bytes"
     8  
     9  	"github.com/juju/cmd"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/juju/charm.v5"
    13  	goyaml "gopkg.in/yaml.v1"
    14  
    15  	"github.com/juju/juju/cmd/envcmd"
    16  	"github.com/juju/juju/juju/testing"
    17  	coretesting "github.com/juju/juju/testing"
    18  )
    19  
    20  type GetSuite struct {
    21  	testing.JujuConnSuite
    22  }
    23  
    24  var _ = gc.Suite(&GetSuite{})
    25  
    26  var getTests = []struct {
    27  	service  string
    28  	expected map[string]interface{}
    29  }{
    30  	{
    31  		"dummy-service",
    32  		map[string]interface{}{
    33  			"service": "dummy-service",
    34  			"charm":   "dummy",
    35  			"settings": map[string]interface{}{
    36  				"title": map[string]interface{}{
    37  					"description": "A descriptive title used for the service.",
    38  					"type":        "string",
    39  					"value":       "Nearly There",
    40  				},
    41  				"skill-level": map[string]interface{}{
    42  					"description": "A number indicating skill.",
    43  					"type":        "int",
    44  					"default":     true,
    45  				},
    46  				"username": map[string]interface{}{
    47  					"description": "The name of the initial account (given admin permissions).",
    48  					"type":        "string",
    49  					"value":       "admin001",
    50  					"default":     true,
    51  				},
    52  				"outlook": map[string]interface{}{
    53  					"description": "No default outlook.",
    54  					"type":        "string",
    55  					"default":     true,
    56  				},
    57  			},
    58  		},
    59  	},
    60  
    61  	// TODO(dfc) add additional services (need more charms)
    62  	// TODO(dfc) add set tests
    63  }
    64  
    65  func (s *GetSuite) TestGetConfig(c *gc.C) {
    66  	sch := s.AddTestingCharm(c, "dummy")
    67  	svc := s.AddTestingService(c, "dummy-service", sch)
    68  	err := svc.UpdateConfigSettings(charm.Settings{"title": "Nearly There"})
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	for _, t := range getTests {
    71  		ctx := coretesting.Context(c)
    72  		code := cmd.Main(envcmd.Wrap(&GetCommand{}), ctx, []string{t.service})
    73  		c.Check(code, gc.Equals, 0)
    74  		c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "")
    75  		// round trip via goyaml to avoid being sucked into a quagmire of
    76  		// map[interface{}]interface{} vs map[string]interface{}. This is
    77  		// also required if we add json support to this command.
    78  		buf, err := goyaml.Marshal(t.expected)
    79  		c.Assert(err, jc.ErrorIsNil)
    80  		expected := make(map[string]interface{})
    81  		err = goyaml.Unmarshal(buf, &expected)
    82  		c.Assert(err, jc.ErrorIsNil)
    83  
    84  		actual := make(map[string]interface{})
    85  		err = goyaml.Unmarshal(ctx.Stdout.(*bytes.Buffer).Bytes(), &actual)
    86  		c.Assert(err, jc.ErrorIsNil)
    87  		c.Assert(actual, gc.DeepEquals, expected)
    88  	}
    89  }