-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathdiff_null_test.go
More file actions
198 lines (175 loc) · 6.33 KB
/
diff_null_test.go
File metadata and controls
198 lines (175 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package desiredset
import (
"encoding/json"
"testing"
"github.com/rancher/fleet/internal/cmd/agent/deployer/objectset"
)
// Test_Diff_NullPatch validates normalizeNullPatch behavior across various
// scenarios including nested nulls, arrays with nulls, and edge cases.
func Test_Diff_NullPatch(t *testing.T) {
key := objectset.ObjectKey{Name: "test", Namespace: "ns"}
tests := []struct {
name string
patch string
expectPatch string
expectEmpty bool
expectErr bool
}{
{
name: "keeps_patch_without_nulls",
patch: `{"metadata":{"labels":{"a":"b"}}}`,
expectPatch: `{"metadata":{"labels":{"a":"b"}}}`,
},
{
name: "fast_path_no_nulls_complex_patch",
patch: `{"spec":{"replicas":3,"strategy":{"type":"RollingUpdate"},"template":{"metadata":{"labels":{"app":"test"}},"spec":{"containers":[{"name":"nginx","image":"nginx:1.14.2"}]}}}}`,
expectPatch: `{"spec":{"replicas":3,"strategy":{"type":"RollingUpdate"},"template":{"metadata":{"labels":{"app":"test"}},"spec":{"containers":[{"name":"nginx","image":"nginx:1.14.2"}]}}}}`,
},
{
name: "removes_null_field",
patch: `{"spec":{"strategy":{"rollingUpdate":null,"type":"RollingUpdate"}}}`,
expectPatch: `{"spec":{"strategy":{"type":"RollingUpdate"}}}`,
},
{
name: "removes_nested_null_fields",
patch: `{"spec":{"template":{"spec":{"securityContext":null,"containers":[{"name":"c1","image":"nginx"}]}}}}`,
expectPatch: `{"spec":{"template":{"spec":{"containers":[{"name":"c1","image":"nginx"}]}}}}`,
},
{
name: "preserves_null_elements_inside_arrays",
patch: `{"spec":{"tolerations":[{"key":"a","operator":null},null,{"key":"b"}]}}`,
expectPatch: `{"spec":{"tolerations":[{"key":"a","operator":null},null,{"key":"b"}]}}`,
},
{
name: "preserves_null_fields_inside_arrays",
patch: `{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"nginx:stable-alpine","imagePullPolicy":null}]}}}}`,
expectPatch: `{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"nginx:stable-alpine","imagePullPolicy":null}]}}}}`,
},
{
name: "removes_multiple_nulls_across_tree",
patch: `{"spec":{"foo":null,"bar":{"baz":null,"keep":"x"}},"metadata":{"annotations":null}}`,
expectPatch: `{"spec":{"bar":{"keep":"x"}}}`,
},
{
name: "empties_patch_when_only_nulls",
patch: `{"spec":{"strategy":{"rollingUpdate":null}}}`,
expectEmpty: true,
},
{
name: "fails_on_malformed_json_with_null",
patch: `{"spec":null,"bad":`,
expectErr: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
patch := []byte(tc.patch)
emptied, err := normalizeNullPatch(key, &patch)
if tc.expectErr {
if err == nil {
t.Fatal("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if emptied != tc.expectEmpty {
t.Fatalf("emptied = %v, want %v", emptied, tc.expectEmpty)
}
if tc.expectEmpty {
return
}
assertPatchJSONEqual(t, string(patch), tc.expectPatch)
})
}
}
// Test_Diff_RemoveNullPatchFields validates the recursive null removal logic
// with a complex nested structure containing maps, arrays, and null values.
// Arrays are preserved because JSON merge patches replace arrays atomically.
func Test_Diff_RemoveNullPatchFields(t *testing.T) {
input := map[string]any{
"spec": map[string]any{
"list": []any{
map[string]any{"name": "a", "value": nil},
nil,
"text",
},
"empty": map[string]any{"foo": nil},
},
"metadata": map[string]any{"labels": map[string]any{"x": "y"}},
}
cleanedAny := removeNullPatchFields(input)
cleaned, ok := cleanedAny.(map[string]any)
if !ok {
t.Fatalf("cleaned type = %T, want map[string]any", cleanedAny)
}
expected := map[string]any{
"spec": map[string]any{
"list": []any{
map[string]any{"name": "a", "value": nil},
nil,
"text",
},
},
"metadata": map[string]any{"labels": map[string]any{"x": "y"}},
}
gotJSON, err := json.Marshal(cleaned)
if err != nil {
t.Fatalf("failed to marshal cleaned: %v", err)
}
wantJSON, err := json.Marshal(expected)
if err != nil {
t.Fatalf("failed to marshal expected: %v", err)
}
if string(gotJSON) != string(wantJSON) {
t.Fatalf("cleaned mismatch\ngot: %s\nwant: %s", gotJSON, wantJSON)
}
}
// assertPatchJSONEqual compares two JSON strings for semantic equality,
// normalizing formatting differences through unmarshal/marshal cycles.
func assertPatchJSONEqual(t *testing.T, got, want string) {
t.Helper()
var gotObj map[string]any
if err := json.Unmarshal([]byte(got), &gotObj); err != nil {
t.Fatalf("failed to unmarshal got json: %v", err)
}
var wantObj map[string]any
if err := json.Unmarshal([]byte(want), &wantObj); err != nil {
t.Fatalf("failed to unmarshal want json: %v", err)
}
gotNorm, err := json.Marshal(gotObj)
if err != nil {
t.Fatalf("failed to marshal got object: %v", err)
}
wantNorm, err := json.Marshal(wantObj)
if err != nil {
t.Fatalf("failed to marshal want object: %v", err)
}
if string(gotNorm) != string(wantNorm) {
t.Fatalf("json mismatch\ngot: %s\nwant: %s", gotNorm, wantNorm)
}
}
// Benchmark_Diff_NullPatch_WithNulls benchmarks normalizeNullPatch with a patch containing nulls.
func Benchmark_Diff_NullPatch_WithNulls(b *testing.B) {
key := objectset.ObjectKey{Name: "test", Namespace: "ns"}
patch := []byte(`{"spec":{"strategy":{"rollingUpdate":null,"type":"RollingUpdate"},"template":{"spec":{"securityContext":null,"containers":[{"name":"nginx","resources":null}]}}}}`)
b.ResetTimer()
for range b.N {
patchCopy := make([]byte, len(patch))
copy(patchCopy, patch)
_, _ = normalizeNullPatch(key, &patchCopy)
}
}
// Benchmark_Diff_NullPatch_WithoutNulls benchmarks normalizeNullPatch with a patch containing no nulls.
// This should be significantly faster due to the fast-path optimization.
func Benchmark_Diff_NullPatch_WithoutNulls(b *testing.B) {
key := objectset.ObjectKey{Name: "test", Namespace: "ns"}
patch := []byte(`{"spec":{"replicas":3,"strategy":{"type":"RollingUpdate"},"template":{"metadata":{"labels":{"app":"test"}},"spec":{"containers":[{"name":"nginx","image":"nginx:1.14.2"}]}}}}`)
b.ResetTimer()
for range b.N {
patchCopy := make([]byte, len(patch))
copy(patchCopy, patch)
_, _ = normalizeNullPatch(key, &patchCopy)
}
}