Skip to content

Commit

Permalink
feat: Refactor ACR tests (PSKD-1400)
Browse files Browse the repository at this point in the history
DCO Remediation Commit for [email protected] <[email protected]>

I, [email protected] <[email protected]>, hereby add my Signed-off-by to this commit: 0b2e1ce

Signed-off-by: [email protected] <[email protected]>
  • Loading branch information
nathan-dickens committed Feb 26, 2025
1 parent 0b2e1ce commit 2206896
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
15 changes: 8 additions & 7 deletions test/acr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package test

import (
"fmt"
"strconv"
"testing"

Expand Down Expand Up @@ -73,11 +74,11 @@ func TestPlanACRPremium(t *testing.T) {
require.NoError(t, err)

tests := map[string]TestCase{
"ACRExists": ACRExists(),
"ACRNameContains": ACRNameContains("acr"),
"ACRSkuMatches": ACRSkuMatches("Premium"),
"ACRAdminMatches": ACRAdminMatches(true),
"ACRGeoReplicationsExist": ACRGeoReplicationExists(defaultGeoLocs),
"ACRExists": ACRExists(),
"ACRNameContains": ACRNameContains("acr"),
"ACRSkuMatches": ACRSkuMatches("Premium"),
"ACRAdminMatches": ACRAdminMatches(true),
"ACRGeoReplicationLocationsMatches": ACRGeoReplicationLocationsMatches(defaultGeoLocs),
}

for name, tc := range tests {
Expand Down Expand Up @@ -105,7 +106,7 @@ func ACRNameContains(name string) TestCase {
return &StringContainsTestCase{
expected: name,
path: []string{"azurerm_container_registry.acr[0]", "{$.name}"},
message: "ACR name does not contain 'acr'",
message: fmt.Sprintf("ACR name does not contain %s", name),
}
}

Expand Down Expand Up @@ -133,7 +134,7 @@ func ACRGeoReplicationsDoNotExist() TestCase {
}
}

func ACRGeoReplicationExists(expected []string) TestCase {
func ACRGeoReplicationLocationsMatches(expected []string) TestCase {
return &ElementsMatchTestCase{
expected: expected,
path: []string{"azurerm_container_registry.acr[0]", "{$.georeplications[*].location}"},
Expand Down
31 changes: 15 additions & 16 deletions test/test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package test

import (
"errors"
"strconv"
"strings"
"testing"

Expand All @@ -15,6 +14,11 @@ import (
"github.com/stretchr/testify/require"
)

const (
STRING = iota
STRING_ARRAY
)

type TestCase interface {
RunTest(t *testing.T, plan *terraform.PlanStruct)
}
Expand All @@ -27,7 +31,7 @@ type StringCompareTestCase struct {
}

func (testCase *StringCompareTestCase) RunTest(t *testing.T, plan *terraform.PlanStruct) {
actual, err := getExpectedFromPlan(plan, testCase.path, "string")
actual, err := getExpectedFromPlan(plan, testCase.path, STRING)
require.NoError(t, err)
assert.Equal(t, testCase.expected, actual, testCase.message)
}
Expand All @@ -40,7 +44,7 @@ type StringContainsTestCase struct {
}

func (testCase *StringContainsTestCase) RunTest(t *testing.T, plan *terraform.PlanStruct) {
actual, err := getExpectedFromPlan(plan, testCase.path, "string")
actual, err := getExpectedFromPlan(plan, testCase.path, STRING)
require.NoError(t, err)
assert.Contains(t, actual, testCase.expected, testCase.message)
}
Expand Down Expand Up @@ -75,30 +79,25 @@ type ElementsMatchTestCase struct {
}

func (testCase *ElementsMatchTestCase) RunTest(t *testing.T, plan *terraform.PlanStruct) {
actual, err := getExpectedFromPlan(plan, testCase.path, "string_array")
actual, err := getExpectedFromPlan(plan, testCase.path, STRING_ARRAY)
require.NoError(t, err)
assert.ElementsMatch(t, actual, testCase.expected, testCase.message)
}

func getExpectedFromPlan(plan *terraform.PlanStruct, path []string, expectedType string) (any, error) {
// Note: implement changes here if the path array changes
func getExpectedFromPlan(plan *terraform.PlanStruct, path []string, expectedType int) (any, error) {
valuesMap := plan.ResourcePlannedValuesMap[path[0]]

if expectedType == "string" {
switch expectedType {
case STRING:
return getJsonPathFromStateResource(valuesMap, path[1])
}
if expectedType == "bool" {
expected, err := getJsonPathFromStateResource(valuesMap, path[1])
if err != nil {
return nil, err
}
return strconv.ParseBool(expected)
}
if expectedType == "string_array" {
case STRING_ARRAY:
expected, err := getJsonPathFromStateResource(valuesMap, path[1])
if err != nil {
return nil, err
}
return strings.Fields(expected), nil
default:
return nil, errors.New("unknown return type")
}
return nil, errors.New("unknown return type")
}

0 comments on commit 2206896

Please sign in to comment.