Skip to content

Commit 760e4e7

Browse files
authored
Merge pull request #3342 from VibhorChinda/test
Replaced all instances of reflect.DeepEqual() with cmp.Equal()
2 parents 4355df7 + 4d3a8c6 commit 760e4e7

35 files changed

+110
-108
lines changed

api/v1alpha3/tags.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package v1alpha3
1818

1919
import (
2020
"fmt"
21-
"reflect"
2221

22+
"github.com/google/go-cmp/cmp"
2323
"k8s.io/apimachinery/pkg/types"
2424

2525
clusterv1alpha3 "sigs.k8s.io/cluster-api/api/v1alpha3"
@@ -29,8 +29,9 @@ import (
2929
type Tags map[string]string
3030

3131
// Equals returns true if the tags are equal.
32+
// This func is deprecated and should not be used.
3233
func (t Tags) Equals(other Tags) bool {
33-
return reflect.DeepEqual(t, other)
34+
return cmp.Equal(t, other)
3435
}
3536

3637
// HasOwned returns true if the tags contains a tag that marks the resource as owned by the cluster from the perspective of this management tooling.

api/v1alpha4/tags.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package v1alpha4
1818

1919
import (
2020
"fmt"
21-
"reflect"
2221

22+
"github.com/google/go-cmp/cmp"
2323
"k8s.io/apimachinery/pkg/types"
2424

2525
clusterv1alpha4 "sigs.k8s.io/cluster-api/api/v1alpha4"
@@ -29,8 +29,9 @@ import (
2929
type Tags map[string]string
3030

3131
// Equals returns true if the tags are equal.
32+
// This func is deprecated and should not be used.
3233
func (t Tags) Equals(other Tags) bool {
33-
return reflect.DeepEqual(t, other)
34+
return cmp.Equal(t, other)
3435
}
3536

3637
// HasOwned returns true if the tags contains a tag that marks the resource as owned by the cluster from the perspective of this management tooling.

api/v1alpha4/tags_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ limitations under the License.
1717
package v1alpha4
1818

1919
import (
20-
"reflect"
2120
"testing"
21+
22+
"github.com/google/go-cmp/cmp"
2223
)
2324

2425
func TestTags_Merge(t *testing.T) {
@@ -79,7 +80,7 @@ func TestTags_Merge(t *testing.T) {
7980
}
8081

8182
tags.Merge(tc.other)
82-
if e, a := tc.expected, tags; !reflect.DeepEqual(e, a) {
83+
if e, a := tc.expected, tags; !cmp.Equal(e, a) {
8384
t.Errorf("expected %#v, got %#v", e, a)
8485
}
8586
})
@@ -155,7 +156,7 @@ func TestTags_Difference(t *testing.T) {
155156
for _, tc := range tests {
156157
t.Run(tc.name, func(t *testing.T) {
157158
out := tc.self.Difference(tc.input)
158-
if e, a := tc.expected, out; !reflect.DeepEqual(e, a) {
159+
if e, a := tc.expected, out; !cmp.Equal(e, a) {
159160
t.Errorf("expected %#v, got %#v", e, a)
160161
}
161162
})

api/v1beta1/awscluster_webhook.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package v1beta1
1818

1919
import (
2020
"fmt"
21-
"reflect"
2221

22+
"github.com/google/go-cmp/cmp"
2323
apierrors "k8s.io/apimachinery/pkg/api/errors"
2424
"k8s.io/apimachinery/pkg/runtime"
2525
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -97,7 +97,7 @@ func (r *AWSCluster) ValidateUpdate(old runtime.Object) error {
9797
} else {
9898
// If old scheme was not nil, the new scheme should be the same.
9999
existingLoadBalancer := oldC.Spec.ControlPlaneLoadBalancer.DeepCopy()
100-
if !reflect.DeepEqual(existingLoadBalancer.Scheme, newLoadBalancer.Scheme) {
100+
if !cmp.Equal(existingLoadBalancer.Scheme, newLoadBalancer.Scheme) {
101101
// Only allow changes from Internet-facing scheme to internet-facing.
102102
if !(existingLoadBalancer.Scheme.String() == ClassicELBSchemeIncorrectInternetFacing.String() &&
103103
newLoadBalancer.Scheme.String() == ClassicELBSchemeInternetFacing.String()) {
@@ -110,7 +110,7 @@ func (r *AWSCluster) ValidateUpdate(old runtime.Object) error {
110110
// The name must be defined when the AWSCluster is created. If it is not defined,
111111
// then the controller generates a default name at runtime, but does not store it,
112112
// so the name remains nil. In either case, the name cannot be changed.
113-
if !reflect.DeepEqual(existingLoadBalancer.Name, newLoadBalancer.Name) {
113+
if !cmp.Equal(existingLoadBalancer.Name, newLoadBalancer.Name) {
114114
allErrs = append(allErrs,
115115
field.Invalid(field.NewPath("spec", "controlPlaneLoadBalancer", "name"),
116116
r.Spec.ControlPlaneLoadBalancer.Name, "field is immutable"),
@@ -120,27 +120,27 @@ func (r *AWSCluster) ValidateUpdate(old runtime.Object) error {
120120
// Block the update for HealthCheckProtocol :
121121
// - if it was not set in old spec but added in new spec
122122
// - if it was set in old spec but changed in new spec
123-
if !reflect.DeepEqual(newLoadBalancer.HealthCheckProtocol, existingLoadBalancer.HealthCheckProtocol) {
123+
if !cmp.Equal(newLoadBalancer.HealthCheckProtocol, existingLoadBalancer.HealthCheckProtocol) {
124124
allErrs = append(allErrs,
125125
field.Invalid(field.NewPath("spec", "controlPlaneLoadBalancer", "healthCheckProtocol"),
126126
newLoadBalancer.HealthCheckProtocol, "field is immutable once set"),
127127
)
128128
}
129129
}
130130

131-
if !reflect.DeepEqual(oldC.Spec.ControlPlaneEndpoint, clusterv1.APIEndpoint{}) &&
132-
!reflect.DeepEqual(r.Spec.ControlPlaneEndpoint, oldC.Spec.ControlPlaneEndpoint) {
131+
if !cmp.Equal(oldC.Spec.ControlPlaneEndpoint, clusterv1.APIEndpoint{}) &&
132+
!cmp.Equal(r.Spec.ControlPlaneEndpoint, oldC.Spec.ControlPlaneEndpoint) {
133133
allErrs = append(allErrs,
134134
field.Invalid(field.NewPath("spec", "controlPlaneEndpoint"), r.Spec.ControlPlaneEndpoint, "field is immutable"),
135135
)
136136
}
137137

138138
// Modifying VPC id is not allowed because it will cause a new VPC creation if set to nil.
139-
if !reflect.DeepEqual(oldC.Spec.NetworkSpec, NetworkSpec{}) &&
140-
!reflect.DeepEqual(oldC.Spec.NetworkSpec.VPC, VPCSpec{}) &&
139+
if !cmp.Equal(oldC.Spec.NetworkSpec, NetworkSpec{}) &&
140+
!cmp.Equal(oldC.Spec.NetworkSpec.VPC, VPCSpec{}) &&
141141
oldC.Spec.NetworkSpec.VPC.ID != "" {
142-
if reflect.DeepEqual(r.Spec.NetworkSpec, NetworkSpec{}) ||
143-
reflect.DeepEqual(r.Spec.NetworkSpec.VPC, VPCSpec{}) ||
142+
if cmp.Equal(r.Spec.NetworkSpec, NetworkSpec{}) ||
143+
cmp.Equal(r.Spec.NetworkSpec.VPC, VPCSpec{}) ||
144144
oldC.Spec.NetworkSpec.VPC.ID != r.Spec.NetworkSpec.VPC.ID {
145145
allErrs = append(allErrs,
146146
field.Invalid(field.NewPath("spec", "network", "vpc", "id"),

api/v1beta1/awsclustercontrolleridentity_webhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package v1beta1
1818

1919
import (
2020
"fmt"
21-
"reflect"
2221

22+
"github.com/google/go-cmp/cmp"
2323
"github.com/pkg/errors"
2424
apierrors "k8s.io/apimachinery/pkg/api/errors"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -78,7 +78,7 @@ func (r *AWSClusterControllerIdentity) ValidateUpdate(old runtime.Object) error
7878
return apierrors.NewBadRequest(fmt.Sprintf("expected an AWSClusterControllerIdentity but got a %T", old))
7979
}
8080

81-
if !reflect.DeepEqual(r.Spec, oldP.Spec) {
81+
if !cmp.Equal(r.Spec, oldP.Spec) {
8282
return errors.New("AWSClusterControllerIdentity is immutable")
8383
}
8484

api/v1beta1/awsclustertemplate_webhook.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20-
"reflect"
21-
20+
"github.com/google/go-cmp/cmp"
2221
apierrors "k8s.io/apimachinery/pkg/api/errors"
2322
"k8s.io/apimachinery/pkg/runtime"
2423
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -57,7 +56,7 @@ func (r *AWSClusterTemplate) ValidateCreate() error {
5756
func (r *AWSClusterTemplate) ValidateUpdate(oldRaw runtime.Object) error {
5857
old := oldRaw.(*AWSClusterTemplate)
5958

60-
if !reflect.DeepEqual(r.Spec, old.Spec) {
59+
if !cmp.Equal(r.Spec, old.Spec) {
6160
return apierrors.NewBadRequest("AWSClusterTemplate.Spec is immutable")
6261
}
6362
return nil

api/v1beta1/awsmachine_webhook.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20-
"reflect"
21-
20+
"github.com/google/go-cmp/cmp"
2221
"github.com/pkg/errors"
2322
apierrors "k8s.io/apimachinery/pkg/api/errors"
2423
"k8s.io/apimachinery/pkg/runtime"
@@ -114,7 +113,7 @@ func (r *AWSMachine) ValidateUpdate(old runtime.Object) error {
114113
delete(cloudInit, "secureSecretsBackend")
115114
}
116115

117-
if !reflect.DeepEqual(oldAWSMachineSpec, newAWSMachineSpec) {
116+
if !cmp.Equal(oldAWSMachineSpec, newAWSMachineSpec) {
118117
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "cannot be modified"))
119118
}
120119

api/v1beta1/awsmachinetemplate_webhook.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20-
"reflect"
21-
20+
"github.com/google/go-cmp/cmp"
2221
apierrors "k8s.io/apimachinery/pkg/api/errors"
2322
"k8s.io/apimachinery/pkg/runtime"
2423
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -139,7 +138,7 @@ func (r *AWSMachineTemplate) ValidateUpdate(old runtime.Object) error {
139138
r.Spec.Template.Spec.CloudInit.SecureSecretsBackend = ""
140139
}
141140

142-
if !reflect.DeepEqual(r.Spec, oldAWSMachineTemplate.Spec) {
141+
if !cmp.Equal(r.Spec, oldAWSMachineTemplate.Spec) {
143142
return apierrors.NewBadRequest("AWSMachineTemplate.Spec is immutable")
144143
}
145144

api/v1beta1/tags.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package v1beta1
1818

1919
import (
2020
"fmt"
21-
"reflect"
2221

22+
"github.com/google/go-cmp/cmp"
2323
"k8s.io/apimachinery/pkg/types"
2424
"k8s.io/apimachinery/pkg/util/validation/field"
2525

@@ -30,8 +30,9 @@ import (
3030
type Tags map[string]string
3131

3232
// Equals returns true if the tags are equal.
33+
// This func is deprecated and should not be used.
3334
func (t Tags) Equals(other Tags) bool {
34-
return reflect.DeepEqual(t, other)
35+
return cmp.Equal(t, other)
3536
}
3637

3738
// HasOwned returns true if the tags contains a tag that marks the resource as owned by the cluster from the perspective of this management tooling.

api/v1beta1/tags_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20-
"reflect"
2120
"sort"
2221
"strings"
2322
"testing"
2423

24+
"github.com/google/go-cmp/cmp"
2525
"k8s.io/apimachinery/pkg/util/validation/field"
2626
)
2727

@@ -83,7 +83,7 @@ func TestTags_Merge(t *testing.T) {
8383
}
8484

8585
tags.Merge(tc.other)
86-
if e, a := tc.expected, tags; !reflect.DeepEqual(e, a) {
86+
if e, a := tc.expected, tags; !cmp.Equal(e, a) {
8787
t.Errorf("expected %#v, got %#v", e, a)
8888
}
8989
})
@@ -159,7 +159,7 @@ func TestTags_Difference(t *testing.T) {
159159
for _, tc := range tests {
160160
t.Run(tc.name, func(t *testing.T) {
161161
out := tc.self.Difference(tc.input)
162-
if e, a := tc.expected, out; !reflect.DeepEqual(e, a) {
162+
if e, a := tc.expected, out; !cmp.Equal(e, a) {
163163
t.Errorf("expected %#v, got %#v", e, a)
164164
}
165165
})
@@ -278,7 +278,7 @@ func TestTags_Validate(t *testing.T) {
278278
sort.Slice(out, getSortFieldErrorsFunc(out))
279279
sort.Slice(tc.expected, getSortFieldErrorsFunc(tc.expected))
280280

281-
if !reflect.DeepEqual(out, tc.expected) {
281+
if !cmp.Equal(out, tc.expected) {
282282
t.Errorf("expected %+v, got %+v", tc.expected, out)
283283
}
284284
})

0 commit comments

Comments
 (0)