github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/controller/listcontrollers_test.go (about) 1 // Copyright 2015,2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package controller_test 5 6 import ( 7 "encoding/json" 8 "fmt" 9 10 "github.com/juju/cmd" 11 "github.com/juju/cmd/cmdtesting" 12 "github.com/juju/errors" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/api/base" 17 "github.com/juju/juju/cmd/juju/controller" 18 "github.com/juju/juju/cmd/modelcmd" 19 "github.com/juju/juju/core/model" 20 "github.com/juju/juju/jujuclient" 21 "github.com/juju/juju/jujuclient/jujuclienttesting" 22 ) 23 24 type ListControllersSuite struct { 25 baseControllerSuite 26 api func(string) controller.ControllerAccessAPI 27 } 28 29 var _ = gc.Suite(&ListControllersSuite{}) 30 31 func (s *ListControllersSuite) TestListControllersEmptyStore(c *gc.C) { 32 s.store = jujuclient.NewMemStore() 33 _, err := s.runListControllers(c) 34 c.Check(errors.Cause(err), gc.Equals, modelcmd.ErrNoControllersDefined) 35 } 36 37 func (s *ListControllersSuite) TestListControllers(c *gc.C) { 38 store := s.createTestClientStore(c) 39 delete(store.Accounts, "aws-test") 40 originallyInStore := &jujuclient.ControllerModels{ 41 CurrentModel: "admin/my-model", 42 Models: map[string]jujuclient.ModelDetails{ 43 "model0": {ModelUUID: "abc", ModelType: model.IAAS}, 44 "my-model": {ModelUUID: "def", ModelType: model.IAAS}, 45 }, 46 } 47 c.Assert(store.Models["mallards"], gc.DeepEquals, originallyInStore) 48 49 s.expectedOutput = ` 50 Use --refresh option with this command to see the latest information. 51 52 Controller Model User Access Cloud/Region Models Machines HA Version 53 aws-test admin/controller - - aws/us-east-1 1 5 - 2.0.1 54 mallards* my-model admin superuser mallards/mallards1 2 - - (unknown) 55 mark-test-prodstack - admin (unknown) prodstack - - - (unknown) 56 57 `[1:] 58 59 s.assertListControllers(c) 60 // Check store was not updated. 61 c.Assert(store.Models["mallards"], gc.DeepEquals, originallyInStore) 62 } 63 64 func (s *ListControllersSuite) TestListControllersRefresh(c *gc.C) { 65 store := s.createTestClientStore(c) 66 originallyInStore := &jujuclient.ControllerModels{ 67 CurrentModel: "admin/my-model", 68 Models: map[string]jujuclient.ModelDetails{ 69 "model0": {ModelUUID: "abc", ModelType: model.IAAS}, 70 "my-model": {ModelUUID: "def", ModelType: model.IAAS}, 71 }, 72 } 73 c.Assert(store.Models["mallards"], gc.DeepEquals, originallyInStore) 74 s.api = func(controllerName string) controller.ControllerAccessAPI { 75 fakeController := &fakeController{controllerName: controllerName} 76 return fakeController 77 } 78 s.expectedOutput = ` 79 Controller Model User Access Cloud/Region Models Machines HA Version 80 aws-test controller admin (unknown) aws/us-east-1 1 2 - 2.0.1 81 mallards* my-model admin superuser mallards/mallards1 2 4 - (unknown) 82 mark-test-prodstack - admin (unknown) prodstack - - - (unknown) 83 84 `[1:] 85 s.assertListControllers(c, "--refresh") 86 // Check store was updated. 87 c.Assert(store.Models["mallards"], gc.DeepEquals, &jujuclient.ControllerModels{ 88 CurrentModel: "admin/my-model", 89 Models: map[string]jujuclient.ModelDetails{ 90 "admin/controller": {ModelUUID: "abc", ModelType: model.IAAS}, 91 "admin/my-model": {ModelUUID: "def", ModelType: model.IAAS}, 92 }, 93 }) 94 } 95 96 func (s *ListControllersSuite) setupAPIForControllerMachines() { 97 s.api = func(controllerName string) controller.ControllerAccessAPI { 98 fakeController := &fakeController{controllerName: controllerName} 99 switch controllerName { 100 case "aws-test": 101 fakeController.machines = map[string][]base.Machine{ 102 "ghi": { 103 {Id: "1", HasVote: true, WantsVote: true, Status: "active"}, 104 {Id: "2", HasVote: true, WantsVote: true, Status: "down"}, 105 {Id: "3", HasVote: false, WantsVote: true, Status: "active"}, 106 }, 107 } 108 case "mallards": 109 fakeController.machines = map[string][]base.Machine{ 110 "abc": { 111 {Id: "1", HasVote: true, WantsVote: true, Status: "active"}, 112 }, 113 "def": { 114 {Id: "1", HasVote: true, WantsVote: true, Status: "active"}, 115 }, 116 } 117 } 118 return fakeController 119 } 120 } 121 122 func (s *ListControllersSuite) TestListControllersKnownHAStatus(c *gc.C) { 123 s.createTestClientStore(c) 124 s.setupAPIForControllerMachines() 125 s.expectedOutput = ` 126 Controller Model User Access Cloud/Region Models Machines HA Version 127 aws-test controller admin (unknown) aws/us-east-1 1 2 1/3 2.0.1 128 mallards* my-model admin superuser mallards/mallards1 2 4 none (unknown) 129 mark-test-prodstack - admin (unknown) prodstack - - - (unknown) 130 131 `[1:] 132 s.assertListControllers(c, "--refresh") 133 } 134 135 func (s *ListControllersSuite) TestListControllersYaml(c *gc.C) { 136 s.expectedOutput = ` 137 controllers: 138 aws-test: 139 current-model: controller 140 user: admin 141 recent-server: this-is-aws-test-of-many-api-endpoints 142 uuid: this-is-the-aws-test-uuid 143 controller-uuid: this-is-the-aws-test-uuid 144 api-endpoints: [this-is-aws-test-of-many-api-endpoints] 145 ca-cert: this-is-aws-test-ca-cert 146 cloud: aws 147 region: us-east-1 148 agent-version: 2.0.1 149 model-count: 1 150 machine-count: 2 151 controller-machines: 152 active: 1 153 total: 3 154 mallards: 155 current-model: my-model 156 user: admin 157 access: superuser 158 recent-server: this-is-another-of-many-api-endpoints 159 uuid: this-is-another-uuid 160 controller-uuid: this-is-another-uuid 161 api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints] 162 ca-cert: this-is-another-ca-cert 163 cloud: mallards 164 region: mallards1 165 model-count: 2 166 machine-count: 4 167 controller-machines: 168 active: 1 169 total: 1 170 mark-test-prodstack: 171 user: admin 172 recent-server: this-is-one-of-many-api-endpoints 173 uuid: this-is-a-uuid 174 controller-uuid: this-is-a-uuid 175 api-endpoints: [this-is-one-of-many-api-endpoints] 176 ca-cert: this-is-a-ca-cert 177 cloud: prodstack 178 current-controller: mallards 179 `[1:] 180 181 s.createTestClientStore(c) 182 s.setupAPIForControllerMachines() 183 s.assertListControllers(c, "--format", "yaml", "--refresh") 184 } 185 186 func intPtr(i int) *int { 187 return &i 188 } 189 190 func (s *ListControllersSuite) TestListControllersJson(c *gc.C) { 191 s.expectedOutput = "" 192 s.createTestClientStore(c) 193 jsonOut := s.assertListControllers(c, "--format", "json") 194 var result controller.ControllerSet 195 err := json.Unmarshal([]byte(jsonOut), &result) 196 c.Assert(err, jc.ErrorIsNil) 197 c.Assert(result, jc.DeepEquals, controller.ControllerSet{ 198 Controllers: map[string]controller.ControllerItem{ 199 "aws-test": { 200 ControllerUUID: "this-is-the-aws-test-uuid", 201 ModelName: "controller", 202 User: "admin", 203 Server: "this-is-aws-test-of-many-api-endpoints", 204 APIEndpoints: []string{"this-is-aws-test-of-many-api-endpoints"}, 205 CACert: "this-is-aws-test-ca-cert", 206 Cloud: "aws", 207 CloudRegion: "us-east-1", 208 AgentVersion: "2.0.1", 209 ModelCount: intPtr(1), 210 MachineCount: intPtr(5), 211 }, 212 "mallards": { 213 ControllerUUID: "this-is-another-uuid", 214 ModelName: "my-model", 215 User: "admin", 216 Access: "superuser", 217 Server: "this-is-another-of-many-api-endpoints", 218 APIEndpoints: []string{"this-is-another-of-many-api-endpoints", "this-is-one-more-of-many-api-endpoints"}, 219 CACert: "this-is-another-ca-cert", 220 Cloud: "mallards", 221 CloudRegion: "mallards1", 222 ModelCount: intPtr(2), 223 }, 224 "mark-test-prodstack": { 225 ControllerUUID: "this-is-a-uuid", 226 User: "admin", 227 Server: "this-is-one-of-many-api-endpoints", 228 APIEndpoints: []string{"this-is-one-of-many-api-endpoints"}, 229 CACert: "this-is-a-ca-cert", 230 Cloud: "prodstack", 231 }, 232 }, 233 CurrentController: "mallards", 234 }) 235 } 236 237 func (s *ListControllersSuite) TestListControllersReadFromStoreErr(c *gc.C) { 238 msg := "fail getting all controllers" 239 errStore := jujuclienttesting.NewStubStore() 240 errStore.SetErrors(errors.New(msg)) 241 s.store = errStore 242 s.expectedErr = fmt.Sprintf("failed to list controllers: %v", msg) 243 s.assertListControllersFailed(c) 244 errStore.CheckCallNames(c, "AllControllers") 245 } 246 247 func (s *ListControllersSuite) TestListControllersUnrecognizedArg(c *gc.C) { 248 s.createTestClientStore(c) 249 s.expectedErr = `unrecognized args: \["whoops"\]` 250 s.assertListControllersFailed(c, "whoops") 251 } 252 253 func (s *ListControllersSuite) TestListControllersUnrecognizedFlag(c *gc.C) { 254 s.createTestClientStore(c) 255 s.expectedErr = `option provided but not defined: -m` 256 s.assertListControllersFailed(c, "-m", "my.world") 257 } 258 259 func (s *ListControllersSuite) TestListControllersUnrecognizedOptionFlag(c *gc.C) { 260 s.createTestClientStore(c) 261 s.expectedErr = `option provided but not defined: --model` 262 s.assertListControllersFailed(c, "--model", "still.my.world") 263 } 264 265 func (s *ListControllersSuite) TestListControllersNoControllers(c *gc.C) { 266 store := s.createTestClientStore(c) 267 store.Controllers = map[string]jujuclient.ControllerDetails{} 268 s.expectedErr = `No controllers registered. 269 270 Please either create a new controller using "juju bootstrap" or connect to 271 another controller that you have been given access to using "juju register". 272 ` 273 s.assertListControllersFailed(c) 274 } 275 276 func (s *ListControllersSuite) runListControllers(c *gc.C, args ...string) (*cmd.Context, error) { 277 return cmdtesting.RunCommand(c, controller.NewListControllersCommandForTest(s.store, s.api), args...) 278 } 279 280 func (s *ListControllersSuite) assertListControllersFailed(c *gc.C, args ...string) { 281 _, err := s.runListControllers(c, args...) 282 c.Assert(err, gc.ErrorMatches, s.expectedErr) 283 } 284 285 func (s *ListControllersSuite) assertListControllers(c *gc.C, args ...string) string { 286 context, err := s.runListControllers(c, args...) 287 c.Assert(err, jc.ErrorIsNil) 288 output := cmdtesting.Stdout(context) 289 if s.expectedOutput != "" { 290 c.Assert(output, gc.Equals, s.expectedOutput) 291 } 292 return output 293 }