github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/modelcmd/credentials_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package modelcmd_test 5 6 import ( 7 "fmt" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "io/ioutil" 13 "path/filepath" 14 15 "github.com/juju/juju/cloud" 16 "github.com/juju/juju/cmd/modelcmd" 17 "github.com/juju/juju/environs" 18 "github.com/juju/juju/jujuclient/jujuclienttesting" 19 _ "github.com/juju/juju/provider/dummy" 20 "github.com/juju/juju/testing" 21 ) 22 23 func init() { 24 dummyProvider, err := environs.Provider("dummy") 25 if err != nil { 26 panic(err) 27 } 28 environs.RegisterProvider("fake", mockProvider{dummyProvider}) 29 } 30 31 type mockProvider struct { 32 environs.EnvironProvider 33 } 34 35 func (mockProvider) CredentialSchemas() map[cloud.AuthType]cloud.CredentialSchema { 36 schema := cloud.CredentialSchema{ 37 { 38 "username", cloud.CredentialAttr{}, 39 }, { 40 "password", cloud.CredentialAttr{}, 41 }, { 42 "key", cloud.CredentialAttr{FileAttr: "key-file"}, 43 }, 44 } 45 return map[cloud.AuthType]cloud.CredentialSchema{ 46 cloud.UserPassAuthType: schema, 47 "interactive": cloud.CredentialSchema{ 48 {"username", cloud.CredentialAttr{}}, 49 }, 50 } 51 } 52 53 func (mockProvider) FinalizeCredential( 54 ctx environs.FinalizeCredentialContext, 55 args environs.FinalizeCredentialParams, 56 ) (*cloud.Credential, error) { 57 if args.Credential.AuthType() == "interactive" { 58 username := args.Credential.Attributes()["username"] 59 fmt.Fprintf(ctx.GetStderr(), "generating credential for %q\n", username) 60 out := cloud.NewCredential(cloud.UserPassAuthType, map[string]string{ 61 "username": username, 62 "password": "sekret", 63 "key": "value", 64 }) 65 return &out, nil 66 } 67 return &args.Credential, nil 68 } 69 70 type credentialsSuite struct { 71 testing.FakeJujuXDGDataHomeSuite 72 cloud cloud.Cloud 73 store *jujuclienttesting.MemStore 74 } 75 76 var _ = gc.Suite(&credentialsSuite{}) 77 78 func (s *credentialsSuite) SetUpTest(c *gc.C) { 79 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 80 s.cloud = cloud.Cloud{ 81 Type: "fake", 82 Regions: []cloud.Region{ 83 {Name: "first-region"}, 84 {Name: "second-region"}, 85 }, 86 } 87 88 dir := c.MkDir() 89 keyFile := filepath.Join(dir, "keyfile") 90 err := ioutil.WriteFile(keyFile, []byte("value"), 0600) 91 c.Assert(err, jc.ErrorIsNil) 92 93 s.store = jujuclienttesting.NewMemStore() 94 s.store.Credentials["cloud"] = cloud.CloudCredential{ 95 DefaultRegion: "second-region", 96 AuthCredentials: map[string]cloud.Credential{ 97 "interactive": cloud.NewCredential("interactive", map[string]string{ 98 "username": "user", 99 }), 100 "secrets": cloud.NewCredential(cloud.UserPassAuthType, map[string]string{ 101 "username": "user", 102 "password": "sekret", 103 "key-file": keyFile, 104 }), 105 }, 106 } 107 } 108 109 func (s *credentialsSuite) assertGetCredentials(c *gc.C, cred, region string) { 110 credential, credentialName, regionName, err := modelcmd.GetCredentials( 111 testing.Context(c), s.store, modelcmd.GetCredentialsParams{ 112 Cloud: s.cloud, 113 CloudName: "cloud", 114 CloudRegion: region, 115 CredentialName: cred, 116 }, 117 ) 118 c.Assert(err, jc.ErrorIsNil) 119 expectedRegion := region 120 if expectedRegion == "" { 121 expectedRegion = s.store.Credentials["cloud"].DefaultRegion 122 if expectedRegion == "" && len(s.cloud.Regions) > 0 { 123 expectedRegion = "first-region" 124 } 125 } 126 c.Assert(regionName, gc.Equals, expectedRegion) 127 c.Assert(credentialName, gc.Equals, cred) 128 c.Assert(credential.Attributes(), jc.DeepEquals, map[string]string{ 129 "key": "value", 130 "username": "user", 131 "password": "sekret", 132 }) 133 } 134 135 func (s *credentialsSuite) TestGetCredentialsUserDefaultRegion(c *gc.C) { 136 s.assertGetCredentials(c, "secrets", "") 137 } 138 139 func (s *credentialsSuite) TestGetCredentialsCloudDefaultRegion(c *gc.C) { 140 creds := s.store.Credentials["cloud"] 141 creds.DefaultRegion = "" 142 s.store.Credentials["cloud"] = creds 143 s.assertGetCredentials(c, "secrets", "") 144 } 145 146 func (s *credentialsSuite) TestGetCredentialsNoRegion(c *gc.C) { 147 creds := s.store.Credentials["cloud"] 148 creds.DefaultRegion = "" 149 s.store.Credentials["cloud"] = creds 150 s.cloud.Regions = nil 151 s.assertGetCredentials(c, "secrets", "") 152 } 153 154 func (s *credentialsSuite) TestGetCredentials(c *gc.C) { 155 s.cloud.Regions = append(s.cloud.Regions, cloud.Region{Name: "third-region"}) 156 s.assertGetCredentials(c, "secrets", "third-region") 157 } 158 159 func (s *credentialsSuite) TestGetCredentialsProviderFinalizeCredential(c *gc.C) { 160 s.assertGetCredentials(c, "interactive", "") 161 }