Skip to content

Commit

Permalink
use random cidr in test cases of vpc and subnet to avoid conflict whi…
Browse files Browse the repository at this point in the history
…le running test (#689)
  • Loading branch information
Jason-Zhang9309 authored Nov 25, 2020
1 parent 33952a7 commit 4ae4afc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
24 changes: 14 additions & 10 deletions huaweicloud/data_source_huaweicloud_vpc_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package huaweicloud

import (
"fmt"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
Expand All @@ -11,18 +12,21 @@ import (

func TestAccVpcSubnetV1DataSource_basic(t *testing.T) {
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5))
tmp := strconv.Itoa(acctest.RandIntRange(1, 254))
cidr := fmt.Sprintf("172.16.%s.0/24", string(tmp))
gateway := fmt.Sprintf("172.16.%s.1", string(tmp))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVpcSubnetV1Config(rName),
Config: testAccDataSourceVpcSubnetV1Config(rName, cidr, gateway),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceVpcSubnetV1Check("data.huaweicloud_vpc_subnet.by_id", rName, "172.16.8.0/24", "172.16.8.1"),
testAccDataSourceVpcSubnetV1Check("data.huaweicloud_vpc_subnet.by_cidr", rName, "172.16.8.0/24", "172.16.8.1"),
testAccDataSourceVpcSubnetV1Check("data.huaweicloud_vpc_subnet.by_name", rName, "172.16.8.0/24", "172.16.8.1"),
testAccDataSourceVpcSubnetV1Check("data.huaweicloud_vpc_subnet.by_vpc_id", rName, "172.16.8.0/24", "172.16.8.1"),
testAccDataSourceVpcSubnetV1Check("data.huaweicloud_vpc_subnet.by_id", rName, cidr, gateway),
testAccDataSourceVpcSubnetV1Check("data.huaweicloud_vpc_subnet.by_cidr", rName, cidr, gateway),
testAccDataSourceVpcSubnetV1Check("data.huaweicloud_vpc_subnet.by_name", rName, cidr, gateway),
testAccDataSourceVpcSubnetV1Check("data.huaweicloud_vpc_subnet.by_vpc_id", rName, cidr, gateway),
resource.TestCheckResourceAttr(
"data.huaweicloud_vpc_subnet.by_id", "status", "ACTIVE"),
resource.TestCheckResourceAttr(
Expand Down Expand Up @@ -69,19 +73,19 @@ func testAccDataSourceVpcSubnetV1Check(n, name, cidr, gateway_ip string) resourc
}
}

func testAccDataSourceVpcSubnetV1Config(rName string) string {
func testAccDataSourceVpcSubnetV1Config(rName, cidr, gateway string) string {
return fmt.Sprintf(`
data "huaweicloud_availability_zones" "test" {}
resource "huaweicloud_vpc" "test" {
name = "%s"
cidr = "172.16.8.0/24"
cidr = "%s"
}
resource "huaweicloud_vpc_subnet" "test" {
name = "%s"
cidr = "172.16.8.0/24"
gateway_ip = "172.16.8.1"
cidr = "%s"
gateway_ip = "%s"
vpc_id = huaweicloud_vpc.test.id
availability_zone = data.huaweicloud_availability_zones.test.names[0]
Expand All @@ -102,5 +106,5 @@ data "huaweicloud_vpc_subnet" "by_name" {
data "huaweicloud_vpc_subnet" "by_vpc_id" {
vpc_id = huaweicloud_vpc_subnet.test.vpc_id
}
`, rName, rName)
`, rName, cidr, rName, cidr, gateway)
}
11 changes: 7 additions & 4 deletions huaweicloud/data_source_huaweicloud_vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package huaweicloud

import (
"fmt"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
Expand All @@ -11,13 +12,15 @@ import (

func TestAccVpcV1DataSource_basic(t *testing.T) {
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5))
tmp := strconv.Itoa(acctest.RandIntRange(1, 254))
cidr := fmt.Sprintf("172.16.%s.0/24", tmp)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVpcV1Config(rName),
Config: testAccDataSourceVpcV1Config(rName, cidr),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceVpcV1Check("data.huaweicloud_vpc.by_id", rName),
testAccDataSourceVpcV1Check("data.huaweicloud_vpc.by_cidr", rName),
Expand Down Expand Up @@ -62,11 +65,11 @@ func testAccDataSourceVpcV1Check(n, rName string) resource.TestCheckFunc {
}
}

func testAccDataSourceVpcV1Config(rName string) string {
func testAccDataSourceVpcV1Config(rName, cidr string) string {
return fmt.Sprintf(`
resource "huaweicloud_vpc" "test" {
name = "%s"
cidr = "172.16.8.0/24"
cidr = "%s"
}
data "huaweicloud_vpc" "by_id" {
Expand All @@ -80,5 +83,5 @@ data "huaweicloud_vpc" "by_cidr" {
data "huaweicloud_vpc" "by_name" {
name = huaweicloud_vpc.test.name
}
`, rName)
`, rName, cidr)
}

0 comments on commit 4ae4afc

Please sign in to comment.