Skip to content

Commit

Permalink
Merge pull request #113 from ruromero/dep-compare
Browse files Browse the repository at this point in the history
fix!: compare objects semantically equal using deep.Equal
  • Loading branch information
ruromero authored Jul 19, 2023
2 parents 0eb1d17 + a09c39f commit 3630e01
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/go-openapi/spec v0.19.9
github.com/go-openapi/strfmt v0.19.5
github.com/go-openapi/validate v0.19.11
github.com/go-test/deep v1.1.0
github.com/google/gnostic v0.5.7-v3refs
github.com/openshift/api v0.0.0-20211209135129-c58d9f695577
github.com/openshift/client-go v0.0.0-20211209144617-7385dd6338e3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ github.com/go-openapi/validate v0.19.11/go.mod h1:Rzou8hA/CBw8donlS6WNEUQupNvUZ0
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0=
github.com/gobuffalo/depgen v0.0.0-20190329151759-d478694a28d3/go.mod h1:3STtPUQYuzV0gBVOY3vy6CfMm/ljR4pABfrTeHNLHUY=
github.com/gobuffalo/depgen v0.1.0/go.mod h1:+ifsuy7fhi15RWncXQQKjWS9JPkdah5sZvtHc2RXGlg=
Expand Down
6 changes: 4 additions & 2 deletions pkg/resource/compare/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/go-test/deep"
oappsv1 "github.com/openshift/api/apps/v1"
buildv1 "github.com/openshift/api/build/v1"
routev1 "github.com/openshift/api/route/v1"
Expand Down Expand Up @@ -674,10 +675,11 @@ func EqualPairs(objects [][2]interface{}) bool {
}

func Equals(deployed interface{}, requested interface{}) bool {
equal := reflect.DeepEqual(deployed, requested)
diffs := deep.Equal(deployed, requested)
equal := len(diffs) == 0
if !equal {
if logger.GetSink().Enabled(1) {
logger.V(1).Info("Objects are not equal", "deployed", deployed, "requested", requested)
logger.V(1).Info("Objects are not equal", "deployed", deployed, "requested", requested, "diffs", diffs)
} else {
logger.Info("Objects are not equal. For more details set the Operator log level to DEBUG.")
}
Expand Down
46 changes: 44 additions & 2 deletions pkg/resource/compare/test/external_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package test

import (
"reflect"
"testing"

"github.com/RHsyseng/operator-utils/pkg/resource/compare"
"github.com/RHsyseng/operator-utils/pkg/resource/test"
oappsv1 "github.com/openshift/api/apps/v1"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"reflect"
"k8s.io/apimachinery/pkg/api/resource"
"sigs.k8s.io/controller-runtime/pkg/client"
"testing"
)

func TestCompareServices(t *testing.T) {
Expand Down Expand Up @@ -115,3 +118,42 @@ func TestCompareCombined(t *testing.T) {
assert.Len(t, deltaMap[dcType].Removed, 1, "Expected 1 removed dc")
assert.Equal(t, deltaMap[dcType].Removed[0].GetName(), "dc3", "Expected removed dc called dc3")
}

func TestCompareDeployment(t *testing.T) {
dep1 := appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: *resource.NewScaledQuantity(1000000, resource.Milli),
},
},
},
},
},
},
},
}
dep2 := appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: *resource.NewScaledQuantity(1, resource.Kilo),
},
},
},
},
},
},
},
}

assert.True(t, compare.Equals(dep1, dep2))
}

0 comments on commit 3630e01

Please sign in to comment.