Skip to content

Commit

Permalink
Add test cases for ToCredentialsProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Aug 12, 2024
1 parent 439070c commit b67adee
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
62 changes: 62 additions & 0 deletions sdk/auth/credential_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
package auth

import (
"testing"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"github.com/stretchr/testify/assert"
)

func TestToCredentialsProvider(t *testing.T) {
// StaticAKCredentialsProvider
ak := credentials.NewAccessKeyCredential("id", "secret")
p, err := ToCredentialsProvider(ak)
assert.Nil(t, err)
_, ok := p.(*credentials.StaticAKCredentialsProvider)
assert.True(t, ok)

// StaticAKCredentialsProvider
sts := credentials.NewStsTokenCredential("id", "secret", "token")
p, err = ToCredentialsProvider(sts)
assert.Nil(t, err)
_, ok = p.(*credentials.StaticSTSCredentialsProvider)
assert.True(t, ok)

bt := credentials.NewBearerTokenCredential("bearertoken")
p, err = ToCredentialsProvider(bt)
assert.Nil(t, err)
_, ok = p.(*credentials.BearerTokenCredentialsProvider)
assert.True(t, ok)

errc := credentials.NewEcsRamRoleCredential("rolename")
p, err = ToCredentialsProvider(errc)
assert.Nil(t, err)
_, ok = p.(*credentials.ECSRAMRoleCredentialsProvider)
assert.True(t, ok)

rpc := credentials.NewRsaKeyPairCredential("privatekey", "publickeyId", 900)
p, err = ToCredentialsProvider(rpc)
assert.Nil(t, err)
_, ok = p.(*credentials.RSAKeyPairCredentialsProvider)
assert.True(t, ok)

rrac := credentials.NewRamRoleArnCredential("akid", "aksecret", "roleArn", "rsn", 900)
p, err = ToCredentialsProvider(rrac)
assert.Nil(t, err)
_, ok = p.(*credentials.RAMRoleARNCredentialsProvider)
assert.True(t, ok)

oidc, err := credentials.NewOIDCCredentialsProviderBuilder().
WithOIDCTokenFilePath("/path/to/oidctoken").
WithOIDCProviderARN("providerarn").
WithRoleArn("rolearn").
Build()
assert.Nil(t, err)
p, err = ToCredentialsProvider(oidc)
assert.Nil(t, err)
_, ok = p.(*credentials.OIDCCredentialsProvider)
assert.True(t, ok)

_, err = ToCredentialsProvider("string")
assert.NotNil(t, err)
assert.Equal(t, "[SDK.UnsupportedCredential] Specified credential (type = string) is not supported, please check", err.Error())
}
12 changes: 6 additions & 6 deletions sdk/auth/credentials/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func mockResponse(statusCode int, content string) (res *http.Response) {
StatusCode: statusCode,
Status: status + " " + http.StatusText(statusCode),
}
res.Body = io.NopCloser(bytes.NewReader([]byte(content)))
res.Body = ioutil.NopCloser(bytes.NewReader([]byte(content)))
return
}

Expand Down Expand Up @@ -126,7 +126,7 @@ vftlY0Hs1vNXcaBgEA==`
StatusCode: 200,
Status: status + " " + http.StatusText(200),
}
res.Body = io.NopCloser(&errorReader{})
res.Body = ioutil.NopCloser(&errorReader{})
return
}
}
Expand Down Expand Up @@ -360,7 +360,7 @@ func TestRAMRoleARNCredentialsProvider_getCredentials(t *testing.T) {
StatusCode: 200,
Status: status + " " + http.StatusText(200),
}
res.Body = io.NopCloser(&errorReader{})
res.Body = ioutil.NopCloser(&errorReader{})
return
}
}
Expand Down Expand Up @@ -601,7 +601,7 @@ func TestECSRAMRoleCredentialsProvider_getRoleName(t *testing.T) {
StatusCode: 200,
Status: status + " " + http.StatusText(200),
}
res.Body = io.NopCloser(&errorReader{})
res.Body = ioutil.NopCloser(&errorReader{})
return
}
}
Expand Down Expand Up @@ -705,7 +705,7 @@ func TestECSRAMRoleCredentialsProvider_getCredentials(t *testing.T) {
StatusCode: 200,
Status: status + " " + http.StatusText(200),
}
res.Body = io.NopCloser(&errorReader{})
res.Body = ioutil.NopCloser(&errorReader{})
return
}
return
Expand Down Expand Up @@ -1044,7 +1044,7 @@ func TestOIDCCredentialsProvider_getCredentials(t *testing.T) {
StatusCode: 200,
Status: status + " " + http.StatusText(200),
}
res.Body = io.NopCloser(&errorReader{})
res.Body = ioutil.NopCloser(&errorReader{})
return
}
}
Expand Down

0 comments on commit b67adee

Please sign in to comment.