github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/apiinfo_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package commands 5 6 import ( 7 "fmt" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/cmd/envcmd" 13 "github.com/juju/juju/environs/configstore" 14 "github.com/juju/juju/testing" 15 ) 16 17 type APIInfoSuite struct { 18 testing.FakeJujuHomeSuite 19 } 20 21 var _ = gc.Suite(&APIInfoSuite{}) 22 23 func (s *APIInfoSuite) TestArgParsing(c *gc.C) { 24 for i, test := range []struct { 25 message string 26 args []string 27 refresh bool 28 user bool 29 password bool 30 cacert bool 31 servers bool 32 envuuid bool 33 srvuuid bool 34 errMatch string 35 }{ 36 { 37 message: "no args skips password", 38 user: true, 39 cacert: true, 40 servers: true, 41 envuuid: true, 42 srvuuid: true, 43 }, { 44 message: "password shown if user specifies", 45 args: []string{"--password"}, 46 user: true, 47 password: true, 48 cacert: true, 49 servers: true, 50 envuuid: true, 51 srvuuid: true, 52 }, { 53 message: "refresh the cache", 54 args: []string{"--refresh"}, 55 refresh: true, 56 user: true, 57 cacert: true, 58 servers: true, 59 envuuid: true, 60 srvuuid: true, 61 }, { 62 message: "just show the user field", 63 args: []string{"user"}, 64 user: true, 65 }, { 66 message: "just show the password field", 67 args: []string{"password"}, 68 password: true, 69 }, { 70 message: "just show the cacert field", 71 args: []string{"ca-cert"}, 72 cacert: true, 73 }, { 74 message: "just show the servers field", 75 args: []string{"state-servers"}, 76 servers: true, 77 }, { 78 message: "just show the envuuid field", 79 args: []string{"environ-uuid"}, 80 envuuid: true, 81 }, { 82 message: "just show the srvuuid field", 83 args: []string{"server-uuid"}, 84 srvuuid: true, 85 }, { 86 message: "show the user and password field", 87 args: []string{"user", "password"}, 88 user: true, 89 password: true, 90 }, { 91 message: "unknown field field", 92 args: []string{"foo"}, 93 errMatch: `unknown fields: "foo"`, 94 }, { 95 message: "multiple unknown fields", 96 args: []string{"user", "pwd", "foo"}, 97 errMatch: `unknown fields: "pwd", "foo"`, 98 }, 99 } { 100 c.Logf("test %v: %s", i, test.message) 101 command := &APIInfoCommand{} 102 err := testing.InitCommand(envcmd.Wrap(command), test.args) 103 if test.errMatch == "" { 104 c.Check(err, jc.ErrorIsNil) 105 c.Check(command.refresh, gc.Equals, test.refresh) 106 c.Check(command.user, gc.Equals, test.user) 107 c.Check(command.password, gc.Equals, test.password) 108 c.Check(command.cacert, gc.Equals, test.cacert) 109 c.Check(command.servers, gc.Equals, test.servers) 110 c.Check(command.envuuid, gc.Equals, test.envuuid) 111 c.Check(command.srvuuid, gc.Equals, test.srvuuid) 112 } else { 113 c.Check(err, gc.ErrorMatches, test.errMatch) 114 } 115 } 116 } 117 118 func (s *APIInfoSuite) TestOutput(c *gc.C) { 119 s.PatchValue(&endpoint, func(c envcmd.EnvCommandBase, refresh bool) (configstore.APIEndpoint, error) { 120 return configstore.APIEndpoint{ 121 Addresses: []string{"localhost:12345", "10.0.3.1:12345"}, 122 CACert: "this is the cacert", 123 EnvironUUID: "deadbeef-dead-beef-dead-deaddeaddead", 124 ServerUUID: "bad0f00d-dead-beef-0000-01234567899a", 125 }, nil 126 }) 127 s.PatchValue(&creds, func(c envcmd.EnvCommandBase) (configstore.APICredentials, error) { 128 return configstore.APICredentials{ 129 User: "tester", 130 Password: "sekrit", 131 }, nil 132 }) 133 134 for i, test := range []struct { 135 args []string 136 output string 137 errMatch string 138 }{ 139 { 140 output: "" + 141 "user: tester\n" + 142 "environ-uuid: deadbeef-dead-beef-dead-deaddeaddead\n" + 143 "server-uuid: bad0f00d-dead-beef-0000-01234567899a\n" + 144 "state-servers:\n" + 145 "- localhost:12345\n" + 146 "- 10.0.3.1:12345\n" + 147 "ca-cert: this is the cacert\n", 148 }, { 149 args: []string{"--password"}, 150 output: "" + 151 "user: tester\n" + 152 "password: sekrit\n" + 153 "environ-uuid: deadbeef-dead-beef-dead-deaddeaddead\n" + 154 "server-uuid: bad0f00d-dead-beef-0000-01234567899a\n" + 155 "state-servers:\n" + 156 "- localhost:12345\n" + 157 "- 10.0.3.1:12345\n" + 158 "ca-cert: this is the cacert\n", 159 }, { 160 args: []string{"--format=yaml"}, 161 output: "" + 162 "user: tester\n" + 163 "environ-uuid: deadbeef-dead-beef-dead-deaddeaddead\n" + 164 "server-uuid: bad0f00d-dead-beef-0000-01234567899a\n" + 165 "state-servers:\n" + 166 "- localhost:12345\n" + 167 "- 10.0.3.1:12345\n" + 168 "ca-cert: this is the cacert\n", 169 }, { 170 args: []string{"--format=json"}, 171 output: `{"user":"tester",` + 172 `"environ-uuid":"deadbeef-dead-beef-dead-deaddeaddead",` + 173 `"server-uuid":"bad0f00d-dead-beef-0000-01234567899a",` + 174 `"state-servers":["localhost:12345","10.0.3.1:12345"],` + 175 `"ca-cert":"this is the cacert"}` + "\n", 176 }, { 177 args: []string{"user"}, 178 output: "tester\n", 179 }, { 180 args: []string{"user", "password"}, 181 output: "" + 182 "user: tester\n" + 183 "password: sekrit\n", 184 }, { 185 args: []string{"state-servers"}, 186 output: "" + 187 "localhost:12345\n" + 188 "10.0.3.1:12345\n", 189 }, { 190 args: []string{"--format=yaml", "user"}, 191 output: "user: tester\n", 192 }, { 193 args: []string{"--format=yaml", "user", "password"}, 194 output: "" + 195 "user: tester\n" + 196 "password: sekrit\n", 197 }, { 198 args: []string{"--format=yaml", "state-servers"}, 199 output: "" + 200 "state-servers:\n" + 201 "- localhost:12345\n" + 202 "- 10.0.3.1:12345\n", 203 }, { 204 args: []string{"--format=json", "user"}, 205 output: `{"user":"tester"}` + "\n", 206 }, { 207 args: []string{"--format=json", "user", "password"}, 208 output: `{"user":"tester","password":"sekrit"}` + "\n", 209 }, { 210 args: []string{"--format=json", "state-servers"}, 211 output: `{"state-servers":["localhost:12345","10.0.3.1:12345"]}` + "\n", 212 }, 213 } { 214 c.Logf("test %v: %v", i, test.args) 215 command := &APIInfoCommand{} 216 ctx, err := testing.RunCommand(c, envcmd.Wrap(command), test.args...) 217 if test.errMatch == "" { 218 c.Check(err, jc.ErrorIsNil) 219 c.Check(testing.Stdout(ctx), gc.Equals, test.output) 220 } else { 221 c.Check(err, gc.ErrorMatches, test.errMatch) 222 } 223 } 224 } 225 226 func (s *APIInfoSuite) TestOutputNoServerUUID(c *gc.C) { 227 s.PatchValue(&endpoint, func(c envcmd.EnvCommandBase, refresh bool) (configstore.APIEndpoint, error) { 228 return configstore.APIEndpoint{ 229 Addresses: []string{"localhost:12345", "10.0.3.1:12345"}, 230 CACert: "this is the cacert", 231 EnvironUUID: "deadbeef-dead-beef-dead-deaddeaddead", 232 }, nil 233 }) 234 s.PatchValue(&creds, func(c envcmd.EnvCommandBase) (configstore.APICredentials, error) { 235 return configstore.APICredentials{ 236 User: "tester", 237 Password: "sekrit", 238 }, nil 239 }) 240 241 expected := "" + 242 "user: tester\n" + 243 "environ-uuid: deadbeef-dead-beef-dead-deaddeaddead\n" + 244 "state-servers:\n" + 245 "- localhost:12345\n" + 246 "- 10.0.3.1:12345\n" + 247 "ca-cert: this is the cacert\n" 248 command := &APIInfoCommand{} 249 ctx, err := testing.RunCommand(c, envcmd.Wrap(command)) 250 c.Check(err, jc.ErrorIsNil) 251 c.Check(testing.Stdout(ctx), gc.Equals, expected) 252 } 253 254 func (s *APIInfoSuite) TestEndpointError(c *gc.C) { 255 s.PatchValue(&endpoint, func(c envcmd.EnvCommandBase, refresh bool) (configstore.APIEndpoint, error) { 256 return configstore.APIEndpoint{}, fmt.Errorf("oops, no endpoint") 257 }) 258 s.PatchValue(&creds, func(c envcmd.EnvCommandBase) (configstore.APICredentials, error) { 259 return configstore.APICredentials{}, nil 260 }) 261 command := &APIInfoCommand{} 262 _, err := testing.RunCommand(c, envcmd.Wrap(command)) 263 c.Assert(err, gc.ErrorMatches, "oops, no endpoint") 264 } 265 266 func (s *APIInfoSuite) TestCredentialsError(c *gc.C) { 267 s.PatchValue(&endpoint, func(c envcmd.EnvCommandBase, refresh bool) (configstore.APIEndpoint, error) { 268 return configstore.APIEndpoint{}, nil 269 }) 270 s.PatchValue(&creds, func(c envcmd.EnvCommandBase) (configstore.APICredentials, error) { 271 return configstore.APICredentials{}, fmt.Errorf("oops, no creds") 272 }) 273 command := &APIInfoCommand{} 274 _, err := testing.RunCommand(c, envcmd.Wrap(command)) 275 c.Assert(err, gc.ErrorMatches, "oops, no creds") 276 } 277 278 func (s *APIInfoSuite) TestNoEnvironment(c *gc.C) { 279 command := &APIInfoCommand{} 280 _, err := testing.RunCommand(c, envcmd.Wrap(command)) 281 c.Assert(err, gc.ErrorMatches, `environment "erewhemos" not found`) 282 }