github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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  	stubStore *jujuclienttesting.StubStore
    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.stubStore = jujuclienttesting.WrapClientStore(s.store)
    37  	s.onRefresh = nil
    38  }
    39  
    40  func (s *SwitchSimpleSuite) refreshModels(store jujuclient.ClientStore, controllerName string) error {
    41  	s.MethodCall(s, "RefreshModels", store, controllerName)
    42  	if s.onRefresh != nil {
    43  		s.onRefresh()
    44  	}
    45  	return s.NextErr()
    46  }
    47  
    48  func (s *SwitchSimpleSuite) run(c *gc.C, args ...string) (*cmd.Context, error) {
    49  	cmd := &switchCommand{
    50  		Store:         s.stubStore,
    51  		RefreshModels: s.refreshModels,
    52  	}
    53  	return coretesting.RunCommand(c, modelcmd.WrapBase(cmd), args...)
    54  }
    55  
    56  func (s *SwitchSimpleSuite) TestNoArgs(c *gc.C) {
    57  	_, err := s.run(c)
    58  	c.Assert(err, gc.ErrorMatches, "no currently specified model")
    59  }
    60  
    61  func (s *SwitchSimpleSuite) TestNoArgsCurrentController(c *gc.C) {
    62  	s.addController(c, "a-controller")
    63  	s.store.CurrentControllerName = "a-controller"
    64  	ctx, err := s.run(c)
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	c.Assert(coretesting.Stdout(ctx), gc.Equals, "a-controller\n")
    67  }
    68  
    69  func (s *SwitchSimpleSuite) TestUnknownControllerNameReturnsError(c *gc.C) {
    70  	s.addController(c, "a-controller")
    71  	s.store.CurrentControllerName = "a-controller"
    72  	_, err := s.run(c, "another-controller:modela")
    73  	c.Assert(err, gc.ErrorMatches, "controller another-controller not found")
    74  }
    75  
    76  func (s *SwitchSimpleSuite) TestNoArgsCurrentModel(c *gc.C) {
    77  	s.addController(c, "a-controller")
    78  	s.store.CurrentControllerName = "a-controller"
    79  	s.store.Models["a-controller"] = &jujuclient.ControllerModels{
    80  		Models:       map[string]jujuclient.ModelDetails{"admin@local/mymodel": {}},
    81  		CurrentModel: "admin@local/mymodel",
    82  	}
    83  	ctx, err := s.run(c)
    84  	c.Assert(err, jc.ErrorIsNil)
    85  	c.Assert(coretesting.Stdout(ctx), gc.Equals, "a-controller:admin@local/mymodel\n")
    86  }
    87  
    88  func (s *SwitchSimpleSuite) TestSwitchWritesCurrentController(c *gc.C) {
    89  	s.addController(c, "a-controller")
    90  	context, err := s.run(c, "a-controller")
    91  	c.Assert(err, jc.ErrorIsNil)
    92  	c.Assert(coretesting.Stderr(context), gc.Equals, " -> a-controller (controller)\n")
    93  	s.stubStore.CheckCalls(c, []testing.StubCall{
    94  		{"CurrentController", nil},
    95  		{"ControllerByName", []interface{}{"a-controller"}},
    96  		{"CurrentModel", []interface{}{"a-controller"}},
    97  		{"SetCurrentController", []interface{}{"a-controller"}},
    98  	})
    99  }
   100  
   101  func (s *SwitchSimpleSuite) TestSwitchWithCurrentController(c *gc.C) {
   102  	s.store.CurrentControllerName = "old"
   103  	s.addController(c, "new")
   104  	context, err := s.run(c, "new")
   105  	c.Assert(err, jc.ErrorIsNil)
   106  	c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> new (controller)\n")
   107  }
   108  
   109  func (s *SwitchSimpleSuite) TestSwitchLocalControllerWithCurrent(c *gc.C) {
   110  	s.store.CurrentControllerName = "old"
   111  	s.addController(c, "new")
   112  	context, err := s.run(c, "new")
   113  	c.Assert(err, jc.ErrorIsNil)
   114  	c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> new (controller)\n")
   115  }
   116  
   117  func (s *SwitchSimpleSuite) TestSwitchSameController(c *gc.C) {
   118  	s.store.CurrentControllerName = "same"
   119  	s.addController(c, "same")
   120  	context, err := s.run(c, "same")
   121  	c.Assert(err, jc.ErrorIsNil)
   122  	c.Assert(coretesting.Stderr(context), gc.Equals, "same (controller) (no change)\n")
   123  	s.stubStore.CheckCalls(c, []testing.StubCall{
   124  		{"CurrentController", nil},
   125  		{"CurrentModel", []interface{}{"same"}},
   126  		{"ControllerByName", []interface{}{"same"}},
   127  	})
   128  }
   129  
   130  func (s *SwitchSimpleSuite) TestSwitchControllerToModel(c *gc.C) {
   131  	s.store.CurrentControllerName = "ctrl"
   132  	s.addController(c, "ctrl")
   133  	s.store.Models["ctrl"] = &jujuclient.ControllerModels{
   134  		Models: map[string]jujuclient.ModelDetails{"admin@local/mymodel": {}},
   135  	}
   136  	context, err := s.run(c, "mymodel")
   137  	c.Assert(err, jc.ErrorIsNil)
   138  	c.Assert(coretesting.Stderr(context), gc.Equals, "ctrl (controller) -> ctrl:admin@local/mymodel\n")
   139  	s.stubStore.CheckCalls(c, []testing.StubCall{
   140  		{"CurrentController", nil},
   141  		{"CurrentModel", []interface{}{"ctrl"}},
   142  		{"ControllerByName", []interface{}{"mymodel"}},
   143  		{"AccountDetails", []interface{}{"ctrl"}},
   144  		{"SetCurrentModel", []interface{}{"ctrl", "admin@local/mymodel"}},
   145  	})
   146  	c.Assert(s.store.Models["ctrl"].CurrentModel, gc.Equals, "admin@local/mymodel")
   147  }
   148  
   149  func (s *SwitchSimpleSuite) TestSwitchControllerToModelDifferentController(c *gc.C) {
   150  	s.store.CurrentControllerName = "old"
   151  	s.addController(c, "new")
   152  	s.store.Models["new"] = &jujuclient.ControllerModels{
   153  		Models: map[string]jujuclient.ModelDetails{"admin@local/mymodel": {}},
   154  	}
   155  	context, err := s.run(c, "new:mymodel")
   156  	c.Assert(err, jc.ErrorIsNil)
   157  	c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> new:admin@local/mymodel\n")
   158  	s.stubStore.CheckCalls(c, []testing.StubCall{
   159  		{"CurrentController", nil},
   160  		{"CurrentModel", []interface{}{"old"}},
   161  		{"ControllerByName", []interface{}{"new:mymodel"}},
   162  		{"ControllerByName", []interface{}{"new"}},
   163  		{"AccountDetails", []interface{}{"new"}},
   164  		{"SetCurrentModel", []interface{}{"new", "admin@local/mymodel"}},
   165  		{"SetCurrentController", []interface{}{"new"}},
   166  	})
   167  	c.Assert(s.store.Models["new"].CurrentModel, gc.Equals, "admin@local/mymodel")
   168  }
   169  
   170  func (s *SwitchSimpleSuite) TestSwitchLocalControllerToModelDifferentController(c *gc.C) {
   171  	s.store.CurrentControllerName = "old"
   172  	s.addController(c, "new")
   173  	s.store.Models["new"] = &jujuclient.ControllerModels{
   174  		Models: map[string]jujuclient.ModelDetails{"admin@local/mymodel": {}},
   175  	}
   176  	context, err := s.run(c, "new:mymodel")
   177  	c.Assert(err, jc.ErrorIsNil)
   178  	c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> new:admin@local/mymodel\n")
   179  	s.stubStore.CheckCalls(c, []testing.StubCall{
   180  		{"CurrentController", nil},
   181  		{"CurrentModel", []interface{}{"old"}},
   182  		{"ControllerByName", []interface{}{"new:mymodel"}},
   183  		{"ControllerByName", []interface{}{"new"}},
   184  		{"AccountDetails", []interface{}{"new"}},
   185  		{"SetCurrentModel", []interface{}{"new", "admin@local/mymodel"}},
   186  		{"SetCurrentController", []interface{}{"new"}},
   187  	})
   188  	c.Assert(s.store.Models["new"].CurrentModel, gc.Equals, "admin@local/mymodel")
   189  }
   190  
   191  func (s *SwitchSimpleSuite) TestSwitchControllerToDifferentControllerCurrentModel(c *gc.C) {
   192  	s.store.CurrentControllerName = "old"
   193  	s.addController(c, "new")
   194  	s.store.Models["new"] = &jujuclient.ControllerModels{
   195  		Models:       map[string]jujuclient.ModelDetails{"admin@local/mymodel": {}},
   196  		CurrentModel: "admin@local/mymodel",
   197  	}
   198  	context, err := s.run(c, "new:mymodel")
   199  	c.Assert(err, jc.ErrorIsNil)
   200  	c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> new:admin@local/mymodel\n")
   201  	s.stubStore.CheckCalls(c, []testing.StubCall{
   202  		{"CurrentController", nil},
   203  		{"CurrentModel", []interface{}{"old"}},
   204  		{"ControllerByName", []interface{}{"new:mymodel"}},
   205  		{"ControllerByName", []interface{}{"new"}},
   206  		{"AccountDetails", []interface{}{"new"}},
   207  		{"SetCurrentModel", []interface{}{"new", "admin@local/mymodel"}},
   208  		{"SetCurrentController", []interface{}{"new"}},
   209  	})
   210  }
   211  
   212  func (s *SwitchSimpleSuite) TestSwitchToModelDifferentOwner(c *gc.C) {
   213  	s.store.CurrentControllerName = "same"
   214  	s.addController(c, "same")
   215  	s.store.Models["same"] = &jujuclient.ControllerModels{
   216  		Models: map[string]jujuclient.ModelDetails{
   217  			"admin@local/mymodel":  {},
   218  			"bianca@local/mymodel": {},
   219  		},
   220  		CurrentModel: "admin@local/mymodel",
   221  	}
   222  	context, err := s.run(c, "bianca/mymodel")
   223  	c.Assert(err, jc.ErrorIsNil)
   224  	c.Assert(coretesting.Stderr(context), gc.Equals, "same:admin@local/mymodel -> same:bianca@local/mymodel\n")
   225  	c.Assert(s.store.Models["same"].CurrentModel, gc.Equals, "bianca@local/mymodel")
   226  }
   227  
   228  func (s *SwitchSimpleSuite) TestSwitchUnknownNoCurrentController(c *gc.C) {
   229  	_, err := s.run(c, "unknown")
   230  	c.Assert(err, gc.ErrorMatches, `"unknown" is not the name of a model or controller`)
   231  	s.stubStore.CheckCalls(c, []testing.StubCall{
   232  		{"CurrentController", nil},
   233  		{"ControllerByName", []interface{}{"unknown"}},
   234  	})
   235  }
   236  
   237  func (s *SwitchSimpleSuite) TestSwitchUnknownCurrentControllerRefreshModels(c *gc.C) {
   238  	s.store.CurrentControllerName = "ctrl"
   239  	s.addController(c, "ctrl")
   240  	s.onRefresh = func() {
   241  		s.store.Models["ctrl"] = &jujuclient.ControllerModels{
   242  			Models: map[string]jujuclient.ModelDetails{"admin@local/unknown": {}},
   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:admin@local/unknown\n")
   248  	s.CheckCallNames(c, "RefreshModels")
   249  }
   250  
   251  func (s *SwitchSimpleSuite) TestSwitchUnknownCurrentControllerRefreshModelsStillUnknown(c *gc.C) {
   252  	s.store.CurrentControllerName = "ctrl"
   253  	s.addController(c, "ctrl")
   254  	_, err := s.run(c, "unknown")
   255  	c.Assert(err, gc.ErrorMatches, `"unknown" is not the name of a model or controller`)
   256  	s.CheckCallNames(c, "RefreshModels")
   257  }
   258  
   259  func (s *SwitchSimpleSuite) TestSwitchUnknownCurrentControllerRefreshModelsFails(c *gc.C) {
   260  	s.store.CurrentControllerName = "ctrl"
   261  	s.addController(c, "ctrl")
   262  	s.SetErrors(errors.New("not very refreshing"))
   263  	_, err := s.run(c, "unknown")
   264  	c.Assert(err, gc.ErrorMatches, "refreshing models cache: not very refreshing")
   265  	s.CheckCallNames(c, "RefreshModels")
   266  }
   267  
   268  func (s *SwitchSimpleSuite) TestSettingWhenEnvVarSet(c *gc.C) {
   269  	os.Setenv("JUJU_MODEL", "using-model")
   270  	_, err := s.run(c, "erewhemos-2")
   271  	c.Assert(err, gc.ErrorMatches, `cannot switch when JUJU_MODEL is overriding the model \(set to "using-model"\)`)
   272  }
   273  
   274  func (s *SwitchSimpleSuite) TestTooManyParams(c *gc.C) {
   275  	_, err := s.run(c, "foo", "bar")
   276  	c.Assert(err, gc.ErrorMatches, `unrecognized args: ."bar".`)
   277  }
   278  
   279  func (s *SwitchSimpleSuite) addController(c *gc.C, name string) {
   280  	s.store.Controllers[name] = jujuclient.ControllerDetails{}
   281  	s.store.Accounts[name] = jujuclient.AccountDetails{
   282  		User: "admin@local",
   283  	}
   284  }