github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/controller/showcontroller_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package controller_test 5 6 import ( 7 "regexp" 8 9 "github.com/juju/cmd" 10 "github.com/juju/errors" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/cmd/juju/controller" 15 "github.com/juju/juju/cmd/modelcmd" 16 "github.com/juju/juju/jujuclient" 17 "github.com/juju/juju/jujuclient/jujuclienttesting" 18 "github.com/juju/juju/testing" 19 ) 20 21 type ShowControllerSuite struct { 22 baseControllerSuite 23 } 24 25 var _ = gc.Suite(&ShowControllerSuite{}) 26 27 func (s *ShowControllerSuite) TestShowOneControllerOneInStore(c *gc.C) { 28 s.controllersYaml = `controllers: 29 local.mallards: 30 uuid: this-is-another-uuid 31 api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints] 32 ca-cert: this-is-another-ca-cert 33 ` 34 s.createTestClientStore(c) 35 36 s.expectedOutput = ` 37 local.mallards: 38 details: 39 uuid: this-is-another-uuid 40 api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints] 41 ca-cert: this-is-another-ca-cert 42 accounts: 43 admin@local: 44 user: admin@local 45 models: 46 admin: 47 uuid: abc 48 my-model: 49 uuid: def 50 current-model: my-model 51 bob@local: 52 user: bob@local 53 bob@remote: 54 user: bob@remote 55 current-account: admin@local 56 `[1:] 57 58 s.assertShowController(c, "local.mallards") 59 } 60 61 func (s *ShowControllerSuite) TestShowControllerWithPasswords(c *gc.C) { 62 s.controllersYaml = `controllers: 63 local.mallards: 64 uuid: this-is-another-uuid 65 api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints] 66 ca-cert: this-is-another-ca-cert 67 ` 68 s.createTestClientStore(c) 69 70 s.expectedOutput = ` 71 local.mallards: 72 details: 73 uuid: this-is-another-uuid 74 api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints] 75 ca-cert: this-is-another-ca-cert 76 accounts: 77 admin@local: 78 user: admin@local 79 password: hunter2 80 models: 81 admin: 82 uuid: abc 83 my-model: 84 uuid: def 85 current-model: my-model 86 bob@local: 87 user: bob@local 88 password: huntert00 89 bob@remote: 90 user: bob@remote 91 current-account: admin@local 92 `[1:] 93 94 s.assertShowController(c, "local.mallards", "--show-passwords") 95 } 96 97 func (s *ShowControllerSuite) TestShowControllerWithBootstrapConfig(c *gc.C) { 98 s.controllersYaml = `controllers: 99 local.mallards: 100 uuid: this-is-another-uuid 101 api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints] 102 ca-cert: this-is-another-ca-cert 103 ` 104 store := s.createTestClientStore(c) 105 store.BootstrapConfig["local.mallards"] = jujuclient.BootstrapConfig{ 106 Config: map[string]interface{}{ 107 "name": "admin", 108 "type": "maas", 109 "extra": "value", 110 }, 111 Credential: "my-credential", 112 Cloud: "mallards", 113 CloudRegion: "mallards1", 114 CloudEndpoint: "http://mallards.local/MAAS", 115 } 116 117 s.expectedOutput = ` 118 local.mallards: 119 details: 120 uuid: this-is-another-uuid 121 api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints] 122 ca-cert: this-is-another-ca-cert 123 accounts: 124 admin@local: 125 user: admin@local 126 models: 127 admin: 128 uuid: abc 129 my-model: 130 uuid: def 131 current-model: my-model 132 bob@local: 133 user: bob@local 134 bob@remote: 135 user: bob@remote 136 current-account: admin@local 137 bootstrap-config: 138 config: 139 extra: value 140 cloud: mallards 141 cloud-type: maas 142 region: mallards1 143 endpoint: http://mallards.local/MAAS 144 credential: my-credential 145 `[1:] 146 147 s.assertShowController(c, "local.mallards") 148 } 149 150 func (s *ShowControllerSuite) TestShowOneControllerManyInStore(c *gc.C) { 151 s.createTestClientStore(c) 152 153 s.expectedOutput = ` 154 local.aws-test: 155 details: 156 uuid: this-is-the-aws-test-uuid 157 api-endpoints: [this-is-aws-test-of-many-api-endpoints] 158 ca-cert: this-is-aws-test-ca-cert 159 accounts: 160 admin@local: 161 user: admin@local 162 models: 163 admin: 164 uuid: ghi 165 current-model: admin 166 `[1:] 167 s.assertShowController(c, "local.aws-test") 168 } 169 170 func (s *ShowControllerSuite) TestShowSomeControllerMoreInStore(c *gc.C) { 171 s.createTestClientStore(c) 172 s.expectedOutput = ` 173 local.aws-test: 174 details: 175 uuid: this-is-the-aws-test-uuid 176 api-endpoints: [this-is-aws-test-of-many-api-endpoints] 177 ca-cert: this-is-aws-test-ca-cert 178 accounts: 179 admin@local: 180 user: admin@local 181 models: 182 admin: 183 uuid: ghi 184 current-model: admin 185 local.mark-test-prodstack: 186 details: 187 uuid: this-is-a-uuid 188 api-endpoints: [this-is-one-of-many-api-endpoints] 189 ca-cert: this-is-a-ca-cert 190 accounts: 191 admin@local: 192 user: admin@local 193 `[1:] 194 195 s.assertShowController(c, "local.aws-test", "local.mark-test-prodstack") 196 } 197 198 func (s *ShowControllerSuite) TestShowControllerJsonOne(c *gc.C) { 199 s.createTestClientStore(c) 200 201 s.expectedOutput = ` 202 {"local.aws-test":{"details":{"uuid":"this-is-the-aws-test-uuid","api-endpoints":["this-is-aws-test-of-many-api-endpoints"],"ca-cert":"this-is-aws-test-ca-cert"},"accounts":{"admin@local":{"user":"admin@local","models":{"admin":{"uuid":"ghi"}},"current-model":"admin"}}}} 203 `[1:] 204 205 s.assertShowController(c, "--format", "json", "local.aws-test") 206 } 207 208 func (s *ShowControllerSuite) TestShowControllerJsonMany(c *gc.C) { 209 s.createTestClientStore(c) 210 s.expectedOutput = ` 211 {"local.aws-test":{"details":{"uuid":"this-is-the-aws-test-uuid","api-endpoints":["this-is-aws-test-of-many-api-endpoints"],"ca-cert":"this-is-aws-test-ca-cert"},"accounts":{"admin@local":{"user":"admin@local","models":{"admin":{"uuid":"ghi"}},"current-model":"admin"}}},"local.mark-test-prodstack":{"details":{"uuid":"this-is-a-uuid","api-endpoints":["this-is-one-of-many-api-endpoints"],"ca-cert":"this-is-a-ca-cert"},"accounts":{"admin@local":{"user":"admin@local"}}}} 212 `[1:] 213 s.assertShowController(c, "--format", "json", "local.aws-test", "local.mark-test-prodstack") 214 } 215 216 func (s *ShowControllerSuite) TestShowControllerReadFromStoreErr(c *gc.C) { 217 s.createTestClientStore(c) 218 219 msg := "fail getting controller" 220 errStore := jujuclienttesting.NewStubStore() 221 errStore.SetErrors(errors.New(msg)) 222 s.store = errStore 223 s.expectedErr = msg 224 225 s.assertShowControllerFailed(c, "test1") 226 errStore.CheckCallNames(c, "ControllerByName") 227 } 228 229 func (s *ShowControllerSuite) TestShowControllerNoArgs(c *gc.C) { 230 s.createTestClientStore(c) 231 232 s.expectedOutput = ` 233 {"local.aws-test":{"details":{"uuid":"this-is-the-aws-test-uuid","api-endpoints":["this-is-aws-test-of-many-api-endpoints"],"ca-cert":"this-is-aws-test-ca-cert"},"accounts":{"admin@local":{"user":"admin@local","models":{"admin":{"uuid":"ghi"}},"current-model":"admin"}}}} 234 `[1:] 235 err := modelcmd.WriteCurrentController("local.aws-test") 236 c.Assert(err, jc.ErrorIsNil) 237 s.assertShowController(c, "--format", "json") 238 } 239 240 func (s *ShowControllerSuite) TestShowControllerNoArgsNoCurrent(c *gc.C) { 241 err := modelcmd.WriteCurrentController("") 242 c.Assert(err, jc.ErrorIsNil) 243 244 s.expectedErr = regexp.QuoteMeta(`there is no active controller`) 245 s.assertShowControllerFailed(c) 246 } 247 248 func (s *ShowControllerSuite) TestShowControllerNotFound(c *gc.C) { 249 s.createTestClientStore(c) 250 251 s.expectedErr = `controller whoops not found` 252 s.assertShowControllerFailed(c, "whoops") 253 } 254 255 func (s *ShowControllerSuite) TestShowControllerUnrecognizedFlag(c *gc.C) { 256 s.expectedErr = `flag provided but not defined: -m` 257 s.assertShowControllerFailed(c, "-m", "my.world") 258 } 259 260 func (s *ShowControllerSuite) TestShowControllerUnrecognizedOptionFlag(c *gc.C) { 261 s.expectedErr = `flag provided but not defined: --model` 262 s.assertShowControllerFailed(c, "--model", "still.my.world") 263 } 264 265 func (s *ShowControllerSuite) runShowController(c *gc.C, args ...string) (*cmd.Context, error) { 266 return testing.RunCommand(c, controller.NewShowControllerCommandForTest(s.store), args...) 267 } 268 269 func (s *ShowControllerSuite) assertShowControllerFailed(c *gc.C, args ...string) { 270 _, err := s.runShowController(c, args...) 271 c.Assert(err, gc.ErrorMatches, s.expectedErr) 272 } 273 274 func (s *ShowControllerSuite) assertShowController(c *gc.C, args ...string) { 275 context, err := s.runShowController(c, args...) 276 c.Assert(err, jc.ErrorIsNil) 277 c.Assert(testing.Stdout(context), gc.Equals, s.expectedOutput) 278 }