mirror of https://github.com/helm/helm
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
285 lines
8.5 KiB
285 lines
8.5 KiB
/*
|
|
Copyright The Helm Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package engine
|
|
|
|
import (
|
|
"math"
|
|
"strings"
|
|
"testing"
|
|
"text/template"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestFuncs(t *testing.T) {
|
|
//TODO write tests for failure cases
|
|
tests := []struct {
|
|
tpl, expect string
|
|
vars interface{}
|
|
}{{
|
|
tpl: `{{ toYaml . }}`,
|
|
expect: `foo: bar`,
|
|
vars: map[string]interface{}{"foo": "bar"},
|
|
}, {
|
|
tpl: `{{ toYamlPretty . }}`,
|
|
expect: "baz:\n - 1\n - 2\n - 3",
|
|
vars: map[string]interface{}{"baz": []int{1, 2, 3}},
|
|
}, {
|
|
tpl: `{{ toYamlPretty (fromYaml .) }}`,
|
|
expect: "foo: 1000000",
|
|
vars: "foo: !!int 1000000",
|
|
}, {
|
|
tpl: `{{ toToml . }}`,
|
|
expect: "foo = \"bar\"\n",
|
|
vars: map[string]interface{}{"foo": "bar"},
|
|
}, {
|
|
tpl: `{{ fromToml . }}`,
|
|
expect: "map[hello:world]",
|
|
vars: `hello = "world"`,
|
|
}, {
|
|
tpl: `{{ fromToml . }}`,
|
|
expect: "map[table:map[keyInTable:valueInTable subtable:map[keyInSubtable:valueInSubTable]]]",
|
|
vars: `
|
|
[table]
|
|
keyInTable = "valueInTable"
|
|
[table.subtable]
|
|
keyInSubtable = "valueInSubTable"`,
|
|
}, {
|
|
tpl: `{{ fromToml . }}`,
|
|
expect: "map[tableArray:[map[keyInElement0:valueInElement0] map[keyInElement1:valueInElement1]]]",
|
|
vars: `
|
|
[[tableArray]]
|
|
keyInElement0 = "valueInElement0"
|
|
[[tableArray]]
|
|
keyInElement1 = "valueInElement1"`,
|
|
}, {
|
|
tpl: `{{ fromToml . }}`,
|
|
expect: "map[Error:toml: line 1: unexpected EOF; expected key separator '=']",
|
|
vars: "one",
|
|
}, {
|
|
tpl: `{{ toJson . }}`,
|
|
expect: `{"foo":"bar"}`,
|
|
vars: map[string]interface{}{"foo": "bar"},
|
|
}, {
|
|
tpl: `{{ fromYaml . }}`,
|
|
expect: "map[hello:world]",
|
|
vars: `hello: world`,
|
|
}, {
|
|
tpl: `{{ fromYamlArray . }}`,
|
|
expect: "[one 2 map[name:helm]]",
|
|
vars: "- one\n- 2\n- name: helm\n",
|
|
}, {
|
|
tpl: `{{ fromYamlArray . }}`,
|
|
expect: "[one 2 map[name:helm]]",
|
|
vars: `["one", 2, { "name": "helm" }]`,
|
|
}, {
|
|
// Regression for https://github.com/helm/helm/issues/2271
|
|
tpl: `{{ toToml . }}`,
|
|
expect: "[mast]\n sail = \"white\"\n",
|
|
vars: map[string]map[string]string{"mast": {"sail": "white"}},
|
|
}, {
|
|
tpl: `{{ fromYaml . }}`,
|
|
expect: "map[Error:error unmarshaling JSON: while decoding JSON: json: cannot unmarshal array into Go value of type map[string]interface {}]",
|
|
vars: "- one\n- two\n",
|
|
}, {
|
|
tpl: `{{ fromJson .}}`,
|
|
expect: `map[hello:world]`,
|
|
vars: `{"hello":"world"}`,
|
|
}, {
|
|
tpl: `{{ fromJson . }}`,
|
|
expect: `map[Error:json: cannot unmarshal array into Go value of type map[string]interface {}]`,
|
|
vars: `["one", "two"]`,
|
|
}, {
|
|
tpl: `{{ fromJsonArray . }}`,
|
|
expect: `[one 2 map[name:helm]]`,
|
|
vars: `["one", 2, { "name": "helm" }]`,
|
|
}, {
|
|
tpl: `{{ fromJsonArray . }}`,
|
|
expect: `[json: cannot unmarshal object into Go value of type []interface {}]`,
|
|
vars: `{"hello": "world"}`,
|
|
}, {
|
|
tpl: `{{ merge .dict (fromYaml .yaml) }}`,
|
|
expect: `map[a:map[b:c]]`,
|
|
vars: map[string]interface{}{"dict": map[string]interface{}{"a": map[string]interface{}{"b": "c"}}, "yaml": `{"a":{"b":"d"}}`},
|
|
}, {
|
|
tpl: `{{ merge (fromYaml .yaml) .dict }}`,
|
|
expect: `map[a:map[b:d]]`,
|
|
vars: map[string]interface{}{"dict": map[string]interface{}{"a": map[string]interface{}{"b": "c"}}, "yaml": `{"a":{"b":"d"}}`},
|
|
}, {
|
|
tpl: `{{ fromYaml . }}`,
|
|
expect: `map[Error:error unmarshaling JSON: while decoding JSON: json: cannot unmarshal array into Go value of type map[string]interface {}]`,
|
|
vars: `["one", "two"]`,
|
|
}, {
|
|
tpl: `{{ fromYamlArray . }}`,
|
|
expect: `[error unmarshaling JSON: while decoding JSON: json: cannot unmarshal object into Go value of type []interface {}]`,
|
|
vars: `hello: world`,
|
|
}, {
|
|
// This should never result in a network lookup. Regression for #7955
|
|
tpl: `{{ lookup "v1" "Namespace" "" "unlikelynamespace99999999" }}`,
|
|
expect: `map[]`,
|
|
vars: `["one", "two"]`,
|
|
}}
|
|
|
|
for _, tt := range tests {
|
|
var b strings.Builder
|
|
err := template.Must(template.New("test").Funcs(funcMap()).Parse(tt.tpl)).Execute(&b, tt.vars)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.expect, b.String(), tt.tpl)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeYAMLScalars(t *testing.T) {
|
|
firstUnsafeInteger := float64(maxSafeYAMLInteger + 1)
|
|
aboveSafeInteger := math.Nextafter(maxSafeYAMLInteger, math.Inf(1))
|
|
|
|
tests := []struct {
|
|
name string
|
|
input any
|
|
expect any
|
|
}{
|
|
{
|
|
name: "non-integer floats stay floats",
|
|
input: map[string]any{"value": 1.5},
|
|
expect: map[string]any{"value": 1.5},
|
|
},
|
|
{
|
|
name: "safe integer floats become integers",
|
|
input: map[string]any{"value": 1.0},
|
|
expect: map[string]any{"value": int64(1)},
|
|
},
|
|
{
|
|
name: "max safe integer float becomes integer",
|
|
input: map[string]any{"value": float64(maxSafeYAMLInteger)},
|
|
expect: map[string]any{"value": int64(maxSafeYAMLInteger)},
|
|
},
|
|
{
|
|
name: "first unsafe integer float stays float",
|
|
input: map[string]any{"value": firstUnsafeInteger},
|
|
expect: map[string]any{"value": firstUnsafeInteger},
|
|
},
|
|
{
|
|
name: "unsafe integer floats stay floats",
|
|
input: map[string]any{"value": aboveSafeInteger},
|
|
expect: map[string]any{"value": aboveSafeInteger},
|
|
},
|
|
{
|
|
name: "safe negative integer floats become integers",
|
|
input: map[string]any{"value": -float64(maxSafeYAMLInteger)},
|
|
expect: map[string]any{"value": -int64(maxSafeYAMLInteger)},
|
|
},
|
|
{
|
|
name: "unsafe negative integer floats stay floats",
|
|
input: map[string]any{"value": -aboveSafeInteger},
|
|
expect: map[string]any{"value": -aboveSafeInteger},
|
|
},
|
|
{
|
|
name: "map keys and nested values are normalized",
|
|
input: map[any]any{
|
|
float64(2): float64(3),
|
|
"nested": map[any]any{
|
|
float64(4): []any{float64(5)},
|
|
},
|
|
},
|
|
expect: map[any]any{
|
|
int64(2): int64(3),
|
|
"nested": map[any]any{
|
|
int64(4): []any{int64(5)},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert.Equal(t, tt.expect, normalizeYAMLScalars(tt.input))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNormalizeYAMLMapKey(t *testing.T) {
|
|
assert.Equal(t, int64(1), normalizeYAMLMapKey(float64(1)))
|
|
assert.Equal(t, "[1 key]", normalizeYAMLMapKey([]any{float64(1), "key"}))
|
|
}
|
|
|
|
// This test to check a function provided by sprig is due to a change in a
|
|
// dependency of sprig. mergo in v0.3.9 changed the way it merges and only does
|
|
// public fields (i.e. those starting with a capital letter). This test, from
|
|
// sprig, fails in the new version. This is a behavior change for mergo that
|
|
// impacts sprig and Helm users. This test will help us to not update to a
|
|
// version of mergo (even accidentally) that causes a breaking change. See
|
|
// sprig changelog and notes for more details.
|
|
// Note, Go modules assume semver is never broken. So, there is no way to tell
|
|
// the tooling to not update to a minor or patch version. `go install` could
|
|
// be used to accidentally update mergo. This test and message should catch
|
|
// the problem and explain why it's happening.
|
|
func TestMerge(t *testing.T) {
|
|
dict := map[string]interface{}{
|
|
"src2": map[string]interface{}{
|
|
"h": 10,
|
|
"i": "i",
|
|
"j": "j",
|
|
},
|
|
"src1": map[string]interface{}{
|
|
"a": 1,
|
|
"b": 2,
|
|
"d": map[string]interface{}{
|
|
"e": "four",
|
|
},
|
|
"g": []int{6, 7},
|
|
"i": "aye",
|
|
"j": "jay",
|
|
"k": map[string]interface{}{
|
|
"l": false,
|
|
},
|
|
},
|
|
"dst": map[string]interface{}{
|
|
"a": "one",
|
|
"c": 3,
|
|
"d": map[string]interface{}{
|
|
"f": 5,
|
|
},
|
|
"g": []int{8, 9},
|
|
"i": "eye",
|
|
"k": map[string]interface{}{
|
|
"l": true,
|
|
},
|
|
},
|
|
}
|
|
tpl := `{{merge .dst .src1 .src2}}`
|
|
var b strings.Builder
|
|
err := template.Must(template.New("test").Funcs(funcMap()).Parse(tpl)).Execute(&b, dict)
|
|
assert.NoError(t, err)
|
|
|
|
expected := map[string]interface{}{
|
|
"a": "one", // key overridden
|
|
"b": 2, // merged from src1
|
|
"c": 3, // merged from dst
|
|
"d": map[string]interface{}{ // deep merge
|
|
"e": "four",
|
|
"f": 5,
|
|
},
|
|
"g": []int{8, 9}, // overridden - arrays are not merged
|
|
"h": 10, // merged from src2
|
|
"i": "eye", // overridden twice
|
|
"j": "jay", // overridden and merged
|
|
"k": map[string]interface{}{
|
|
"l": true, // overridden
|
|
},
|
|
}
|
|
assert.Equal(t, expected, dict["dst"])
|
|
}
|