Skip to content

Commit a3a8c68

Browse files
authored
Merge pull request #10619 from sbueringer/pr-replace-reflect-euqal-tests
🌱 Replace reflect.DeepEqual in tests
2 parents 5e62dcf + 7c1a48c commit a3a8c68

File tree

10 files changed

+26
-31
lines changed

10 files changed

+26
-31
lines changed

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"context"
2222
"fmt"
23-
"reflect"
2423
"testing"
2524
"time"
2625

@@ -1558,7 +1557,7 @@ func TestKubeadmConfigReconciler_Reconcile_DiscoveryReconcileBehaviors(t *testin
15581557
},
15591558
validateDiscovery: func(g *WithT, c *bootstrapv1.KubeadmConfig) error {
15601559
d := c.Spec.JoinConfiguration.Discovery
1561-
g.Expect(reflect.DeepEqual(d.BootstrapToken.CACertHashes, caHash)).To(BeTrue())
1560+
g.Expect(d.BootstrapToken.CACertHashes).To(BeComparableTo(caHash))
15621561
return nil
15631562
},
15641563
},

exp/addons/api/v1beta1/clusterresourcesetbinding_types_test.go

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

1919
import (
20-
"reflect"
2120
"testing"
2221
"time"
2322

23+
"github.com/google/go-cmp/cmp"
2424
. "github.com/onsi/gomega"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
)
@@ -207,7 +207,7 @@ func TestSetResourceBinding(t *testing.T) {
207207
tt.resourceSetBinding.SetBinding(tt.resourceBinding)
208208
exist := false
209209
for _, b := range tt.resourceSetBinding.Resources {
210-
if reflect.DeepEqual(b.ResourceRef, tt.resourceBinding.ResourceRef) {
210+
if cmp.Equal(b.ResourceRef, tt.resourceBinding.ResourceRef) {
211211
gs.Expect(tt.resourceBinding.Applied).To(BeEquivalentTo(b.Applied))
212212
exist = true
213213
}

exp/addons/internal/controllers/clusterresourceset_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ package controllers
1919
import (
2020
"crypto/sha1" //nolint: gosec
2121
"fmt"
22-
"reflect"
2322
"testing"
2423
"time"
2524

25+
"github.com/google/go-cmp/cmp"
2626
. "github.com/onsi/gomega"
2727
"github.com/pkg/errors"
2828
corev1 "k8s.io/api/core/v1"
@@ -1032,7 +1032,7 @@ func configMapHasBeenUpdated(env *envtest.Environment, key client.ObjectKey, new
10321032
return err
10331033
}
10341034

1035-
if !reflect.DeepEqual(cm.Data, newState.Data) {
1035+
if !cmp.Equal(cm.Data, newState.Data) {
10361036
return errors.Errorf("configMap %s hasn't been updated yet", key.Name)
10371037
}
10381038

internal/apis/core/exp/addons/v1alpha3/clusterresourcesetbinding_types_test.go

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

1919
import (
20-
"reflect"
2120
"testing"
2221
"time"
2322

23+
"github.com/google/go-cmp/cmp"
2424
. "github.com/onsi/gomega"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
)
@@ -147,7 +147,7 @@ func TestSetResourceBinding(t *testing.T) {
147147
tt.resourceSetBinding.SetBinding(tt.resourceBinding)
148148
exist := false
149149
for _, b := range tt.resourceSetBinding.Resources {
150-
if reflect.DeepEqual(b.ResourceRef, tt.resourceBinding.ResourceRef) {
150+
if cmp.Equal(b.ResourceRef, tt.resourceBinding.ResourceRef) {
151151
gs.Expect(tt.resourceBinding.Applied).To(BeEquivalentTo(b.Applied))
152152
exist = true
153153
}

internal/apis/core/exp/addons/v1alpha4/clusterresourcesetbinding_types_test.go

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

1919
import (
20-
"reflect"
2120
"testing"
2221
"time"
2322

23+
"github.com/google/go-cmp/cmp"
2424
. "github.com/onsi/gomega"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
)
@@ -147,7 +147,7 @@ func TestSetResourceBinding(t *testing.T) {
147147
tt.resourceSetBinding.SetBinding(tt.resourceBinding)
148148
exist := false
149149
for _, b := range tt.resourceSetBinding.Resources {
150-
if reflect.DeepEqual(b.ResourceRef, tt.resourceBinding.ResourceRef) {
150+
if cmp.Equal(b.ResourceRef, tt.resourceBinding.ResourceRef) {
151151
gs.Expect(tt.resourceBinding.Applied).To(BeEquivalentTo(b.Applied))
152152
exist = true
153153
}

internal/controllers/clusterclass/clusterclass_controller_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package clusterclass
1919
import (
2020
"context"
2121
"fmt"
22-
"reflect"
2322
"testing"
2423
"time"
2524

@@ -188,10 +187,10 @@ func assertStatusVariables(actualClusterClass *clusterv1.ClusterClass) error {
188187
if specVar.Required != statusVarDefinition.Required {
189188
return errors.Errorf("ClusterClass status variable %s required field does not match. Expecte %v. Got %v", specVar.Name, statusVarDefinition.Required, statusVarDefinition.Required)
190189
}
191-
if !reflect.DeepEqual(specVar.Schema, statusVarDefinition.Schema) {
190+
if !cmp.Equal(specVar.Schema, statusVarDefinition.Schema) {
192191
return errors.Errorf("ClusterClass status variable %s schema does not match. Expected %v. Got %v", specVar.Name, specVar.Schema, statusVarDefinition.Schema)
193192
}
194-
if !reflect.DeepEqual(specVar.Metadata, statusVarDefinition.Metadata) {
193+
if !cmp.Equal(specVar.Metadata, statusVarDefinition.Metadata) {
195194
return errors.Errorf("ClusterClass status variable %s metadata does not match. Expected %v. Got %v", specVar.Name, specVar.Metadata, statusVarDefinition.Metadata)
196195
}
197196
}
@@ -719,15 +718,15 @@ func TestReconciler_extensionConfigToClusterClass(t *testing.T) {
719718
{NamespacedName: types.NamespacedName{Namespace: onePatchClusterClass.Namespace, Name: onePatchClusterClass.Name}},
720719
{NamespacedName: types.NamespacedName{Namespace: twoPatchClusterClass.Namespace, Name: twoPatchClusterClass.Name}},
721720
}
722-
if got := r.extensionConfigToClusterClass(context.Background(), firstExtConfig); !reflect.DeepEqual(got, firstExtConfigExpected) {
721+
if got := r.extensionConfigToClusterClass(context.Background(), firstExtConfig); !cmp.Equal(got, firstExtConfigExpected) {
723722
t.Errorf("extensionConfigToClusterClass() = %v, want %v", got, firstExtConfigExpected)
724723
}
725724

726725
// Expect only twoPatchClusterClass to trigger a reconcile as it's the only class with a reference to ExtensionCopnfig `runtime2`.
727726
secondExtConfigExpected := []reconcile.Request{
728727
{NamespacedName: types.NamespacedName{Namespace: twoPatchClusterClass.Namespace, Name: twoPatchClusterClass.Name}},
729728
}
730-
if got := r.extensionConfigToClusterClass(context.Background(), secondExtConfig); !reflect.DeepEqual(got, secondExtConfigExpected) {
729+
if got := r.extensionConfigToClusterClass(context.Background(), secondExtConfig); !cmp.Equal(got, secondExtConfigExpected) {
731730
t.Errorf("extensionConfigToClusterClass() = %v, want %v", got, secondExtConfigExpected)
732731
}
733732
})

internal/controllers/topology/cluster/cluster_controller_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ package cluster
1818

1919
import (
2020
"fmt"
21-
"reflect"
2221
"testing"
2322
"time"
2423

25-
"github.com/google/go-cmp/cmp"
2624
. "github.com/onsi/gomega"
2725
corev1 "k8s.io/api/core/v1"
2826
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -1425,7 +1423,7 @@ func TestReconciler_DefaultCluster(t *testing.T) {
14251423
got := &clusterv1.Cluster{}
14261424
g.Expect(fakeClient.Get(ctx, client.ObjectKey{Name: tt.initialCluster.Name, Namespace: tt.initialCluster.Namespace}, got)).To(Succeed())
14271425
// Compare the spec of the two clusters to ensure that variables are defaulted correctly.
1428-
g.Expect(reflect.DeepEqual(got.Spec, tt.wantCluster.Spec)).To(BeTrue(), cmp.Diff(got.Spec, tt.wantCluster.Spec))
1426+
g.Expect(got.Spec).To(BeComparableTo(tt.wantCluster.Spec))
14291427
})
14301428
}
14311429
}

internal/runtime/client/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import (
2323
"fmt"
2424
"net/http"
2525
"net/http/httptest"
26-
"reflect"
2726
"regexp"
2827
"testing"
2928

29+
"github.com/google/go-cmp/cmp"
3030
. "github.com/onsi/gomega"
3131
corev1 "k8s.io/api/core/v1"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -1227,7 +1227,7 @@ func Test_aggregateResponses(t *testing.T) {
12271227
t.Run(tt.name, func(t *testing.T) {
12281228
aggregateSuccessfulResponses(tt.aggregateResponse, tt.responses)
12291229

1230-
if !reflect.DeepEqual(tt.aggregateResponse, tt.want) {
1230+
if !cmp.Equal(tt.aggregateResponse, tt.want) {
12311231
t.Errorf("aggregateSuccessfulResponses() got = %v, want %v", tt.aggregateResponse, tt.want)
12321232
}
12331233
})

internal/topology/variables/schema_test.go

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

1919
import (
20-
"reflect"
2120
"testing"
2221

22+
"github.com/google/go-cmp/cmp"
2323
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
2424
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2525
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -196,7 +196,7 @@ func Test_convertToAPIExtensionsJSONSchemaProps(t *testing.T) {
196196
}
197197
return
198198
}
199-
if !reflect.DeepEqual(got, tt.want) {
199+
if !cmp.Equal(got, tt.want) {
200200
t.Errorf("convertToAPIExtensionsJSONSchemaProps() got = %v, want %v", got, tt.want)
201201
}
202202
})

util/patch/patch_test.go

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

1919
import (
20-
"reflect"
2120
"testing"
2221

2322
"github.com/google/go-cmp/cmp"
@@ -108,7 +107,7 @@ func TestPatchHelper(t *testing.T) {
108107
if err := env.Get(ctx, key, objAfter); err != nil {
109108
return false
110109
}
111-
return reflect.DeepEqual(obj.GetOwnerReferences(), objAfter.GetOwnerReferences())
110+
return cmp.Equal(obj.GetOwnerReferences(), objAfter.GetOwnerReferences())
112111
}, timeout).Should(BeTrue())
113112
})
114113
})
@@ -543,7 +542,7 @@ func TestPatchHelper(t *testing.T) {
543542
return false
544543
}
545544

546-
return reflect.DeepEqual(obj.Finalizers, objAfter.Finalizers)
545+
return cmp.Equal(obj.Finalizers, objAfter.Finalizers)
547546
}, timeout).Should(BeTrue())
548547
})
549548

@@ -628,7 +627,7 @@ func TestPatchHelper(t *testing.T) {
628627
}
629628

630629
return objAfter.Spec.Paused &&
631-
reflect.DeepEqual(obj.Spec.InfrastructureRef, objAfter.Spec.InfrastructureRef)
630+
cmp.Equal(obj.Spec.InfrastructureRef, objAfter.Spec.InfrastructureRef)
632631
}, timeout).Should(BeTrue())
633632
})
634633

@@ -666,7 +665,7 @@ func TestPatchHelper(t *testing.T) {
666665
if err := env.Get(ctx, key, objAfter); err != nil {
667666
return false
668667
}
669-
return reflect.DeepEqual(objAfter.Status, obj.Status)
668+
return cmp.Equal(objAfter.Status, obj.Status)
670669
}, timeout).Should(BeTrue())
671670
})
672671

@@ -718,7 +717,7 @@ func TestPatchHelper(t *testing.T) {
718717

719718
return obj.Status.InfrastructureReady == objAfter.Status.InfrastructureReady &&
720719
conditions.IsTrue(objAfter, clusterv1.ReadyCondition) &&
721-
reflect.DeepEqual(obj.Spec, objAfter.Spec)
720+
cmp.Equal(obj.Spec, objAfter.Spec)
722721
}, timeout).Should(BeTrue())
723722
})
724723
})
@@ -774,7 +773,7 @@ func TestPatchHelper(t *testing.T) {
774773
return false
775774
}
776775

777-
return reflect.DeepEqual(obj.Spec, objAfter.Spec) &&
776+
return cmp.Equal(obj.Spec, objAfter.Spec) &&
778777
obj.GetGeneration() == objAfter.Status.ObservedGeneration
779778
}, timeout).Should(BeTrue())
780779
})
@@ -823,8 +822,8 @@ func TestPatchHelper(t *testing.T) {
823822
return false
824823
}
825824

826-
return reflect.DeepEqual(obj.Spec, objAfter.Spec) &&
827-
reflect.DeepEqual(obj.Status, objAfter.Status) &&
825+
return cmp.Equal(obj.Spec, objAfter.Spec) &&
826+
cmp.Equal(obj.Status, objAfter.Status) &&
828827
obj.GetGeneration() == objAfter.Status.ObservedGeneration
829828
}, timeout).Should(BeTrue())
830829
})

0 commit comments

Comments
 (0)