github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/cloud/listcredentials_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cloud_test 5 6 import ( 7 "strings" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 jujucloud "github.com/juju/juju/cloud" 13 "github.com/juju/juju/cmd/juju/cloud" 14 "github.com/juju/juju/environs" 15 "github.com/juju/juju/jujuclient" 16 "github.com/juju/juju/jujuclient/jujuclienttesting" 17 "github.com/juju/juju/testing" 18 ) 19 20 type listCredentialsSuite struct { 21 testing.BaseSuite 22 store jujuclient.CredentialGetter 23 personalCloudsFunc func() (map[string]jujucloud.Cloud, error) 24 cloudByNameFunc func(string) (*jujucloud.Cloud, error) 25 } 26 27 var _ = gc.Suite(&listCredentialsSuite{ 28 personalCloudsFunc: func() (map[string]jujucloud.Cloud, error) { 29 return map[string]jujucloud.Cloud{ 30 "mycloud": {}, 31 }, nil 32 }, 33 cloudByNameFunc: func(string) (*jujucloud.Cloud, error) { 34 return &jujucloud.Cloud{Type: "test-provider"}, nil 35 }, 36 }) 37 38 func (s *listCredentialsSuite) SetUpSuite(c *gc.C) { 39 s.BaseSuite.SetUpSuite(c) 40 environs.RegisterProvider("test-provider", &mockProvider{}) 41 } 42 43 func (s *listCredentialsSuite) SetUpTest(c *gc.C) { 44 s.BaseSuite.SetUpTest(c) 45 s.store = &jujuclienttesting.MemStore{ 46 Credentials: map[string]jujucloud.CloudCredential{ 47 "aws": { 48 DefaultRegion: "ap-southeast-2", 49 DefaultCredential: "down", 50 AuthCredentials: map[string]jujucloud.Credential{ 51 "bob": jujucloud.NewCredential( 52 jujucloud.AccessKeyAuthType, 53 map[string]string{ 54 "access-key": "key", 55 "secret-key": "secret", 56 }, 57 ), 58 "down": jujucloud.NewCredential( 59 jujucloud.UserPassAuthType, 60 map[string]string{ 61 "username": "user", 62 "password": "password", 63 }, 64 ), 65 }, 66 }, 67 "google": { 68 AuthCredentials: map[string]jujucloud.Credential{ 69 "default": jujucloud.NewCredential( 70 jujucloud.OAuth2AuthType, 71 map[string]string{ 72 "client-id": "id", 73 "client-email": "email", 74 "private-key": "key", 75 }, 76 ), 77 }, 78 }, 79 "azure": { 80 AuthCredentials: map[string]jujucloud.Credential{ 81 "azhja": jujucloud.NewCredential( 82 jujucloud.UserPassAuthType, 83 map[string]string{ 84 "application-id": "app-id", 85 "application-password": "app-secret", 86 "subscription-id": "subscription-id", 87 "tenant-id": "tenant-id", 88 }, 89 ), 90 }, 91 }, 92 "mycloud": { 93 AuthCredentials: map[string]jujucloud.Credential{ 94 "me": jujucloud.NewCredential( 95 jujucloud.AccessKeyAuthType, 96 map[string]string{ 97 "access-key": "key", 98 "secret-key": "secret", 99 }, 100 ), 101 }, 102 }, 103 }, 104 } 105 } 106 107 func (s *listCredentialsSuite) TestListCredentialsTabular(c *gc.C) { 108 out := s.listCredentials(c) 109 c.Assert(out, gc.Equals, ` 110 CLOUD CREDENTIALS 111 aws down*, bob 112 azure azhja 113 google default 114 mycloud me 115 116 `[1:]) 117 } 118 119 func (s *listCredentialsSuite) TestListCredentialsTabularFiltered(c *gc.C) { 120 out := s.listCredentials(c, "aws") 121 c.Assert(out, gc.Equals, ` 122 CLOUD CREDENTIALS 123 aws down*, bob 124 125 `[1:]) 126 } 127 128 func (s *listCredentialsSuite) TestListCredentialsYAMLWithSecrets(c *gc.C) { 129 out := s.listCredentials(c, "--format", "yaml", "--show-secrets") 130 c.Assert(out, gc.Equals, ` 131 credentials: 132 aws: 133 default-credential: down 134 default-region: ap-southeast-2 135 bob: 136 auth-type: access-key 137 access-key: key 138 secret-key: secret 139 down: 140 auth-type: userpass 141 password: password 142 username: user 143 azure: 144 azhja: 145 auth-type: userpass 146 application-id: app-id 147 application-password: app-secret 148 subscription-id: subscription-id 149 tenant-id: tenant-id 150 google: 151 default: 152 auth-type: oauth2 153 client-email: email 154 client-id: id 155 private-key: key 156 mycloud: 157 me: 158 auth-type: access-key 159 access-key: key 160 secret-key: secret 161 `[1:]) 162 } 163 164 func (s *listCredentialsSuite) TestListCredentialsYAMLNoSecrets(c *gc.C) { 165 out := s.listCredentials(c, "--format", "yaml") 166 c.Assert(out, gc.Equals, ` 167 credentials: 168 aws: 169 default-credential: down 170 default-region: ap-southeast-2 171 bob: 172 auth-type: access-key 173 access-key: key 174 down: 175 auth-type: userpass 176 username: user 177 azure: 178 azhja: 179 auth-type: userpass 180 application-id: app-id 181 subscription-id: subscription-id 182 tenant-id: tenant-id 183 google: 184 default: 185 auth-type: oauth2 186 client-email: email 187 client-id: id 188 mycloud: 189 me: 190 auth-type: access-key 191 access-key: key 192 `[1:]) 193 } 194 195 func (s *listCredentialsSuite) TestListCredentialsYAMLFiltered(c *gc.C) { 196 out := s.listCredentials(c, "--format", "yaml", "azure") 197 c.Assert(out, gc.Equals, ` 198 credentials: 199 azure: 200 azhja: 201 auth-type: userpass 202 application-id: app-id 203 subscription-id: subscription-id 204 tenant-id: tenant-id 205 `[1:]) 206 } 207 208 func (s *listCredentialsSuite) TestListCredentialsJSONWithSecrets(c *gc.C) { 209 out := s.listCredentials(c, "--format", "json", "--show-secrets") 210 c.Assert(out, gc.Equals, ` 211 {"credentials":{"aws":{"default-credential":"down","default-region":"ap-southeast-2","cloud-credentials":{"bob":{"auth-type":"access-key","details":{"access-key":"key","secret-key":"secret"}},"down":{"auth-type":"userpass","details":{"password":"password","username":"user"}}}},"azure":{"cloud-credentials":{"azhja":{"auth-type":"userpass","details":{"application-id":"app-id","application-password":"app-secret","subscription-id":"subscription-id","tenant-id":"tenant-id"}}}},"google":{"cloud-credentials":{"default":{"auth-type":"oauth2","details":{"client-email":"email","client-id":"id","private-key":"key"}}}},"mycloud":{"cloud-credentials":{"me":{"auth-type":"access-key","details":{"access-key":"key","secret-key":"secret"}}}}}} 212 `[1:]) 213 } 214 215 func (s *listCredentialsSuite) TestListCredentialsJSONNoSecrets(c *gc.C) { 216 out := s.listCredentials(c, "--format", "json") 217 c.Assert(out, gc.Equals, ` 218 {"credentials":{"aws":{"default-credential":"down","default-region":"ap-southeast-2","cloud-credentials":{"bob":{"auth-type":"access-key","details":{"access-key":"key"}},"down":{"auth-type":"userpass","details":{"username":"user"}}}},"azure":{"cloud-credentials":{"azhja":{"auth-type":"userpass","details":{"application-id":"app-id","subscription-id":"subscription-id","tenant-id":"tenant-id"}}}},"google":{"cloud-credentials":{"default":{"auth-type":"oauth2","details":{"client-email":"email","client-id":"id"}}}},"mycloud":{"cloud-credentials":{"me":{"auth-type":"access-key","details":{"access-key":"key"}}}}}} 219 `[1:]) 220 } 221 222 func (s *listCredentialsSuite) TestListCredentialsJSONFiltered(c *gc.C) { 223 out := s.listCredentials(c, "--format", "json", "azure") 224 c.Assert(out, gc.Equals, ` 225 {"credentials":{"azure":{"cloud-credentials":{"azhja":{"auth-type":"userpass","details":{"application-id":"app-id","subscription-id":"subscription-id","tenant-id":"tenant-id"}}}}}} 226 `[1:]) 227 } 228 229 func (s *listCredentialsSuite) TestListCredentialsEmpty(c *gc.C) { 230 s.store = &jujuclienttesting.MemStore{ 231 Credentials: map[string]jujucloud.CloudCredential{ 232 "aws": { 233 AuthCredentials: map[string]jujucloud.Credential{ 234 "bob": jujucloud.NewCredential( 235 jujucloud.OAuth2AuthType, 236 map[string]string{}, 237 ), 238 }, 239 }, 240 }, 241 } 242 out := strings.Replace(s.listCredentials(c), "\n", "", -1) 243 c.Assert(out, gc.Equals, "CLOUD CREDENTIALSaws bob") 244 245 out = strings.Replace(s.listCredentials(c, "--format", "yaml"), "\n", "", -1) 246 c.Assert(out, gc.Equals, "credentials: aws: bob: auth-type: oauth2") 247 248 out = strings.Replace(s.listCredentials(c, "--format", "json"), "\n", "", -1) 249 c.Assert(out, gc.Equals, `{"credentials":{"aws":{"cloud-credentials":{"bob":{"auth-type":"oauth2"}}}}}`) 250 } 251 252 func (s *listCredentialsSuite) TestListCredentialsNone(c *gc.C) { 253 listCmd := cloud.NewListCredentialsCommandForTest(jujuclienttesting.NewMemStore(), s.personalCloudsFunc, s.cloudByNameFunc) 254 ctx, err := testing.RunCommand(c, listCmd) 255 c.Assert(err, jc.ErrorIsNil) 256 c.Assert(testing.Stderr(ctx), gc.Equals, "") 257 out := strings.Replace(testing.Stdout(ctx), "\n", "", -1) 258 c.Assert(out, gc.Equals, "No credentials to display.") 259 260 ctx, err = testing.RunCommand(c, listCmd, "--format", "yaml") 261 c.Assert(err, jc.ErrorIsNil) 262 c.Assert(testing.Stderr(ctx), gc.Equals, "") 263 out = strings.Replace(testing.Stdout(ctx), "\n", "", -1) 264 c.Assert(out, gc.Equals, "credentials: {}") 265 266 ctx, err = testing.RunCommand(c, listCmd, "--format", "json") 267 c.Assert(err, jc.ErrorIsNil) 268 c.Assert(testing.Stderr(ctx), gc.Equals, "") 269 out = strings.Replace(testing.Stdout(ctx), "\n", "", -1) 270 c.Assert(out, gc.Equals, `{"credentials":{}}`) 271 } 272 273 func (s *listCredentialsSuite) listCredentials(c *gc.C, args ...string) string { 274 ctx, err := testing.RunCommand(c, cloud.NewListCredentialsCommandForTest(s.store, s.personalCloudsFunc, s.cloudByNameFunc), args...) 275 c.Assert(err, jc.ErrorIsNil) 276 c.Assert(testing.Stderr(ctx), gc.Equals, "") 277 return testing.Stdout(ctx) 278 }