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 16, 2024
1 parent 413499f commit 93ee11a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
16 changes: 7 additions & 9 deletions sdk/auth/credentials/provider/env_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider_test
package provider

import (
"os"
Expand All @@ -7,23 +7,21 @@ import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"

"github.com/stretchr/testify/assert"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials/provider"
)

func TestEnvResolve(t *testing.T) {
p := provider.NewEnvProvider()
assert.Equal(t, &provider.EnvProvider{}, p)
p := NewEnvProvider()
assert.Equal(t, &EnvProvider{}, p)
c, err := p.Resolve()
assert.Nil(t, c)
assert.Nil(t, err)
os.Setenv(provider.ENVAccessKeyID, "")
os.Setenv(provider.ENVAccessKeySecret, "")
os.Setenv(ENVAccessKeyID, "")
os.Setenv(ENVAccessKeySecret, "")
c, err = p.Resolve()
assert.Nil(t, c)
assert.EqualError(t, err, "Environmental variable (ALIBABACLOUD_ACCESS_KEY_ID or ALIBABACLOUD_ACCESS_KEY_SECRET) is empty")
os.Setenv(provider.ENVAccessKeyID, "AccessKeyId")
os.Setenv(provider.ENVAccessKeySecret, "AccessKeySecret")
os.Setenv(ENVAccessKeyID, "AccessKeyId")
os.Setenv(ENVAccessKeySecret, "AccessKeySecret")
c, err = p.Resolve()
assert.Nil(t, err)
assert.Equal(t, &credentials.AccessKeyCredential{AccessKeyId: "AccessKeyId", AccessKeySecret: "AccessKeySecret"}, c)
Expand Down
10 changes: 7 additions & 3 deletions sdk/auth/credentials/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import (

// Environmental virables that may be used by the provider
const (
ENVAccessKeyID = "ALIBABA_CLOUD_ACCESS_KEY_ID"
// Deprecated: don't use it outside of this project
ENVAccessKeyID = "ALIBABA_CLOUD_ACCESS_KEY_ID"
// Deprecated: don't use it outside of this project
ENVAccessKeySecret = "ALIBABA_CLOUD_ACCESS_KEY_SECRET"
ENVCredentialFile = "ALIBABA_CLOUD_CREDENTIALS_FILE"
ENVEcsMetadata = "ALIBABA_CLOUD_ECS_METADATA"
// Deprecated: don't use it outside of this project
ENVCredentialFile = "ALIBABA_CLOUD_CREDENTIALS_FILE"
// Deprecated: don't use it outside of this project
ENVEcsMetadata = "ALIBABA_CLOUD_ECS_METADATA"
)

// When you want to customize the provider, you only need to implement the method of the interface.
Expand Down
7 changes: 7 additions & 0 deletions sdk/requests/acs_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -119,6 +120,12 @@ func Test_AcsRequest(t *testing.T) {
assert.Equal(t, []byte(nil), r.GetContent())
r.SetContent([]byte("The Content"))
assert.True(t, bytes.Equal([]byte("The Content"), r.GetContent()))

// get/set for tracer span
assert.Equal(t, nil, r.GetTracerSpan())
span := opentracing.StartSpan("test_operation")
r.SetTracerSpan(span)
assert.Equal(t, span, r.GetTracerSpan())
}

type AcsRequestTest struct {
Expand Down
10 changes: 8 additions & 2 deletions sdk/requests/roa_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ func Test_RoaRequest_initWithCommonRequest(t *testing.T) {
assert.Equal(t, "GET", r.GetMethod())
assert.Equal(t, "ROA", r.GetStyle())
assert.Equal(t, "common", r.Headers["x-sdk-invoke-type"])
// assert.Equal(t, "version", r.GetVersion())
// assert.Equal(t, "action", r.GetActionName())

// test for x-acs-action
assert.Equal(t, "", r.GetHeaders()["x-acs-action"])

common = NewCommonRequest()
common.ApiName = "MockAPIName"
r.initWithCommonRequest(common)
assert.Equal(t, "MockAPIName", r.GetHeaders()["x-acs-action"])
}

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

0 comments on commit 93ee11a

Please sign in to comment.