Skip to content

Commit

Permalink
Treat empty error and nil/null as equivalent.
Browse files Browse the repository at this point in the history
  • Loading branch information
barney-s committed Feb 12, 2025
1 parent dc12732 commit 4012ec6
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pkg/controller/instance/delta/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ func cleanMetadata(obj *unstructured.Unstructured) {
//
// Records a Difference if values don't match or are of different types.
func walkCompare(desired, observed interface{}, path string, differences *[]Difference) {
// Special case: treat empty array and nil as equivalent
if isEmptyArrayOrNil(desired) && isEmptyArrayOrNil(observed) {
return
}

switch d := desired.(type) {
case map[string]interface{}:
e, ok := observed.(map[string]interface{})
Expand All @@ -118,6 +113,12 @@ func walkCompare(desired, observed interface{}, path string, differences *[]Diff
}
walkMap(d, e, path, differences)

case nil:
// Special case: treat empty array and nil as equivalent
if isEmptyArrayOrNil(desired) && isEmptyArrayOrNil(observed) {
return
}

case []interface{}:
e, ok := observed.([]interface{})
if !ok {
Expand Down
118 changes: 118 additions & 0 deletions pkg/controller/instance/delta/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,121 @@ func TestCompare_EmptyMaps(t *testing.T) {
})
}
}

func TestCompare_EmptyArraysAndNulls(t *testing.T) {
tests := []struct {
name string
desired *unstructured.Unstructured
observed *unstructured.Unstructured
wantDiff bool
}{
{
name: "empty array equals null in nested structure",
desired: &unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
"template": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"env": []interface{}{},
},
},
},
},
},
},
observed: &unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
"template": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"env": nil,
},
},
},
},
},
},
wantDiff: false,
},
{
name: "null equals empty array in nested structure",
desired: &unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
"template": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"env": nil,
},
},
},
},
},
},
observed: &unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
"template": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"env": []interface{}{},
},
},
},
},
},
},
wantDiff: false,
},
{
name: "non-empty array differs from null",
desired: &unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
"template": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"env": []interface{}{
map[string]interface{}{
"name": "TEST",
"value": "value",
},
},
},
},
},
},
},
},
observed: &unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
"template": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"env": nil,
},
},
},
},
},
},
wantDiff: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
differences, err := Compare(tt.desired, tt.observed)
if err != nil {
t.Errorf("Compare() error = %v", err)
return
}
if (len(differences) > 0) != tt.wantDiff {
t.Errorf("Compare() got differences = %v, want diff = %v", differences, tt.wantDiff)
}
})
}
}

0 comments on commit 4012ec6

Please sign in to comment.