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 + "x", ""}, {"pre-" + c + "-tail", "pre-"}, {"pre-" + strings.ToUpper(c) + "X: y", "pre-"}, {"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.: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: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 </tmp/a\nkey: " + c + "\nother: $(date)\nEOF\ncat </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) } }