From 9312c26b65476777433379bfdba72dc5f783d04e Mon Sep 17 00:00:00 2001 From: chjmil Date: Thu, 27 Feb 2025 15:07:09 -0500 Subject: [PATCH] test: add subnet tests Signed-off-by: chjmil --- docs/CONFIG-VARS.md | 6 +-- test/default_integration_plan_new_test.go | 51 +++++++++++++++++++++-- test/helpers.go | 5 +++ 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/docs/CONFIG-VARS.md b/docs/CONFIG-VARS.md index 883d8ae5..ce31e87c 100644 --- a/docs/CONFIG-VARS.md +++ b/docs/CONFIG-VARS.md @@ -127,14 +127,14 @@ 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": {}, } @@ -142,7 +142,7 @@ The default values for the `subnets` variable are as follows: 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 = { diff --git a/test/default_integration_plan_new_test.go b/test/default_integration_plan_new_test.go index a99f1ad8..9ef7a2a8 100644 --- a/test/default_integration_plan_new_test.go +++ b/test/default_integration_plan_new_test.go @@ -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 @@ -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) + }) + } + }) + } +} diff --git a/test/helpers.go b/test/helpers.go index 9a4357e8..e3a6deee 100644 --- a/test/helpers.go +++ b/test/helpers.go @@ -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]