github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/cmd/juju/commands/switch_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"errors"
     8  	"os"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/cmd/modelcmd"
    16  	_ "github.com/juju/juju/juju"
    17  	"github.com/juju/juju/jujuclient"
    18  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    19  	coretesting "github.com/juju/juju/testing"
    20  )
    21  
    22  type SwitchSimpleSuite struct {
    23  	coretesting.FakeJujuXDGDataHomeSuite
    24  	testing.Stub
    25  	store             *jujuclienttesting.MemStore
    26  	currentController string
    27  	onRefresh         func()
    28  }
    29  
    30  var _ = gc.Suite(&SwitchSimpleSuite{})
    31  
    32  func (s *SwitchSimpleSuite) SetUpTest(c *gc.C) {
    33  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    34  	s.Stub.ResetCalls()
    35  	s.store = jujuclienttesting.NewMemStore()
    36  	s.currentController = ""
    37  	s.onRefresh = nil
    38  }
    39  
    40  func (s *SwitchSimpleSuite) refreshModels(store jujuclient.ClientStore, controllerName, accountName string) error {
    41  	s.MethodCall(s, "RefreshModels", store, controllerName, accountName)
    42  	if s.onRefresh != nil {
    43  		s.onRefresh()
    44  	}
    45  	return s.NextErr()
    46  }
    47  
    48  func (s *SwitchSimpleSuite) readCurrentController() (string, error) {
    49  	s.MethodCall(s, "ReadCurrentController")
    50  	return s.currentController, s.NextErr()
    51  }
    52  
    53  func (s *SwitchSimpleSuite) writeCurrentController(current string) error {
    54  	s.MethodCall(s, "WriteCurrentController", current)
    55  	if err := s.NextErr(); err != nil {
    56  		return err
    57  	}
    58  	s.currentController = current
    59  	return nil
    60  }
    61  
    62  func (s *SwitchSimpleSuite) run(c *gc.C, args ...string) (*cmd.Context, error) {
    63  	cmd := &switchCommand{
    64  		Store:                  s.store,
    65  		RefreshModels:          s.refreshModels,
    66  		ReadCurrentController:  s.readCurrentController,
    67  		WriteCurrentController: s.writeCurrentController,
    68  	}
    69  	return coretesting.RunCommand(c, modelcmd.WrapBase(cmd), args...)
    70  }
    71  
    72  func (s *SwitchSimpleSuite) TestNoArgs(c *gc.C) {
    73  	_, err := s.run(c)
    74  	c.Assert(err, gc.ErrorMatches, "no currently specified model")
    75  }
    76  
    77  func (s *SwitchSimpleSuite) TestNoArgsCurrentController(c *gc.C) {
    78  	s.addController(c, "a-controller")
    79  	s.currentController = "a-controller"
    80  	ctx, err := s.run(c)
    81  	c.Assert(err, jc.ErrorIsNil)
    82  	c.Assert(coretesting.Stdout(ctx), gc.Equals, "a-controller\n")
    83  }
    84  
    85  func (s *SwitchSimpleSuite) TestUnknownControllerNameReturnsError(c *gc.C) {
    86  	s.addController(c, "a-controller")
    87  	s.currentController = "a-controller"
    88  	_, err := s.run(c, "another-controller:modela")
    89  	c.Assert(err, gc.ErrorMatches, "controller another-controller not found")
    90  }
    91  
    92  func (s *SwitchSimpleSuite) TestNoArgsCurrentModel(c *gc.C) {
    93  	s.addController(c, "a-controller")
    94  	s.currentController = "a-controller"
    95  	s.store.Models["a-controller"] = jujuclient.ControllerAccountModels{
    96  		map[string]*jujuclient.AccountModels{
    97  			"admin@local": {
    98  				Models:       map[string]jujuclient.ModelDetails{"mymodel": {}},
    99  				CurrentModel: "mymodel",
   100  			},
   101  		},
   102  	}
   103  	ctx, err := s.run(c)
   104  	c.Assert(err, jc.ErrorIsNil)
   105  	c.Assert(coretesting.Stdout(ctx), gc.Equals, "a-controller:mymodel\n")
   106  }
   107  
   108  func (s *SwitchSimpleSuite) TestSwitchWritesCurrentController(c *gc.C) {
   109  	s.addController(c, "a-controller")
   110  	context, err := s.run(c, "a-controller")
   111  	c.Assert(err, jc.ErrorIsNil)
   112  	c.Assert(coretesting.Stderr(context), gc.Equals, " -> a-controller (controller)\n")
   113  	s.CheckCalls(c, []testing.StubCall{
   114  		{"ReadCurrentController", nil},
   115  		{"WriteCurrentController", []interface{}{"a-controller"}},
   116  	})
   117  }
   118  
   119  func (s *SwitchSimpleSuite) TestSwitchWithCurrentController(c *gc.C) {
   120  	s.currentController = "old"
   121  	s.addController(c, "new")
   122  	context, err := s.run(c, "new")
   123  	c.Assert(err, jc.ErrorIsNil)
   124  	c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> new (controller)\n")
   125  }
   126  
   127  func (s *SwitchSimpleSuite) TestSwitchLocalControllerWithCurrent(c *gc.C) {
   128  	s.currentController = "old"
   129  	s.addController(c, "local.new")
   130  	context, err := s.run(c, "new")
   131  	c.Assert(err, jc.ErrorIsNil)
   132  	c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> local.new (controller)\n")
   133  }
   134  
   135  func (s *SwitchSimpleSuite) TestSwitchSameController(c *gc.C) {
   136  	s.currentController = "same"
   137  	s.addController(c, "same")
   138  	context, err := s.run(c, "same")
   139  	c.Assert(err, jc.ErrorIsNil)
   140  	c.Assert(coretesting.Stderr(context), gc.Equals, "same (controller) (no change)\n")
   141  	s.CheckCalls(c, []testing.StubCall{
   142  		{"ReadCurrentController", nil},
   143  	})
   144  }
   145  
   146  func (s *SwitchSimpleSuite) TestSwitchControllerToModel(c *gc.C) {
   147  	s.currentController = "ctrl"
   148  	s.addController(c, "ctrl")
   149  	s.store.Models["ctrl"] = jujuclient.ControllerAccountModels{
   150  		map[string]*jujuclient.AccountModels{
   151  			"admin@local": {
   152  				Models: map[string]jujuclient.ModelDetails{"mymodel": {}},
   153  			},
   154  		},
   155  	}
   156  	context, err := s.run(c, "mymodel")
   157  	c.Assert(err, jc.ErrorIsNil)
   158  	c.Assert(coretesting.Stderr(context), gc.Equals, "ctrl (controller) -> ctrl:mymodel\n")
   159  	s.CheckCalls(c, []testing.StubCall{
   160  		{"ReadCurrentController", nil},
   161  	})
   162  	c.Assert(s.store.Models["ctrl"].AccountModels["admin@local"].CurrentModel, gc.Equals, "mymodel")
   163  }
   164  
   165  func (s *SwitchSimpleSuite) TestSwitchControllerToModelDifferentController(c *gc.C) {
   166  	s.currentController = "old"
   167  	s.addController(c, "new")
   168  	s.store.Models["new"] = jujuclient.ControllerAccountModels{
   169  		map[string]*jujuclient.AccountModels{
   170  			"admin@local": {
   171  				Models: map[string]jujuclient.ModelDetails{"mymodel": {}},
   172  			},
   173  		},
   174  	}
   175  	context, err := s.run(c, "new:mymodel")
   176  	c.Assert(err, jc.ErrorIsNil)
   177  	c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> new:mymodel\n")
   178  	s.CheckCalls(c, []testing.StubCall{
   179  		{"ReadCurrentController", nil},
   180  		{"WriteCurrentController", []interface{}{"new"}},
   181  	})
   182  	c.Assert(s.store.Models["new"].AccountModels["admin@local"].CurrentModel, gc.Equals, "mymodel")
   183  }
   184  
   185  func (s *SwitchSimpleSuite) TestSwitchLocalControllerToModelDifferentController(c *gc.C) {
   186  	s.currentController = "old"
   187  	s.addController(c, "local.new")
   188  	s.store.Models["local.new"] = jujuclient.ControllerAccountModels{
   189  		map[string]*jujuclient.AccountModels{
   190  			"admin@local": {
   191  				Models: map[string]jujuclient.ModelDetails{"mymodel": {}},
   192  			},
   193  		},
   194  	}
   195  	context, err := s.run(c, "new:mymodel")
   196  	c.Assert(err, jc.ErrorIsNil)
   197  	c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> local.new:mymodel\n")
   198  	s.CheckCalls(c, []testing.StubCall{
   199  		{"ReadCurrentController", nil},
   200  		{"WriteCurrentController", []interface{}{"local.new"}},
   201  	})
   202  	c.Assert(s.store.Models["local.new"].AccountModels["admin@local"].CurrentModel, gc.Equals, "mymodel")
   203  }
   204  
   205  func (s *SwitchSimpleSuite) TestSwitchControllerToDifferentControllerCurrentModel(c *gc.C) {
   206  	s.currentController = "old"
   207  	s.addController(c, "new")
   208  	s.store.Models["new"] = jujuclient.ControllerAccountModels{
   209  		map[string]*jujuclient.AccountModels{
   210  			"admin@local": {
   211  				Models:       map[string]jujuclient.ModelDetails{"mymodel": {}},
   212  				CurrentModel: "mymodel",
   213  			},
   214  		},
   215  	}
   216  	context, err := s.run(c, "new:mymodel")
   217  	c.Assert(err, jc.ErrorIsNil)
   218  	c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> new:mymodel\n")
   219  	s.CheckCalls(c, []testing.StubCall{
   220  		{"ReadCurrentController", nil},
   221  		{"WriteCurrentController", []interface{}{"new"}},
   222  	})
   223  }
   224  
   225  func (s *SwitchSimpleSuite) TestSwitchUnknownNoCurrentController(c *gc.C) {
   226  	_, err := s.run(c, "unknown")
   227  	c.Assert(err, gc.ErrorMatches, `"unknown" is not the name of a model or controller`)
   228  	s.CheckCalls(c, []testing.StubCall{
   229  		{"ReadCurrentController", nil},
   230  	})
   231  }
   232  
   233  func (s *SwitchSimpleSuite) TestSwitchUnknownCurrentControllerRefreshModels(c *gc.C) {
   234  	s.currentController = "ctrl"
   235  	s.addController(c, "ctrl")
   236  	s.onRefresh = func() {
   237  		s.store.Models["ctrl"] = jujuclient.ControllerAccountModels{
   238  			map[string]*jujuclient.AccountModels{
   239  				"admin@local": {
   240  					Models: map[string]jujuclient.ModelDetails{"unknown": {}},
   241  				},
   242  			},
   243  		}
   244  	}
   245  	ctx, err := s.run(c, "unknown")
   246  	c.Assert(err, jc.ErrorIsNil)
   247  	c.Assert(coretesting.Stderr(ctx), gc.Equals, "ctrl (controller) -> ctrl:unknown\n")
   248  	s.CheckCalls(c, []testing.StubCall{
   249  		{"ReadCurrentController", nil},
   250  		{"RefreshModels", []interface{}{s.store, "ctrl", "admin@local"}},
   251  	})
   252  }
   253  
   254  func (s *SwitchSimpleSuite) TestSwitchUnknownCurrentControllerRefreshModelsStillUnknown(c *gc.C) {
   255  	s.currentController = "ctrl"
   256  	s.addController(c, "ctrl")
   257  	_, err := s.run(c, "unknown")
   258  	c.Assert(err, gc.ErrorMatches, `"unknown" is not the name of a model or controller`)
   259  	s.CheckCalls(c, []testing.StubCall{
   260  		{"ReadCurrentController", nil},
   261  		{"RefreshModels", []interface{}{s.store, "ctrl", "admin@local"}},
   262  	})
   263  }
   264  
   265  func (s *SwitchSimpleSuite) TestSwitchUnknownCurrentControllerRefreshModelsFails(c *gc.C) {
   266  	s.currentController = "ctrl"
   267  	s.addController(c, "ctrl")
   268  	s.SetErrors(nil, errors.New("not very refreshing"))
   269  	_, err := s.run(c, "unknown")
   270  	c.Assert(err, gc.ErrorMatches, "refreshing models cache: not very refreshing")
   271  	s.CheckCalls(c, []testing.StubCall{
   272  		{"ReadCurrentController", nil},
   273  		{"RefreshModels", []interface{}{s.store, "ctrl", "admin@local"}},
   274  	})
   275  }
   276  
   277  func (s *SwitchSimpleSuite) TestSettingWhenEnvVarSet(c *gc.C) {
   278  	os.Setenv("JUJU_MODEL", "using-model")
   279  	_, err := s.run(c, "erewhemos-2")
   280  	c.Assert(err, gc.ErrorMatches, `cannot switch when JUJU_MODEL is overriding the model \(set to "using-model"\)`)
   281  }
   282  
   283  func (s *SwitchSimpleSuite) TestTooManyParams(c *gc.C) {
   284  	_, err := s.run(c, "foo", "bar")
   285  	c.Assert(err, gc.ErrorMatches, `unrecognized args: ."bar".`)
   286  }
   287  
   288  func (s *SwitchSimpleSuite) addController(c *gc.C, name string) {
   289  	s.store.Controllers[name] = jujuclient.ControllerDetails{}
   290  	s.store.Accounts[name] = &jujuclient.ControllerAccounts{
   291  		CurrentAccount: "admin@local",
   292  	}
   293  }