Skip to content

Commit

Permalink
test: add subnet tests
Browse files Browse the repository at this point in the history
Signed-off-by: chjmil <[email protected]>
  • Loading branch information
saschjmil committed Feb 27, 2025
1 parent f413cb4 commit 9312c26
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
6 changes: 3 additions & 3 deletions docs/CONFIG-VARS.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,22 @@ The default values for the `subnets` variable are as follows:
aks = {
"prefixes": ["192.168.0.0/23"],
"service_endpoints": ["Microsoft.Sql"],
"private_endpoint_network_policies": "Disabled",
"private_endpoint_network_policies": "Enabled",
"private_link_service_network_policies_enabled": false,
"service_delegations": {},
}
misc = {
"prefixes": ["192.168.2.0/24"],
"service_endpoints": ["Microsoft.Sql"],
"private_endpoint_network_policies": "Disabled",
"private_endpoint_network_policies": "Enabled",
"private_link_service_network_policies_enabled": false,
"service_delegations": {},
}
## If using ha storage then the following is also added
netapp = {
"prefixes": ["192.168.3.0/24"],
"service_endpoints": [],
"private_endpoint_network_policies": "Disabled",
"private_endpoint_network_policies": "Enabled",
"private_link_service_network_policies_enabled": false,
"service_delegations": {
netapp = {
Expand Down
51 changes: 47 additions & 4 deletions test/default_integration_plan_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,6 @@ func TestPlanNodePools(t *testing.T) {
// Test the default additional nodepool variables when using the sample-input-defaults.tfvars file.
// Verify that the tfplan is using the default variables from the CONFIG-VARS
func TestPlanAdditionalNodePools(t *testing.T) {
type attrTuple struct {
expectedValue string
jsonPath string
}

type nodepoolTestcase struct {
expected map[string]attrTuple
Expand Down Expand Up @@ -617,3 +613,50 @@ func TestPlanGeneral(t *testing.T) {
})
}
}

func TestDefaultSubnets(t *testing.T) {
type subnetTestcase struct {
expected map[string]attrTuple
}

subnetTests := map[string]subnetTestcase{
"aks": {
expected: map[string]attrTuple{
"prefixes": {`["192.168.0.0/23"]`, "{$.address_prefixes}"},
"serviceEndpoints": {`["Microsoft.Sql"]`, "{$.service_endpoints}"},
"privateEndpointNetworkPolicies": {`Enabled`, "{$.private_endpoint_network_policies}"},
"privateLinkServiceNetworkPoliciesEnabled": {`false`, "{$.private_link_service_network_policies_enabled}"},
"serviceDelegations": {``, "{$.service_delegations}"},
},
},
"misc": {
expected: map[string]attrTuple{
"prefixes": {`["192.168.2.0/24"]`, "{$.address_prefixes}"},
"serviceEndpoints": {`["Microsoft.Sql"]`, "{$.service_endpoints}"},
"privateEndpointNetworkPolicies": {`Enabled`, "{$.private_endpoint_network_policies}"},
"privateLinkServiceNetworkPoliciesEnabled": {`false`, "{$.private_link_service_network_policies_enabled}"},
"serviceDelegations": {``, "{$.service_delegations}"},
},
},
}

variables := getDefaultPlanVars(t)
plan, err := initPlanWithVariables(t, variables)
require.NotNil(t, plan)
require.NoError(t, err)

for name, tc := range subnetTests {
t.Run(name, func(t *testing.T) {
resourceMapName := "module.vnet.azurerm_subnet.subnet[\"" + name + "\"]"
for attrName, attrTuple := range tc.expected {
t.Run(attrName, func(t *testing.T) {
runTest(t, testCase{
expected: attrTuple.expectedValue,
resourceMapName: resourceMapName,
attributeJsonPath: attrTuple.jsonPath,
}, plan)
})
}
})
}
}
5 changes: 5 additions & 0 deletions test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import (
"k8s.io/client-go/util/jsonpath"
)

type attrTuple struct {
expectedValue string
jsonPath string
}

// getJsonPathFromResourcePlannedValuesMap retrieves the value of a jsonpath query on a given *terraform.PlanStruct
func getJsonPathFromResourcePlannedValuesMap(t *testing.T, plan *terraform.PlanStruct, resourceMapName string, jsonPath string) (string, error) {
valuesMap, exists := plan.ResourcePlannedValuesMap[resourceMapName]
Expand Down

0 comments on commit 9312c26

Please sign in to comment.