github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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) TestNoArgsCurrentModel(c *gc.C) { 86 s.addController(c, "a-controller") 87 s.currentController = "a-controller" 88 s.store.Models["a-controller"] = jujuclient.ControllerAccountModels{ 89 map[string]*jujuclient.AccountModels{ 90 "admin@local": { 91 Models: map[string]jujuclient.ModelDetails{"mymodel": {}}, 92 CurrentModel: "mymodel", 93 }, 94 }, 95 } 96 ctx, err := s.run(c) 97 c.Assert(err, jc.ErrorIsNil) 98 c.Assert(coretesting.Stdout(ctx), gc.Equals, "a-controller:mymodel\n") 99 } 100 101 func (s *SwitchSimpleSuite) TestSwitchWritesCurrentController(c *gc.C) { 102 s.addController(c, "a-controller") 103 context, err := s.run(c, "a-controller") 104 c.Assert(err, jc.ErrorIsNil) 105 c.Assert(coretesting.Stderr(context), gc.Equals, " -> a-controller (controller)\n") 106 s.CheckCalls(c, []testing.StubCall{ 107 {"ReadCurrentController", nil}, 108 {"WriteCurrentController", []interface{}{"a-controller"}}, 109 }) 110 } 111 112 func (s *SwitchSimpleSuite) TestSwitchWithCurrentController(c *gc.C) { 113 s.currentController = "old" 114 s.addController(c, "new") 115 context, err := s.run(c, "new") 116 c.Assert(err, jc.ErrorIsNil) 117 c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> new (controller)\n") 118 } 119 120 func (s *SwitchSimpleSuite) TestSwitchLocalControllerWithCurrent(c *gc.C) { 121 s.currentController = "old" 122 s.addController(c, "local.new") 123 context, err := s.run(c, "new") 124 c.Assert(err, jc.ErrorIsNil) 125 c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> local.new (controller)\n") 126 } 127 128 func (s *SwitchSimpleSuite) TestSwitchSameController(c *gc.C) { 129 s.currentController = "same" 130 s.addController(c, "same") 131 context, err := s.run(c, "same") 132 c.Assert(err, jc.ErrorIsNil) 133 c.Assert(coretesting.Stderr(context), gc.Equals, "same (controller) (no change)\n") 134 s.CheckCalls(c, []testing.StubCall{ 135 {"ReadCurrentController", nil}, 136 }) 137 } 138 139 func (s *SwitchSimpleSuite) TestSwitchControllerToModel(c *gc.C) { 140 s.currentController = "ctrl" 141 s.addController(c, "ctrl") 142 s.store.Models["ctrl"] = jujuclient.ControllerAccountModels{ 143 map[string]*jujuclient.AccountModels{ 144 "admin@local": { 145 Models: map[string]jujuclient.ModelDetails{"mymodel": {}}, 146 }, 147 }, 148 } 149 context, err := s.run(c, "mymodel") 150 c.Assert(err, jc.ErrorIsNil) 151 c.Assert(coretesting.Stderr(context), gc.Equals, "ctrl (controller) -> ctrl:mymodel\n") 152 s.CheckCalls(c, []testing.StubCall{ 153 {"ReadCurrentController", nil}, 154 }) 155 c.Assert(s.store.Models["ctrl"].AccountModels["admin@local"].CurrentModel, gc.Equals, "mymodel") 156 } 157 158 func (s *SwitchSimpleSuite) TestSwitchControllerToModelDifferentController(c *gc.C) { 159 s.currentController = "old" 160 s.addController(c, "new") 161 s.store.Models["new"] = jujuclient.ControllerAccountModels{ 162 map[string]*jujuclient.AccountModels{ 163 "admin@local": { 164 Models: map[string]jujuclient.ModelDetails{"mymodel": {}}, 165 }, 166 }, 167 } 168 context, err := s.run(c, "new:mymodel") 169 c.Assert(err, jc.ErrorIsNil) 170 c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> new:mymodel\n") 171 s.CheckCalls(c, []testing.StubCall{ 172 {"ReadCurrentController", nil}, 173 {"WriteCurrentController", []interface{}{"new"}}, 174 }) 175 c.Assert(s.store.Models["new"].AccountModels["admin@local"].CurrentModel, gc.Equals, "mymodel") 176 } 177 178 func (s *SwitchSimpleSuite) TestSwitchLocalControllerToModelDifferentController(c *gc.C) { 179 s.currentController = "old" 180 s.addController(c, "local.new") 181 s.store.Models["local.new"] = jujuclient.ControllerAccountModels{ 182 map[string]*jujuclient.AccountModels{ 183 "admin@local": { 184 Models: map[string]jujuclient.ModelDetails{"mymodel": {}}, 185 }, 186 }, 187 } 188 context, err := s.run(c, "new:mymodel") 189 c.Assert(err, jc.ErrorIsNil) 190 c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> local.new:mymodel\n") 191 s.CheckCalls(c, []testing.StubCall{ 192 {"ReadCurrentController", nil}, 193 {"WriteCurrentController", []interface{}{"local.new"}}, 194 }) 195 c.Assert(s.store.Models["local.new"].AccountModels["admin@local"].CurrentModel, gc.Equals, "mymodel") 196 } 197 198 func (s *SwitchSimpleSuite) TestSwitchControllerToDifferentControllerCurrentModel(c *gc.C) { 199 s.currentController = "old" 200 s.addController(c, "new") 201 s.store.Models["new"] = jujuclient.ControllerAccountModels{ 202 map[string]*jujuclient.AccountModels{ 203 "admin@local": { 204 Models: map[string]jujuclient.ModelDetails{"mymodel": {}}, 205 CurrentModel: "mymodel", 206 }, 207 }, 208 } 209 context, err := s.run(c, "new:mymodel") 210 c.Assert(err, jc.ErrorIsNil) 211 c.Assert(coretesting.Stderr(context), gc.Equals, "old (controller) -> new:mymodel\n") 212 s.CheckCalls(c, []testing.StubCall{ 213 {"ReadCurrentController", nil}, 214 {"WriteCurrentController", []interface{}{"new"}}, 215 }) 216 } 217 218 func (s *SwitchSimpleSuite) TestSwitchUnknownNoCurrentController(c *gc.C) { 219 _, err := s.run(c, "unknown") 220 c.Assert(err, gc.ErrorMatches, `"unknown" is not the name of a model or controller`) 221 s.CheckCalls(c, []testing.StubCall{ 222 {"ReadCurrentController", nil}, 223 }) 224 } 225 226 func (s *SwitchSimpleSuite) TestSwitchUnknownCurrentControllerRefreshModels(c *gc.C) { 227 s.currentController = "ctrl" 228 s.addController(c, "ctrl") 229 s.onRefresh = func() { 230 s.store.Models["ctrl"] = jujuclient.ControllerAccountModels{ 231 map[string]*jujuclient.AccountModels{ 232 "admin@local": { 233 Models: map[string]jujuclient.ModelDetails{"unknown": {}}, 234 }, 235 }, 236 } 237 } 238 ctx, err := s.run(c, "unknown") 239 c.Assert(err, jc.ErrorIsNil) 240 c.Assert(coretesting.Stderr(ctx), gc.Equals, "ctrl (controller) -> ctrl:unknown\n") 241 s.CheckCalls(c, []testing.StubCall{ 242 {"ReadCurrentController", nil}, 243 {"RefreshModels", []interface{}{s.store, "ctrl", "admin@local"}}, 244 }) 245 } 246 247 func (s *SwitchSimpleSuite) TestSwitchUnknownCurrentControllerRefreshModelsStillUnknown(c *gc.C) { 248 s.currentController = "ctrl" 249 s.addController(c, "ctrl") 250 _, err := s.run(c, "unknown") 251 c.Assert(err, gc.ErrorMatches, `"unknown" is not the name of a model or controller`) 252 s.CheckCalls(c, []testing.StubCall{ 253 {"ReadCurrentController", nil}, 254 {"RefreshModels", []interface{}{s.store, "ctrl", "admin@local"}}, 255 }) 256 } 257 258 func (s *SwitchSimpleSuite) TestSwitchUnknownCurrentControllerRefreshModelsFails(c *gc.C) { 259 s.currentController = "ctrl" 260 s.addController(c, "ctrl") 261 s.SetErrors(nil, errors.New("not very refreshing")) 262 _, err := s.run(c, "unknown") 263 c.Assert(err, gc.ErrorMatches, "refreshing models cache: not very refreshing") 264 s.CheckCalls(c, []testing.StubCall{ 265 {"ReadCurrentController", nil}, 266 {"RefreshModels", []interface{}{s.store, "ctrl", "admin@local"}}, 267 }) 268 } 269 270 func (s *SwitchSimpleSuite) TestSettingWhenEnvVarSet(c *gc.C) { 271 os.Setenv("JUJU_MODEL", "using-model") 272 _, err := s.run(c, "erewhemos-2") 273 c.Assert(err, gc.ErrorMatches, `cannot switch when JUJU_MODEL is overriding the model \(set to "using-model"\)`) 274 } 275 276 func (s *SwitchSimpleSuite) TestTooManyParams(c *gc.C) { 277 _, err := s.run(c, "foo", "bar") 278 c.Assert(err, gc.ErrorMatches, `unrecognized args: ."bar".`) 279 } 280 281 func (s *SwitchSimpleSuite) addController(c *gc.C, name string) { 282 s.store.Controllers[name] = jujuclient.ControllerDetails{} 283 s.store.Accounts[name] = &jujuclient.ControllerAccounts{ 284 CurrentAccount: "admin@local", 285 } 286 }