github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/modelcmd/controller_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package modelcmd_test
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/cmd/cmdtesting"
     9  	"github.com/juju/errors"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/cmd/modelcmd"
    14  	"github.com/juju/juju/jujuclient"
    15  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  type ControllerCommandSuite struct {
    20  	testing.FakeJujuXDGDataHomeSuite
    21  }
    22  
    23  var _ = gc.Suite(&ControllerCommandSuite{})
    24  
    25  func (s *ControllerCommandSuite) TestControllerCommandNoneSpecified(c *gc.C) {
    26  	_, err := initTestControllerCommand(c, nil)
    27  	c.Assert(err, gc.ErrorMatches, "no controller specified(.|\n)*")
    28  }
    29  
    30  func (s *ControllerCommandSuite) TestControllerCommandInitSystemFile(c *gc.C) {
    31  	// If there is a current-controller file, use that.
    32  	err := modelcmd.WriteCurrentController("foo")
    33  	c.Assert(err, jc.ErrorIsNil)
    34  	store := jujuclienttesting.NewMemStore()
    35  	store.Accounts["foo"] = &jujuclient.ControllerAccounts{
    36  		CurrentAccount: "bar@baz",
    37  	}
    38  	store.Controllers["foo"] = jujuclient.ControllerDetails{}
    39  	testEnsureControllerName(c, store, "foo")
    40  }
    41  
    42  func (s *ControllerCommandSuite) TestControllerCommandInitExplicit(c *gc.C) {
    43  	// Take controller name from command line arg, and it trumps the current-
    44  	// controller file.
    45  	err := modelcmd.WriteCurrentController("foo")
    46  	c.Assert(err, jc.ErrorIsNil)
    47  	store := jujuclienttesting.NewMemStore()
    48  	store.Accounts["explicit"] = &jujuclient.ControllerAccounts{
    49  		CurrentAccount: "bar@baz",
    50  	}
    51  	store.Controllers["explicit"] = jujuclient.ControllerDetails{}
    52  	testEnsureControllerName(c, store, "explicit", "-c", "explicit")
    53  	testEnsureControllerName(c, store, "explicit", "--controller", "explicit")
    54  }
    55  
    56  func (s *ControllerCommandSuite) TestWrapWithoutFlags(c *gc.C) {
    57  	cmd := new(testControllerCommand)
    58  	wrapped := modelcmd.WrapController(cmd, modelcmd.ControllerSkipFlags)
    59  	err := cmdtesting.InitCommand(wrapped, []string{"-s", "testsys"})
    60  	c.Assert(err, gc.ErrorMatches, "flag provided but not defined: -s")
    61  }
    62  
    63  type testControllerCommand struct {
    64  	modelcmd.ControllerCommandBase
    65  }
    66  
    67  func (c *testControllerCommand) Info() *cmd.Info {
    68  	panic("should not be called")
    69  }
    70  
    71  func (c *testControllerCommand) Run(ctx *cmd.Context) error {
    72  	panic("should not be called")
    73  }
    74  
    75  func initTestControllerCommand(c *gc.C, store jujuclient.ClientStore, args ...string) (*testControllerCommand, error) {
    76  	cmd := new(testControllerCommand)
    77  	cmd.SetClientStore(store)
    78  	wrapped := modelcmd.WrapController(cmd)
    79  	return cmd, cmdtesting.InitCommand(wrapped, args)
    80  }
    81  
    82  func testEnsureControllerName(c *gc.C, store jujuclient.ClientStore, expect string, args ...string) {
    83  	cmd, err := initTestControllerCommand(c, store, args...)
    84  	c.Assert(err, jc.ErrorIsNil)
    85  	c.Assert(cmd.ControllerName(), gc.Equals, expect)
    86  }
    87  
    88  type ControllerSuite struct {
    89  	store jujuclient.ControllerStore
    90  }
    91  
    92  var _ = gc.Suite(&ControllerSuite{})
    93  
    94  func (s *ControllerSuite) SetUpTest(c *gc.C) {
    95  	controller := jujuclient.ControllerDetails{ControllerUUID: "controller-uuid"}
    96  	anothercontroller := jujuclient.ControllerDetails{ControllerUUID: "another-uuid"}
    97  	s.store = &jujuclienttesting.MemStore{
    98  		Controllers: map[string]jujuclient.ControllerDetails{
    99  			"local.controller":        controller,
   100  			"anothercontroller":       anothercontroller,
   101  			"local.anothercontroller": jujuclient.ControllerDetails{},
   102  		},
   103  	}
   104  }
   105  
   106  func (s *ControllerSuite) TestLocalNameFound(c *gc.C) {
   107  	name, err := modelcmd.ResolveControllerName(s.store, "local.controller")
   108  	c.Assert(err, jc.ErrorIsNil)
   109  	c.Assert(name, gc.DeepEquals, "local.controller")
   110  }
   111  
   112  func (s *ControllerSuite) TestLocalNameFallback(c *gc.C) {
   113  	name, err := modelcmd.ResolveControllerName(s.store, "controller")
   114  	c.Assert(name, gc.DeepEquals, "local.controller")
   115  	c.Assert(err, jc.ErrorIsNil)
   116  }
   117  
   118  func (s *ControllerSuite) TestNonLocalController(c *gc.C) {
   119  	name, err := modelcmd.ResolveControllerName(s.store, "anothercontroller")
   120  	c.Assert(name, gc.DeepEquals, "anothercontroller")
   121  	c.Assert(err, jc.ErrorIsNil)
   122  }
   123  
   124  func (s *ControllerSuite) TestOnlyLocalController(c *gc.C) {
   125  	name, err := modelcmd.ResolveControllerName(s.store, "local.anothercontroller")
   126  	c.Assert(name, gc.DeepEquals, "local.anothercontroller")
   127  	c.Assert(err, jc.ErrorIsNil)
   128  }
   129  
   130  func (s *ControllerSuite) TestNotFound(c *gc.C) {
   131  	_, err := modelcmd.ResolveControllerName(s.store, "foo")
   132  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
   133  	// We should report on the passed in controller name.
   134  	c.Assert(err, gc.ErrorMatches, ".* foo .*")
   135  }