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

     1  // Copyright 2012-2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package service_test
     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  	goyaml "gopkg.in/yaml.v1"
    13  
    14  	"github.com/juju/juju/cmd/envcmd"
    15  	"github.com/juju/juju/cmd/juju/service"
    16  	coretesting "github.com/juju/juju/testing"
    17  )
    18  
    19  type GetSuite struct {
    20  	coretesting.FakeJujuHomeSuite
    21  	fake *fakeServiceAPI
    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": "Specifies title",
    38  					"type":        "string",
    39  					"value":       "Nearly There",
    40  				},
    41  				"skill-level": map[string]interface{}{
    42  					"description": "Specifies skill-level",
    43  					"value":       100,
    44  					"type":        "int",
    45  				},
    46  				"username": map[string]interface{}{
    47  					"description": "Specifies username",
    48  					"type":        "string",
    49  					"value":       "admin001",
    50  				},
    51  				"outlook": map[string]interface{}{
    52  					"description": "Specifies outlook",
    53  					"type":        "string",
    54  					"value":       "true",
    55  				},
    56  			},
    57  		},
    58  	},
    59  
    60  	// TODO(dfc) add additional services (need more charms)
    61  	// TODO(dfc) add set tests
    62  }
    63  
    64  func (s *GetSuite) SetUpTest(c *gc.C) {
    65  	s.FakeJujuHomeSuite.SetUpTest(c)
    66  	s.fake = &fakeServiceAPI{servName: "dummy-service", charmName: "dummy",
    67  		values: map[string]interface{}{
    68  			"title":       "Nearly There",
    69  			"skill-level": 100,
    70  			"username":    "admin001",
    71  			"outlook":     "true",
    72  		}}
    73  }
    74  
    75  func (s *GetSuite) TestGetCommandInit(c *gc.C) {
    76  	// missing args
    77  	err := coretesting.InitCommand(&service.GetCommand{}, []string{})
    78  	c.Assert(err, gc.ErrorMatches, "no service name specified")
    79  }
    80  
    81  func (s *GetSuite) TestGetConfig(c *gc.C) {
    82  	for _, t := range getTests {
    83  		ctx := coretesting.Context(c)
    84  		code := cmd.Main(envcmd.Wrap(service.NewGetCommand(s.fake)), ctx, []string{t.service})
    85  		c.Check(code, gc.Equals, 0)
    86  		c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "")
    87  		// round trip via goyaml to avoid being sucked into a quagmire of
    88  		// map[interface{}]interface{} vs map[string]interface{}. This is
    89  		// also required if we add json support to this command.
    90  		buf, err := goyaml.Marshal(t.expected)
    91  		c.Assert(err, jc.ErrorIsNil)
    92  		expected := make(map[string]interface{})
    93  		err = goyaml.Unmarshal(buf, &expected)
    94  		c.Assert(err, jc.ErrorIsNil)
    95  
    96  		actual := make(map[string]interface{})
    97  		err = goyaml.Unmarshal(ctx.Stdout.(*bytes.Buffer).Bytes(), &actual)
    98  		c.Assert(err, jc.ErrorIsNil)
    99  		c.Assert(actual, gc.DeepEquals, expected)
   100  	}
   101  }