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

testcase: Adds new unit test case for resource alicloud_cdn_real_time_log_delivery alicloud_cen_transit_router_vbr_attachment alicloud_cen_transit_router_vpc_attachment #4523

Merged
merged 1 commit into from
Jan 25, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
210 changes: 210 additions & 0 deletions alicloud/resource_alicloud_cdn_real_time_log_delivery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ package alicloud

import (
"fmt"
"os"
"reflect"
"testing"

"github.com/agiledragon/gomonkey/v2"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/alibabacloud-go/tea/tea"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/stretchr/testify/assert"

"github.com/alibabacloud-go/tea-rpc/client"

"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -87,3 +97,203 @@ resource "alicloud_log_store" "default" {
}
`, name, fmt.Sprintf("%s.example.com", name))
}

func TestAccAlicloudCDNRealTimeLogDelivery_unit(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

单测100%覆盖率

p := Provider().(*schema.Provider).ResourcesMap
d, _ := schema.InternalMap(p["alicloud_cdn_real_time_log_delivery"].Schema).Data(nil, nil)
dCreate, _ := schema.InternalMap(p["alicloud_cdn_real_time_log_delivery"].Schema).Data(nil, nil)
dCreate.MarkNewResource()
for key, value := range map[string]interface{}{
"domain": "MockDomain",
"project": "project",
"logstore": "logstore",
"sls_region": "sls_region",
} {
err := dCreate.Set(key, value)
assert.Nil(t, err)
err = d.Set(key, value)
assert.Nil(t, err)
}
region := os.Getenv("ALICLOUD_REGION")
rawClient, err := sharedClientForRegion(region)
if err != nil {
t.Skipf("Skipping the test case with err: %s", err)
t.Skipped()
}
rawClient = rawClient.(*connectivity.AliyunClient)
ReadMockResponse := map[string]interface{}{
"Domain": "MockDomain",
"Logstore": "logstore",
"Project": "project",
"Region": "sls_region",
"Status": "status",
}

responseMock := map[string]func(errorCode string) (map[string]interface{}, error){
"RetryError": func(errorCode string) (map[string]interface{}, error) {
return nil, &tea.SDKError{
Code: String(errorCode),
Data: String(errorCode),
Message: String(errorCode),
}
},
"NotFoundError": func(errorCode string) (map[string]interface{}, error) {
return nil, GetNotFoundErrorFromString(GetNotFoundMessage("alicloud_cdn_real_time_log_delivery", "MockDomain"))
},
"NoRetryError": func(errorCode string) (map[string]interface{}, error) {
return nil, &tea.SDKError{
Code: String(errorCode),
Data: String(errorCode),
Message: String(errorCode),
}
},
"CreateNormal": func(errorCode string) (map[string]interface{}, error) {
result := ReadMockResponse
result["Domain"] = "MockDomain"
return result, nil
},
"UpdateNormal": func(errorCode string) (map[string]interface{}, error) {
result := ReadMockResponse
return result, nil
},
"DeleteNormal": func(errorCode string) (map[string]interface{}, error) {
result := ReadMockResponse
return result, nil
},
"ReadNormal": func(errorCode string) (map[string]interface{}, error) {
result := ReadMockResponse
return result, nil
},
}
// Create
t.Run("CreateClientAbnormal", func(t *testing.T) {
patches := gomonkey.ApplyMethod(reflect.TypeOf(&connectivity.AliyunClient{}), "NewCdnClient", func(_ *connectivity.AliyunClient) (*client.Client, error) {
return nil, &tea.SDKError{
Code: String("loadEndpoint error"),
Data: String("loadEndpoint error"),
Message: String("loadEndpoint error"),
}
})
err := resourceAlicloudCdnRealTimeLogDeliveryCreate(d, rawClient)
patches.Reset()
assert.NotNil(t, err)
})
t.Run("CreateAbnormal", func(t *testing.T) {
retryFlag := true
noRetryFlag := true
patches := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
if retryFlag {
retryFlag = false
return responseMock["RetryError"]("InternalError")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["CreateNormal"]("")
})
err := resourceAlicloudCdnRealTimeLogDeliveryCreate(d, rawClient)
patches.Reset()
assert.NotNil(t, err)
})
t.Run("CreateNormal", func(t *testing.T) {
retryFlag := false
noRetryFlag := false
patches := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
if retryFlag {
retryFlag = false
return responseMock["RetryError"]("Throttling")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["CreateNormal"]("")
})
err := resourceAlicloudCdnRealTimeLogDeliveryCreate(dCreate, rawClient)
patches.Reset()
assert.Nil(t, err)
})

// Set ID for Update and Delete Method
d.SetId(fmt.Sprint("CenId", ":", "MockTransitRouterAttachmentId"))

// Delete
t.Run("DeleteClientAbnormal", func(t *testing.T) {
patches := gomonkey.ApplyMethod(reflect.TypeOf(&connectivity.AliyunClient{}), "NewCdnClient", func(_ *connectivity.AliyunClient) (*client.Client, error) {
return nil, &tea.SDKError{
Code: String("loadEndpoint error"),
Data: String("loadEndpoint error"),
Message: String("loadEndpoint error"),
}
})
err := resourceAlicloudCdnRealTimeLogDeliveryDelete(d, rawClient)
patches.Reset()
assert.NotNil(t, err)
})
t.Run("DeleteMockAbnormal", func(t *testing.T) {
retryFlag := true
noRetryFlag := true
patches := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
if retryFlag {
retryFlag = false
return responseMock["RetryError"]("Throttling")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["DeleteNormal"]("")
})
err := resourceAlicloudCdnRealTimeLogDeliveryDelete(d, rawClient)
patches.Reset()
assert.NotNil(t, err)
})
t.Run("DeleteMockNormal", func(t *testing.T) {
retryFlag := false
noRetryFlag := false
patches := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
if retryFlag {
retryFlag = false
return responseMock["RetryError"]("Throttling")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["DeleteNormal"]("")
})
err := resourceAlicloudCdnRealTimeLogDeliveryDelete(d, rawClient)
patches.Reset()
assert.Nil(t, err)
})

//Read
t.Run("ReadDescribeCdnRealTimeLogDeliveryNotFound", func(t *testing.T) {
patcheDorequest := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
NotFoundFlag := true
noRetryFlag := false
if NotFoundFlag {
return responseMock["NotFoundError"]("ResourceNotfound")
} else if noRetryFlag {
return responseMock["NoRetryError"]("NoRetryError")
}
return responseMock["ReadNormal"]("")
})
err := resourceAlicloudCdnRealTimeLogDeliveryRead(d, rawClient)
patcheDorequest.Reset()
assert.Nil(t, err)
})

t.Run("ReadDescribeCdnRealTimeLogDeliveryAbnormal", func(t *testing.T) {
patcheDorequest := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
retryFlag := false
noRetryFlag := true
if retryFlag {
return responseMock["RetryError"]("Throttling")
} else if noRetryFlag {
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["ReadNormal"]("")
})
err := resourceAlicloudCdnRealTimeLogDeliveryRead(d, rawClient)
patcheDorequest.Reset()
assert.NotNil(t, err)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ func resourceAlicloudCenTransitRouterVbrAttachmentRead(d *schema.ResourceData, m
func resourceAlicloudCenTransitRouterVbrAttachmentUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
cbnService := CbnService{client}
conn, err := client.NewCbnClient()
if err != nil {
return WrapError(err)
}
var response map[string]interface{}
parts, err1 := ParseResourceId(d.Id(), 2)
if err1 != nil {
Expand Down Expand Up @@ -228,10 +232,6 @@ func resourceAlicloudCenTransitRouterVbrAttachmentUpdate(d *schema.ResourceData,
request["DryRun"] = d.Get("dry_run")
}
action := "UpdateTransitRouterVbrAttachmentAttribute"
conn, err := client.NewCbnClient()
if err != nil {
return WrapError(err)
}
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-09-12"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
Expand Down