-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: graysonwu <[email protected]>
- Loading branch information
Showing
1 changed file
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* Copyright © 2020 VMware, Inc. All Rights Reserved. | ||
SPDX-License-Identifier: MPL-2.0 */ | ||
|
||
package nsxt | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
var accTestPolicyVniPoolCreateAttributes = map[string]string{ | ||
"display_name": getAccTestResourceName(), | ||
"description": "terraform created", | ||
} | ||
|
||
var accTestPolicyVniPoolUpdateAttributes = map[string]string{ | ||
"display_name": getAccTestResourceName(), | ||
"description": "terraform updated", | ||
} | ||
|
||
func TestAccResourceNsxtPolicyVniPool_basic(t *testing.T) { | ||
testResourceName := "nsxt_policy_vni_pool.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: func(state *terraform.State) error { | ||
return testAccNsxtPolicyVniPoolCheckDestroy(state, accTestPolicyVniPoolUpdateAttributes["display_name"]) | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccNsxtPolicyVniPoolCreate(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccNsxtPolicyVniPoolExists(accTestPolicyVniPoolCreateAttributes["display_name"], testResourceName), | ||
resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyVniPoolCreateAttributes["display_name"]), | ||
resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyVniPoolCreateAttributes["description"]), | ||
|
||
resource.TestCheckResourceAttrSet(testResourceName, "start"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "end"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "path"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "revision"), | ||
resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), | ||
), | ||
}, | ||
{ | ||
Config: testAccNsxtPolicyVniPoolUpdate(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccNsxtPolicyVniPoolExists(accTestPolicyVniPoolUpdateAttributes["display_name"], testResourceName), | ||
resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyVniPoolUpdateAttributes["display_name"]), | ||
resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyVniPoolUpdateAttributes["description"]), | ||
|
||
resource.TestCheckResourceAttrSet(testResourceName, "start"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "end"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "path"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "revision"), | ||
resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccResourceNsxtPolicyVniPool_importBasic(t *testing.T) { | ||
testResourceName := "nsxt_policy_vni_pool.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: func(state *terraform.State) error { | ||
return testAccNsxtPolicyVniPoolCheckDestroy(state, accTestPolicyVniPoolUpdateAttributes["display_name"]) | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccNsxtPolicyVniPoolUpdate(), | ||
}, | ||
{ | ||
ResourceName: testResourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccNsxtPolicyVniPoolExists(displayName string, resourceName string) resource.TestCheckFunc { | ||
return func(state *terraform.State) error { | ||
|
||
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) | ||
|
||
rs, ok := state.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("Policy VNI Pool resource %s not found in resources", resourceName) | ||
} | ||
|
||
resourceID := rs.Primary.ID | ||
if resourceID == "" { | ||
return fmt.Errorf("Policy VNI Pool resource ID not set in resources") | ||
} | ||
|
||
exists, err := resourceNsxtPolicyVniPoolExists(resourceID, connector, testAccIsGlobalManager()) | ||
if err != nil { | ||
return err | ||
} | ||
if !exists { | ||
return fmt.Errorf("Policy VNI Pool %s does not exist", resourceID) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccNsxtPolicyVniPoolCheckDestroy(state *terraform.State, displayName string) error { | ||
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) | ||
for _, rs := range state.RootModule().Resources { | ||
|
||
if rs.Type != "nsxt_policy_vni_pool" { | ||
continue | ||
} | ||
|
||
resourceID := rs.Primary.Attributes["id"] | ||
exists, err := resourceNsxtPolicyVniPoolExists(resourceID, connector, testAccIsGlobalManager()) | ||
if err == nil { | ||
return err | ||
} | ||
|
||
if exists { | ||
return fmt.Errorf("Policy Evpn Tenant %s still exists", displayName) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func testAccNsxtPolicyVniPoolCreate() string { | ||
attrMap := accTestPolicyVniPoolCreateAttributes | ||
return fmt.Sprintf(` | ||
resource "nsxt_policy_vni_pool" "test" { | ||
display_name = "%s" | ||
description = "%s" | ||
start = 75001 | ||
end = 75100 | ||
tag { | ||
scope = "scope1" | ||
tag = "tag1" | ||
} | ||
}`, attrMap["display_name"], attrMap["description"]) | ||
} | ||
|
||
func testAccNsxtPolicyVniPoolUpdate() string { | ||
attrMap := accTestPolicyVniPoolUpdateAttributes | ||
return fmt.Sprintf(` | ||
resource "nsxt_policy_vni_pool" "test" { | ||
display_name = "%s" | ||
description = "%s" | ||
start = 75101 | ||
end = 75600 | ||
}`, attrMap["display_name"], attrMap["description"]) | ||
} |