github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/cloud/showcredential_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cloud_test 5 6 import ( 7 "github.com/juju/cmd/cmdtesting" 8 "github.com/juju/errors" 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/apiserver/common" 14 "github.com/juju/juju/apiserver/params" 15 "github.com/juju/juju/cmd/juju/cloud" 16 _ "github.com/juju/juju/provider/all" 17 coretesting "github.com/juju/juju/testing" 18 ) 19 20 type ShowCredentialSuite struct { 21 coretesting.BaseSuite 22 23 api *fakeCredentialContentAPI 24 } 25 26 var _ = gc.Suite(&ShowCredentialSuite{}) 27 28 func (s *ShowCredentialSuite) SetUpTest(c *gc.C) { 29 s.BaseSuite.SetUpTest(c) 30 31 s.api = &fakeCredentialContentAPI{v: 2} 32 } 33 34 func (s *ShowCredentialSuite) TestShowCredentialBadArgs(c *gc.C) { 35 cmd := cloud.NewShowCredentialCommandForTest(s.api) 36 _, err := cmdtesting.RunCommand(c, cmd, "cloud") 37 c.Assert(err, gc.ErrorMatches, "both cloud and credential name are needed") 38 _, err = cmdtesting.RunCommand(c, cmd, "cloud", "credential", "extra") 39 c.Assert(err, gc.ErrorMatches, `only cloud and credential names are supported`) 40 } 41 42 func (s *ShowCredentialSuite) TestShowCredentialAPIVersion(c *gc.C) { 43 s.api.v = 1 44 cmd := cloud.NewShowCredentialCommandForTest(s.api) 45 ctx, err := cmdtesting.RunCommand(c, cmd) 46 c.Assert(err, jc.ErrorIsNil) 47 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "credential content lookup is not supported by this version of Juju\n") 48 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, ``) 49 s.api.CheckCallNames(c, "BestAPIVersion", "Close") 50 } 51 52 func (s *ShowCredentialSuite) TestShowCredentialAPICallError(c *gc.C) { 53 s.api.SetErrors(errors.New("boom"), nil) 54 cmd := cloud.NewShowCredentialCommandForTest(s.api) 55 ctx, err := cmdtesting.RunCommand(c, cmd) 56 c.Assert(err, gc.ErrorMatches, "boom") 57 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "Getting credential content failed with: boom\n") 58 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, ``) 59 s.api.CheckCallNames(c, "BestAPIVersion", "CredentialContents", "Close") 60 } 61 62 func (s *ShowCredentialSuite) TestShowCredentialNone(c *gc.C) { 63 s.api.contents = []params.CredentialContentResult{} 64 cmd := cloud.NewShowCredentialCommandForTest(s.api) 65 ctx, err := cmdtesting.RunCommand(c, cmd) 66 c.Assert(err, jc.ErrorIsNil) 67 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "No credential to display\n") 68 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, ``) 69 s.api.CheckCallNames(c, "BestAPIVersion", "CredentialContents", "Close") 70 } 71 72 func (s *ShowCredentialSuite) TestShowCredentialOne(c *gc.C) { 73 s.api.contents = []params.CredentialContentResult{ 74 { 75 Result: ¶ms.ControllerCredentialInfo{ 76 Content: params.CredentialContent{ 77 Cloud: "aws", 78 Name: "credential-name", 79 AuthType: "userpass", 80 Attributes: map[string]string{ 81 "username": "fred", 82 "password": "sekret"}, 83 }, 84 Models: []params.ModelAccess{ 85 {Model: "abcmodel", Access: "admin"}, 86 {Model: "xyzmodel", Access: "read"}, 87 {Model: "no-access-model"}, 88 }, 89 }, 90 }, 91 } 92 cmd := cloud.NewShowCredentialCommandForTest(s.api) 93 ctx, err := cmdtesting.RunCommand(c, cmd, "--show-secrets") 94 c.Assert(err, jc.ErrorIsNil) 95 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, ``) 96 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, ` 97 controller-credentials: 98 aws: 99 credential-name: 100 content: 101 auth-type: userpass 102 password: sekret 103 username: fred 104 models: 105 abcmodel: admin 106 no-access-model: no access 107 xyzmodel: read 108 `[1:]) 109 s.api.CheckCallNames(c, "BestAPIVersion", "CredentialContents", "Close") 110 c.Assert(s.api.inclsecrets, jc.IsTrue) 111 } 112 113 func (s *ShowCredentialSuite) TestShowCredentialMany(c *gc.C) { 114 s.api.contents = []params.CredentialContentResult{ 115 { 116 Result: ¶ms.ControllerCredentialInfo{ 117 Content: params.CredentialContent{ 118 Cloud: "cloud-name", 119 Name: "one", 120 AuthType: "userpass", 121 Attributes: map[string]string{"username": "fred"}, 122 }, 123 // Don't have models here. 124 }, 125 }, { 126 Error: common.ServerError(errors.New("boom")), 127 }, { 128 Result: ¶ms.ControllerCredentialInfo{ 129 Content: params.CredentialContent{ 130 Cloud: "cloud-name", 131 Name: "two", 132 AuthType: "userpass", 133 Attributes: map[string]string{ 134 "username": "fred", 135 "something": "visible-attr", 136 "password": "sekret", 137 "hidden": "very-very-sekret", 138 }, 139 }, 140 Models: []params.ModelAccess{ 141 {Model: "abcmodel", Access: "admin"}, 142 {Model: "xyzmodel", Access: "read"}, 143 {Model: "no-access-model"}, 144 }, 145 }, 146 }, { 147 Result: ¶ms.ControllerCredentialInfo{ 148 Content: params.CredentialContent{ 149 Cloud: "diff-cloud", 150 Name: "three", 151 AuthType: "oauth1", 152 Attributes: map[string]string{ 153 "something": "visible-attr", 154 }, 155 }, 156 Models: []params.ModelAccess{ 157 {Model: "klmmodel", Access: "write"}, 158 }, 159 }, 160 }, 161 } 162 cmd := cloud.NewShowCredentialCommandForTest(s.api) 163 ctx, err := cmdtesting.RunCommand(c, cmd) 164 c.Assert(err, jc.ErrorIsNil) 165 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "boom\n") 166 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, ` 167 controller-credentials: 168 cloud-name: 169 one: 170 content: 171 auth-type: userpass 172 username: fred 173 models: {} 174 two: 175 content: 176 auth-type: userpass 177 hidden: very-very-sekret 178 password: sekret 179 something: visible-attr 180 username: fred 181 models: 182 abcmodel: admin 183 no-access-model: no access 184 xyzmodel: read 185 diff-cloud: 186 three: 187 content: 188 auth-type: oauth1 189 something: visible-attr 190 models: 191 klmmodel: write 192 `[1:]) 193 s.api.CheckCallNames(c, "BestAPIVersion", "CredentialContents", "Close") 194 } 195 196 type fakeCredentialContentAPI struct { 197 testing.Stub 198 v int 199 contents []params.CredentialContentResult 200 inclsecrets bool 201 } 202 203 func (f *fakeCredentialContentAPI) CredentialContents(cloud, credential string, withSecrets bool) ([]params.CredentialContentResult, error) { 204 f.AddCall("CredentialContents", cloud, credential, withSecrets) 205 f.inclsecrets = withSecrets 206 return f.contents, f.NextErr() 207 } 208 209 func (f *fakeCredentialContentAPI) Close() error { 210 f.AddCall("Close") 211 return f.NextErr() 212 } 213 214 func (f *fakeCredentialContentAPI) BestAPIVersion() int { 215 f.AddCall("BestAPIVersion") 216 return f.v 217 }