Skip to content

Commit

Permalink
chore: fix: test(er/ut): improve coverage of resource codes (#5096)
Browse files Browse the repository at this point in the history
* chore(er): format time value and fix the missing param

* test(er/ut): improve coverage of resource codes

* test(er/ut): use er datasource to query az info
  • Loading branch information
Lance52259 authored Jun 29, 2024
1 parent c8bd389 commit a8b9b6b
Show file tree
Hide file tree
Showing 33 changed files with 859 additions and 867 deletions.
8 changes: 8 additions & 0 deletions docs/resources/er_static_route.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ In addition to all arguments above, the following attributes are exported:

* `updated_at` - The latest update time of the static route.

## Timeouts

This resource provides the following timeouts configuration options:

* `create` - Default is 5 minutes.
* `update` - Default is 5 minutes.
* `delete` - Default is 2 minutes.

## Import

Static routes can be imported using the related `route_table_id` and their `id`, separated by a slash (/), e.g.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/GehirnInc/crypt v0.0.0-20200316065508-bb7000b8a962
github.com/chnsz/golangsdk v0.0.0-20240624075804-edb862aaa73a
github.com/chnsz/golangsdk v0.0.0-20240628074404-f04cd77ad44a
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-uuid v1.0.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chnsz/golangsdk v0.0.0-20240624075804-edb862aaa73a h1:q+hV7Y6DEYO6CfQ6vQXydHZ0awa2/b3uXsx5yWkz/RI=
github.com/chnsz/golangsdk v0.0.0-20240624075804-edb862aaa73a/go.mod h1:Erm4hDWxXgAdbkG3+hhJFgRzEL1TvvcroWzw2Gax4uI=
github.com/chnsz/golangsdk v0.0.0-20240628074404-f04cd77ad44a h1:3YjSSDC9ywWHhP+wC4c3ZEDIbqXElOjLV1onL2VTid4=
github.com/chnsz/golangsdk v0.0.0-20240628074404-f04cd77ad44a/go.mod h1:Erm4hDWxXgAdbkG3+hhJFgRzEL1TvvcroWzw2Gax4uI=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
Expand All @@ -15,8 +14,7 @@ import (
func TestAccDataSourceAssociations_basic(t *testing.T) {
var (
name = acceptance.RandomAccResourceName()
bgpAsNum = acctest.RandIntRange(64512, 65534)
baseConfig = testAccAssociation_basic(name, bgpAsNum)
baseConfig = testAccDataSourceAssociations_base(name)

all = "data.huaweicloud_er_associations.test"
dc = acceptance.InitDataSourceCheck(all)
Expand All @@ -34,53 +32,67 @@ func TestAccDataSourceAssociations_basic(t *testing.T) {
dcByNotFoundInstanceId = acceptance.InitDataSourceCheck(byNotFoundInstanceId)
)

resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDatasourceAssociations_basic(baseConfig),
Config: testAccDataSourceAssociations_basic_step1(baseConfig),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(all, "associations.#"),
resource.TestCheckResourceAttrSet(all, "associations.0.resource_id"),
resource.TestCheckResourceAttrSet(all, "associations.0.created_at"),
resource.TestCheckResourceAttrSet(all, "associations.0.updated_at"),

dcByStatus.CheckResourceExists(),
resource.TestCheckOutput("is_status_filter_useful", "true"),

resource.TestMatchResourceAttr(all, "associations.0.created_at",
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)),
resource.TestMatchResourceAttr(all, "associations.0.updated_at",
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)),
// Check whether filter parameter 'status' is effective.
dcByAttachmentId.CheckResourceExists(),
resource.TestCheckOutput("is_attachment_id_filter_useful", "true"),

// Check whether filter parameter 'attachment_type' is effective.
dcByAttachmentType.CheckResourceExists(),
resource.TestCheckOutput("is_attachment_type_filter_useful", "true"),

// Check whether filter parameter 'status' is effective.
dcByStatus.CheckResourceExists(),
resource.TestCheckOutput("is_status_filter_useful", "true"),
),
},
// If the instance ID does not exist, the data source will not report the error.
// Just return an empty list.
{
Config: testAccDatasourceAssociations_instanceIdNotFound(baseConfig),
// Checks whether the resource returns the expected empty list when the instance ID does not exist.
Config: testAccDataSourceAssociations_basic_step2(baseConfig),
Check: resource.ComposeTestCheckFunc(
dcByNotFoundInstanceId.CheckResourceExists(),
// If the instance ID does not exist, the data source will not report the error.
// Just return an empty list.
resource.TestCheckResourceAttr(byNotFoundInstanceId, "associations.#", "0"),
),
},
// If the routing table ID does not exist, the data source will report an error: 'route table {uuid} not found'.
{
Config: testAccDatasourceAssociations_routeTableIdNotFound(baseConfig),
// Checks whether the resource returns the expected error when the route table ID does not exist.
// Please ensure that the test account has 'ER FullAccess' permission for version 5.0.
Config: testAccDataSourceAssociations_basic_step3(baseConfig),
// If the routing table ID does not exist, the data source will report an error: 'route table {uuid} not found'.
ExpectError: regexp.MustCompile(`route table [a-f0-9-]+ not found`),
},
},
})
}

func testAccDatasourceAssociations_basic(baseConfig string) string {
func testAccDataSourceAssociations_base(name string) string {
return fmt.Sprintf(`
%[1]s
resource "huaweicloud_er_association" "test" {
instance_id = huaweicloud_er_instance.test.id
route_table_id = huaweicloud_er_route_table.test.id
attachment_id = huaweicloud_er_vpc_attachment.test.id
}
`, testAccAssociation_base(name))
}

func testAccDataSourceAssociations_basic_step1(baseConfig string) string {
return fmt.Sprintf(`
%[1]s
Expand Down Expand Up @@ -169,7 +181,7 @@ output "is_status_filter_useful" {
`, baseConfig)
}

func testAccDatasourceAssociations_instanceIdNotFound(baseConfig string) string {
func testAccDataSourceAssociations_basic_step2(baseConfig string) string {
randUUID, _ := uuid.GenerateUUID()

return fmt.Sprintf(`
Expand All @@ -186,7 +198,7 @@ data "huaweicloud_er_associations" "instance_id_not_found" {
`, baseConfig, randUUID)
}

func testAccDatasourceAssociations_routeTableIdNotFound(baseConfig string) string {
func testAccDataSourceAssociations_basic_step3(baseConfig string) string {
randUUID, _ := uuid.GenerateUUID()

return fmt.Sprintf(`
Expand Down
Loading

0 comments on commit a8b9b6b

Please sign in to comment.