github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/juju/apiinfo_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 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 errMatch string 34 }{ 35 { 36 message: "no args skips password", 37 user: true, 38 cacert: true, 39 servers: true, 40 envuuid: true, 41 }, { 42 message: "password shown if user specifies", 43 args: []string{"--password"}, 44 user: true, 45 password: true, 46 cacert: true, 47 servers: true, 48 envuuid: true, 49 }, { 50 message: "refresh the cache", 51 args: []string{"--refresh"}, 52 refresh: true, 53 user: true, 54 cacert: true, 55 servers: true, 56 envuuid: true, 57 }, { 58 message: "just show the user field", 59 args: []string{"user"}, 60 user: true, 61 }, { 62 message: "just show the password field", 63 args: []string{"password"}, 64 password: true, 65 }, { 66 message: "just show the cacert field", 67 args: []string{"ca-cert"}, 68 cacert: true, 69 }, { 70 message: "just show the servers field", 71 args: []string{"state-servers"}, 72 servers: true, 73 }, { 74 message: "just show the envuuid field", 75 args: []string{"environ-uuid"}, 76 envuuid: true, 77 }, { 78 message: "show the user and password field", 79 args: []string{"user", "password"}, 80 user: true, 81 password: true, 82 }, { 83 message: "unknown field field", 84 args: []string{"foo"}, 85 errMatch: `unknown fields: "foo"`, 86 }, { 87 message: "multiple unknown fields", 88 args: []string{"user", "pwd", "foo"}, 89 errMatch: `unknown fields: "pwd", "foo"`, 90 }, 91 } { 92 c.Logf("test %v: %s", i, test.message) 93 command := &APIInfoCommand{} 94 err := testing.InitCommand(envcmd.Wrap(command), test.args) 95 if test.errMatch == "" { 96 c.Check(err, jc.ErrorIsNil) 97 c.Check(command.refresh, gc.Equals, test.refresh) 98 c.Check(command.user, gc.Equals, test.user) 99 c.Check(command.password, gc.Equals, test.password) 100 c.Check(command.cacert, gc.Equals, test.cacert) 101 c.Check(command.servers, gc.Equals, test.servers) 102 c.Check(command.envuuid, gc.Equals, test.envuuid) 103 } else { 104 c.Check(err, gc.ErrorMatches, test.errMatch) 105 } 106 } 107 } 108 109 func (s *APIInfoSuite) TestOutput(c *gc.C) { 110 s.PatchValue(&endpoint, func(c envcmd.EnvCommandBase, refresh bool) (configstore.APIEndpoint, error) { 111 return configstore.APIEndpoint{ 112 Addresses: []string{"localhost:12345", "10.0.3.1:12345"}, 113 CACert: "this is the cacert", 114 EnvironUUID: "deadbeef-dead-beef-dead-deaddeaddead", 115 }, nil 116 }) 117 s.PatchValue(&creds, func(c envcmd.EnvCommandBase) (configstore.APICredentials, error) { 118 return configstore.APICredentials{ 119 User: "tester", 120 Password: "sekrit", 121 }, nil 122 }) 123 124 for i, test := range []struct { 125 args []string 126 output string 127 errMatch string 128 }{ 129 { 130 output: "" + 131 "user: tester\n" + 132 "environ-uuid: deadbeef-dead-beef-dead-deaddeaddead\n" + 133 "state-servers:\n" + 134 "- localhost:12345\n" + 135 "- 10.0.3.1:12345\n" + 136 "ca-cert: this is the cacert\n", 137 }, { 138 args: []string{"--password"}, 139 output: "" + 140 "user: tester\n" + 141 "password: sekrit\n" + 142 "environ-uuid: deadbeef-dead-beef-dead-deaddeaddead\n" + 143 "state-servers:\n" + 144 "- localhost:12345\n" + 145 "- 10.0.3.1:12345\n" + 146 "ca-cert: this is the cacert\n", 147 }, { 148 args: []string{"--format=yaml"}, 149 output: "" + 150 "user: tester\n" + 151 "environ-uuid: deadbeef-dead-beef-dead-deaddeaddead\n" + 152 "state-servers:\n" + 153 "- localhost:12345\n" + 154 "- 10.0.3.1:12345\n" + 155 "ca-cert: this is the cacert\n", 156 }, { 157 args: []string{"--format=json"}, 158 output: `{"user":"tester",` + 159 `"environ-uuid":"deadbeef-dead-beef-dead-deaddeaddead",` + 160 `"state-servers":["localhost:12345","10.0.3.1:12345"],` + 161 `"ca-cert":"this is the cacert"}` + "\n", 162 }, { 163 args: []string{"user"}, 164 output: "tester\n", 165 }, { 166 args: []string{"user", "password"}, 167 output: "" + 168 "user: tester\n" + 169 "password: sekrit\n", 170 }, { 171 args: []string{"state-servers"}, 172 output: "" + 173 "localhost:12345\n" + 174 "10.0.3.1:12345\n", 175 }, { 176 args: []string{"--format=yaml", "user"}, 177 output: "user: tester\n", 178 }, { 179 args: []string{"--format=yaml", "user", "password"}, 180 output: "" + 181 "user: tester\n" + 182 "password: sekrit\n", 183 }, { 184 args: []string{"--format=yaml", "state-servers"}, 185 output: "" + 186 "state-servers:\n" + 187 "- localhost:12345\n" + 188 "- 10.0.3.1:12345\n", 189 }, { 190 args: []string{"--format=json", "user"}, 191 output: `{"user":"tester"}` + "\n", 192 }, { 193 args: []string{"--format=json", "user", "password"}, 194 output: `{"user":"tester","password":"sekrit"}` + "\n", 195 }, { 196 args: []string{"--format=json", "state-servers"}, 197 output: `{"state-servers":["localhost:12345","10.0.3.1:12345"]}` + "\n", 198 }, 199 } { 200 c.Logf("test %v: %v", i, test.args) 201 command := &APIInfoCommand{} 202 ctx, err := testing.RunCommand(c, envcmd.Wrap(command), test.args...) 203 if test.errMatch == "" { 204 c.Check(err, jc.ErrorIsNil) 205 c.Check(testing.Stdout(ctx), gc.Equals, test.output) 206 } else { 207 c.Check(err, gc.ErrorMatches, test.errMatch) 208 } 209 } 210 } 211 212 func (s *APIInfoSuite) TestEndpointError(c *gc.C) { 213 s.PatchValue(&endpoint, func(c envcmd.EnvCommandBase, refresh bool) (configstore.APIEndpoint, error) { 214 return configstore.APIEndpoint{}, fmt.Errorf("oops, no endpoint") 215 }) 216 s.PatchValue(&creds, func(c envcmd.EnvCommandBase) (configstore.APICredentials, error) { 217 return configstore.APICredentials{}, nil 218 }) 219 command := &APIInfoCommand{} 220 _, err := testing.RunCommand(c, envcmd.Wrap(command)) 221 c.Assert(err, gc.ErrorMatches, "oops, no endpoint") 222 } 223 224 func (s *APIInfoSuite) TestCredentialsError(c *gc.C) { 225 s.PatchValue(&endpoint, func(c envcmd.EnvCommandBase, refresh bool) (configstore.APIEndpoint, error) { 226 return configstore.APIEndpoint{}, nil 227 }) 228 s.PatchValue(&creds, func(c envcmd.EnvCommandBase) (configstore.APICredentials, error) { 229 return configstore.APICredentials{}, fmt.Errorf("oops, no creds") 230 }) 231 command := &APIInfoCommand{} 232 _, err := testing.RunCommand(c, envcmd.Wrap(command)) 233 c.Assert(err, gc.ErrorMatches, "oops, no creds") 234 } 235 236 func (s *APIInfoSuite) TestNoEnvironment(c *gc.C) { 237 command := &APIInfoCommand{} 238 _, err := testing.RunCommand(c, envcmd.Wrap(command)) 239 c.Assert(err, gc.ErrorMatches, `environment "erewhemos" not found`) 240 }