Skip to content

Commit

Permalink
chore(waf): improve test case execution efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
deer-hang committed Nov 4, 2024
1 parent d20815a commit a916259
Showing 1 changed file with 76 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@ import (
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

// Before running the test case, please ensure that there is at least one WAF instance in the current region.
func TestAccDatasourceWAFAddressGroups_basic(t *testing.T) {
name := acceptance.RandomAccResourceName()
rName := "data.huaweicloud_waf_address_groups.test"
dc := acceptance.InitDataSourceCheck(rName)
var (
name = acceptance.RandomAccResourceName()
rName = "data.huaweicloud_waf_address_groups.test"
dc = acceptance.InitDataSourceCheck(rName)

byName = "data.huaweicloud_waf_address_groups.name_filter"
dcByName = acceptance.InitDataSourceCheck(byName)

byIpAddress = "data.huaweicloud_waf_address_groups.ip_address_filter"
dcByIpAddress = acceptance.InitDataSourceCheck(byIpAddress)

byAllParameters = "data.huaweicloud_waf_address_groups.all_parameters_filter"
dcByAllParameters = acceptance.InitDataSourceCheck(byAllParameters)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPrecheckWafInstance(t)
acceptance.TestAccPreCheckEpsID(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
Expand All @@ -34,65 +47,103 @@ func TestAccDatasourceWAFAddressGroups_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(rName, "groups.0.accept_count"),
resource.TestCheckResourceAttrSet(rName, "groups.0.process_status"),

dcByName.CheckResourceExists(),
resource.TestCheckOutput("name_filter_is_useful", "true"),

dcByIpAddress.CheckResourceExists(),
resource.TestCheckOutput("ip_address_filter_is_useful", "true"),

resource.TestCheckOutput("enterprise_project_id_filter_is_useful", "true"),
dcByAllParameters.CheckResourceExists(),
resource.TestCheckOutput("all_parameters_filter_is_useful", "true"),
),
},
},
})
}

func testAccDatasourceAddressGroups_base(name string) string {
return fmt.Sprintf(`
resource "huaweicloud_waf_address_group" "test" {
name = "%[1]s"
description = "example_description"
ip_addresses = ["192.168.1.0/24"]
enterprise_project_id = "%[2]s"
}
`, name, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST)
}

func testAccDatasourceAddressGroups_basic(name string) string {
return fmt.Sprintf(`
%s
%[1]s
data "huaweicloud_waf_address_groups" "test" {
enterprise_project_id = "%[2]s"
depends_on = [huaweicloud_waf_address_group.test]
}
data "huaweicloud_waf_address_groups" "name_filter" {
# Filter by name
locals {
name = data.huaweicloud_waf_address_groups.test.groups.0.name
}
data "huaweicloud_waf_address_groups" "name_filter" {
enterprise_project_id = "%[2]s"
name = local.name
depends_on = [huaweicloud_waf_address_group.test]
}
locals {
name = data.huaweicloud_waf_address_groups.test.groups.0.name
name_filter_result = [
for v in data.huaweicloud_waf_address_groups.name_filter.groups[*].name : v == local.name
]
}
output "name_filter_is_useful" {
value = length(data.huaweicloud_waf_address_groups.name_filter.groups) > 0 && alltrue(
[for v in data.huaweicloud_waf_address_groups.name_filter.groups[*].name : v == local.name]
)
value = length(local.name_filter_result) > 0 && alltrue(local.name_filter_result)
}
# Filter by ip_address
locals {
ip_address = data.huaweicloud_waf_address_groups.test.groups.0.ip_addresses
}
data "huaweicloud_waf_address_groups" "ip_address_filter" {
ip_address = data.huaweicloud_waf_address_groups.test.groups.0.ip_addresses
enterprise_project_id = "%[2]s"
ip_address = local.ip_address
depends_on = [huaweicloud_waf_address_group.test]
}
locals {
ip_address = data.huaweicloud_waf_address_groups.test.groups.0.ip_addresses
ip_address_filter_result = [
for v in data.huaweicloud_waf_address_groups.ip_address_filter.groups[*].ip_addresses : v == local.ip_address
]
}
output "ip_address_filter_is_useful" {
value = length(data.huaweicloud_waf_address_groups.ip_address_filter.groups) > 0 && alltrue(
[for v in data.huaweicloud_waf_address_groups.ip_address_filter.groups[*].ip_addresses : v == local.ip_address]
)
value = length(local.ip_address_filter_result) > 0 && alltrue(local.ip_address_filter_result)
}
data "huaweicloud_waf_address_groups" "enterprise_project_id_filter" {
enterprise_project_id = data.huaweicloud_waf_address_groups.test.groups.0.enterprise_project_id
# Filter by all parameters
data "huaweicloud_waf_address_groups" "all_parameters_filter" {
enterprise_project_id = "%[2]s"
name = local.name
ip_address = local.ip_address
depends_on = [huaweicloud_waf_address_group.test]
}
locals {
enterprise_project_id = data.huaweicloud_waf_address_groups.test.groups.0.enterprise_project_id
all_parameters_filter_result = [
for v in data.huaweicloud_waf_address_groups.all_parameters_filter.groups[*] :
v.name == local.name && v.ip_addresses == local.ip_address
]
}
output "enterprise_project_id_filter_is_useful" {
value = length(data.huaweicloud_waf_address_groups.enterprise_project_id_filter.groups) > 0 && alltrue(
[for v in data.huaweicloud_waf_address_groups.enterprise_project_id_filter.groups[*].enterprise_project_id : v == local.enterprise_project_id]
)
output "all_parameters_filter_is_useful" {
value = length(local.all_parameters_filter_result) > 0 && alltrue(local.all_parameters_filter_result)
}
`, testAddressGroup_basic(name))
`, testAccDatasourceAddressGroups_base(name), acceptance.HW_ENTERPRISE_PROJECT_ID_TEST)
}

0 comments on commit a916259

Please sign in to comment.