Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve test cases #108

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading