Skip to content

Commit 6fb9157

Browse files
committed
replaced reflect with cmp
Signed-off-by: tariq-hasan <[email protected]>
1 parent fc858d1 commit 6fb9157

File tree

9 files changed

+31
-29
lines changed

9 files changed

+31
-29
lines changed

pkg/controller.v1beta1/experiment/manifest/generator_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ package manifest
1919
import (
2020
"errors"
2121
"math"
22-
"reflect"
2322
"testing"
2423

2524
"github.com/golang/mock/gomock"
25+
"github.com/google/go-cmp/cmp"
2626
batchv1 "k8s.io/api/batch/v1"
2727
v1 "k8s.io/api/core/v1"
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -152,8 +152,8 @@ func TestGetRunSpecWithHP(t *testing.T) {
152152
} else if !tc.Err {
153153
if err != nil {
154154
t.Errorf("Case: %v failed. Expected nil, got %v", tc.testDescription, err)
155-
} else if !reflect.DeepEqual(tc.expectedRunSpec, actualRunSpec) {
156-
t.Errorf("Case: %v failed. Expected %v\n got %v", tc.testDescription, tc.expectedRunSpec.Object, actualRunSpec.Object)
155+
} else if diff := cmp.Diff(tc.expectedRunSpec.Object, actualRunSpec.Object); diff != "" {
156+
t.Errorf("Case: %v failed. Diff (-expected, +actual):\n%s", tc.testDescription, diff)
157157
}
158158
}
159159
}
@@ -335,8 +335,8 @@ spec:
335335
} else if !tc.Err {
336336
if err != nil {
337337
t.Errorf("Case: %v failed. Expected nil, got %v", tc.testDescription, err)
338-
} else if !reflect.DeepEqual(expectedRunSpec, actualRunSpec) {
339-
t.Errorf("Case: %v failed. Expected %v\n got %v", tc.testDescription, expectedRunSpec.Object, actualRunSpec.Object)
338+
} else if diff := cmp.Diff(expectedRunSpec.Object, actualRunSpec.Object); diff != "" {
339+
t.Errorf("Case: %v failed. Diff (-expected, +actual):\n%s", tc.testDescription, diff)
340340
}
341341
}
342342
}

pkg/controller.v1beta1/suggestion/composer/composer_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import (
2222
stdlog "log"
2323
"os"
2424
"path/filepath"
25-
"reflect"
2625
"strings"
2726
"sync"
2827
"testing"
2928
"time"
3029

30+
"github.com/google/go-cmp/cmp"
3131
"github.com/onsi/gomega"
3232
"github.com/spf13/viper"
3333
appsv1 "k8s.io/api/apps/v1"
@@ -631,8 +631,8 @@ func TestDesiredRBAC(t *testing.T) {
631631
func metaEqual(expected, actual metav1.ObjectMeta) bool {
632632
return expected.Name == actual.Name &&
633633
expected.Namespace == actual.Namespace &&
634-
reflect.DeepEqual(expected.Labels, actual.Labels) &&
635-
reflect.DeepEqual(expected.Annotations, actual.Annotations) &&
634+
cmp.Equal(expected.Labels, actual.Labels) &&
635+
cmp.Equal(expected.Annotations, actual.Annotations) &&
636636
(len(actual.OwnerReferences) > 0 &&
637637
expected.OwnerReferences[0].APIVersion == actual.OwnerReferences[0].APIVersion &&
638638
expected.OwnerReferences[0].Kind == actual.OwnerReferences[0].Kind &&

pkg/controller.v1beta1/suggestion/suggestionclient/suggestionclient_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ package suggestionclient
1919
import (
2020
"errors"
2121
"fmt"
22-
"reflect"
2322
"testing"
2423
"time"
2524

2625
"github.com/golang/mock/gomock"
26+
"github.com/google/go-cmp/cmp"
2727
"github.com/onsi/gomega"
2828
"google.golang.org/grpc"
2929
"google.golang.org/grpc/codes"
@@ -578,8 +578,8 @@ func TestConvertTrialObservation(t *testing.T) {
578578
}
579579
for _, tc := range tcs {
580580
actualObservation := convertTrialObservation(tc.strategies, tc.inObservation)
581-
if !reflect.DeepEqual(actualObservation, tc.expectedObservation) {
582-
t.Errorf("Case: %v failed.\nExpected observation: %v \ngot: %v", tc.testDescription, tc.expectedObservation, actualObservation)
581+
if diff := cmp.Diff(tc.expectedObservation, actualObservation); diff != "" {
582+
t.Errorf("Case: %v failed.\nDiff (-expected, +actual):\n%s", tc.testDescription, diff)
583583
}
584584
}
585585
}

pkg/controller.v1beta1/trial/util/job_util_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ limitations under the License.
1717
package util
1818

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

2322
batchv1 "k8s.io/api/batch/v1"
2423
corev1 "k8s.io/api/core/v1"
2524
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2625
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2726

27+
"github.com/google/go-cmp/cmp"
2828
trialsv1beta1 "github.com/kubeflow/katib/pkg/apis/controller/trials/v1beta1"
2929
"github.com/kubeflow/katib/pkg/controller.v1beta1/util"
3030
)
@@ -114,8 +114,8 @@ func TestGetDeployedJobStatus(t *testing.T) {
114114
} else if !tc.err {
115115
if err != nil {
116116
t.Errorf("Case: %v failed. Expected nil, got %v", tc.testDescription, err)
117-
} else if !reflect.DeepEqual(tc.expectedTrialJobStatus, actualTrialJobStatus) {
118-
t.Errorf("Case: %v failed. Expected %v\n got %v", tc.testDescription, tc.expectedTrialJobStatus, actualTrialJobStatus)
117+
} else if diff := cmp.Diff(tc.expectedTrialJobStatus, actualTrialJobStatus); diff != "" {
118+
t.Errorf("Case: %v failed. Diff (-expected, +actual):\n%s", tc.testDescription, diff)
119119
}
120120
}
121121
}

pkg/metricscollector/v1beta1/file-metricscollector/file-metricscollector_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ package sidecarmetricscollector
1919
import (
2020
"os"
2121
"path/filepath"
22-
"reflect"
2322
"testing"
2423
"time"
2524

25+
"github.com/google/go-cmp/cmp"
2626
commonv1beta1 "github.com/kubeflow/katib/pkg/apis/controller/common/v1beta1"
2727
v1beta1 "github.com/kubeflow/katib/pkg/apis/manager/v1beta1"
2828
"github.com/kubeflow/katib/pkg/controller.v1beta1/consts"
@@ -159,8 +159,8 @@ func TestCollectObservationLog(t *testing.T) {
159159
if (err != nil) != test.err {
160160
t.Errorf("\nGOT: \n%v\nWANT: %v\n", err, test.err)
161161
} else {
162-
if !reflect.DeepEqual(actual, test.expected) {
163-
t.Errorf("Expected %v\n got %v", test.expected, actual)
162+
if diff := cmp.Diff(test.expected, actual); diff != "" {
163+
t.Errorf("Diff (-expected, +actual):\n%s", diff)
164164
}
165165
}
166166
})

pkg/suggestion/v1beta1/goptuna/converter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ func toGoptunaParams(
311311
}
312312
ir := float64(p)
313313
internalParams[name] = ir
314-
// externalParams[name] = p is prohibited because of reflect.DeepEqual() will be false
314+
// externalParams[name] = p is prohibited because of cmp.Diff() will not be empty
315315
// at findGoptunaTrialIDByParam() function.
316316
externalParams[name] = d.ToExternalRepr(ir)
317317
case goptuna.StepIntUniformDistribution:
@@ -321,7 +321,7 @@ func toGoptunaParams(
321321
}
322322
ir := float64(p)
323323
internalParams[name] = ir
324-
// externalParams[name] = p is prohibited because of reflect.DeepEqual() will be false
324+
// externalParams[name] = p is prohibited because of cmp.Diff() will not be empty
325325
// at findGoptunaTrialIDByParam() function.
326326
externalParams[name] = d.ToExternalRepr(ir)
327327
case goptuna.CategoricalDistribution:

pkg/suggestion/v1beta1/goptuna/converter_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ limitations under the License.
1717
package suggestion_goptuna_v1beta1
1818

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

2322
"github.com/c-bata/goptuna"
23+
"github.com/google/go-cmp/cmp"
2424
api_v1_beta1 "github.com/kubeflow/katib/pkg/apis/manager/v1beta1"
2525
)
2626

@@ -185,8 +185,8 @@ func Test_toGoptunaSearchSpace(t *testing.T) {
185185
t.Errorf("toGoptunaSearchSpace() error = %v, wantErr %v", err, tt.wantErr)
186186
return
187187
}
188-
if !reflect.DeepEqual(got, tt.want) {
189-
t.Errorf("toGoptunaSearchSpace() got = %v, want %v", got, tt.want)
188+
if diff := cmp.Diff(tt.want, got); diff != "" {
189+
t.Errorf("toGoptunaSearchSpace() mismatch (-want, +got):\n%s", diff)
190190
}
191191
})
192192
}

pkg/suggestion/v1beta1/goptuna/sample.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ package suggestion_goptuna_v1beta1
1818

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

2423
"github.com/c-bata/goptuna"
24+
"github.com/google/go-cmp/cmp"
2525
api_v1_beta1 "github.com/kubeflow/katib/pkg/apis/manager/v1beta1"
2626
)
2727

@@ -119,7 +119,7 @@ func findGoptunaTrialIDByParam(study *goptuna.Study, trialMapping map[string]int
119119
continue
120120
}
121121

122-
if reflect.DeepEqual(ktrial.Params, trials[i].Params) {
122+
if diff := cmp.Diff(ktrial.Params, trials[i].Params); diff == "" {
123123
return trials[i].ID, nil
124124
}
125125
}

pkg/webhook/v1beta1/pod/inject_webhook_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import (
2020
"context"
2121
"fmt"
2222
"path/filepath"
23-
"reflect"
2423
"sync"
2524
"testing"
2625
"time"
2726

27+
"github.com/google/go-cmp/cmp"
2828
"github.com/onsi/gomega"
2929
appsv1 "k8s.io/api/apps/v1"
3030
batchv1 "k8s.io/api/batch/v1"
@@ -541,8 +541,10 @@ func TestGetMetricsCollectorArgs(t *testing.T) {
541541
t.Errorf("Case: %v failed. Expected nil, got %v", tc.Name, err)
542542
} else if tc.Err && err == nil {
543543
t.Errorf("Case: %v failed. Expected err, got nil", tc.Name)
544-
} else if !tc.Err && !reflect.DeepEqual(tc.ExpectedArgs, args) {
545-
t.Errorf("Case %v failed. ExpectedArgs: %v, got %v", tc.Name, tc.ExpectedArgs, args)
544+
} else if !tc.Err {
545+
if diff := cmp.Diff(tc.ExpectedArgs, args); diff != "" {
546+
t.Errorf("Case %v failed. Diff (-expected, +got): %v", tc.Name, diff)
547+
}
546548
}
547549
}
548550
}
@@ -1061,8 +1063,8 @@ func TestMutatePodMetadata(t *testing.T) {
10611063

10621064
for _, tc := range testCases {
10631065
mutatePodMetadata(tc.pod, tc.trial)
1064-
if !reflect.DeepEqual(tc.mutatedPod, tc.pod) {
1065-
t.Errorf("Case %v. Expected Pod %v, got %v", tc.testDescription, tc.mutatedPod, tc.pod)
1066+
if diff := cmp.Diff(tc.mutatedPod, tc.pod); diff != "" {
1067+
t.Errorf("Case %v failed. Diff (-expected, +got): %v", tc.testDescription, diff)
10661068
}
10671069
}
10681070
}

0 commit comments

Comments
 (0)