Skip to content

Commit

Permalink
WIP: CT tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Dec 19, 2024
1 parent 670fa6b commit d1047df
Show file tree
Hide file tree
Showing 5 changed files with 466 additions and 456 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package tfapstra_test
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"strconv"
"strings"
"testing"
Expand All @@ -14,7 +15,6 @@ import (
testutils "github.com/Juniper/terraform-provider-apstra/apstra/test_utils"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/stretchr/testify/require"
)
Expand All @@ -36,51 +36,51 @@ type resourceDataCenterConnectivityTemplateInterface struct {
name string
description string
tags []string
ipLinks []resourceDataCenterConnectivityTemplatePrimitiveIpLink
routingZoneConstraints []resourceDataCenterConnectivityTemplatePrimitiveRoutingZoneConstraint
virtualNetworkMultiples []resourceDataCenterConnectivityTemplatePrimitiveVirtualNetworkMultiple
virtualNetworkSingles []resourceDataCenterConnectivityTemplatePrimitiveVirtualNetworkSingle
ipLinks map[string]resourceDataCenterConnectivityTemplatePrimitiveIpLink
routingZoneConstraints map[string]resourceDataCenterConnectivityTemplatePrimitiveRoutingZoneConstraint
virtualNetworkMultiples map[string]resourceDataCenterConnectivityTemplatePrimitiveVirtualNetworkMultiple
virtualNetworkSingles map[string]resourceDataCenterConnectivityTemplatePrimitiveVirtualNetworkSingle
}

func (o resourceDataCenterConnectivityTemplateInterface) render(rType, rName string) string {
ipLinks := "null"
if len(o.ipLinks) > 0 {
sb := new(strings.Builder)
for _, ipLink := range o.ipLinks {
sb.WriteString(ipLink.render(2))
for k, v := range o.ipLinks {
sb.WriteString(tfapstra.Indent(2, k+" = "+v.render(2)))
}

ipLinks = "[\n" + sb.String() + " ]"
ipLinks = "{\n" + sb.String() + " }"
}

routingZoneConstraints := "null"
if len(o.routingZoneConstraints) > 0 {
sb := new(strings.Builder)
for _, routingZoneConstraint := range o.routingZoneConstraints {
sb.WriteString(routingZoneConstraint.render(2))
for k, v := range o.routingZoneConstraints {
sb.WriteString(tfapstra.Indent(2, k+" = "+v.render(2)))
}

routingZoneConstraints = "[\n" + sb.String() + " ]"
routingZoneConstraints = "{\n" + sb.String() + " }"
}

virtualNetworkMultiples := "null"
if len(o.virtualNetworkMultiples) > 0 {
sb := new(strings.Builder)
for _, virtualNetworkMultiple := range o.virtualNetworkMultiples {
sb.WriteString(virtualNetworkMultiple.render(2))
for k, v := range o.virtualNetworkMultiples {
sb.WriteString(tfapstra.Indent(2, k+" = "+v.render(2)))
}

virtualNetworkMultiples = "[\n" + sb.String() + " ]"
virtualNetworkMultiples = "{\n" + sb.String() + " }"
}

virtualNetworkSingles := "null"
if len(o.virtualNetworkSingles) > 0 {
sb := new(strings.Builder)
for _, virtualNetworkSingle := range o.virtualNetworkSingles {
sb.WriteString(virtualNetworkSingle.render(2))
for k, v := range o.virtualNetworkSingles {
sb.WriteString(tfapstra.Indent(2, k+" = "+v.render(2)))
}

virtualNetworkSingles = "[\n" + sb.String() + " ]"
virtualNetworkSingles = "{\n" + sb.String() + " }"
}

return fmt.Sprintf(resourceDataCenterConnectivityTemplateInterfaceHCL,
Expand Down Expand Up @@ -115,24 +115,32 @@ func (o resourceDataCenterConnectivityTemplateInterface) testChecks(t testing.TB
result.append(t, "TestCheckTypeSetElemAttr", "tags.*", tag)
}

result.append(t, "TestCheckResourceAttr", "ip_links.#", strconv.Itoa(len(o.ipLinks)))
for _, ipLink := range o.ipLinks {
result.appendSetNestedCheck(t, "ip_links.*", ipLink.valueAsMapForChecks())
result.append(t, "TestCheckResourceAttr", "ip_links.%", strconv.Itoa(len(o.ipLinks)))
for k, v := range o.ipLinks {
for _, check := range v.testChecks("ip_links." + k) {
result.append(t, check[0], check[1:]...)
}
}

result.append(t, "TestCheckResourceAttr", "routing_zone_constraints.#", strconv.Itoa(len(o.routingZoneConstraints)))
for _, routingZoneConstraint := range o.routingZoneConstraints {
result.appendSetNestedCheck(t, "routing_zone_constraints.*", routingZoneConstraint.valueAsMapForChecks())
result.append(t, "TestCheckResourceAttr", "routing_zone_constraints.%", strconv.Itoa(len(o.routingZoneConstraints)))
for k, v := range o.routingZoneConstraints {
for _, check := range v.testChecks("routing_zone_constraints." + k) {
result.append(t, check[0], check[1:]...)
}
}

result.append(t, "TestCheckResourceAttr", "virtual_network_multiples.#", strconv.Itoa(len(o.virtualNetworkMultiples)))
for _, virtualNetworkMultiple := range o.virtualNetworkMultiples {
result.appendSetNestedCheck(t, "virtual_network_multiples.*", virtualNetworkMultiple.valueAsMapForChecks())
result.append(t, "TestCheckResourceAttr", "virtual_network_multiples.%", strconv.Itoa(len(o.virtualNetworkMultiples)))
for k, v := range o.virtualNetworkMultiples {
for _, check := range v.testChecks("virtual_network_multiples." + k) {
result.append(t, check[0], check[1:]...)
}
}

result.append(t, "TestCheckResourceAttr", "virtual_network_singles.#", strconv.Itoa(len(o.virtualNetworkSingles)))
for _, virtualNetworkSingle := range o.virtualNetworkSingles {
result.appendSetNestedCheck(t, "virtual_network_singles.*", virtualNetworkSingle.valueAsMapForChecks())
result.append(t, "TestCheckResourceAttr", "virtual_network_singles.%", strconv.Itoa(len(o.virtualNetworkSingles)))
for k, v := range o.virtualNetworkSingles {
for _, check := range v.testChecks("virtual_network_singles." + k) {
result.append(t, check[0], check[1:]...)
}
}

return result
Expand Down Expand Up @@ -197,7 +205,7 @@ func TestResourceDatacenteConnectivityTemplateInterface(t *testing.T) {
name: acctest.RandString(6),
description: acctest.RandString(32),
tags: randomStrings(3, 6),
ipLinks: randomIpLinks(t, ctx, 3, bp, cleanup),
ipLinks: randomIpLinks(t, ctx, 1, bp, cleanup),
routingZoneConstraints: randomRoutingZoneConstraints(t, ctx, 3, bp, cleanup),
virtualNetworkMultiples: randomVirtualNetworkMultiples(t, ctx, 3, bp, cleanup),
virtualNetworkSingles: randomVirtualNetworkSingles(t, ctx, 3, bp, cleanup),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package tfapstra_test
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"math/rand/v2"
"strconv"
"strings"
Expand All @@ -14,7 +15,6 @@ import (
tfapstra "github.com/Juniper/terraform-provider-apstra/apstra"
testutils "github.com/Juniper/terraform-provider-apstra/apstra/test_utils"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

Expand All @@ -32,18 +32,18 @@ type resourceDataCenterConnectivityTemplateLoopback struct {
name string
description string
tags []string
bgpPeeringIpEndoints []resourceDataCenterConnectivityTemplatePrimitiveBgpPeeringIpEndpoint
bgpPeeringIpEndoints map[string]resourceDataCenterConnectivityTemplatePrimitiveBgpPeeringIpEndpoint
}

func (o resourceDataCenterConnectivityTemplateLoopback) render(rType, rName string) string {
bgpPeeringIpEndoints := "null"
if len(o.bgpPeeringIpEndoints) > 0 {
sb := new(strings.Builder)
for _, bgpPeeringIpEndpoint := range o.bgpPeeringIpEndoints {
sb.WriteString(bgpPeeringIpEndpoint.render(2))
for k, v := range o.bgpPeeringIpEndoints {
sb.WriteString(tfapstra.Indent(2, k+" = "+v.render(2)))
}

bgpPeeringIpEndoints = "[\n" + sb.String() + " ]"
bgpPeeringIpEndoints = "{\n" + sb.String() + " }"
}

return fmt.Sprintf(resourceDataCenterConnectivityTemplateLoopbackHCL,
Expand Down Expand Up @@ -75,9 +75,11 @@ func (o resourceDataCenterConnectivityTemplateLoopback) testChecks(t testing.TB,
result.append(t, "TestCheckTypeSetElemAttr", "tags.*", tag)
}

result.append(t, "TestCheckResourceAttr", "bgp_peering_ip_endpoints.#", strconv.Itoa(len(o.bgpPeeringIpEndoints)))
for _, bgpPeeringIpEndoint := range o.bgpPeeringIpEndoints {
result.appendSetNestedCheck(t, "bgp_peering_ip_endpoints.*", bgpPeeringIpEndoint.valueAsMapForChecks())
result.append(t, "TestCheckResourceAttr", "bgp_peering_ip_endpoints.%", strconv.Itoa(len(o.bgpPeeringIpEndoints)))
for k, v := range o.bgpPeeringIpEndoints {
for _, check := range v.testChecks("bgp_peering_ip_endpoints." + k) {
result.append(t, check[0], check[1:]...)
}
}

return result
Expand Down
Loading

0 comments on commit d1047df

Please sign in to comment.