Skip to content

Commit

Permalink
testcase: Improve the effectiveness of test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanghx0991 committed Feb 8, 2022
1 parent 01a4061 commit a116924
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 11 deletions.
3 changes: 2 additions & 1 deletion alicloud/connectivity/regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ var CloudSsoSupportRegions = []Region{Shanghai, USWest1}
var SWASSupportRegions = []Region{Qingdao, Hangzhou, Beijing, Shenzhen, Shanghai, GuangZhou, Huhehaote, ChengDu, Zhangjiakou, Hongkong, APSouthEast1}
var SurveillanceSystemSupportRegions = []Region{Beijing, Shenzhen, Qingdao}
var VodSupportRegions = []Region{Shanghai}
var OpenSearchSupportRegions = []Region{Beijing, Shenzhen, Hangzhou, Zhangjiakou, Qingdao, Shanghai, APSouthEast1}
var OpenSearchSupportRegions = []Region{Hangzhou, APSouthEast1}
var PvtzSupportRegions = []Region{Hangzhou}
var GraphDatabaseSupportRegions = []Region{Shenzhen, Beijing, Qingdao, Shanghai, Hongkong, Zhangjiakou, Hangzhou, APSouthEast1, APSouthEast5, USWest1, USEast1, APSouth1}
var DBFSSystemSupportRegions = []Region{Hangzhou}
var EAISSystemSupportRegions = []Region{Hangzhou}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,11 @@ func TestAccAlicloudECSDedicatedHostCluster_unit(t *testing.T) {
})
t.Run("DeleteMockAbnormal", func(t *testing.T) {
retryFlag := true
noRetryFlag := 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 {
// retry until the timeout comes
retryFlag = false
return responseMock["RetryError"]("Throttling")
} else if noRetryFlag {
noRetryFlag = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func resourceEssScalingConfigurationConfigDependence(name string) string {
owners = "system"
}
data "alicloud_instance_types" "t5" {
instance_type_family = "ecs.t5"
instance_type_family = "ecs.g6e"
availability_zone = "${data.alicloud_zones.default.zones.0.id}"
}
resource "alicloud_kms_key" "key" {
Expand Down
95 changes: 95 additions & 0 deletions alicloud/resource_alicloud_event_bridge_event_bus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,108 @@ package alicloud

import (
"fmt"
"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
log "github.com/sirupsen/logrus"
"strings"
"testing"
"time"

"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func init() {
resource.AddTestSweepers(
"alicloud_event_bridge_event_bus",
&resource.Sweeper{
Name: "alicloud_event_bridge_event_bus",
F: testSweepEventBridgeBus,
})
}

func testSweepEventBridgeBus(region string) error {
rawClient, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting Alicloud client: %s", err)
}
client := rawClient.(*connectivity.AliyunClient)

prefixes := []string{
"tf-testAcc",
"tf_testAcc",
}
action := "ListEventBuses"
request := make(map[string]interface{})
request["Limit"] = PageSizeLarge
var response map[string]interface{}
conn, err := client.NewEventbridgeClient()
if err != nil {
return WrapError(err)
}
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-04-01"), StringPointer("AK"), nil, request, &runtime)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_event_bridge_event_buses", action, AlibabaCloudSdkGoERROR)
}
if fmt.Sprint(response["Success"]) == "false" {
return WrapError(fmt.Errorf("ListEventBuses failed, response: %v", response))
}
resp, err := jsonpath.Get("$.Data.EventBuses", response)
if err != nil {
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.Data.EventBuses", response)
}
result, _ := resp.([]interface{})
for _, v := range result {
item := v.(map[string]interface{})
skip := true
for _, prefix := range prefixes {
if strings.HasPrefix(strings.ToLower(item["EventBusName"].(string)), strings.ToLower(prefix)) {
skip = false
}
}
if skip {
log.Printf("[INFO] Skipping EventBridgeBus: %s", item["EventBusName"].(string))
continue
}
action := "DeleteEventBus"
request = map[string]interface{}{
"EventBusName": item["EventBusName"],
}
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-04-01"), StringPointer("AK"), nil, request, &runtime)
if err != nil {
log.Printf("[ERROR] Failed to delete EventBridgeBus (%s): %s", item["EventBusName"].(string), err)
}
log.Printf("[INFO] Delete EventBridgeBus (%s) Success", item["EventBusName"].(string))
}
if nextToken, ok := response["NextToken"].(string); ok && nextToken != "" {
request["NextToken"] = nextToken
} else {
break
}
}
return nil
}

func TestName(t *testing.T) {
testSweepEventBridgeBus("eu-central-1")
}

func TestAccAlicloudEventBridgeEventBus_basic(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_event_bridge_event_bus.default"
Expand Down
2 changes: 1 addition & 1 deletion alicloud/resource_alicloud_log_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAccAlicloudLogDashboard_basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{
"project_name": name,
"project_name": "${alicloud_log_project.default.name}",
"dashboard_name": "dashboard_name",
"display_name": displayname,
"char_list": `[{\"title\":\"new_title\",\"type\":\"map\",\"search\":{\"logstore\":\"new_logstore\",\"topic\":\"new_topic\",\"query\":\"method: GET | select ip_to_province(remote_addr) as province , count(1) as pv group by province order by pv desc \",\"start\":\"-86400s\",\"end\":\"now\"},\"display\":{\"xAxis\":[\"province\"],\"yAxis\":[\"aini\"],\"xPos\":0,\"yPos\":0,\"width\":10,\"height\":12,\"displayName\":\"xixihaha911\"}}]`,
Expand Down
4 changes: 2 additions & 2 deletions alicloud/resource_alicloud_nas_file_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestAccAlicloudNasFileSystem_basic(t *testing.T) {
Config: testAccConfig(map[string]interface{}{
"protocol_type": "${data.alicloud_nas_protocols.example.protocols.0}",
"storage_type": "Capacity",
"zone_id": "${data.alicloud_nas_zones.default.zones.1.zone_id}",
"zone_id": "${data.alicloud_nas_zones.default.zones.0.zone_id}",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestAccAlicloudNasFileSystemEncrypt(t *testing.T) {
"protocol_type": "NFS",
"storage_type": "Capacity",
"encrypt_type": "1",
"zone_id": "${data.alicloud_nas_zones.default.zones.1.zone_id}",
"zone_id": "${data.alicloud_nas_zones.default.zones.0.zone_id}",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
Expand Down
8 changes: 5 additions & 3 deletions alicloud/resource_alicloud_open_search_app_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func testSweepOpenSearchAppGroup(region string) error {
}

func TestAccAlicloudOpenSearchAppGroup_basic0(t *testing.T) {
checkoutSupportedRegions(t, true, connectivity.OpenSearchSupportRegions)
var v map[string]interface{}
resourceId := "alicloud_open_search_app_group.default"
ra := resourceAttrInit(resourceId, AlicloudOpenSearchAppGroupMap0)
Expand Down Expand Up @@ -153,14 +154,14 @@ func TestAccAlicloudOpenSearchAppGroup_basic0(t *testing.T) {
{
"doc_size": "100",
"compute_resource": "500",
"spec": "opensearch.private.common",
"spec": "opensearch.share.common",
},
},
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"app_group_name": name,
"type": "standard",
"type": "enhanced",
"payment_type": "PayAsYouGo",
}),
),
Expand Down Expand Up @@ -192,7 +193,7 @@ func TestAccAlicloudOpenSearchAppGroup_basic0(t *testing.T) {
{
"doc_size": "200",
"compute_resource": "1000",
"spec": "opensearch.private.compute",
"spec": "opensearch.share.common",
},
},
}),
Expand All @@ -212,6 +213,7 @@ func TestAccAlicloudOpenSearchAppGroup_basic0(t *testing.T) {
})
}
func TestAccAlicloudOpenSearchAppGroup_basic1(t *testing.T) {
checkoutSupportedRegions(t, true, connectivity.OpenSearchSupportRegions)
var v map[string]interface{}
resourceId := "alicloud_open_search_app_group.default"
ra := resourceAttrInit(resourceId, AlicloudOpenSearchAppGroupMap0)
Expand Down
1 change: 1 addition & 0 deletions alicloud/resource_alicloud_pvtz_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func testSweepPrivateZoneEndpoint(region string) error {
}

func TestAccAlicloudPrivateZoneEndpoint_basic0(t *testing.T) {
checkoutSupportedRegions(t, true, connectivity.PvtzSupportRegions)
var v map[string]interface{}
resourceId := "alicloud_pvtz_endpoint.default"
ra := resourceAttrInit(resourceId, AlicloudPrivateZoneEndpointMap0)
Expand Down
3 changes: 2 additions & 1 deletion alicloud/resource_alicloud_route_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,11 @@ func TestAccAlicloudVpcRouteTable_unit(t *testing.T) {
})
t.Run("DeleteMockAbnormal", func(t *testing.T) {
retryFlag := true
noRetryFlag := 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 {
// retry until the timeout comes
retryFlag = false
return responseMock["RetryError"]("OperationConflict")
} else if noRetryFlag {
noRetryFlag = false
Expand Down
3 changes: 2 additions & 1 deletion alicloud/resource_alicloud_vpc_bgp_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,10 @@ func TestAccAlicloudVPCBgpGroup_unit(t *testing.T) {
})
t.Run("DeleteMockAbnormal", func(t *testing.T) {
retryFlag := true
noRetryFlag := 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 {
retryFlag = false
// retry until the timeout comes
return responseMock["RetryError"]("Throttling")
} else if noRetryFlag {
Expand Down

0 comments on commit a116924

Please sign in to comment.