Files
cozystack/hack/values-injection/compare_test.go
T
Myasnikov Daniil fb00301f0b test(hack): check chart output for arbitrary tenant values in unit-tests
hack/values-injection renders every chart under packages/apps and
packages/extra with generated values for each string field and map
key its schema allows, including the charts its HelmReleases install,
and fails when a value changes the structure of the output rather
than only its content. Fields multiline by design are listed in
hack/testdata/values-injection/allowlist.txt.

It runs as values-injection-check, a prerequisite of unit-tests, and
the Helm Charts conventions in docs/agents/overview.md gain the rule it
enforces.

Assisted-by: LLM
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
2026-12-03 10:03:22 +00:00

204 lines
8.9 KiB
Go

package main
import (
"reflect"
"strings"
"testing"
)
const c = "cnry123abc"
func TestNormKey(t *testing.T) {
for _, tc := range []struct{ in, want string }{
{"plain", "plain"},
{c, "<C>"},
{c + "x", "<X>"},
{"pre-" + c + "-tail", "pre-<C>"},
{"pre-" + strings.ToUpper(c) + "X: y", "pre-<X>"},
{"pre-" + c[:5], "pre-" + c[:5]},
} {
if got := normKey(tc.in, c); got != tc.want {
t.Errorf("normKey(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
func types(entries []shapeEntry) []string {
var out []string
for _, e := range entries {
out = append(out, joinPath(e.path)+":"+e.typ)
}
return out
}
func TestAddedShape(t *testing.T) {
base := map[string]any{"data": map[string]any{"v": c, "n": float64(1)}}
for _, tc := range []struct {
name string
payload any
want []string
marked bool
}{
{"same shape, other text", map[string]any{"data": map[string]any{"v": c + " other", "n": float64(2)}}, nil, false},
{"the payload's own key", map[string]any{"data": map[string]any{"v": c, "n": float64(1), c + "x": true}}, []string{"data.<X>:bool"}, true},
{"a key carrying the canary", map[string]any{"data": map[string]any{"v": c, "n": float64(1), "k" + c: "y"}}, []string{"data.k<C>:str"}, true},
{"a value turned into a number", map[string]any{"data": map[string]any{"v": float64(7), "n": float64(1)}}, []string{"data.v:int"}, false},
{"an int turned into a float", map[string]any{"data": map[string]any{"v": c, "n": 1.5}}, []string{"data.n:float"}, false},
{"a value turned into a map", map[string]any{"data": map[string]any{"v": map[string]any{}, "n": float64(1)}}, []string{"data.v:{}"}, false},
} {
t.Run(tc.name, func(t *testing.T) {
got := added(tc.payload, c, shape(base, c))
if !reflect.DeepEqual(types(got), tc.want) {
t.Fatalf("added = %v, want %v", types(got), tc.want)
}
marked := false
for _, e := range got {
marked = marked || e.marked
}
if marked != tc.marked {
t.Fatalf("marked = %v, want %v", marked, tc.marked)
}
})
}
}
func TestDescribeOrdersAsPythonReprAndKeepsFour(t *testing.T) {
entries := []shapeEntry{
{path: []any{"b"}, typ: "str"},
{path: []any{"a", 1}, typ: "str"},
{path: []any{"a", 0}, typ: "str"},
{path: []any{"c"}, typ: "str"},
{path: []any{"a"}, typ: "{}"},
}
// By repr, "(('a', 0)" sorts before "(('a',)": a space before a parenthesis.
want := "+a.0:str, +a.1:str, +a:{}, +b:str"
if got := describe(entries); got != want {
t.Fatalf("describe = %q, want %q", got, want)
}
}
func TestShellSkeleton(t *testing.T) {
base := parseScript("set -e\necho " + c + " >/tmp/out\n")
if !base.ok {
t.Fatal("the reference script does not parse")
}
same := parseScript("set -e\necho " + c + "-other-text >/tmp/out\n")
if !same.ok || same.skeleton != base.skeleton {
t.Fatal("a change of literal text changed the skeleton")
}
for _, injected := range []string{
"set -e\necho " + c + "$(true " + c + "x) >/tmp/out\n",
"set -e\necho " + c + "`true " + c + "x` >/tmp/out\n",
"set -e\necho " + c + ";true " + c + "x; >/tmp/out\n",
"set -e\necho " + c + " " + c + "x >/tmp/out\n",
} {
p := parseScript(injected)
if !p.ok {
t.Fatalf("%q does not parse", injected)
}
if p.skeleton == base.skeleton {
t.Errorf("%q has the skeleton of the reference", injected)
}
}
if parseScript("echo \"" + c + "\n").ok {
t.Error("an unterminated quote parsed")
}
}
func TestHeredocs(t *testing.T) {
p := parseScript("cat <<EOF >/tmp/a\nkey: " + c + "\nother: $(date)\nEOF\ncat <<EOF\nEOF\ncat <<'Q'\nq: 1\nQ\n")
if !p.ok {
t.Fatal("the script does not parse")
}
// The empty heredoc has no body to compare and is not listed.
want := []string{"key: " + c + "\nother: X\n", "q: 1\n"}
if !reflect.DeepEqual(p.heredocs, want) {
t.Fatalf("heredocs = %q, want %q", p.heredocs, want)
}
}
// configMap is a manifest of one ConfigMap with data.
func configMap(data map[string]any) map[string]any {
return map[string]any{"apiVersion": "v1", "kind": "ConfigMap",
"metadata": map[string]any{"name": "probe"}, "data": data}
}
func findingTypes(fs []finding) []string {
var out []string
for _, f := range fs {
out = append(out, f.typ)
}
return out
}
func compareObjects(t *testing.T, b, p any) []string {
t.Helper()
ref := prepare([]doc{{"t.yaml", b}}, c)
return findingTypes(compare(memo{}, ref, []doc{{"t.yaml", p}}, nil, "", c))
}
func TestCompare(t *testing.T) {
podWith := func(script string) map[string]any {
return map[string]any{"apiVersion": "v1", "kind": "Pod", "metadata": map[string]any{"name": "p"},
"spec": map[string]any{"containers": []any{map[string]any{
"name": "c", "command": []any{"/bin/sh", "-ec", script}}}}}
}
heredoc := func(body string) string { return "cat <<EOF >/etc/conf.yaml\n" + body + "\nEOF\n" }
for _, tc := range []struct {
name string
b, p any
want []string
}{
{"same", configMap(map[string]any{"v": c}), configMap(map[string]any{"v": c}), nil},
{"key injected", configMap(map[string]any{"v": c}), configMap(map[string]any{"v": c, c + "x": "y"}), []string{"yaml-inject"}},
{"type changed", configMap(map[string]any{"v": c}), configMap(map[string]any{"v": true}), []string{"yaml-shape"}},
{"namespace chosen",
map[string]any{"apiVersion": "v1", "kind": "ConfigMap", "metadata": map[string]any{"name": "probe", "namespace": "tenant-test"}},
map[string]any{"apiVersion": "v1", "kind": "ConfigMap", "metadata": map[string]any{"name": "probe", "namespace": c}},
[]string{"namespace"}},
{"shell word added", podWith("echo " + c), podWith("echo " + c + " " + c + "x"), []string{"shell-injection"}},
{"shell broken", podWith("echo " + c), podWith("echo " + c + "\""), []string{"shell-break"}},
{"script under a .sh key", configMap(map[string]any{"run.sh": "echo " + c}), configMap(map[string]any{"run.sh": "echo " + c + ";true " + c + "x;"}), []string{"shell-injection"}},
{"script with a shebang", configMap(map[string]any{"entry": "#!/bin/bash\necho " + c}), configMap(map[string]any{"entry": "#!/bin/bash\necho " + c + "$(true " + c + "x)"}), []string{"shell-injection"}},
{"sh -c in args", map[string]any{"command": []any{"/bin/bash"}, "args": []any{"-c", "echo " + c}}, map[string]any{"command": []any{"/bin/bash"}, "args": []any{"-c", "echo " + c + " " + c + "x"}}, []string{"shell-injection"}},
{"newline into a script", podWith("echo '" + c + "'"), podWith("echo '" + c + "\n" + c + "x: y'"), []string{"script-newline"}},
{"heredoc key added", podWith(heredoc("a: " + c)), podWith(heredoc("a: " + c + "\n" + c + "x: y")), []string{"heredoc-inject", "script-newline"}},
{"heredoc line added", podWith(heredoc("a = " + c)), podWith(heredoc("a = " + c + "\n" + c + "x")), []string{"heredoc-newline", "script-newline"}},
{"embedded YAML key", configMap(map[string]any{"conf": "a: " + c + "\n"}), configMap(map[string]any{"conf": "a: " + c + "\n" + c + "x: y\n"}), []string{"embedded-inject"}},
{"embedded JSON key", configMap(map[string]any{"conf": `{"a": "` + c + `"}`}), configMap(map[string]any{"conf": `{"a": "` + c + `", "` + c + `x": "y"}`}), []string{"embedded-inject"}},
{"embedded JSON broken", configMap(map[string]any{"conf": `{"a": "` + c + `"}`}), configMap(map[string]any{"conf": `{"a": "` + c + `"` + c + `x"}`}), []string{"embedded-break"}},
{"embedded line", configMap(map[string]any{"conf": "[main]\na = " + c + "\n"}), configMap(map[string]any{"conf": "[main]\na = " + c + "\n" + c + "x\n"}), []string{"embedded-newline"}},
{"embedded quote", configMap(map[string]any{"conf": "[main]\na = \"" + c + "\"\n"}), configMap(map[string]any{"conf": "[main]\na = \"" + c + "\"" + c + "x\"\n"}), []string{"embedded-quote"}},
} {
t.Run(tc.name, func(t *testing.T) {
if got := compareObjects(t, tc.b, tc.p); !reflect.DeepEqual(got, tc.want) {
t.Fatalf("findings = %v, want %v", got, tc.want)
}
})
}
}
func TestCompareDocuments(t *testing.T) {
ref := prepare([]doc{{"a.yaml", configMap(map[string]any{"v": c})}}, c)
extra := []doc{{"a.yaml", configMap(map[string]any{"v": c})}, {"a.yaml", configMap(map[string]any{"v": c + "x"})}}
got := compare(memo{}, ref, extra, nil, "", c)
if len(got) != 1 || got[0].typ != "doc-injection" || got[0].detail != "1 -> 2 documents; a.yaml ('v1', 'ConfigMap'); a.yaml ('v1', 'ConfigMap')" {
t.Fatalf("findings = %+v", got)
}
out := "data:\n v: " + c + `{{print "` + c + `tp" "leval"}}` + "\n"
got = compare(memo{}, ref, []doc{{"a.yaml", configMap(map[string]any{"v": c + c + "tpleval"})}}, nil, "x: "+c+"tpleval", c)
if len(got) != 1 || got[0].typ != "tpl-eval" || got[0].source != "a.yaml" {
t.Fatalf("an evaluated template was not reported: %+v", got)
}
if got := compare(memo{}, ref, []doc{{"a.yaml", configMap(map[string]any{"v": out})}}, nil, out, c); len(got) != 0 {
t.Fatalf("a template rendered as text was reported: %+v", got)
}
_, err := decodeStream("a: [" + c)
got = compare(memo{}, ref, nil, err, "", c)
if len(got) != 1 || got[0].typ != "yaml-break" {
t.Fatalf("a render that does not decode was not yaml-break: %+v", got)
}
}