Skip to content

Commit

Permalink
Merge pull request #1503 from ksamoray/test_test
Browse files Browse the repository at this point in the history
Fix test failures for v9.0
  • Loading branch information
ksamoray authored Jan 20, 2025
2 parents 2ccd53e + 879b812 commit c216852
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 7 deletions.
43 changes: 43 additions & 0 deletions nsxt/data_source_nsxt_policy_bridge_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ package nsxt

import (
"fmt"
"github.com/vmware/vsphere-automation-sdk-go/lib/vapi/std/errors"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx"
"testing"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
ep "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sites/enforcement_points"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sites/enforcement_points/edge_clusters"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)

Expand Down Expand Up @@ -42,6 +45,39 @@ func TestAccDataSourceNsxtPolicyBridgeProfile_basic(t *testing.T) {
})
}

func getEdgeClusterPath() (string, error) {
connector, err := testAccGetPolicyConnector()
if err != nil {
return "", fmt.Errorf("error during test client initialization: %v", err)
}
client := nsx.NewEdgeClustersClient(connector)
clusterList, err := client.List(nil, nil, nil, nil, nil)
if err != nil {
return "", fmt.Errorf("error during edge cluster list retrieval: %v", err)
}
clusterName := getEdgeClusterName()
clusterID := ""
if clusterList.Results != nil {
for _, objList := range clusterList.Results {
if *objList.DisplayName == clusterName {
clusterID = *objList.Id
break
}
}
}
if clusterID != "" {
enClient := edge_clusters.NewEdgeNodesClient(connector)
objList, err := enClient.List(defaultSite, defaultEnforcementPoint, clusterID, nil, nil, nil, nil, nil, nil)
if err != nil {
return "", fmt.Errorf("error during edge node list retrieval: %v", err)
}
if len(objList.Results) > 0 {
return *objList.Results[0].Path, nil
}
}
return "", errors.NotFound{}
}

func testAccDataSourceNsxtPolicyBridgeProfileCreate(name string) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
Expand All @@ -59,6 +95,13 @@ func testAccDataSourceNsxtPolicyBridgeProfileCreate(name string) error {
uuid, _ := uuid.NewRandom()
id := uuid.String()

// Get Edge path
edgePath, err := getEdgeClusterPath()
if err == nil {
obj.EdgePaths = []string{edgePath}
} else if !isNotFoundError(err) {
return err
}
client := ep.NewEdgeBridgeProfilesClient(connector)
err = client.Patch(defaultSite, defaultEnforcementPoint, id, obj)

Expand Down
2 changes: 1 addition & 1 deletion nsxt/resource_nsxt_policy_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func TestAccResourceNsxtPolicyProject_900defaultSecurityProfile(t *testing.T) {
// Create: Set T0, Ext GW connection, Ext IPv4 Block, activate default DFW
Config: testAccNsxtPolicyProjectMinimalistic(),
Check: resource.ComposeTestCheckFunc(
testAccNsxtPolicyProjectGetSecurityProfileNSEnabled(testResourceName, false),
testAccNsxtPolicyProjectGetSecurityProfileNSEnabled(testResourceName, true),
),
},
{
Expand Down
89 changes: 83 additions & 6 deletions nsxt/resource_nsxt_vpc_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ var accTestVpcSubnetUpdateAttributes = map[string]string{
"reserved_ip_ranges": "11.11.50.4-11.11.50.6",
}

var accTestVpcSubnetReservedIPRangeAttributes = map[string]string{
"display_name": getAccTestResourceName(),
"description": "terraform created",
"access_mode": "Isolated",
"enabled": "true",
"ipv4_subnet_size": "32",
"ip_addresses": "10.110.1.0/26",
"reserved_ip_ranges": "10.110.1.3-10.110.1.15",
}

func TestAccResourceNsxtVpcSubnet_basic(t *testing.T) {
testResourceName := "nsxt_vpc_subnet.test"

Expand Down Expand Up @@ -116,8 +126,6 @@ func TestAccResourceNsxtVpcSubnet_subnetSize(t *testing.T) {
resource.TestCheckResourceAttr(testResourceName, "ip_addresses.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.reserved_ip_ranges.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.reserved_ip_ranges.0", accTestVpcSubnetCreateAttributes["reserved_ip_ranges"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.options.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.options.0.option121.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.options.0.other.#", "1"),
Expand All @@ -138,8 +146,6 @@ func TestAccResourceNsxtVpcSubnet_subnetSize(t *testing.T) {
resource.TestCheckResourceAttr(testResourceName, "ipv4_subnet_size", accTestVpcSubnetCreateAttributes["ipv4_subnet_size"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.reserved_ip_ranges.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.reserved_ip_ranges.0", accTestVpcSubnetUpdateAttributes["reserved_ip_ranges"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.options.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.options.0.option121.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.options.0.other.#", "1"),
Expand All @@ -166,6 +172,44 @@ func TestAccResourceNsxtVpcSubnet_subnetSize(t *testing.T) {
})
}

func TestAccResourceNsxtVpcSubnet_reservedIPRange(t *testing.T) {
testResourceName := "nsxt_vpc_subnet.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccOnlyVPC(t)
},
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNsxtVpcSubnetCheckDestroy(state, accTestVpcSubnetUpdateAttributes["display_name"])
},
Steps: []resource.TestStep{
{
Config: testAccNsxtVpcSubnetReservedIPRangeTemplate(true),
Check: resource.ComposeTestCheckFunc(
testAccNsxtVpcSubnetExists(accTestVpcSubnetCreateAttributes["display_name"], testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", accTestVpcSubnetReservedIPRangeAttributes["display_name"]),
resource.TestCheckResourceAttr(testResourceName, "description", accTestVpcSubnetReservedIPRangeAttributes["description"]),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.reserved_ip_ranges.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.reserved_ip_ranges.0", accTestVpcSubnetReservedIPRangeAttributes["reserved_ip_ranges"]),
resource.TestCheckResourceAttr(testResourceName, "ip_addresses.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.options.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.options.0.option121.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "dhcp_config.0.dhcp_server_additional_config.0.options.0.other.#", "1"),

resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
resource.TestCheckResourceAttrSet(testResourceName, "revision"),
resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"),
),
},
},
})
}

func TestAccResourceNsxtVpcSubnet_importBasic(t *testing.T) {
name := getAccTestResourceName()
testResourceName := "nsxt_vpc_subnet.test"
Expand Down Expand Up @@ -301,10 +345,9 @@ resource "nsxt_vpc_subnet" "test" {
values = ["abc", "def"]
}
}
reserved_ip_ranges = ["%s"]
}
}
}`, testAccNsxtPolicyMultitenancyContext(), attrMap["display_name"], attrMap["description"], attrMap["ipv4_subnet_size"], attrMap["reserved_ip_ranges"])
}`, testAccNsxtPolicyMultitenancyContext(), attrMap["display_name"], attrMap["description"], attrMap["ipv4_subnet_size"])
}

func testAccNsxtVpcSubnetMinimalisticIsolated() string {
Expand All @@ -325,3 +368,37 @@ resource "nsxt_vpc_subnet" "test" {
access_mode = "Public"
}`, testAccNsxtPolicyMultitenancyContext(), accTestVpcSubnetUpdateAttributes["display_name"])
}

func testAccNsxtVpcSubnetReservedIPRangeTemplate(createFlow bool) string {
attrMap := accTestVpcSubnetReservedIPRangeAttributes

return fmt.Sprintf(`
resource "nsxt_vpc_subnet" "test" {
%s
display_name = "%s"
description = "%s"
ip_addresses = ["%s"]
access_mode = "Public"
dhcp_config {
mode = "DHCP_SERVER"
dhcp_server_additional_config {
options {
option121 {
static_route {
network = "2.1.1.0/24"
next_hop = "2.3.1.3"
}
}
other {
code = "119"
values = ["abc", "def"]
}
}
reserved_ip_ranges = ["%s"]
}
}
}`, testAccNsxtPolicyMultitenancyContext(), attrMap["display_name"], attrMap["description"], attrMap["ip_addresses"], attrMap["reserved_ip_ranges"])
}

0 comments on commit c216852

Please sign in to comment.