From 512ab0d4161a9a6b7f851bdce4b6fbf5ff5846a9 Mon Sep 17 00:00:00 2001 From: Anna Khmelnitsky Date: Fri, 31 Jul 2020 14:30:17 -0700 Subject: [PATCH] Refactor policy lb pool tests to allow parallelism --- ..._nsxt_policy_ip_address_allocation_test.go | 2 +- ...e_nsxt_policy_ip_pool_block_subnet_test.go | 43 ++++++++++--------- ..._nsxt_policy_ip_pool_static_subnet_test.go | 32 ++++++-------- nsxt/resource_nsxt_policy_ip_pool_test.go | 4 +- 4 files changed, 39 insertions(+), 42 deletions(-) diff --git a/nsxt/resource_nsxt_policy_ip_address_allocation_test.go b/nsxt/resource_nsxt_policy_ip_address_allocation_test.go index d7e3351ee..0f1a1885c 100644 --- a/nsxt/resource_nsxt_policy_ip_address_allocation_test.go +++ b/nsxt/resource_nsxt_policy_ip_address_allocation_test.go @@ -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" { diff --git a/nsxt/resource_nsxt_policy_ip_pool_block_subnet_test.go b/nsxt/resource_nsxt_policy_ip_pool_block_subnet_test.go index a1fd4dcf9..6bc57261d 100644 --- a/nsxt/resource_nsxt_policy_ip_pool_block_subnet_test.go +++ b/nsxt/resource_nsxt_policy_ip_pool_block_subnet_test.go @@ -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" @@ -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"), @@ -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" @@ -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"), @@ -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"), @@ -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{ @@ -110,7 +111,7 @@ func TestAccResourceNsxtPolicyIPPoolBlockSubnet_import_basic(t *testing.T) { }, Steps: []resource.TestStep{ { - Config: testAccNSXPolicyIPPoolBlockSubnetCreateTemplate(name), + Config: testAccNSXPolicyIPPoolBlockSubnetCreateTemplate(poolName, name), }, { ResourceName: testResourceName, @@ -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) @@ -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) } @@ -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) } @@ -188,16 +192,15 @@ 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" { @@ -205,14 +208,13 @@ resource "nsxt_policy_ip_pool_block_subnet" "test" { 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" { @@ -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" { @@ -251,5 +252,5 @@ resource "nsxt_policy_ip_pool_block_subnet" "test" { scope = "scope2" tag = "tag2" } -}`, nsxtPolicyBlockSubnetPoolID, nsxtPolicyBlockSubnetPoolID, name) +}`, poolName, name) } diff --git a/nsxt/resource_nsxt_policy_ip_pool_static_subnet_test.go b/nsxt/resource_nsxt_policy_ip_pool_static_subnet_test.go index 53e12f8e8..70c212e48 100644 --- a/nsxt/resource_nsxt_policy_ip_pool_static_subnet_test.go +++ b/nsxt/resource_nsxt_policy_ip_pool_static_subnet_test.go @@ -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" @@ -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{ @@ -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) } @@ -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) } @@ -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" { @@ -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" { @@ -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" { @@ -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" { @@ -306,5 +302,5 @@ resource "nsxt_policy_ip_pool_static_subnet" "test" { scope = "scope2" tag = "tag2" } -}`, nsxtPolicyStaticSubnetPoolID, nsxtPolicyStaticSubnetPoolID, name) +}`, name) } diff --git a/nsxt/resource_nsxt_policy_ip_pool_test.go b/nsxt/resource_nsxt_policy_ip_pool_test.go index a42f5cbb5..50f889a94 100644 --- a/nsxt/resource_nsxt_policy_ip_pool_test.go +++ b/nsxt/resource_nsxt_policy_ip_pool_test.go @@ -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{ @@ -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{