-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resource
apstra_datacenter_external_gateway
tests
- Loading branch information
1 parent
07b0094
commit 52793d0
Showing
8 changed files
with
302 additions
and
22 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
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
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,155 @@ | ||
package tfapstra_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/Juniper/apstra-go-sdk/apstra" | ||
testutils "github.com/Juniper/terraform-provider-apstra/apstra/test_utils" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"net" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
const ( | ||
resourceDataCenterExternalGateway = ` | ||
resource "apstra_datacenter_external_gateway" "test" { | ||
blueprint_id = "%s" | ||
name = "%s" | ||
ip_address = "%s" | ||
asn = %d | ||
evpn_route_types = "%s" | ||
local_gateway_nodes = ["%s"] | ||
ttl = %s | ||
keepalive_time = %s | ||
hold_time = %s | ||
} | ||
` | ||
) | ||
|
||
type testCaseResourceExternalGateway struct { | ||
name string | ||
ipAddress net.IP | ||
asn uint32 | ||
routeTypes apstra.RemoteGatewayRouteTypes | ||
nodes string | ||
ttl *uint8 | ||
keepaliveTime *uint16 | ||
holdTime *uint16 | ||
testCheckFunc resource.TestCheckFunc | ||
} | ||
|
||
func renderResourceDataCenterExternalGateway(tc testCaseResourceExternalGateway, bp *apstra.TwoStageL3ClosClient) string { | ||
return fmt.Sprintf(resourceDataCenterExternalGateway, | ||
bp.Id(), | ||
tc.name, | ||
tc.ipAddress, | ||
tc.asn, | ||
tc.routeTypes.Value, | ||
tc.nodes, | ||
intPtrOrNull(tc.ttl), | ||
intPtrOrNull(tc.keepaliveTime), | ||
intPtrOrNull(tc.holdTime), | ||
) | ||
} | ||
|
||
func TestResourceDatacenterExternalGateway(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
bp, bpDelete, err := testutils.BlueprintC(ctx) | ||
if err != nil { | ||
t.Fatal(errors.Join(err, bpDelete(ctx))) | ||
} | ||
defer func() { | ||
err = bpDelete(ctx) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
|
||
leafIds := systemIds(ctx, t, bp, "leaf") | ||
uint8Val3 := uint8(3) | ||
uint16Val1 := uint16(1) | ||
uint16Val3 := uint16(3) | ||
|
||
testCases := []testCaseResourceExternalGateway{ | ||
{ | ||
name: "name1", | ||
ipAddress: net.IP{1, 1, 1, 1}, | ||
asn: 1, | ||
routeTypes: apstra.RemoteGatewayRouteTypesAll, | ||
nodes: leafIds[0], | ||
testCheckFunc: resource.ComposeAggregateTestCheckFunc([]resource.TestCheckFunc{ | ||
resource.TestCheckResourceAttrSet("apstra_datacenter_external_gateway.test", "id"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "blueprint_id", bp.Id().String()), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "name", "name1"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "ip_address", "1.1.1.1"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "asn", "1"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "evpn_route_types", apstra.RemoteGatewayRouteTypesAll.Value), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "local_gateway_nodes.#", "1"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "local_gateway_nodes.0", leafIds[0]), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "ttl", "30"), // default | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "keepalive_time", "10"), // default | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "hold_time", "30"), // default | ||
}...), | ||
}, | ||
{ | ||
name: "name2", | ||
ipAddress: net.IP{1, 1, 1, 2}, | ||
asn: 2, | ||
routeTypes: apstra.RemoteGatewayRouteTypesFiveOnly, | ||
nodes: strings.Join(leafIds[1:], `","`), | ||
testCheckFunc: resource.ComposeAggregateTestCheckFunc([]resource.TestCheckFunc{ | ||
resource.TestCheckResourceAttrSet("apstra_datacenter_external_gateway.test", "id"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "blueprint_id", bp.Id().String()), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "name", "name2"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "ip_address", "1.1.1.2"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "asn", "2"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "evpn_route_types", apstra.RemoteGatewayRouteTypesFiveOnly.Value), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "local_gateway_nodes.#", strconv.Itoa(len(leafIds)-1)), | ||
resource.TestCheckTypeSetElemAttr("apstra_datacenter_external_gateway.test", "local_gateway_nodes.*", leafIds[1]), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "ttl", "30"), // default | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "keepalive_time", "10"), // default | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "hold_time", "30"), // default | ||
}...), | ||
}, | ||
{ | ||
name: "name3", | ||
ipAddress: net.IP{1, 1, 1, 3}, | ||
asn: 3, | ||
routeTypes: apstra.RemoteGatewayRouteTypesAll, | ||
nodes: leafIds[0], | ||
ttl: &uint8Val3, | ||
keepaliveTime: &uint16Val1, | ||
holdTime: &uint16Val3, | ||
testCheckFunc: resource.ComposeAggregateTestCheckFunc([]resource.TestCheckFunc{ | ||
resource.TestCheckResourceAttrSet("apstra_datacenter_external_gateway.test", "id"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "blueprint_id", bp.Id().String()), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "name", "name3"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "ip_address", "1.1.1.3"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "asn", "3"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "evpn_route_types", apstra.RemoteGatewayRouteTypesAll.Value), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "local_gateway_nodes.#", "1"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "local_gateway_nodes.0", leafIds[0]), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "ttl", "3"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "keepalive_time", "1"), | ||
resource.TestCheckResourceAttr("apstra_datacenter_external_gateway.test", "hold_time", "3"), | ||
}...), | ||
}, | ||
} | ||
|
||
steps := make([]resource.TestStep, len(testCases)) | ||
for i, tc := range testCases { | ||
steps[i] = resource.TestStep{ | ||
Config: insecureProviderConfigHCL + renderResourceDataCenterExternalGateway(tc, bp), | ||
Check: tc.testCheckFunc, | ||
} | ||
} | ||
|
||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: steps, | ||
}) | ||
} |
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,85 @@ | ||
package tfapstra_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/Juniper/apstra-go-sdk/apstra" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"golang.org/x/exp/constraints" | ||
"net" | ||
"testing" | ||
) | ||
|
||
func systemIds(ctx context.Context, t *testing.T, bp *apstra.TwoStageL3ClosClient, role string) []string { | ||
query := new(apstra.PathQuery). | ||
SetBlueprintType(apstra.BlueprintTypeStaging). | ||
SetBlueprintId(bp.Id()). | ||
SetClient(bp.Client()). | ||
Node([]apstra.QEEAttribute{ | ||
apstra.NodeTypeSystem.QEEAttribute(), | ||
{Key: "role", Value: apstra.QEStringVal(role)}, | ||
{Key: "name", Value: apstra.QEStringVal("n_system")}, | ||
}) | ||
|
||
var result struct { | ||
Items []struct { | ||
System struct { | ||
Id string `json:"id"` | ||
} `json:"n_system"` | ||
} `json:"items"` | ||
} | ||
|
||
err := query.Do(ctx, &result) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
ids := make([]string, len(result.Items)) | ||
for i, item := range result.Items { | ||
ids[i] = item.System.Id | ||
} | ||
|
||
return ids | ||
} | ||
|
||
func stringPtrOrNull(in *string) string { | ||
if in == nil { | ||
return "null" | ||
} | ||
return `"` + *in + `"` | ||
} | ||
|
||
func stringOrNull(in string) string { | ||
if in == "" { | ||
return "null" | ||
} | ||
return `"` + in + `"` | ||
} | ||
|
||
func intPtrOrNull[A constraints.Integer](in *A) string { | ||
if in == nil { | ||
return "null" | ||
} | ||
return fmt.Sprintf("%d", *in) | ||
} | ||
|
||
func ipOrNull(in *net.IPNet) string { | ||
if in == nil { | ||
return "null" | ||
} | ||
return `"` + in.String() + `"` | ||
} | ||
|
||
func randIpAddressMust(t *testing.T, cidrBlock string) net.IP { | ||
s, err := acctest.RandIpAddress(cidrBlock) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
ip := net.ParseIP(s) | ||
if ip == nil { | ||
t.Fatalf("randIpAddressMust failed to parse IP address %q", s) | ||
} | ||
|
||
return ip | ||
} |
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,22 @@ | ||
package tfapstra_test | ||
|
||
import ( | ||
tfapstra "github.com/Juniper/terraform-provider-apstra/apstra" | ||
"github.com/hashicorp/terraform-plugin-framework/providerserver" | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
) | ||
|
||
const ( | ||
insecureProviderConfigHCL = ` | ||
provider "apstra" { | ||
tls_validation_disabled = true | ||
blueprint_mutex_enabled = false | ||
} | ||
` | ||
) | ||
|
||
var ( | ||
testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){ | ||
"apstra": providerserver.NewProtocol6WithError(tfapstra.NewProvider()), | ||
} | ||
) |
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
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
Oops, something went wrong.