Skip to content

Commit

Permalink
tests for ProcessContent
Browse files Browse the repository at this point in the history
Signed-off-by: Gerd Oberlechner <[email protected]>
  • Loading branch information
geoberle committed Nov 25, 2024
1 parent 6e830b5 commit de809a7
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
77 changes: 77 additions & 0 deletions tooling/templatize/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/Azure/ARO-HCP/tooling/templatize/internal/testutil"
)

func TestConfigProvider(t *testing.T) {
Expand Down Expand Up @@ -231,3 +233,78 @@ func TestConvertToInterface(t *testing.T) {
assert.IsType(t, expected, map[string]any{})
assert.IsType(t, expected["key2"], map[string]any{})
}

func TestPreprocessContent(t *testing.T) {
fileContent, err := os.ReadFile("../../testdata/test.bicepparam")
assert.Nil(t, err)

processed, err := PreprocessContent(
fileContent,
map[string]any{
"regionRG": "bahamas",
"clusterService": map[string]any{
"imageTag": "cs-image",
},
},
)
assert.Nil(t, err)
testutil.CompareWithFixture(t, processed, testutil.WithExtension("bicepparam"))
}

func TestPreprocessContentMissingKey(t *testing.T) {
testCases := []struct {
name string
content string
vars map[string]any
shouldFail bool
}{
{
name: "missing key",
content: "foo: {{ .bar }}",
vars: map[string]any{
"baz": "bar",
},
shouldFail: true,
},
{
name: "missing nested key",
content: "foo: {{ .bar.baz }}",
vars: map[string]any{
"baz": "bar",
},
shouldFail: true,
},
{
name: "no missing key",
content: "foo: {{ .bar }}",
vars: map[string]any{
"bar": "bar",
},
shouldFail: false,
},
{
name: "no missing nested key",
content: "foo: {{ .bar.baz }}",
vars: map[string]any{
"bar": map[string]any{
"baz": "baz",
},
},
shouldFail: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := PreprocessContent(
[]byte(tc.content),
tc.vars,
)
if tc.shouldFail {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// copy from dev-infrastructure/configurations/region.bicepparam
using '../templates/region.bicep'

// dns
param baseDNSZoneName = 'hcp.osadev.cloud'
param baseDNSZoneResourceGroup = 'global'

// CS
param csImage = 'cs-image'
param regionRG = 'bahamas'

0 comments on commit de809a7

Please sign in to comment.