From 8d055e271a9334f3fce075ea3d050c95fb9bcfdb Mon Sep 17 00:00:00 2001 From: Calvin Krist Date: Tue, 7 May 2024 18:12:34 -0400 Subject: [PATCH] 12987: test toToml int conversion --- cmd/helm/template_test.go | 6 ++++++ cmd/helm/testdata/output/issue-12987.txt | 4 ++++ cmd/helm/testdata/testcharts/issue-12987/Chart.yaml | 3 +++ .../testdata/testcharts/issue-12987/templates/object.yaml | 2 ++ cmd/helm/testdata/testcharts/issue-12987/values.yaml | 1 + pkg/engine/engine_test.go | 7 ++++++- pkg/engine/funcs_test.go | 8 ++++++++ 7 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 cmd/helm/testdata/output/issue-12987.txt create mode 100644 cmd/helm/testdata/testcharts/issue-12987/Chart.yaml create mode 100644 cmd/helm/testdata/testcharts/issue-12987/templates/object.yaml create mode 100644 cmd/helm/testdata/testcharts/issue-12987/values.yaml diff --git a/cmd/helm/template_test.go b/cmd/helm/template_test.go index e5b939879..2ae99d21e 100644 --- a/cmd/helm/template_test.go +++ b/cmd/helm/template_test.go @@ -161,6 +161,12 @@ func TestTemplateCmd(t *testing.T) { cmd: fmt.Sprintf("template '%s' -f %s/extra_values.yaml", chartPath, chartPath), golden: "output/template-subchart-cm-set-file.txt", }, + { + // Ensure that toToml processes integers as ints, and not floats + name: "template with toToml processing an integer", + cmd: fmt.Sprintf("template '%s'", "testdata/testcharts/issue-12987"), + golden: "output/issue-12987.txt", + }, } runTestCmd(t, tests) } diff --git a/cmd/helm/testdata/output/issue-12987.txt b/cmd/helm/testdata/output/issue-12987.txt new file mode 100644 index 000000000..aa6db5318 --- /dev/null +++ b/cmd/helm/testdata/output/issue-12987.txt @@ -0,0 +1,4 @@ +--- +# Source: issue-12987/templates/object.yaml +data: + bar = 9 diff --git a/cmd/helm/testdata/testcharts/issue-12987/Chart.yaml b/cmd/helm/testdata/testcharts/issue-12987/Chart.yaml new file mode 100644 index 000000000..840ed97ca --- /dev/null +++ b/cmd/helm/testdata/testcharts/issue-12987/Chart.yaml @@ -0,0 +1,3 @@ +apiVersion: v2 +name: issue-12987 +version: 0.1.0 diff --git a/cmd/helm/testdata/testcharts/issue-12987/templates/object.yaml b/cmd/helm/testdata/testcharts/issue-12987/templates/object.yaml new file mode 100644 index 000000000..40683065b --- /dev/null +++ b/cmd/helm/testdata/testcharts/issue-12987/templates/object.yaml @@ -0,0 +1,2 @@ +data: + {{ .Values | toToml }} diff --git a/cmd/helm/testdata/testcharts/issue-12987/values.yaml b/cmd/helm/testdata/testcharts/issue-12987/values.yaml new file mode 100644 index 000000000..fc5847994 --- /dev/null +++ b/cmd/helm/testdata/testcharts/issue-12987/values.yaml @@ -0,0 +1 @@ +bar: 9 diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index f8be52bf6..f7900c256 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -98,6 +98,7 @@ func TestRender(t *testing.T) { {Name: "templates/test3", Data: []byte("{{.noValue}}")}, {Name: "templates/test4", Data: []byte("{{toJson .Values}}")}, {Name: "templates/test5", Data: []byte("{{getHostByName \"helm.sh\"}}")}, + {Name: "templates/test6", Data: []byte("{{ .Values.config | toToml }}")}, }, Values: map[string]interface{}{"outer": "DEFAULT", "inner": "DEFAULT"}, } @@ -109,6 +110,9 @@ func TestRender(t *testing.T) { "global": map[string]interface{}{ "callme": "Ishmael", }, + "config": map[string]interface{}{ + "bar": 9, + }, }, } @@ -125,8 +129,9 @@ func TestRender(t *testing.T) { "moby/templates/test1": "Spouter Inn", "moby/templates/test2": "ishmael", "moby/templates/test3": "", - "moby/templates/test4": `{"global":{"callme":"Ishmael"},"inner":"inn","outer":"spouter"}`, + "moby/templates/test4": `{"config":{"bar":9},"global":{"callme":"Ishmael"},"inner":"inn","outer":"spouter"}`, "moby/templates/test5": "", + "moby/templates/test6": "bar = 9\n", } for name, data := range expect { diff --git a/pkg/engine/funcs_test.go b/pkg/engine/funcs_test.go index 29bc121b5..0f5acfdf2 100644 --- a/pkg/engine/funcs_test.go +++ b/pkg/engine/funcs_test.go @@ -99,6 +99,14 @@ func TestFuncs(t *testing.T) { tpl: `{{ lookup "v1" "Namespace" "" "unlikelynamespace99999999" }}`, expect: `map[]`, vars: `["one", "two"]`, + }, { + tpl: `{{ toToml . }}`, + expect: "bar = 9\n", + vars: map[string]int{"bar": 9}, + }, { + tpl: `{{ toToml . }}`, + expect: "bar = 9.0\n", + vars: map[string]float32{"bar": 9.0}, }} for _, tt := range tests {