Skip to content

Commit

Permalink
Merge pull request #411 from terraform-providers/refactor_pool_test
Browse files Browse the repository at this point in the history
Refactor policy lb pool tests to allow parallelism
  • Loading branch information
annakhm authored Aug 4, 2020
2 parents 19937e9 + 512ab0d commit ecec573
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 42 deletions.
2 changes: 1 addition & 1 deletion nsxt/resource_nsxt_policy_ip_address_allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ data "nsxt_policy_realization_info" "realization_info" {
func testAccNsxtPolicyIPAddressAllocationDependenciesTemplate() string {
return fmt.Sprintf(`
resource "nsxt_policy_ip_pool" "test" {
display_name = "tfpool1"
display_name = "tfpool4"
}
resource "nsxt_policy_ip_pool_static_subnet" "test" {
Expand Down
43 changes: 22 additions & 21 deletions nsxt/resource_nsxt_policy_ip_pool_block_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
"testing"
)

var nsxtPolicyBlockSubnetPoolID = "tfpool1"

// TODO: remove extra test step config once IP Blocks don't need a delay to delete
func TestAccResourceNsxtPolicyIPPoolBlockSubnet_minimal(t *testing.T) {
poolName := "tfpool5"
name := "blocksubnet1"
testResourceName := "nsxt_policy_ip_pool_block_subnet.test"

Expand All @@ -26,7 +25,7 @@ func TestAccResourceNsxtPolicyIPPoolBlockSubnet_minimal(t *testing.T) {
},
Steps: []resource.TestStep{
{
Config: testAccNSXPolicyIPPoolBlockSubnetCreateMinimalTemplate(name),
Config: testAccNSXPolicyIPPoolBlockSubnetCreateMinimalTemplate(poolName, name),
Check: resource.ComposeTestCheckFunc(
testAccNSXPolicyIPPoolBlockSubnetCheckExists(testResourceName),
resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"),
Expand All @@ -48,7 +47,8 @@ func TestAccResourceNsxtPolicyIPPoolBlockSubnet_minimal(t *testing.T) {
}

func TestAccResourceNsxtPolicyIPPoolBlockSubnet_basic(t *testing.T) {
name := "tfpool1"
poolName := "tfpool1"
name := "subnet1"
updatedName := fmt.Sprintf("%s-updated", name)
testResourceName := "nsxt_policy_ip_pool_block_subnet.test"

Expand All @@ -60,7 +60,7 @@ func TestAccResourceNsxtPolicyIPPoolBlockSubnet_basic(t *testing.T) {
},
Steps: []resource.TestStep{
{
Config: testAccNSXPolicyIPPoolBlockSubnetCreateTemplate(name),
Config: testAccNSXPolicyIPPoolBlockSubnetCreateTemplate(poolName, name),
Check: resource.ComposeTestCheckFunc(
testAccNSXPolicyIPPoolBlockSubnetCheckExists(testResourceName),
resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"),
Expand All @@ -76,7 +76,7 @@ func TestAccResourceNsxtPolicyIPPoolBlockSubnet_basic(t *testing.T) {
),
},
{
Config: testAccNSXPolicyIPPoolBlockSubnetUpdateTemplate(updatedName),
Config: testAccNSXPolicyIPPoolBlockSubnetUpdateTemplate(poolName, updatedName),
Check: resource.ComposeTestCheckFunc(
testAccNSXPolicyIPPoolBlockSubnetCheckExists(testResourceName),
resource.TestCheckResourceAttr(testResourceName, "tag.#", "2"),
Expand All @@ -99,7 +99,8 @@ func TestAccResourceNsxtPolicyIPPoolBlockSubnet_basic(t *testing.T) {
}

func TestAccResourceNsxtPolicyIPPoolBlockSubnet_import_basic(t *testing.T) {
name := "tfpool1"
poolName := "tfpool7"
name := "subnet2"
testResourceName := "nsxt_policy_ip_pool_block_subnet.test"

resource.Test(t, resource.TestCase{
Expand All @@ -110,7 +111,7 @@ func TestAccResourceNsxtPolicyIPPoolBlockSubnet_import_basic(t *testing.T) {
},
Steps: []resource.TestStep{
{
Config: testAccNSXPolicyIPPoolBlockSubnetCreateTemplate(name),
Config: testAccNSXPolicyIPPoolBlockSubnetCreateTemplate(poolName, name),
},
{
ResourceName: testResourceName,
Expand Down Expand Up @@ -143,6 +144,7 @@ func testAccNSXPolicyIPPoolBlockSubnetImporterGetID(s *terraform.State) (string,
}

func testAccNSXPolicyIPPoolBlockSubnetCheckExists(resourceName string) resource.TestCheckFunc {

return func(state *terraform.State) error {
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))
client := ip_pools.NewDefaultIpSubnetsClient(connector)
Expand All @@ -153,11 +155,12 @@ func testAccNSXPolicyIPPoolBlockSubnetCheckExists(resourceName string) resource.
}

resourceID := rs.Primary.ID
poolID := getPolicyIDFromPath(rs.Primary.Attributes["pool_path"])
if resourceID == "" {
return fmt.Errorf("NSX Policy Block Subnet resource ID not set in resources")
}

_, err := client.Get(nsxtPolicyBlockSubnetPoolID, resourceID)
_, err := client.Get(poolID, resourceID)
if err != nil {
return fmt.Errorf("Failed to find Block Subnet %s", resourceID)
}
Expand All @@ -175,8 +178,9 @@ func testAccNSXPolicyIPPoolBlockSubnetCheckDestroy(state *terraform.State) error
continue
}

resourceID := rs.Primary.Attributes["id"]
_, err := client.Get(nsxtPolicyBlockSubnetPoolID, resourceID)
resourceID := rs.Primary.ID
poolID := getPolicyIDFromPath(rs.Primary.Attributes["pool_path"])
_, err := client.Get(poolID, resourceID)
if err == nil {
return fmt.Errorf("Block Subnet still exists %s", resourceID)
}
Expand All @@ -188,31 +192,29 @@ func testAccNSXPolicyIPPoolBlockSubnetCheckDestroy(state *terraform.State) error
func testAccNSXPolicyIPPoolBlockSubnetIPBlockTemplate() string {
return fmt.Sprintf(`
resource "nsxt_policy_ip_block" "block1" {
display_name = "tfblock1"
display_name = "tfblock2"
cidr = "11.11.12.0/24"
}`)
}

func testAccNSXPolicyIPPoolBlockSubnetCreateMinimalTemplate(name string) string {
func testAccNSXPolicyIPPoolBlockSubnetCreateMinimalTemplate(poolName string, name string) string {
return testAccNSXPolicyIPPoolBlockSubnetIPBlockTemplate() + fmt.Sprintf(`
resource "nsxt_policy_ip_pool" "pool1" {
display_name = "%s"
nsx_id = "%s"
}
resource "nsxt_policy_ip_pool_block_subnet" "test" {
display_name = "%s"
size = 4
pool_path = nsxt_policy_ip_pool.pool1.path
block_path = nsxt_policy_ip_block.block1.path
}`, nsxtPolicyBlockSubnetPoolID, nsxtPolicyBlockSubnetPoolID, name)
}`, poolName, name)
}

func testAccNSXPolicyIPPoolBlockSubnetCreateTemplate(name string) string {
func testAccNSXPolicyIPPoolBlockSubnetCreateTemplate(poolName string, name string) string {
return testAccNSXPolicyIPPoolBlockSubnetIPBlockTemplate() + fmt.Sprintf(`
resource "nsxt_policy_ip_pool" "pool1" {
display_name = "%s"
nsx_id = "%s"
}
resource "nsxt_policy_ip_pool_block_subnet" "test" {
Expand All @@ -226,14 +228,13 @@ resource "nsxt_policy_ip_pool_block_subnet" "test" {
scope = "scope2"
tag = "tag2"
}
}`, nsxtPolicyBlockSubnetPoolID, nsxtPolicyBlockSubnetPoolID, name)
}`, poolName, name)
}

func testAccNSXPolicyIPPoolBlockSubnetUpdateTemplate(name string) string {
func testAccNSXPolicyIPPoolBlockSubnetUpdateTemplate(poolName string, name string) string {
return testAccNSXPolicyIPPoolBlockSubnetIPBlockTemplate() + fmt.Sprintf(`
resource "nsxt_policy_ip_pool" "pool1" {
display_name = "%s"
nsx_id = "%s"
}
resource "nsxt_policy_ip_pool_block_subnet" "test" {
Expand All @@ -251,5 +252,5 @@ resource "nsxt_policy_ip_pool_block_subnet" "test" {
scope = "scope2"
tag = "tag2"
}
}`, nsxtPolicyBlockSubnetPoolID, nsxtPolicyBlockSubnetPoolID, name)
}`, poolName, name)
}
32 changes: 14 additions & 18 deletions nsxt/resource_nsxt_policy_ip_pool_static_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"testing"
)

var nsxtPolicyStaticSubnetPoolID = "tfpool1"

func TestAccResourceNsxtPolicyIPPoolStaticSubnet_minimal(t *testing.T) {
name := "staticsubnet1"
testResourceName := "nsxt_policy_ip_pool_static_subnet.test"
Expand Down Expand Up @@ -113,7 +111,7 @@ func TestAccResourceNsxtPolicyIPPoolStaticSubnet_basic(t *testing.T) {
}

func TestAccResourceNsxtPolicyIPPoolStaticSubnet_import_basic(t *testing.T) {
name := "tfpool1"
name := "tfpool8"
testResourceName := "nsxt_policy_ip_pool_static_subnet.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -164,11 +162,12 @@ func testAccNSXPolicyIPPoolStaticSubnetCheckExists(resourceName string) resource
}

resourceID := rs.Primary.ID
poolID := getPolicyIDFromPath(rs.Primary.Attributes["pool_path"])
if resourceID == "" {
return fmt.Errorf("NSX Policy Static Subnet resource ID not set in resources")
}

_, err := client.Get(nsxtPolicyStaticSubnetPoolID, resourceID)
_, err := client.Get(poolID, resourceID)
if err != nil {
return fmt.Errorf("Failed to find Static Subnet %s", resourceID)
}
Expand All @@ -186,8 +185,9 @@ func testAccNSXPolicyIPPoolStaticSubnetCheckDestroy(state *terraform.State) erro
continue
}

resourceID := rs.Primary.Attributes["id"]
_, err := client.Get(nsxtPolicyStaticSubnetPoolID, resourceID)
resourceID := rs.Primary.ID
poolID := getPolicyIDFromPath(rs.Primary.Attributes["pool_path"])
_, err := client.Get(poolID, resourceID)
if err == nil {
return fmt.Errorf("Static Subnet still exists %s", resourceID)
}
Expand All @@ -199,8 +199,7 @@ func testAccNSXPolicyIPPoolStaticSubnetCheckDestroy(state *terraform.State) erro
func testAccNSXPolicyIPPoolStaticSubnetCreateMinimalTemplate(name string) string {
return fmt.Sprintf(`
resource "nsxt_policy_ip_pool" "pool1" {
display_name = "%s"
nsx_id = "%s"
display_name = "tf-pool-static-subnet"
}
resource "nsxt_policy_ip_pool_static_subnet" "test" {
Expand All @@ -211,14 +210,13 @@ resource "nsxt_policy_ip_pool_static_subnet" "test" {
start = "12.12.12.10"
end = "12.12.12.20"
}
}`, nsxtPolicyStaticSubnetPoolID, nsxtPolicyStaticSubnetPoolID, name)
}`, name)
}

func testAccNSXPolicyIPPoolStaticSubnetCreateTemplate(name string) string {
return fmt.Sprintf(`
resource "nsxt_policy_ip_pool" "pool1" {
display_name = "%s"
nsx_id = "%s"
display_name = "tf-pool-static-subnet"
}
resource "nsxt_policy_ip_pool_static_subnet" "test" {
Expand All @@ -234,14 +232,13 @@ resource "nsxt_policy_ip_pool_static_subnet" "test" {
scope = "scope2"
tag = "tag2"
}
}`, nsxtPolicyStaticSubnetPoolID, nsxtPolicyStaticSubnetPoolID, name)
}`, name)
}

func testAccNSXPolicyIPPoolStaticSubnet3AllocationsTemplate(name string) string {
return fmt.Sprintf(`
resource "nsxt_policy_ip_pool" "pool1" {
display_name = "%s"
nsx_id = "%s"
display_name = "tf-pool-static-subnet"
}
resource "nsxt_policy_ip_pool_static_subnet" "test" {
Expand Down Expand Up @@ -272,14 +269,13 @@ resource "nsxt_policy_ip_pool_static_subnet" "test" {
scope = "scope2"
tag = "tag2"
}
}`, nsxtPolicyStaticSubnetPoolID, nsxtPolicyStaticSubnetPoolID, name)
}`, name)
}

func testAccNSXPolicyIPPoolStaticSubnet2AllocationsTemplate(name string) string {
return fmt.Sprintf(`
resource "nsxt_policy_ip_pool" "pool1" {
display_name = "%s"
nsx_id = "%s"
display_name = "tf-pool-static-subnet"
}
resource "nsxt_policy_ip_pool_static_subnet" "test" {
Expand All @@ -306,5 +302,5 @@ resource "nsxt_policy_ip_pool_static_subnet" "test" {
scope = "scope2"
tag = "tag2"
}
}`, nsxtPolicyStaticSubnetPoolID, nsxtPolicyStaticSubnetPoolID, name)
}`, name)
}
4 changes: 2 additions & 2 deletions nsxt/resource_nsxt_policy_ip_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestAccResourceNsxtPolicyIPPool_minimal(t *testing.T) {
name := "tfpool1"
name := "tfpool0"
testResourceName := "nsxt_policy_ip_pool.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestAccResourceNsxtPolicyIPPool_basic(t *testing.T) {
}

func TestAccResourceNsxtPolicyIPPool_import_basic(t *testing.T) {
name := "tfpool1"
name := "tfpool2"
testResourceName := "nsxt_policy_ip_pool.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down

0 comments on commit ecec573

Please sign in to comment.