Skip to content

Commit

Permalink
testcase: Adds new unit test case for resource alicloud_cen_transit_r…
Browse files Browse the repository at this point in the history
…outer_route_table_association alicloud_cen_transit_router_route_table_propagation alicloud_cen_transit_router_route_table
  • Loading branch information
thisnihonglei committed Jan 21, 2022
1 parent 8adee5e commit b863349
Show file tree
Hide file tree
Showing 5 changed files with 864 additions and 4 deletions.
8 changes: 4 additions & 4 deletions alicloud/resource_alicloud_cen_transit_router_route_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ func resourceAlicloudCenTransitRouterRouteTableRead(d *schema.ResourceData, meta
func resourceAlicloudCenTransitRouterRouteTableUpdate(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 All @@ -157,10 +161,6 @@ func resourceAlicloudCenTransitRouterRouteTableUpdate(d *schema.ResourceData, me
request["DryRun"] = d.Get("dry_run")
}
action := "UpdateTransitRouterRouteTable"
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ package alicloud

import (
"fmt"
"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"
"os"
"reflect"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"

"github.com/alibabacloud-go/tea-rpc/client"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)
Expand Down Expand Up @@ -150,3 +158,254 @@ resource "alicloud_cen_transit_router_route_table" "default" {
`, name)
}

func TestAccAlicloudCenTransitRouterRouteTableAssociation_unit(t *testing.T) {
p := Provider().(*schema.Provider).ResourcesMap
d, _ := schema.InternalMap(p["alicloud_cen_transit_router_route_table_association"].Schema).Data(nil, nil)
dCreate, _ := schema.InternalMap(p["alicloud_cen_transit_router_route_table_association"].Schema).Data(nil, nil)
dCreate.MarkNewResource()
for key, value := range map[string]interface{}{
"transit_router_attachment_id": "MockTransitRouterAttachmentId",
"transit_router_route_table_id": "TransitRouterRouteTableId",
"dry_run": false,
} {
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{}{
"TransitRouterAssociations": []interface{}{
map[string]interface{}{
"Status": "Active",
"TransitRouterAttachmentId": "MockTransitRouterAttachmentId",
"TransitRouterRouteTableId": "TransitRouterRouteTableId",
},
},
}

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_cen_transit_router_route_table_association", "MockTransitRouterAttachmentId"))
},
"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["TransitRouterAttachmentId"] = "MockTransitRouterAttachmentId"
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{}), "NewCbnClient", func(_ *connectivity.AliyunClient) (*client.Client, error) {
return nil, &tea.SDKError{
Code: String("loadEndpoint error"),
Data: String("loadEndpoint error"),
Message: String("loadEndpoint error"),
}
})
err := resourceAlicloudCenTransitRouterRouteTableAssociationCreate(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"]("Operation.Blocking")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["CreateNormal"]("")
})
err := resourceAlicloudCenTransitRouterRouteTableAssociationCreate(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 := resourceAlicloudCenTransitRouterRouteTableAssociationCreate(dCreate, rawClient)
patches.Reset()
assert.Nil(t, err)
})

// Set ID for Update and Delete Method
d.SetId(fmt.Sprint("MockTransitRouterAttachmentId", ":", "TransitRouterRouteTableId"))
// Update
t.Run("UpdateNormal", func(t *testing.T) {
patcheDescribeCenTransitRouterRouteTableAssociation := gomonkey.ApplyMethod(reflect.TypeOf(&CbnService{}), "DescribeCenTransitRouterRouteTableAssociation", func(*CbnService, string) (map[string]interface{}, error) {
return responseMock["UpdateNormal"]("")
})
err := resourceAlicloudCenTransitRouterRouteTableAssociationUpdate(d, rawClient)
patcheDescribeCenTransitRouterRouteTableAssociation.Reset()
assert.Nil(t, err)
})

// Delete
t.Run("DeleteClientAbnormal", func(t *testing.T) {
patches := gomonkey.ApplyMethod(reflect.TypeOf(&connectivity.AliyunClient{}), "NewCbnClient", func(_ *connectivity.AliyunClient) (*client.Client, error) {
return nil, &tea.SDKError{
Code: String("loadEndpoint error"),
Data: String("loadEndpoint error"),
Message: String("loadEndpoint error"),
}
})
err := resourceAlicloudCenTransitRouterRouteTableAssociationDelete(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"]("Operation.Blocking")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["DeleteNormal"]("")
})
err := resourceAlicloudCenTransitRouterRouteTableAssociationDelete(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"]("Operation.Blocking")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["DeleteNormal"]("")
})
patcheDescribeCenTransitRouterRouteTableAssociation := gomonkey.ApplyMethod(reflect.TypeOf(&CbnService{}), "DescribeCenTransitRouterRouteTableAssociation", func(*CbnService, string) (map[string]interface{}, error) {
return responseMock["NotFoundError"]("ResourceNotfound")
})
err := resourceAlicloudCenTransitRouterRouteTableAssociationDelete(d, rawClient)
patches.Reset()
patcheDescribeCenTransitRouterRouteTableAssociation.Reset()
assert.Nil(t, err)
})

t.Run("DeleteNonRetryableError", func(t *testing.T) {
retryFlag := false
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 {
return responseMock["RetryError"]("Throttling")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["DeleteNormal"]("")
})
err := resourceAlicloudCenTransitRouterRouteTableAssociationDelete(d, rawClient)
patches.Reset()
assert.NotNil(t, err)
})

t.Run("DeleteMockParseResourceId", func(t *testing.T) {
resourceData1, _ := schema.InternalMap(p["alicloud_cen_transit_router_route_table_association"].Schema).Data(nil, nil)
resourceData1.SetId("MockId")
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"]("RetryError")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["DeleteNormal"]("")
})
err := resourceAlicloudCenTransitRouterRouteTableAssociationDelete(resourceData1, rawClient)
patches.Reset()
assert.NotNil(t, err)
})

//Read
t.Run("ReadDescribeCenTransitRouterRouteTableAssociationNotFound", 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 := resourceAlicloudCenTransitRouterRouteTableAssociationRead(d, rawClient)
patcheDorequest.Reset()
assert.Nil(t, err)
})

t.Run("ReadDescribeCenTransitRouterRouteTableAssociationNormal", 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 := resourceAlicloudCenTransitRouterRouteTableAssociationRead(d, rawClient)
patcheDorequest.Reset()
assert.NotNil(t, err)
})
}

0 comments on commit b863349

Please sign in to comment.