| 1013 | } |
| 1014 | |
| 1015 | func TestRenderRecursionLimit(t *testing.T) { |
| 1016 | modTime := time.Now() |
| 1017 | |
| 1018 | // endless recursion should produce an error |
| 1019 | c := &chart.Chart{ |
| 1020 | Metadata: &chart.Metadata{Name: "bad"}, |
| 1021 | Templates: []*common.File{ |
| 1022 | {Name: "templates/base", ModTime: modTime, Data: []byte(`{{include "recursion" . }}`)}, |
| 1023 | {Name: "templates/recursion", ModTime: modTime, Data: []byte(`{{define "recursion"}}{{include "recursion" . }}{{end}}`)}, |
| 1024 | }, |
| 1025 | } |
| 1026 | v := common.Values{ |
| 1027 | "Values": "", |
| 1028 | "Chart": c.Metadata, |
| 1029 | "Release": common.Values{ |
| 1030 | "Name": "TestRelease", |
| 1031 | }, |
| 1032 | } |
| 1033 | expectErr := "rendering template has a nested reference name: recursion: unable to execute template" |
| 1034 | |
| 1035 | _, err := Render(c, v) |
| 1036 | if err == nil || !strings.HasSuffix(err.Error(), expectErr) { |
| 1037 | t.Errorf("Expected err with suffix: %s", expectErr) |
| 1038 | } |
| 1039 | |
| 1040 | // calling the same function many times is ok |
| 1041 | times := 4000 |
| 1042 | phrase := "All work and no play makes Jack a dull boy" |
| 1043 | printFunc := `{{define "overlook"}}{{printf "` + phrase + `\n"}}{{end}}` |
| 1044 | var repeatedIncl strings.Builder |
| 1045 | for range times { |
| 1046 | repeatedIncl.WriteString(`{{include "overlook" . }}`) |
| 1047 | } |
| 1048 | |
| 1049 | d := &chart.Chart{ |
| 1050 | Metadata: &chart.Metadata{Name: "overlook"}, |
| 1051 | Templates: []*common.File{ |
| 1052 | {Name: "templates/quote", ModTime: modTime, Data: []byte(repeatedIncl.String())}, |
| 1053 | {Name: "templates/_function", ModTime: modTime, Data: []byte(printFunc)}, |
| 1054 | }, |
| 1055 | } |
| 1056 | |
| 1057 | out, err := Render(d, v) |
| 1058 | if err != nil { |
| 1059 | t.Fatal(err) |
| 1060 | } |
| 1061 | |
| 1062 | var expect string |
| 1063 | var expectSb1062 strings.Builder |
| 1064 | for range times { |
| 1065 | expectSb1062.WriteString(phrase + "\n") |
| 1066 | } |
| 1067 | expect += expectSb1062.String() |
| 1068 | if got := out["overlook/templates/quote"]; got != expect { |
| 1069 | t.Errorf("Expected %q, got %q (%v)", expect, got, out) |
| 1070 | } |
| 1071 | |
| 1072 | } |