|
| 1 | +// Copyright 2016 Canonical Ltd. |
| 2 | +// Licensed under the AGPLv3, see LICENCE file for details. |
| 3 | + |
| 4 | +package cloud_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + |
| 10 | + jc "github.com/juju/testing/checkers" |
| 11 | + gc "gopkg.in/check.v1" |
| 12 | + "gopkg.in/juju/names.v2" |
| 13 | + |
| 14 | + jujucloud "github.com/juju/juju/cloud" |
| 15 | + "github.com/juju/juju/cmd/juju/cloud" |
| 16 | + "github.com/juju/juju/jujuclient" |
| 17 | + "github.com/juju/juju/jujuclient/jujuclienttesting" |
| 18 | + "github.com/juju/juju/testing" |
| 19 | +) |
| 20 | + |
| 21 | +type updateCredentialSuite struct { |
| 22 | + testing.BaseSuite |
| 23 | +} |
| 24 | + |
| 25 | +var _ = gc.Suite(&updateCredentialSuite{}) |
| 26 | + |
| 27 | +func (s *updateCredentialSuite) TestBadArgs(c *gc.C) { |
| 28 | + cmd := cloud.NewUpdateCredentialCommand() |
| 29 | + _, err := testing.RunCommand(c, cmd) |
| 30 | + c.Assert(err, gc.ErrorMatches, "Usage: juju update-credential <cloud-name> <credential-name>") |
| 31 | + _, err = testing.RunCommand(c, cmd, "cloud", "credential", "extra") |
| 32 | + c.Assert(err, gc.ErrorMatches, `unrecognized args: \["extra"\]`) |
| 33 | +} |
| 34 | + |
| 35 | +func (s *updateCredentialSuite) TestMissingCredential(c *gc.C) { |
| 36 | + store := &jujuclienttesting.MemStore{ |
| 37 | + Controllers: map[string]jujuclient.ControllerDetails{ |
| 38 | + "controller": {}, |
| 39 | + }, |
| 40 | + CurrentControllerName: "controller", |
| 41 | + Credentials: map[string]jujucloud.CloudCredential{ |
| 42 | + "aws": { |
| 43 | + AuthCredentials: map[string]jujucloud.Credential{ |
| 44 | + "my-credential": jujucloud.NewCredential(jujucloud.AccessKeyAuthType, nil), |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + } |
| 49 | + cmd := cloud.NewUpdateCredentialCommandForTest(store, nil) |
| 50 | + ctx, err := testing.RunCommand(c, cmd, "aws", "foo") |
| 51 | + c.Assert(err, jc.ErrorIsNil) |
| 52 | + output := testing.Stderr(ctx) |
| 53 | + output = strings.Replace(output, "\n", "", -1) |
| 54 | + c.Assert(output, gc.Equals, `No credential called "foo" exists for cloud "aws"`) |
| 55 | +} |
| 56 | + |
| 57 | +func (s *updateCredentialSuite) TestBadCloudName(c *gc.C) { |
| 58 | + store := &jujuclienttesting.MemStore{ |
| 59 | + Controllers: map[string]jujuclient.ControllerDetails{ |
| 60 | + "controller": {}, |
| 61 | + }, |
| 62 | + CurrentControllerName: "controller", |
| 63 | + } |
| 64 | + cmd := cloud.NewUpdateCredentialCommandForTest(store, nil) |
| 65 | + ctx, err := testing.RunCommand(c, cmd, "somecloud", "foo") |
| 66 | + c.Assert(err, jc.ErrorIsNil) |
| 67 | + output := testing.Stderr(ctx) |
| 68 | + output = strings.Replace(output, "\n", "", -1) |
| 69 | + c.Assert(output, gc.Equals, `No credentials exist for cloud "somecloud"`) |
| 70 | +} |
| 71 | + |
| 72 | +func (s *updateCredentialSuite) TestUpdate(c *gc.C) { |
| 73 | + store := &jujuclienttesting.MemStore{ |
| 74 | + Controllers: map[string]jujuclient.ControllerDetails{ |
| 75 | + "controller": {}, |
| 76 | + }, |
| 77 | + CurrentControllerName: "controller", |
| 78 | + Accounts: map[string]jujuclient.AccountDetails{ |
| 79 | + "controller": { |
| 80 | + User: "admin@local", |
| 81 | + }, |
| 82 | + }, |
| 83 | + Credentials: map[string]jujucloud.CloudCredential{ |
| 84 | + "aws": { |
| 85 | + AuthCredentials: map[string]jujucloud.Credential{ |
| 86 | + "my-credential": jujucloud.NewCredential(jujucloud.AccessKeyAuthType, nil), |
| 87 | + "another-credential": jujucloud.NewCredential(jujucloud.UserPassAuthType, nil), |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | + fake := &fakeUpdateCredentialAPI{ |
| 93 | + clouds: map[names.CloudTag]jujucloud.Cloud{ |
| 94 | + names.NewCloudTag("aws"): {}, |
| 95 | + }, |
| 96 | + } |
| 97 | + cmd := cloud.NewUpdateCredentialCommandForTest(store, fake) |
| 98 | + ctx, err := testing.RunCommand(c, cmd, "aws", "my-credential") |
| 99 | + c.Assert(err, jc.ErrorIsNil) |
| 100 | + output := testing.Stderr(ctx) |
| 101 | + output = strings.Replace(output, "\n", "", -1) |
| 102 | + c.Assert(output, gc.Equals, `Updated credential "my-credential" for user "admin@local" on cloud "aws".`) |
| 103 | + c.Assert(fake.creds, jc.DeepEquals, map[names.CloudCredentialTag]jujucloud.Credential{ |
| 104 | + names.NewCloudCredentialTag("aws/admin@local/my-credential"): jujucloud.NewCredential(jujucloud.AccessKeyAuthType, nil), |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +func (s *updateCredentialSuite) TestInvalidCloud(c *gc.C) { |
| 109 | + store := &jujuclienttesting.MemStore{ |
| 110 | + Controllers: map[string]jujuclient.ControllerDetails{ |
| 111 | + "controller": {}, |
| 112 | + }, |
| 113 | + CurrentControllerName: "controller", |
| 114 | + Accounts: map[string]jujuclient.AccountDetails{ |
| 115 | + "controller": { |
| 116 | + User: "admin@local", |
| 117 | + }, |
| 118 | + }, |
| 119 | + Credentials: map[string]jujucloud.CloudCredential{ |
| 120 | + "aws": { |
| 121 | + AuthCredentials: map[string]jujucloud.Credential{ |
| 122 | + "my-credential": jujucloud.NewCredential(jujucloud.AccessKeyAuthType, nil), |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | + fake := &fakeUpdateCredentialAPI{} |
| 128 | + cmd := cloud.NewUpdateCredentialCommandForTest(store, fake) |
| 129 | + _, err := testing.RunCommand(c, cmd, "aws", "my-credential") |
| 130 | + c.Assert(err, gc.ErrorMatches, `cannot update credential "my-credential" for cloud "aws" because controller does not run on the specified cloud`) |
| 131 | +} |
| 132 | + |
| 133 | +type fakeUpdateCredentialAPI struct { |
| 134 | + creds map[names.CloudCredentialTag]jujucloud.Credential |
| 135 | + clouds map[names.CloudTag]jujucloud.Cloud |
| 136 | +} |
| 137 | + |
| 138 | +func (f *fakeUpdateCredentialAPI) UpdateCredential(tag names.CloudCredentialTag, credential jujucloud.Credential) error { |
| 139 | + if _, ok := f.clouds[tag.Cloud()]; !ok { |
| 140 | + return fmt.Errorf("error") |
| 141 | + } |
| 142 | + if f.creds == nil { |
| 143 | + f.creds = make(map[names.CloudCredentialTag]jujucloud.Credential) |
| 144 | + } |
| 145 | + f.creds[tag] = credential |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func (f *fakeUpdateCredentialAPI) Clouds() (map[names.CloudTag]jujucloud.Cloud, error) { |
| 150 | + return f.clouds, nil |
| 151 | +} |
| 152 | + |
| 153 | +func (*fakeUpdateCredentialAPI) Close() error { |
| 154 | + return nil |
| 155 | +} |
0 commit comments