diff --git a/go.mod b/go.mod index 6cf3d9d..eb7ea26 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 9730ee5..b70e119 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/resource/compare/defaults.go b/pkg/resource/compare/defaults.go index f5b7514..dbbf716 100644 --- a/pkg/resource/compare/defaults.go +++ b/pkg/resource/compare/defaults.go @@ -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" @@ -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.") } diff --git a/pkg/resource/compare/test/external_test.go b/pkg/resource/compare/test/external_test.go index 8d4d61f..5bbead3 100644 --- a/pkg/resource/compare/test/external_test.go +++ b/pkg/resource/compare/test/external_test.go @@ -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) { @@ -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)) +}