Skip to content

Commit

Permalink
test: message support added for assert failures (PSKD-1363)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Illiano committed Feb 26, 2025
1 parent 0641367 commit 4bfb6c7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
13 changes: 12 additions & 1 deletion test/acr_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestPlanAcrDisabledNew(t *testing.T) {
resourceMapName: "azurerm_container_registry.acr[0]",
attributeJsonPath: "{$}",
retriever: resourceRetrieverRequireNotExist,
message: "Azure Container Registry (ACR) present when it should not be",
},
}

Expand All @@ -42,20 +43,25 @@ func TestPlanACRStandardNew(t *testing.T) {
expected: "[]",
resourceMapName: "azurerm_container_registry.acr[0]",
attributeJsonPath: "{$.georeplications}",
message: "Geo-replications found when they should not be present",
},
"nameTest": {
resourceMapName: "azurerm_container_registry.acr[0]",
attributeJsonPath: "{$.name}",
assertFunction: assert.Contains,
message: "ACR name does not contain 'acr'",
},
"skuTest": {
expected: "Standard",
resourceMapName: "azurerm_container_registry.acr[0]",
attributeJsonPath: "{$.sku}",
message: "Unexpected ACR SKU value",
},
"adminEnabledTest": {
expected: "true",
resourceMapName: "azurerm_container_registry.acr[0]",
attributeJsonPath: "{$.admin_enabled}",
message: "Unexpected ACR admin_enabled value",
},
}

Expand Down Expand Up @@ -89,20 +95,25 @@ func TestPlanACRPremiumNew(t *testing.T) {
expected: "southeastus3 southeastus5",
resourceMapName: "azurerm_container_registry.acr[0]",
attributeJsonPath: "{$.georeplications[*].location}",
message: "Geo-replications do not match expected values",
},
"nameTest": {
resourceMapName: "azurerm_container_registry.acr[0]",
attributeJsonPath: "{$.name}",
assertFunction: assert.Contains,
message: "ACR name does not contain 'acr'",
},
"skuTest": {
expected: "Premium",
resourceMapName: "azurerm_container_registry.acr[0]",
attributeJsonPath: "{$.sku}",
message: "Unexpected ACR SKU value",
},
"adminEnabledTest": {
expected: "true",
resourceMapName: "azurerm_container_registry.acr[0]",
attributeJsonPath: "{$.admin_enabled}",
message: "Unexpected ACR admin_enabled value",
},
}

Expand Down Expand Up @@ -133,6 +144,6 @@ func TestPlanACRPremiumNew(t *testing.T) {
func resourceRetrieverRequireNotExist(t *testing.T, plan *terraform.PlanStruct, resourceMapName string,
attributeJsonPath string) (string, error) {
_, exists := plan.ResourcePlannedValuesMap[resourceMapName]
assert.False(t, exists, "Should not be present")
assert.False(t, exists, resourceMapName+"."+attributeJsonPath+" should not be present")
return "", nil
}
3 changes: 2 additions & 1 deletion test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type testCase struct {
resourceMapName string
attributeJsonPath string
assertFunction assert.ComparisonAssertionFunc
message string
}

// runTest runs a test case
Expand All @@ -109,7 +110,7 @@ func runTest(t *testing.T, tc testCase, plan *terraform.PlanStruct) {
assertFn = assert.Equal
}
validateFn := validation.AssertComparison(assertFn, tc.expected)
validateFn(t, actual)
validateFn(t, actual, tc.message)
}

// A Retriever retrieves the value from a *terraform.PlanStruct plan,
Expand Down

0 comments on commit 4bfb6c7

Please sign in to comment.