Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 replacing DeepEqual with cmp Diff in tests #8310

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package v1alpha3

import (
"reflect"
"testing"
"time"

"github.com/google/go-cmp/cmp"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestSetResourceBinding(t *testing.T) {
tt.resourceSetBinding.SetBinding(tt.resourceBinding)
exist := false
for _, b := range tt.resourceSetBinding.Resources {
if reflect.DeepEqual(b.ResourceRef, tt.resourceBinding.ResourceRef) {
if diff := cmp.Diff(b.ResourceRef, tt.resourceBinding.ResourceRef); diff == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if diff := cmp.Diff(b.ResourceRef, tt.resourceBinding.ResourceRef); diff == "" {
if cmp.Equal(b.ResourceRef, tt.resourceBinding.ResourceRef) {

How about this?

(same in the other 2 clusterresourcesetbingins_types_test.go files)

gs.Expect(tt.resourceBinding.Applied).To(BeEquivalentTo(b.Applied))
exist = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package v1alpha4

import (
"reflect"
"testing"
"time"

"github.com/google/go-cmp/cmp"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestSetResourceBinding(t *testing.T) {
tt.resourceSetBinding.SetBinding(tt.resourceBinding)
exist := false
for _, b := range tt.resourceSetBinding.Resources {
if reflect.DeepEqual(b.ResourceRef, tt.resourceBinding.ResourceRef) {
if diff := cmp.Diff(b.ResourceRef, tt.resourceBinding.ResourceRef); diff == "" {
gs.Expect(tt.resourceBinding.Applied).To(BeEquivalentTo(b.Applied))
exist = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package v1beta1

import (
"reflect"
"testing"
"time"

"github.com/google/go-cmp/cmp"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestSetResourceBinding(t *testing.T) {
tt.resourceSetBinding.SetBinding(tt.resourceBinding)
exist := false
for _, b := range tt.resourceSetBinding.Resources {
if reflect.DeepEqual(b.ResourceRef, tt.resourceBinding.ResourceRef) {
if diff := cmp.Diff(b.ResourceRef, tt.resourceBinding.ResourceRef); diff == "" {
gs.Expect(tt.resourceBinding.Applied).To(BeEquivalentTo(b.Applied))
exist = true
}
Expand Down
6 changes: 3 additions & 3 deletions internal/runtime/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"regexp"
"testing"

"github.com/google/go-cmp/cmp"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -1210,8 +1210,8 @@ func Test_aggregateResponses(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
aggregateSuccessfulResponses(tt.aggregateResponse, tt.responses)

if !reflect.DeepEqual(tt.aggregateResponse, tt.want) {
t.Errorf("aggregateSuccessfulResponses() got = %v, want %v", tt.aggregateResponse, tt.want)
if diff := cmp.Diff(tt.aggregateResponse, tt.want); diff != "" {
t.Errorf("aggregateSuccessfulResponses() got = %v, want %v, diff = %v", tt.aggregateResponse, tt.want, diff)
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions internal/topology/variables/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
package variables

import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
Expand Down Expand Up @@ -196,8 +196,8 @@ func Test_convertToAPIExtensionsJSONSchemaProps(t *testing.T) {
}
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("convertToAPIExtensionsJSONSchemaProps() got = %v, want %v", got, tt.want)
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("convertToAPIExtensionsJSONSchemaProps() got = %v, want %v, diff %v", got, tt.want, diff)
}
})
}
Expand Down
40 changes: 40 additions & 0 deletions util/conditions/compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2020 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package conditions

import (
"fmt"
"log"

"github.com/google/go-cmp/cmp"
)

func Check(actual interface{}, expected interface{}) (result bool, err error) {
defer func() error {
if recover() != nil {
log.Printf("panic occured got %v expected %v", actual, expected)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about the logs here. If the error needs to be logged it should be handled by the function using this util function.

err = fmt.Errorf("panic occured got %v expected %v", actual, expected)
}
return nil
}()
diff := cmp.Diff(actual, expected)
if diff == "" {
return true, err
}
log.Printf("mismatch of objects actual %v expected %v diff %v", actual, expected, diff)
return false, err
}
Loading