1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
jc "github.com/juju/testing/checkers"
10
gc "gopkg.in/check.v1"
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"
20
type listCredentialsSuite struct {
22
store jujuclient.CredentialGetter
23
personalCloudsFunc func() (map[string]jujucloud.Cloud, error)
24
cloudByNameFunc func(string) (*jujucloud.Cloud, error)
27
var _ = gc.Suite(&listCredentialsSuite{
28
personalCloudsFunc: func() (map[string]jujucloud.Cloud, error) {
29
return map[string]jujucloud.Cloud{
33
cloudByNameFunc: func(string) (*jujucloud.Cloud, error) {
34
return &jujucloud.Cloud{Type: "test-provider"}, nil
38
func (s *listCredentialsSuite) SetUpSuite(c *gc.C) {
39
s.BaseSuite.SetUpSuite(c)
40
environs.RegisterProvider("test-provider", &mockProvider{})
43
func (s *listCredentialsSuite) SetUpTest(c *gc.C) {
44
s.BaseSuite.SetUpTest(c)
45
s.store = &jujuclienttesting.MemStore{
46
Credentials: map[string]jujucloud.CloudCredential{
48
DefaultRegion: "ap-southeast-2",
49
DefaultCredential: "down",
50
AuthCredentials: map[string]jujucloud.Credential{
51
"bob": jujucloud.NewCredential(
52
jujucloud.AccessKeyAuthType,
55
"secret-key": "secret",
58
"down": jujucloud.NewCredential(
59
jujucloud.UserPassAuthType,
62
"password": "password",
68
AuthCredentials: map[string]jujucloud.Credential{
69
"default": jujucloud.NewCredential(
70
jujucloud.OAuth2AuthType,
73
"client-email": "email",
80
AuthCredentials: map[string]jujucloud.Credential{
81
"azhja": jujucloud.NewCredential(
82
jujucloud.UserPassAuthType,
84
"application-id": "app-id",
85
"application-password": "app-secret",
86
"subscription-id": "subscription-id",
87
"tenant-id": "tenant-id",
93
AuthCredentials: map[string]jujucloud.Credential{
94
"me": jujucloud.NewCredential(
95
jujucloud.AccessKeyAuthType,
98
"secret-key": "secret",
107
func (s *listCredentialsSuite) TestListCredentialsTabular(c *gc.C) {
108
out := s.listCredentials(c)
109
c.Assert(out, gc.Equals, `
119
func (s *listCredentialsSuite) TestListCredentialsTabularFiltered(c *gc.C) {
120
out := s.listCredentials(c, "aws")
121
c.Assert(out, gc.Equals, `
128
func (s *listCredentialsSuite) TestListCredentialsYAMLWithSecrets(c *gc.C) {
129
out := s.listCredentials(c, "--format", "yaml", "--show-secrets")
130
c.Assert(out, gc.Equals, `
133
default-credential: down
134
default-region: ap-southeast-2
136
auth-type: access-key
146
application-id: app-id
147
application-password: app-secret
148
subscription-id: subscription-id
158
auth-type: access-key
164
func (s *listCredentialsSuite) TestListCredentialsYAMLNoSecrets(c *gc.C) {
165
out := s.listCredentials(c, "--format", "yaml")
166
c.Assert(out, gc.Equals, `
169
default-credential: down
170
default-region: ap-southeast-2
172
auth-type: access-key
180
application-id: app-id
181
subscription-id: subscription-id
190
auth-type: access-key
195
func (s *listCredentialsSuite) TestListCredentialsYAMLFiltered(c *gc.C) {
196
out := s.listCredentials(c, "--format", "yaml", "azure")
197
c.Assert(out, gc.Equals, `
202
application-id: app-id
203
subscription-id: subscription-id
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")
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")
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: {}")
227
// TODO(axw) test json once json marshaling works properly
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)