github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/controller/getconfig_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package controller_test
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/errors"
    14  	"github.com/juju/juju/cmd/juju/controller"
    15  	jujucontroller "github.com/juju/juju/controller"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  type GetConfigSuite struct {
    20  	baseControllerSuite
    21  }
    22  
    23  var _ = gc.Suite(&GetConfigSuite{})
    24  
    25  func (s *GetConfigSuite) SetUpTest(c *gc.C) {
    26  	s.baseControllerSuite.SetUpTest(c)
    27  	s.createTestClientStore(c)
    28  }
    29  
    30  func (s *GetConfigSuite) run(c *gc.C, args ...string) (*cmd.Context, error) {
    31  	command := controller.NewGetConfigCommandForTest(&fakeControllerAPI{}, s.store)
    32  	return testing.RunCommand(c, command, args...)
    33  }
    34  
    35  func (s *GetConfigSuite) TestInit(c *gc.C) {
    36  	// zero or one args is fine.
    37  	err := testing.InitCommand(controller.NewGetConfigCommandForTest(&fakeControllerAPI{}, s.store), nil)
    38  	c.Check(err, jc.ErrorIsNil)
    39  	err = testing.InitCommand(controller.NewGetConfigCommandForTest(&fakeControllerAPI{}, s.store), []string{"one"})
    40  	c.Check(err, jc.ErrorIsNil)
    41  	// More than one is not allowed.
    42  	err = testing.InitCommand(controller.NewGetConfigCommandForTest(&fakeControllerAPI{}, s.store), []string{"one", "two"})
    43  	c.Check(err, gc.ErrorMatches, `unrecognized args: \["two"\]`)
    44  }
    45  
    46  func (s *GetConfigSuite) TestSingleValue(c *gc.C) {
    47  	context, err := s.run(c, "controller-uuid")
    48  	c.Assert(err, jc.ErrorIsNil)
    49  
    50  	output := strings.TrimSpace(testing.Stdout(context))
    51  	c.Assert(output, gc.Equals, "uuid")
    52  }
    53  
    54  func (s *GetConfigSuite) TestSingleValueJSON(c *gc.C) {
    55  	context, err := s.run(c, "--format=json", "controller-uuid")
    56  	c.Assert(err, jc.ErrorIsNil)
    57  
    58  	output := strings.TrimSpace(testing.Stdout(context))
    59  	c.Assert(output, gc.Equals, `"uuid"`)
    60  }
    61  
    62  func (s *GetConfigSuite) TestAllValues(c *gc.C) {
    63  	context, err := s.run(c)
    64  	c.Assert(err, jc.ErrorIsNil)
    65  
    66  	output := strings.TrimSpace(testing.Stdout(context))
    67  	expected := "" +
    68  		"api-port: 1234\n" +
    69  		"controller-uuid: uuid"
    70  	c.Assert(output, gc.Equals, expected)
    71  }
    72  
    73  func (s *GetConfigSuite) TestAllValuesJSON(c *gc.C) {
    74  	context, err := s.run(c, "--format=json")
    75  	c.Assert(err, jc.ErrorIsNil)
    76  
    77  	output := strings.TrimSpace(testing.Stdout(context))
    78  	expected := `{"api-port":1234,"controller-uuid":"uuid"}`
    79  	c.Assert(output, gc.Equals, expected)
    80  }
    81  
    82  func (s *GetConfigSuite) TestError(c *gc.C) {
    83  	command := controller.NewGetConfigCommandForTest(&fakeControllerAPI{err: errors.New("error")}, s.store)
    84  	_, err := testing.RunCommand(c, command)
    85  	c.Assert(err, gc.ErrorMatches, "error")
    86  }
    87  
    88  type fakeControllerAPI struct {
    89  	err error
    90  }
    91  
    92  func (f *fakeControllerAPI) Close() error {
    93  	return nil
    94  }
    95  
    96  func (f *fakeControllerAPI) ControllerConfig() (jujucontroller.Config, error) {
    97  	if f.err != nil {
    98  		return nil, f.err
    99  	}
   100  	return map[string]interface{}{
   101  		"controller-uuid": "uuid",
   102  		"api-port":        1234,
   103  	}, nil
   104  }