github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"os"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/cmd/cmdtesting"
    11  	"github.com/juju/errors"
    12  	"github.com/juju/testing"
    13  	jc "github.com/juju/testing/checkers"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	jujucmd "github.com/juju/juju/cmd"
    17  	"github.com/juju/juju/cmd/modelcmd"
    18  	"github.com/juju/juju/juju/osenv"
    19  	"github.com/juju/juju/jujuclient"
    20  )
    21  
    22  type ControllerCommandSuite struct {
    23  	testing.IsolationSuite
    24  }
    25  
    26  var _ = gc.Suite(&ControllerCommandSuite{})
    27  
    28  func (s *ControllerCommandSuite) TestControllerCommandNoneSpecified(c *gc.C) {
    29  	cmd, err := runTestControllerCommand(c, jujuclient.NewMemStore())
    30  	c.Assert(err, jc.ErrorIsNil)
    31  	controllerName, err := cmd.ControllerName()
    32  	c.Assert(errors.Cause(err), gc.Equals, modelcmd.ErrNoControllersDefined)
    33  	c.Assert(controllerName, gc.Equals, "")
    34  }
    35  
    36  func (s *ControllerCommandSuite) TestControllerCommandInitCurrentController(c *gc.C) {
    37  	store := jujuclient.NewMemStore()
    38  	store.CurrentControllerName = "foo"
    39  	store.Accounts["foo"] = jujuclient.AccountDetails{
    40  		User: "bar",
    41  	}
    42  	store.Controllers["foo"] = jujuclient.ControllerDetails{}
    43  	testEnsureControllerName(c, store, "foo")
    44  }
    45  
    46  func (s *ControllerCommandSuite) TestControllerCommandInitExplicit(c *gc.C) {
    47  	// Take controller name from command line arg, and it trumps the current-
    48  	// controller file.
    49  	store := jujuclient.NewMemStore()
    50  	store.CurrentControllerName = "foo"
    51  	store.Accounts["explicit"] = jujuclient.AccountDetails{
    52  		User: "bar",
    53  	}
    54  	store.Controllers["explicit"] = jujuclient.ControllerDetails{}
    55  	testEnsureControllerName(c, store, "explicit", "-c", "explicit")
    56  	testEnsureControllerName(c, store, "explicit", "--controller", "explicit")
    57  	os.Setenv(osenv.JujuControllerEnvKey, "explicit")
    58  	testEnsureControllerName(c, store, "explicit")
    59  }
    60  
    61  func (s *ControllerCommandSuite) TestWrapWithoutFlags(c *gc.C) {
    62  	cmd := new(testControllerCommand)
    63  	wrapped := modelcmd.WrapController(cmd, modelcmd.WrapControllerSkipControllerFlags)
    64  	err := cmdtesting.InitCommand(wrapped, []string{"-s", "testsys"})
    65  	c.Assert(err, gc.ErrorMatches, "option provided but not defined: -s")
    66  }
    67  
    68  func (s *ControllerCommandSuite) TestInnerCommand(c *gc.C) {
    69  	cmd := new(testControllerCommand)
    70  	wrapped := modelcmd.WrapController(cmd)
    71  	c.Assert(modelcmd.InnerCommand(wrapped), gc.Equals, cmd)
    72  }
    73  
    74  type testControllerCommand struct {
    75  	modelcmd.ControllerCommandBase
    76  }
    77  
    78  func (c *testControllerCommand) Info() *cmd.Info {
    79  	return jujucmd.Info(&cmd.Info{
    80  		Name:        "testControllerCommand",
    81  		FlagKnownAs: "option",
    82  	})
    83  }
    84  
    85  func (c *testControllerCommand) Run(ctx *cmd.Context) error {
    86  	return nil
    87  }
    88  
    89  func testEnsureControllerName(c *gc.C, store jujuclient.ClientStore, expect string, args ...string) {
    90  	cmd, err := runTestControllerCommand(c, store, args...)
    91  	c.Assert(err, jc.ErrorIsNil)
    92  	controllerName, err := cmd.ControllerName()
    93  	c.Assert(err, jc.ErrorIsNil)
    94  	c.Assert(controllerName, gc.Equals, expect)
    95  }
    96  
    97  func runTestControllerCommand(c *gc.C, store jujuclient.ClientStore, args ...string) (modelcmd.ControllerCommand, error) {
    98  	cmd := modelcmd.WrapController(new(testControllerCommand))
    99  	cmd.SetClientStore(store)
   100  	_, err := cmdtesting.RunCommand(c, cmd, args...)
   101  	return cmd, errors.Trace(err)
   102  }