Skip to content

Commit

Permalink
test: add some test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Nov 21, 2024
1 parent 7c165fb commit 9829564
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
8 changes: 4 additions & 4 deletions integration/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func Test_CreateClusterTokenWithROArequestWithXMLWithPOST(t *testing.T) {
request.SetScheme("HTTPS")
response, err := client.CreateCluster(request)
assert.NotNil(t, err)
assert.Equal(t, 400, response.GetHttpStatus())
assert.Contains(t, err.Error(), "Request body can't be empty")
assert.Equal(t, 404, response.GetHttpStatus())
assert.Contains(t, err.Error(), "InvalidRequestBody")
}

func Test_DeleteClusterWithROArequestWithXMLWithDelete(t *testing.T) {
Expand All @@ -45,7 +45,7 @@ func Test_DeleteClusterWithROArequestWithXMLWithDelete(t *testing.T) {
request.SetScheme("HTTPS")
response, err := client.DeleteCluster(request)
assert.NotNil(t, err)
assert.Equal(t, 400, response.GetHttpStatus())
assert.Equal(t, 404, response.GetHttpStatus())
assert.Contains(t, err.Error(), "ErrorClusterNotFound")
}

Expand All @@ -57,7 +57,7 @@ func Test_DeleteClusterWithROArequestWithJSONWithDelete(t *testing.T) {
request.SetScheme("HTTPS")
response, err := client.DeleteCluster(request)
assert.NotNil(t, err)
assert.Equal(t, 400, response.GetHttpStatus())
assert.Equal(t, 404, response.GetHttpStatus())
assert.Contains(t, err.Error(), "ErrorClusterNotFound")
}

Expand Down
7 changes: 7 additions & 0 deletions sdk/auth/credentials/cli_profile_credentials_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ func TestCLIProfileCredentialsProvider_GetCredentials(t *testing.T) {
assert.Equal(t, "ststoken", cc.SecurityToken)
assert.Equal(t, "cli_profile/ram_role_arn/ram_role_arn/static_ak", cc.ProviderName)

provider.innerProvider = new(testProvider)
cc, err = provider.GetCredentials()
assert.Nil(t, err)
assert.Equal(t, "test", cc.AccessKeyId)
assert.Equal(t, "test", cc.AccessKeySecret)
assert.Equal(t, "cli_profile/test", cc.ProviderName)

os.Setenv("ALIBABA_CLOUD_CLI_PROFILE_DISABLED", "True")
_, err = provider.GetCredentials()
assert.NotNil(t, err)
Expand Down
1 change: 1 addition & 0 deletions sdk/auth/credentials/default_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (provider *DefaultCredentialsProvider) GetCredentials() (cc *Credentials, e
if provider.lastUsedProvider != nil {
inner, err1 := provider.lastUsedProvider.GetCredentials()
if err1 != nil {
err = err1
return
}

Expand Down
40 changes: 40 additions & 0 deletions sdk/auth/credentials/default_credentials_provider_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package credentials

import (
"errors"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -165,4 +166,43 @@ func TestDefaultCredentialsProvider_GetCredentials(t *testing.T) {
cc, err = provider.GetCredentials()
assert.Nil(t, err)
assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "aksecret", SecurityToken: "ststoken", ProviderName: "default/cli_profile/ram_role_arn/ram_role_arn/static_ak"}, cc)

provider.lastUsedProvider = new(testProvider)
cc, err = provider.GetCredentials()
assert.Nil(t, err)
assert.Equal(t, "test", cc.AccessKeyId)
assert.Equal(t, "test", cc.AccessKeySecret)
assert.Equal(t, "default/test", cc.ProviderName)

provider.lastUsedProvider = new(testErrorProvider)
_, err = provider.GetCredentials()
assert.Equal(t, "error", err.Error())
}

type testProvider struct {
}

func (provider *testProvider) GetCredentials() (cc *Credentials, err error) {
cc = &Credentials{
AccessKeyId: "test",
AccessKeySecret: "test",
ProviderName: "",
}
return
}

func (provider *testProvider) GetProviderName() string {
return "test"
}

type testErrorProvider struct {
}

func (provider *testErrorProvider) GetCredentials() (cc *Credentials, err error) {
err = errors.New("error")
return
}

func (provider *testErrorProvider) GetProviderName() string {
return "test"
}
1 change: 1 addition & 0 deletions sdk/auth/credentials/fixtures/.alibabacloud/credentials
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ access_key_id = foo
access_key_secret = bar
role_arn = role_arn
role_session_name = session_name
policy = policy

[noram]
type = ram_role_arn
Expand Down
11 changes: 11 additions & 0 deletions sdk/auth/credentials/profile_credentials_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,15 @@ func TestProfileCredentialsProviderGetCredentials(t *testing.T) {
assert.Equal(t, "aksecret", cc.AccessKeySecret)
assert.Equal(t, "ststoken", cc.SecurityToken)
assert.Equal(t, "profile/ram_role_arn/static_ak", cc.ProviderName)

provider.innerProvider = new(testProvider)
cc, err = provider.GetCredentials()
assert.Nil(t, err)
assert.Equal(t, "test", cc.AccessKeyId)
assert.Equal(t, "test", cc.AccessKeySecret)
assert.Equal(t, "profile/test", cc.ProviderName)

provider.innerProvider = new(testErrorProvider)
_, err = provider.GetCredentials()
assert.Equal(t, "error", err.Error())
}
20 changes: 20 additions & 0 deletions sdk/auth/credentials/uri_credentials_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package credentials

import (
"errors"
"io/ioutil"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -122,6 +123,25 @@ func TestURLCredentialsProvider_getCredentials(t *testing.T) {

p.expirationTimestamp = time.Now().Unix() + 300
assert.False(t, p.needUpdateCredential())

// case 4: mock read response error
hookDo = func(fn do) do {
return func(req *http.Request) (res *http.Response, err error) {
res = &http.Response{
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: map[string][]string{},
StatusCode: 200,
Status: "200 " + http.StatusText(200),
}
res.Body = ioutil.NopCloser(&errorReader{})
return
}
}
_, err = p.getCredentials()
assert.NotNil(t, err)
assert.Equal(t, "read failed", err.Error())
}

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

0 comments on commit 9829564

Please sign in to comment.