Skip to content

Commit

Permalink
improve test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian authored and yndu13 committed Aug 21, 2024
1 parent 8d935d4 commit b0bdc55
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
15 changes: 10 additions & 5 deletions credentials/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (s *Config) SetSTSEndpoint(v string) *Config {
}

// NewCredential return a credential according to the type in config.
// if config is nil, the function will use default provider chain to get credential.
// if config is nil, the function will use default provider chain to get credentials.
// please see README.md for detail.
func NewCredential(config *Config) (credential Credential, err error) {
if config == nil {
Expand Down Expand Up @@ -383,12 +383,12 @@ func doAction(request *request.CommonRequest, runtime *utils.Runtime) (content [
return
}
}
trans := &http.Transport{}
transport := &http.Transport{}
if proxy != nil && runtime.Proxy != "" {
trans.Proxy = http.ProxyURL(proxy)
transport.Proxy = http.ProxyURL(proxy)
}
trans.DialContext = utils.Timeout(time.Duration(runtime.ConnectTimeout) * time.Second)
httpClient.Transport = trans
transport.DialContext = utils.Timeout(time.Duration(runtime.ConnectTimeout) * time.Second)
httpClient.Transport = transport
httpResponse, err := hookDo(httpClient.Do)(httpRequest)
if err != nil {
return
Expand Down Expand Up @@ -417,6 +417,7 @@ type credentialsProviderWrap struct {
provider providers.CredentialsProvider
}

// Deprecated: use GetCredential() instead of
func (cp *credentialsProviderWrap) GetAccessKeyId() (accessKeyId *string, err error) {
cc, err := cp.provider.GetCredentials()
if err != nil {
Expand All @@ -426,6 +427,7 @@ func (cp *credentialsProviderWrap) GetAccessKeyId() (accessKeyId *string, err er
return
}

// Deprecated: use GetCredential() instead of
func (cp *credentialsProviderWrap) GetAccessKeySecret() (accessKeySecret *string, err error) {
cc, err := cp.provider.GetCredentials()
if err != nil {
Expand All @@ -435,6 +437,7 @@ func (cp *credentialsProviderWrap) GetAccessKeySecret() (accessKeySecret *string
return
}

// Deprecated: use GetCredential() instead of
func (cp *credentialsProviderWrap) GetSecurityToken() (securityToken *string, err error) {
cc, err := cp.provider.GetCredentials()
if err != nil {
Expand All @@ -444,10 +447,12 @@ func (cp *credentialsProviderWrap) GetSecurityToken() (securityToken *string, er
return
}

// Deprecated: don't use it
func (cp *credentialsProviderWrap) GetBearerToken() (bearerToken *string) {
return tea.String("")
}

// Get credentials
func (cp *credentialsProviderWrap) GetCredential() (cm *CredentialModel, err error) {
c, err := cp.provider.GetCredentials()
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions credentials/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ func TestNewCredentialWithAK(t *testing.T) {
assert.Equal(t, "AccessKeyId", *cm.AccessKeyId)
assert.Equal(t, "AccessKeySecret", *cm.AccessKeySecret)
assert.Equal(t, "", *cm.SecurityToken)

// test deprecated methods
accessKeyId, err := cred.GetAccessKeyId()
assert.Nil(t, err)
assert.Equal(t, "AccessKeyId", *accessKeyId)
accessKeySecret, err := cred.GetAccessKeySecret()
assert.Nil(t, err)
assert.Equal(t, "AccessKeySecret", *accessKeySecret)
securityToken, err := cred.GetSecurityToken()
assert.Nil(t, err)
assert.Equal(t, "", *securityToken)
}

func TestNewCredentialWithSts(t *testing.T) {
Expand Down Expand Up @@ -194,6 +205,11 @@ func TestNewCredentialWithRAMRoleARN(t *testing.T) {
cred, err = NewCredential(config)
assert.Nil(t, err)
assert.NotNil(t, cred)

config.SetRoleSessionName("role_session_name")
cred, err = NewCredential(config)
assert.Nil(t, err)
assert.NotNil(t, cred)
}

func TestNewCredentialWithBearerToken(t *testing.T) {
Expand Down

0 comments on commit b0bdc55

Please sign in to comment.