Skip to content

Commit 6295e02

Browse files
committed
test(engine): deep-copy chart values in render helpers
Address review feedback from gemini-code-assist on pkg/engine/render_test.go:71: maps.Copy does a shallow copy of chrt.Values. Today's tests only mutate top-level keys, but a future contributor writing something like `values["podSubnets"] = append(values["podSubnets"].([]any), x)` could corrupt chrt.Values via the shared slice's backing array, and downstream tests loading the same chart would pick up the mutation. Introduce cloneValues — a small recursive deep-copy handling maps, slices, and primitives — and use it in the renderCozystackWith, renderGenericWith, and renderChartTemplate helpers where chrt.Values is the starting point. The existing maps.Copy for merging in caller- supplied overrides is kept; overrides maps are built fresh per test so they are already safe. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 3a8c0a5 commit 6295e02

1 file changed

Lines changed: 44 additions & 7 deletions

File tree

‎pkg/engine/render_test.go‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,40 @@ const testEndpoint = "https://talm-test.invalid:6443"
4242
// advertisedSubnets explicitly.
4343
const testAdvertisedSubnet = "192.168.1.0/24"
4444

45+
// cloneValues returns a recursive deep copy of the chart values map.
46+
// maps.Copy is a shallow copy — mutating a nested map or slice in a
47+
// test would leak into chrt.Values and corrupt subsequent renders.
48+
// Since chart values consist only of maps, slices, and primitives,
49+
// a small switch + recursion suffices; no external dep needed.
50+
func cloneValues(src map[string]any) map[string]any {
51+
dst := make(map[string]any, len(src))
52+
for k, v := range src {
53+
dst[k] = deepClone(v)
54+
}
55+
return dst
56+
}
57+
58+
func deepClone(v any) any {
59+
switch x := v.(type) {
60+
case map[string]any:
61+
out := make(map[string]any, len(x))
62+
for k, vv := range x {
63+
out[k] = deepClone(vv)
64+
}
65+
return out
66+
case []any:
67+
out := make([]any, len(x))
68+
for i, vv := range x {
69+
out[i] = deepClone(vv)
70+
}
71+
return out
72+
default:
73+
// Primitives (string, bool, int, float, nil) are immutable —
74+
// safe to share.
75+
return v
76+
}
77+
}
78+
4579
// renderChartTemplate renders a chart template in offline mode and returns the output.
4680
// talosVersion sets the TalosVersion in the Helm engine context (empty string for legacy).
4781
func renderChartTemplate(t *testing.T, chartPath string, templateFile string, talosVersion ...string) string {
@@ -66,9 +100,9 @@ func renderChartTemplate(t *testing.T, chartPath string, templateFile string, ta
66100
// for cozystack and generic presets post-issue-25 fix). Tests
67101
// that specifically exercise the required-endpoint or empty-
68102
// discovery guards build their own values maps and do not go
69-
// through this helper.
70-
values := make(map[string]any)
71-
maps.Copy(values, chrt.Values)
103+
// through this helper. cloneValues deep-copies so a mutation
104+
// here never leaks into chrt.Values.
105+
values := cloneValues(chrt.Values)
72106
if v, _ := values["endpoint"].(string); v == "" {
73107
values["endpoint"] = testEndpoint
74108
}
@@ -1116,8 +1150,10 @@ func renderCozystackWith(t *testing.T, lookup func(string, string, string) (map[
11161150
if err != nil {
11171151
t.Fatalf("load chart: %v", err)
11181152
}
1119-
values := make(map[string]any)
1120-
maps.Copy(values, chrt.Values)
1153+
// Deep-copy chart values so mutations in this helper (or in the
1154+
// caller's overrides) never leak into chrt.Values and corrupt
1155+
// subsequent renders.
1156+
values := cloneValues(chrt.Values)
11211157
// Default endpoint for tests that don't exercise the required guard.
11221158
// Caller overrides via the overrides map if it wants to trigger `required`.
11231159
if v, _ := values["endpoint"].(string); v == "" {
@@ -1147,8 +1183,9 @@ func renderGenericWith(t *testing.T, lookup func(string, string, string) (map[st
11471183
if err != nil {
11481184
t.Fatalf("load chart: %v", err)
11491185
}
1150-
values := make(map[string]any)
1151-
maps.Copy(values, chrt.Values)
1186+
// Deep-copy chart values so mutations in this helper (or in the
1187+
// caller's overrides) never leak into chrt.Values.
1188+
values := cloneValues(chrt.Values)
11521189
if v, _ := values["endpoint"].(string); v == "" {
11531190
values["endpoint"] = testEndpoint
11541191
}

0 commit comments

Comments
 (0)