Skip to content

Commit abf2a39

Browse files
refactor(controller): use simple equality checks in cluster tests
- pkg/cluster-handler/controller/multigrescluster/multigrescluster_controller_test.go: Replace `cmp.Diff` with standard `!=` operators for verifying scalar fields (strings, integers). This aligns with Go style guidelines to prefer simple comparison operators for simple values, reserving `cmp.Diff` for complex structures where a unified diff is necessary for debugging.
1 parent 2dc9e48 commit abf2a39

File tree

1 file changed

+58
-58
lines changed

1 file changed

+58
-58
lines changed

pkg/cluster-handler/controller/multigrescluster/multigrescluster_controller_test.go

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ func TestMultigresClusterReconciler_Reconcile_Success(t *testing.T) {
174174
}
175175

176176
expectedAddr := clusterName + "-global-topo-client." + namespace + ".svc:2379"
177-
if diff := cmp.Diff(expectedAddr, cell.Spec.GlobalTopoServer.Address); diff != "" {
178-
t.Errorf("Wiring Bug! Cell has wrong Topo Address mismatch (-want +got):\n%s", diff)
177+
if got, want := cell.Spec.GlobalTopoServer.Address, expectedAddr; got != want {
178+
t.Errorf("Wiring Bug! Cell has wrong Topo Address got %q, want %q", got, want)
179179
}
180180
},
181181
},
@@ -211,17 +211,17 @@ func TestMultigresClusterReconciler_Reconcile_Success(t *testing.T) {
211211
if err := c.Get(ctx, types.NamespacedName{Name: clusterName + "-global-topo", Namespace: namespace}, ts); err != nil {
212212
t.Fatal(err)
213213
}
214-
if diff := cmp.Diff("etcd:topo", ts.Spec.Etcd.Image); diff != "" {
215-
t.Errorf("TopoServer image mismatch (-want +got):\n%s", diff)
214+
if got, want := ts.Spec.Etcd.Image, "etcd:topo"; got != want {
215+
t.Errorf("TopoServer image mismatch got %q, want %q", got, want)
216216
}
217217

218218
// Check Admin uses admin-core
219219
deploy := &appsv1.Deployment{}
220220
if err := c.Get(ctx, types.NamespacedName{Name: clusterName + "-multiadmin", Namespace: namespace}, deploy); err != nil {
221221
t.Fatal(err)
222222
}
223-
if diff := cmp.Diff(int32(5), *deploy.Spec.Replicas); diff != "" {
224-
t.Errorf("MultiAdmin replicas mismatch (-want +got):\n%s", diff)
223+
if got, want := *deploy.Spec.Replicas, int32(5); got != want {
224+
t.Errorf("MultiAdmin replicas mismatch got %d, want %d", got, want)
225225
}
226226

227227
// Verify Wiring for independent template
@@ -230,8 +230,8 @@ func TestMultigresClusterReconciler_Reconcile_Success(t *testing.T) {
230230
t.Fatal("Expected Cell 'zone-a' to exist")
231231
}
232232
expectedAddr := clusterName + "-global-topo-client." + namespace + ".svc:2379"
233-
if diff := cmp.Diff(expectedAddr, cell.Spec.GlobalTopoServer.Address); diff != "" {
234-
t.Errorf("Wiring Bug (Independent)! Cell has wrong Topo Address mismatch (-want +got):\n%s", diff)
233+
if got, want := cell.Spec.GlobalTopoServer.Address, expectedAddr; got != want {
234+
t.Errorf("Wiring Bug (Independent)! Cell has wrong Topo Address got %q, want %q", got, want)
235235
}
236236
},
237237
},
@@ -243,7 +243,7 @@ func TestMultigresClusterReconciler_Reconcile_Success(t *testing.T) {
243243
c.Spec.MultiAdmin = multigresv1alpha1.MultiAdminConfig{TemplateRef: "default-core"}
244244
c.Spec.TemplateDefaults.CoreTemplate = ""
245245
},
246-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
246+
// Using defaults
247247
validate: func(t testing.TB, c client.Client) {
248248
ctx := t.Context()
249249
deploy := &appsv1.Deployment{}
@@ -292,8 +292,8 @@ func TestMultigresClusterReconciler_Reconcile_Success(t *testing.T) {
292292
t.Fatal(err)
293293
}
294294

295-
if diff := cmp.Diff(1, len(tg.Spec.Shards)); diff != "" {
296-
t.Fatalf("Shard count mismatch (-want +got):\n%s", diff)
295+
if got, want := len(tg.Spec.Shards), 1; got != want {
296+
t.Fatalf("Shard count mismatch got %d, want %d", got, want)
297297
}
298298

299299
orchCells := tg.Spec.Shards[0].MultiOrch.Cells
@@ -307,7 +307,7 @@ func TestMultigresClusterReconciler_Reconcile_Success(t *testing.T) {
307307
preReconcileUpdate: func(t testing.TB, c *multigresv1alpha1.MultigresCluster) {
308308
c.Spec.Images.ImagePullSecrets = []corev1.LocalObjectReference{{Name: "my-secret"}}
309309
},
310-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
310+
// Using defaults
311311
validate: func(t testing.TB, c client.Client) {
312312
ctx := t.Context()
313313
deploy := &appsv1.Deployment{}
@@ -328,15 +328,15 @@ func TestMultigresClusterReconciler_Reconcile_Success(t *testing.T) {
328328
}
329329
c.Spec.TemplateDefaults.CoreTemplate = ""
330330
},
331-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
331+
// Using defaults
332332
validate: func(t testing.TB, c client.Client) {
333333
ctx := t.Context()
334334
ts := &multigresv1alpha1.TopoServer{}
335335
if err := c.Get(ctx, types.NamespacedName{Name: clusterName + "-global-topo", Namespace: namespace}, ts); err != nil {
336336
t.Fatal("Global TopoServer not created")
337337
}
338-
if diff := cmp.Diff("etcd:inline", ts.Spec.Etcd.Image); diff != "" {
339-
t.Errorf("TopoServer image mismatch (-want +got):\n%s", diff)
338+
if got, want := ts.Spec.Etcd.Image, "etcd:inline"; got != want {
339+
t.Errorf("TopoServer image mismatch got %q, want %q", got, want)
340340
}
341341
},
342342
},
@@ -365,8 +365,8 @@ func TestMultigresClusterReconciler_Reconcile_Success(t *testing.T) {
365365
if err := c.Get(ctx, types.NamespacedName{Name: clusterName + "-global-topo", Namespace: namespace}, ts); err != nil {
366366
t.Fatal("Global TopoServer not created")
367367
}
368-
if diff := cmp.Diff(DefaultEtcdReplicas, *ts.Spec.Etcd.Replicas); diff != "" {
369-
t.Errorf("Expected default replicas mismatch (-want +got):\n%s", diff)
368+
if got, want := *ts.Spec.Etcd.Replicas, DefaultEtcdReplicas; got != want {
369+
t.Errorf("Expected default replicas mismatch got %d, want %d", got, want)
370370
}
371371
// Verify MultiAdmin NOT created
372372
deploy := &appsv1.Deployment{}
@@ -412,15 +412,15 @@ func TestMultigresClusterReconciler_Reconcile_Success(t *testing.T) {
412412
},
413413
}
414414
},
415-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
415+
// Using defaults
416416
validate: func(t testing.TB, c client.Client) {
417417
ctx := t.Context()
418418
cell := &multigresv1alpha1.Cell{}
419419
if err := c.Get(ctx, types.NamespacedName{Name: clusterName + "-zone-a", Namespace: namespace}, cell); err != nil {
420420
t.Fatal(err)
421421
}
422-
if diff := cmp.Diff("", cell.Spec.GlobalTopoServer.Address); diff != "" {
423-
t.Errorf("Address mismatch (-want +got):\n%s", diff)
422+
if got, want := cell.Spec.GlobalTopoServer.Address, ""; got != want {
423+
t.Errorf("Address mismatch got %q, want %q", got, want)
424424
}
425425
},
426426
},
@@ -441,8 +441,8 @@ func TestMultigresClusterReconciler_Reconcile_Success(t *testing.T) {
441441
if err := c.Get(ctx, types.NamespacedName{Name: clusterName + "-zone-a", Namespace: namespace}, cell); err != nil {
442442
t.Fatal(err)
443443
}
444-
if diff := cmp.Diff(int32(4), *cell.Spec.MultiGateway.Replicas); diff != "" {
445-
t.Errorf("Cell inline spec ignored (-want +got):\n%s", diff)
444+
if got, want := *cell.Spec.MultiGateway.Replicas, int32(4); got != want {
445+
t.Errorf("Cell inline spec ignored got %d, want %d", got, want)
446446
}
447447
},
448448
},
@@ -452,13 +452,13 @@ func TestMultigresClusterReconciler_Reconcile_Success(t *testing.T) {
452452
c.Spec.TemplateDefaults = multigresv1alpha1.TemplateDefaults{} // Empty
453453
c.Spec.MultiAdmin = multigresv1alpha1.MultiAdminConfig{} // Empty
454454
},
455-
existingObjects: []client.Object{cellTpl, shardTpl}, // No Core Template
455+
// Using defaults (coreTpl presence doesn't hurt)
456456
validate: func(t testing.TB, c client.Client) {
457457
// Verify Cell got empty topo address
458458
cell := &multigresv1alpha1.Cell{}
459459
_ = c.Get(t.Context(), types.NamespacedName{Name: clusterName + "-zone-a", Namespace: namespace}, cell)
460-
if diff := cmp.Diff("", cell.Spec.GlobalTopoServer.Address); diff != "" {
461-
t.Errorf("Expected empty topo address mismatch (-want +got):\n%s", diff)
460+
if got, want := cell.Spec.GlobalTopoServer.Address, ""; got != want {
461+
t.Errorf("Expected empty topo address mismatch got %q, want %q", got, want)
462462
}
463463
},
464464
},
@@ -755,19 +755,19 @@ func TestMultigresClusterReconciler_Reconcile_Failure(t *testing.T) {
755755
failureConfig: &testutil.FailureConfig{OnGet: testutil.FailOnKeyName("admin-core-fail", errBoom)},
756756
},
757757
"Error: Create GlobalTopo Failed": {
758-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
759-
failureConfig: &testutil.FailureConfig{OnCreate: testutil.FailOnObjectName(clusterName+"-global-topo", errBoom)},
758+
// Using defaults
759+
failureConfig: &testutil.FailureConfig{OnCreate: testutil.FailOnObjectName(clusterName+"-global-topo", errBoom)},
760760
},
761761
"Error: Create MultiAdmin Failed": {
762-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
763-
failureConfig: &testutil.FailureConfig{OnCreate: testutil.FailOnObjectName(clusterName+"-multiadmin", errBoom)},
762+
// Using defaults
763+
failureConfig: &testutil.FailureConfig{OnCreate: testutil.FailOnObjectName(clusterName+"-multiadmin", errBoom)},
764764
},
765765
"Error: Resolve CellTemplate Failed": {
766-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
767-
failureConfig: &testutil.FailureConfig{OnGet: testutil.FailOnKeyName("default-cell", errBoom)},
766+
// Using defaults
767+
failureConfig: &testutil.FailureConfig{OnGet: testutil.FailOnKeyName("default-cell", errBoom)},
768768
},
769769
"Error: List Existing Cells Failed (Reconcile Loop)": {
770-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
770+
// Using defaults
771771
failureConfig: &testutil.FailureConfig{
772772
OnList: func(list client.ObjectList) error {
773773
if _, ok := list.(*multigresv1alpha1.CellList); ok {
@@ -778,8 +778,8 @@ func TestMultigresClusterReconciler_Reconcile_Failure(t *testing.T) {
778778
},
779779
},
780780
"Error: Create Cell Failed": {
781-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
782-
failureConfig: &testutil.FailureConfig{OnCreate: testutil.FailOnObjectName(clusterName+"-zone-a", errBoom)},
781+
// Using defaults
782+
failureConfig: &testutil.FailureConfig{OnCreate: testutil.FailOnObjectName(clusterName+"-zone-a", errBoom)},
783783
},
784784
"Error: Prune Cell Failed": {
785785
existingObjects: []client.Object{
@@ -789,7 +789,7 @@ func TestMultigresClusterReconciler_Reconcile_Failure(t *testing.T) {
789789
failureConfig: &testutil.FailureConfig{OnDelete: testutil.FailOnObjectName(clusterName+"-zone-b", errBoom)},
790790
},
791791
"Error: List Existing TableGroups Failed": {
792-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
792+
// Using defaults
793793
failureConfig: &testutil.FailureConfig{
794794
OnList: func(list client.ObjectList) error {
795795
if _, ok := list.(*multigresv1alpha1.TableGroupList); ok {
@@ -800,12 +800,12 @@ func TestMultigresClusterReconciler_Reconcile_Failure(t *testing.T) {
800800
},
801801
},
802802
"Error: Resolve ShardTemplate Failed": {
803-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
804-
failureConfig: &testutil.FailureConfig{OnGet: testutil.FailOnKeyName("default-shard", errBoom)},
803+
// Using defaults
804+
failureConfig: &testutil.FailureConfig{OnGet: testutil.FailOnKeyName("default-shard", errBoom)},
805805
},
806806
"Error: Create TableGroup Failed": {
807-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
808-
failureConfig: &testutil.FailureConfig{OnCreate: testutil.FailOnObjectName(clusterName+"-db1-tg1", errBoom)},
807+
// Using defaults
808+
failureConfig: &testutil.FailureConfig{OnCreate: testutil.FailOnObjectName(clusterName+"-db1-tg1", errBoom)},
809809
},
810810
"Error: Prune TableGroup Failed": {
811811
existingObjects: []client.Object{
@@ -815,7 +815,7 @@ func TestMultigresClusterReconciler_Reconcile_Failure(t *testing.T) {
815815
failureConfig: &testutil.FailureConfig{OnDelete: testutil.FailOnObjectName(clusterName+"-orphan-tg", errBoom)},
816816
},
817817
"Error: UpdateStatus (List Cells Failed)": {
818-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
818+
// Using defaults
819819
failureConfig: &testutil.FailureConfig{
820820
OnList: func() func(client.ObjectList) error {
821821
count := 0
@@ -832,7 +832,7 @@ func TestMultigresClusterReconciler_Reconcile_Failure(t *testing.T) {
832832
},
833833
},
834834
"Error: UpdateStatus (List TableGroups Failed)": {
835-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
835+
// Using defaults
836836
failureConfig: &testutil.FailureConfig{
837837
OnList: func() func(client.ObjectList) error {
838838
count := 0
@@ -849,8 +849,8 @@ func TestMultigresClusterReconciler_Reconcile_Failure(t *testing.T) {
849849
},
850850
},
851851
"Error: Update Status Failed (API Error)": {
852-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
853-
failureConfig: &testutil.FailureConfig{OnStatusUpdate: testutil.FailOnObjectName(clusterName, errBoom)},
852+
// Using defaults
853+
failureConfig: &testutil.FailureConfig{OnStatusUpdate: testutil.FailOnObjectName(clusterName, errBoom)},
854854
},
855855
"Error: Global Topo Resolution Failed (During Cell Reconcile)": {
856856
preReconcileUpdate: func(t testing.TB, c *multigresv1alpha1.MultigresCluster) {
@@ -921,7 +921,7 @@ func TestMultigresClusterReconciler_Reconcile_Failure(t *testing.T) {
921921
c.Spec.Databases[0].Name = longName
922922
c.Spec.Databases[0].TableGroups[0].Name = longName
923923
},
924-
existingObjects: []client.Object{coreTpl, cellTpl, shardTpl},
924+
// Using defaults
925925
},
926926
}
927927

@@ -1097,13 +1097,13 @@ func TestTemplateLogic_Unit(t *testing.T) {
10971097
MultiGateway: multigresv1alpha1.StatelessSpec{Replicas: ptr.To(int32(99))},
10981098
}
10991099
gw, _ = MergeCellConfig(tpl, overrides, inline)
1100-
if diff := cmp.Diff(int32(99), *gw.Replicas); diff != "" {
1101-
t.Errorf("MergeCellConfig inline priority mismatch (-want +got):\n%s", diff)
1100+
if got, want := *gw.Replicas, int32(99); got != want {
1101+
t.Errorf("MergeCellConfig inline priority mismatch got %d, want %d", got, want)
11021102
}
11031103

11041104
gw, _ = MergeCellConfig(nil, overrides, nil)
1105-
if diff := cmp.Diff(int32(2), *gw.Replicas); diff != "" {
1106-
t.Errorf("MergeCellConfig nil template mismatch (-want +got):\n%s", diff)
1105+
if got, want := *gw.Replicas, int32(2); got != want {
1106+
t.Errorf("MergeCellConfig nil template mismatch got %d, want %d", got, want)
11071107
}
11081108

11091109
tplNil := &multigresv1alpha1.CellTemplate{
@@ -1112,8 +1112,8 @@ func TestTemplateLogic_Unit(t *testing.T) {
11121112
},
11131113
}
11141114
gw, _ = MergeCellConfig(tplNil, overrides, nil)
1115-
if diff := cmp.Diff("qux", gw.PodAnnotations["baz"]); diff != "" {
1116-
t.Errorf("MergeCellConfig nil map init mismatch (-want +got):\n%s", diff)
1115+
if got, want := gw.PodAnnotations["baz"], "qux"; got != want {
1116+
t.Errorf("MergeCellConfig nil map init mismatch got %q, want %q", got, want)
11171117
}
11181118
})
11191119

@@ -1207,14 +1207,14 @@ func TestTemplateLogic_Unit(t *testing.T) {
12071207
},
12081208
}
12091209
res := ResolveGlobalTopo(spec, core)
1210-
if diff := cmp.Diff("resolved", res.Etcd.Image); diff != "" {
1211-
t.Errorf("ResolveGlobalTopo template mismatch (-want +got):\n%s", diff)
1210+
if got, want := res.Etcd.Image, "resolved"; got != want {
1211+
t.Errorf("ResolveGlobalTopo template mismatch got %q, want %q", got, want)
12121212
}
12131213

12141214
spec2 := &multigresv1alpha1.GlobalTopoServerSpec{TemplateRef: "t1", Etcd: &multigresv1alpha1.EtcdSpec{Image: "inline"}}
12151215
res2 := ResolveGlobalTopo(spec2, nil)
1216-
if diff := cmp.Diff("inline", res2.Etcd.Image); diff != "" {
1217-
t.Errorf("ResolveGlobalTopo inline fallback mismatch (-want +got):\n%s", diff)
1216+
if got, want := res2.Etcd.Image, "inline"; got != want {
1217+
t.Errorf("ResolveGlobalTopo inline fallback mismatch got %q, want %q", got, want)
12181218
}
12191219

12201220
spec4 := &multigresv1alpha1.GlobalTopoServerSpec{}
@@ -1234,14 +1234,14 @@ func TestTemplateLogic_Unit(t *testing.T) {
12341234
},
12351235
}
12361236
res := ResolveMultiAdmin(spec, core)
1237-
if diff := cmp.Diff(int32(10), *res.Replicas); diff != "" {
1238-
t.Errorf("ResolveMultiAdmin template mismatch (-want +got):\n%s", diff)
1237+
if got, want := *res.Replicas, int32(10); got != want {
1238+
t.Errorf("ResolveMultiAdmin template mismatch got %d, want %d", got, want)
12391239
}
12401240

12411241
spec2 := &multigresv1alpha1.MultiAdminConfig{TemplateRef: "t1", Spec: &multigresv1alpha1.StatelessSpec{Replicas: ptr.To(int32(5))}}
12421242
res2 := ResolveMultiAdmin(spec2, nil)
1243-
if diff := cmp.Diff(int32(5), *res2.Replicas); diff != "" {
1244-
t.Errorf("ResolveMultiAdmin inline fallback mismatch (-want +got):\n%s", diff)
1243+
if got, want := *res2.Replicas, int32(5); got != want {
1244+
t.Errorf("ResolveMultiAdmin inline fallback mismatch got %d, want %d", got, want)
12451245
}
12461246

12471247
res3 := ResolveMultiAdmin(&multigresv1alpha1.MultiAdminConfig{}, nil)

0 commit comments

Comments
 (0)