Skip to content

Commit

Permalink
docs: (PSKD-1390) convert default required value location to table tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Dominno <[email protected]>
  • Loading branch information
iadomi committed Feb 26, 2025
1 parent dae9644 commit c4a6c9b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 23 deletions.
84 changes: 84 additions & 0 deletions test/default_integration_plan_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,87 @@ func TestPlanAdditionalNodePools(t *testing.T) {
})
}
}

// Test the default location variable when using the sample-input-defaults.tfvars file.
// Verify that the tfplan is using the default location variable from the CONFIG-VARS which in this case is "eastus"
// module.aks.data.azurerm_public_ip.cluster_public_ip[0] location is set after apply.
func TestPlanLocation(t *testing.T) {
storageTests := map[string]testCase{
"networkSecurityGroupLocationTest": {
expected: "eastus",
resourceMapName: "azurerm_network_security_group.nsg[0]",
attributeJsonPath: "{$.location}",
},
"resourceGroupAKSRGLocationTest": {
expected: "eastus",
resourceMapName: "azurerm_resource_group.aks_rg[0]",
attributeJsonPath: "{$.location}",
},
"userAssignedIdentityUAILocationTest": {
expected: "eastus",
resourceMapName: "azurerm_user_assigned_identity.uai[0]",
attributeJsonPath: "{$.location}",
},
"kubernetesClusterAKSLocationTest": {
expected: "eastus",
resourceMapName: "module.aks.azurerm_kubernetes_cluster.aks",
attributeJsonPath: "{$.location}",
},
"jumpLinuxVirtualMachineVMLocationTest": {
expected: "eastus",
resourceMapName: "module.jump[0].azurerm_linux_virtual_machine.vm",
attributeJsonPath: "{$.location}",
},
"jumpNetworkInterfaceVMNICLocationTest": {
expected: "eastus",
resourceMapName: "module.jump[0].azurerm_network_interface.vm_nic",
attributeJsonPath: "{$.location}",
},
"jumpPublicIPVMPIPLocationTest": {
expected: "eastus",
resourceMapName: "module.jump[0].azurerm_public_ip.vm_ip[0]",
attributeJsonPath: "{$.location}",
},
"nfsManagedDiskVMDataDisk0LocationTest": {
expected: "eastus",
resourceMapName: "module.nfs[0].azurerm_managed_disk.vm_data_disk[0]",
attributeJsonPath: "{$.location}",
},
"nfsManagedDiskVMDataDisk1LocationTest": {
expected: "eastus",
resourceMapName: "module.nfs[0].azurerm_managed_disk.vm_data_disk[1]",
attributeJsonPath: "{$.location}",
},
"nfsManagedDiskVMDataDisk2LocationTest": {
expected: "eastus",
resourceMapName: "module.nfs[0].azurerm_managed_disk.vm_data_disk[2]",
attributeJsonPath: "{$.location}",
},
"nfsManagedDiskVMDataDisk3LocationTest": {
expected: "eastus",
resourceMapName: "module.nfs[0].azurerm_managed_disk.vm_data_disk[3]",
attributeJsonPath: "{$.location}",
},
"nfsNetworkInterfaceVMNICLocationTest": {
expected: "eastus",
resourceMapName: "module.nfs[0].azurerm_network_interface.vm_nic",
attributeJsonPath: "{$.location}",
},
"virtualNetworkVNETLocationTest": {
expected: "eastus",
resourceMapName: "module.vnet.azurerm_virtual_network.vnet[0]",
attributeJsonPath: "{$.location}",
},
}

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

for name, tc := range storageTests {
t.Run(name, func(t *testing.T) {
runTest(t, tc, plan)
})
}
}
43 changes: 22 additions & 21 deletions test/default_integration_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,30 @@ func TestDefaults(t *testing.T) {
// prefix
assert.Equal(t, variables["prefix"], plan.RawPlan.OutputChanges["prefix"].After.(string))

// iadomi: TEST MOVED TO TEST TABLE
// location
// module.aks.data.azurerm_public_ip.cluster_public_ip[0] location is set after apply.
locationResources := []string{
"azurerm_network_security_group.nsg[0]",
"azurerm_resource_group.aks_rg[0]",
"azurerm_user_assigned_identity.uai[0]",
"module.aks.azurerm_kubernetes_cluster.aks",
"module.jump[0].azurerm_linux_virtual_machine.vm",
"module.jump[0].azurerm_network_interface.vm_nic",
"module.jump[0].azurerm_public_ip.vm_ip[0]",
"module.nfs[0].azurerm_linux_virtual_machine.vm",
"module.nfs[0].azurerm_managed_disk.vm_data_disk[0]",
"module.nfs[0].azurerm_managed_disk.vm_data_disk[1]",
"module.nfs[0].azurerm_managed_disk.vm_data_disk[2]",
"module.nfs[0].azurerm_managed_disk.vm_data_disk[3]",
"module.nfs[0].azurerm_network_interface.vm_nic",
"module.vnet.azurerm_virtual_network.vnet[0]",
}
for _, value := range locationResources {
locationResource := plan.ResourcePlannedValuesMap[value]
locationAttributes := locationResource.AttributeValues["location"]
assert.Equal(t, variables["location"], locationAttributes, "Unexpected location")
}
// locationResources := []string{
// "azurerm_network_security_group.nsg[0]",
// "azurerm_resource_group.aks_rg[0]",
// "azurerm_user_assigned_identity.uai[0]",
// "module.aks.azurerm_kubernetes_cluster.aks",
// "module.jump[0].azurerm_linux_virtual_machine.vm",
// "module.jump[0].azurerm_network_interface.vm_nic",
// "module.jump[0].azurerm_public_ip.vm_ip[0]",
// "module.nfs[0].azurerm_linux_virtual_machine.vm",
// "module.nfs[0].azurerm_managed_disk.vm_data_disk[0]",
// "module.nfs[0].azurerm_managed_disk.vm_data_disk[1]",
// "module.nfs[0].azurerm_managed_disk.vm_data_disk[2]",
// "module.nfs[0].azurerm_managed_disk.vm_data_disk[3]",
// "module.nfs[0].azurerm_network_interface.vm_nic",
// "module.vnet.azurerm_virtual_network.vnet[0]",
// }
// for _, value := range locationResources {
// locationResource := plan.ResourcePlannedValuesMap[value]
// locationAttributes := locationResource.AttributeValues["location"]
// assert.Equal(t, variables["location"], locationAttributes, "Unexpected location")
// }
assert.Equal(t, variables["location"], plan.RawPlan.OutputChanges["location"].After.(string), "Unexpected location")

// tags - defaults to empty so there is nothing to test. If we wanted to test it, this is how we would
Expand Down
5 changes: 3 additions & 2 deletions test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"strings"
"testing"

"test/validation"

"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
tfjson "github.com/hashicorp/terraform-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/client-go/util/jsonpath"
"test/validation"
)

// getJsonPathFromResourcePlannedValuesMap retrieves the value of a jsonpath query on a given *terraform.PlanStruct
Expand Down Expand Up @@ -69,7 +70,7 @@ func getPlanVars(t *testing.T, tfVarsPath string) map[string]interface{} {
func initPlanWithVariables(t *testing.T, variables map[string]interface{}) (*terraform.PlanStruct, error) {
// Create a temporary plan file
planFileName := "testplan-" + variables["prefix"].(string) + ".tfplan"
planFilePath := filepath.Join("/tmp/", planFileName)
planFilePath := filepath.Join(os.TempDir(), planFileName)
defer os.Remove(planFilePath)

// Copy the terraform folder to a temp folder
Expand Down

0 comments on commit c4a6c9b

Please sign in to comment.