From 206d4a90539e09c4d36aec6cebbdd6f96279b34a Mon Sep 17 00:00:00 2001 From: Daniel Cheng Date: Mon, 10 Feb 2020 12:25:41 -0500 Subject: [PATCH] add test for template recursion Signed-off-by: Daniel Cheng --- pkg/engine/engine_test.go | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index f3609fcbd..d5f36aac8 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -643,3 +643,58 @@ func TestAlterFuncMap_tplinclude(t *testing.T) { } } + +func TestRenderRecursionLimit(t *testing.T) { + // endless recursion should produce an error + c := &chart.Chart{ + Metadata: &chart.Metadata{Name: "bad"}, + Templates: []*chart.File{ + {Name: "templates/base", Data: []byte(`{{include "recursion" . }}`)}, + {Name: "templates/recursion", Data: []byte(`{{define "recursion"}}{{include "recursion" . }}{{end}}`)}, + }, + } + v := chartutil.Values{ + "Values": "", + "Chart": c.Metadata, + "Release": chartutil.Values{ + "Name": "TestRelease", + }, + } + expectErr := "rendering template has a nested reference name: recursion: unable to execute template" + + _, err := Render(c, v) + if err == nil || !strings.HasSuffix(err.Error(), expectErr) { + t.Errorf("Expected err with suffix: %s", expectErr) + } + + // calling the same function many times is ok + times := 4000 + phrase := "All work and no play makes Jack a dull boy" + printFunc := `{{define "overlook"}}{{printf "` + phrase + `\n"}}{{end}}` + var repeatedIncl string + for i := 0; i < times; i++ { + repeatedIncl += `{{include "overlook" . }}` + } + + d := &chart.Chart{ + Metadata: &chart.Metadata{Name: "overlook"}, + Templates: []*chart.File{ + {Name: "templates/quote", Data: []byte(repeatedIncl)}, + {Name: "templates/_function", Data: []byte(printFunc)}, + }, + } + + out, err := Render(d, v) + if err != nil { + t.Fatal(err) + } + + var expect string + for i := 0; i < times; i++ { + expect += phrase + "\n" + } + if got := out["overlook/templates/quote"]; got != expect { + t.Errorf("Expected %q, got %q (%v)", expect, got, out) + } + +}