From b0bdc55a4371e561ae8989858055b6fb26de0548 Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Wed, 21 Aug 2024 00:05:02 +0800 Subject: [PATCH] improve test cases --- credentials/credential.go | 15 ++++++++++----- credentials/credential_test.go | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/credentials/credential.go b/credentials/credential.go index b62bc74..8f6381a 100644 --- a/credentials/credential.go +++ b/credentials/credential.go @@ -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 { @@ -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 @@ -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 { @@ -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 { @@ -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 { @@ -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 { diff --git a/credentials/credential_test.go b/credentials/credential_test.go index 4a752ee..c41f71c 100644 --- a/credentials/credential_test.go +++ b/credentials/credential_test.go @@ -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) { @@ -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) {