fix: ValuesTemplate quote issue (#787)
* fix: ValuesTemplate quote issue Signed-off-by: yxxhero <aiopsclub@163.com> * optimize goccy yaml options Signed-off-by: yxxhero <aiopsclub@163.com> * add more options Signed-off-by: yxxhero <aiopsclub@163.com> * fix tests Signed-off-by: yxxhero <aiopsclub@163.com> * fix tests Signed-off-by: yxxhero <aiopsclub@163.com> --------- Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
parent
219602ebc7
commit
7c72e2ba3e
|
|
@ -0,0 +1,45 @@
|
|||
package state
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/helmfile/helmfile/pkg/filesystem"
|
||||
"github.com/helmfile/helmfile/pkg/runtime"
|
||||
"github.com/helmfile/helmfile/pkg/tmpl"
|
||||
)
|
||||
|
||||
func TestExecuteTemplateExpressions(t *testing.T) {
|
||||
render := tmpl.NewFileRenderer(filesystem.DefaultFileSystem(), "", map[string]interface{}{
|
||||
"Values": map[string]interface{}{
|
||||
"foo": map[string]interface{}{
|
||||
"releaseName": "foo",
|
||||
},
|
||||
},
|
||||
"Release": map[string]interface{}{
|
||||
"Name": "foo",
|
||||
},
|
||||
})
|
||||
|
||||
v := runtime.GoccyGoYaml
|
||||
runtime.GoccyGoYaml = true
|
||||
t.Cleanup(func() {
|
||||
runtime.GoccyGoYaml = v
|
||||
})
|
||||
|
||||
rs := ReleaseSpec{
|
||||
Name: "foo",
|
||||
Chart: "bar",
|
||||
Namespace: "baz",
|
||||
ValuesTemplate: []interface{}{
|
||||
map[string]interface{}{
|
||||
"fullnameOverride": "{{ .Values | get (printf \"%s.releaseName\" .Release.Name) .Release.Name }}",
|
||||
},
|
||||
},
|
||||
}
|
||||
result, err := rs.ExecuteTemplateExpressions(render)
|
||||
|
||||
require.NoErrorf(t, err, "failed to execute template expressions: %v", err)
|
||||
require.Equalf(t, result.ValuesTemplate[0].(map[string]interface{})["fullnameOverride"], "foo", "failed to execute template expressions")
|
||||
}
|
||||
|
|
@ -67,6 +67,8 @@ func Marshal(v interface{}) ([]byte, error) {
|
|||
yamlEncoder := yaml.NewEncoder(
|
||||
&b,
|
||||
yaml.Indent(2),
|
||||
yaml.UseSingleQuote(true),
|
||||
yaml.UseLiteralStyleIfMultiline(true),
|
||||
)
|
||||
err := yamlEncoder.Encode(v)
|
||||
defer func() {
|
||||
|
|
|
|||
|
|
@ -11,6 +11,13 @@ import (
|
|||
func testYamlMarshal(t *testing.T, goccyGoYaml bool) {
|
||||
t.Helper()
|
||||
|
||||
var yamlLibraryName string
|
||||
if goccyGoYaml {
|
||||
yamlLibraryName = "goccy/go-yaml"
|
||||
} else {
|
||||
yamlLibraryName = "gopkg.in/yaml.v2"
|
||||
}
|
||||
|
||||
v := runtime.GoccyGoYaml
|
||||
runtime.GoccyGoYaml = goccyGoYaml
|
||||
t.Cleanup(func() {
|
||||
|
|
@ -25,7 +32,7 @@ func testYamlMarshal(t *testing.T, goccyGoYaml bool) {
|
|||
Annotation string `yaml:"annotation"`
|
||||
} `yaml:"info"`
|
||||
|
||||
expected string
|
||||
expected map[string]string
|
||||
}{
|
||||
{
|
||||
Name: "John",
|
||||
|
|
@ -41,14 +48,17 @@ func testYamlMarshal(t *testing.T, goccyGoYaml bool) {
|
|||
// - https://github.com/helmfile/helmfile/pull/675
|
||||
Annotation: "on",
|
||||
}},
|
||||
expected: "name: John\ninfo:\n- age: 20\n address: New York\n annotation: \"on\"\n",
|
||||
expected: map[string]string{
|
||||
"goccy/go-yaml": "name: John\ninfo:\n- age: 20\n address: New York\n annotation: 'on'\n",
|
||||
"gopkg.in/yaml.v2": "name: John\ninfo:\n- age: 20\n address: New York\n annotation: \"on\"\n",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
actual, err := Marshal(tt)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.expected, string(actual))
|
||||
require.Equal(t, tt.expected[yamlLibraryName], string(actual))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue