github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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 local: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 local: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 local: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) TestListCredentialsJSON(c *gc.C) { 209 // TODO(axw) test once json marshalling works properly 210 c.Skip("not implemented: credentials don't marshal to JSON yet") 211 } 212 213 func (s *listCredentialsSuite) TestListCredentialsNone(c *gc.C) { 214 listCmd := cloud.NewListCredentialsCommandForTest(jujuclienttesting.NewMemStore(), s.personalCloudsFunc, s.cloudByNameFunc) 215 ctx, err := testing.RunCommand(c, listCmd) 216 c.Assert(err, jc.ErrorIsNil) 217 c.Assert(testing.Stderr(ctx), gc.Equals, "") 218 out := strings.Replace(testing.Stdout(ctx), "\n", "", -1) 219 c.Assert(out, gc.Equals, "CLOUD CREDENTIALS") 220 221 ctx, err = testing.RunCommand(c, listCmd, "--format", "yaml") 222 c.Assert(err, jc.ErrorIsNil) 223 c.Assert(testing.Stderr(ctx), gc.Equals, "") 224 out = strings.Replace(testing.Stdout(ctx), "\n", "", -1) 225 c.Assert(out, gc.Equals, "credentials: {}") 226 227 // TODO(axw) test json once json marshaling works properly 228 } 229 230 func (s *listCredentialsSuite) listCredentials(c *gc.C, args ...string) string { 231 ctx, err := testing.RunCommand(c, cloud.NewListCredentialsCommandForTest(s.store, s.personalCloudsFunc, s.cloudByNameFunc), args...) 232 c.Assert(err, jc.ErrorIsNil) 233 c.Assert(testing.Stderr(ctx), gc.Equals, "") 234 return testing.Stdout(ctx) 235 }