Allow for templatefile recursion (up to 1024 depth default) (#1250)

Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Co-authored-by: Janos <86970079+janosdebugs@users.noreply.github.com>
This commit is contained in:
Christian Mesh
2024-03-11 10:00:06 -04:00
committed by GitHub
parent b052880246
commit b51396fed5
9 changed files with 172 additions and 45 deletions

View File

@@ -151,7 +151,7 @@ func TestTemplateFile(t *testing.T) {
cty.StringVal("testdata/recursive.tmpl"),
cty.MapValEmpty(cty.String),
cty.NilVal,
`testdata/recursive.tmpl:1,3-16: Error in function call; Call to function "templatefile" failed: cannot recursively call templatefile from inside templatefile or templatestring.`,
`maximum recursion depth 1024 reached in testdata/recursive.tmpl:1,3-16, testdata/recursive.tmpl:1,3-16 ... `,
},
{
cty.StringVal("testdata/list.tmpl"),
@@ -219,6 +219,50 @@ func TestTemplateFile(t *testing.T) {
}
}
func Test_templateMaxRecursionDepth(t *testing.T) {
tests := []struct {
Input string
Want int
Err string
}{
{
"",
1024,
``,
}, {
"4096",
4096,
``,
}, {
"apple",
-1,
`invalid value for TF_TEMPLATE_RECURSION_DEPTH: strconv.Atoi: parsing "apple": invalid syntax`,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("templateMaxRecursion(%s)", test.Input), func(t *testing.T) {
os.Setenv("TF_TEMPLATE_RECURSION_DEPTH", test.Input)
got, err := templateMaxRecursionDepth()
if test.Err != "" {
if err == nil {
t.Fatal("succeeded; want error")
}
if got, want := err.Error(), test.Err; got != want {
t.Errorf("wrong error\ngot: %s\nwant: %s", got, want)
}
return
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if got != test.Want {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}
func TestFileExists(t *testing.T) {
tests := []struct {
Path cty.Value