Skip to content

Commit 9aa0f9f

Browse files
authored
Merge pull request #8266 from DiptoChakrabarty/cmpconv
🌱 replace reflect deepequal in bootstrap kubeadm
2 parents 55e2bde + d556d91 commit 9aa0f9f

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

bootstrap/kubeadm/api/v1alpha4/kubeadm_types_test.go

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

1919
import (
2020
"encoding/json"
21-
"reflect"
2221
"testing"
2322

23+
"github.com/google/go-cmp/cmp"
2424
. "github.com/onsi/gomega"
2525
"github.com/pkg/errors"
2626
)
@@ -119,11 +119,12 @@ func roundtrip(input string, bts *BootstrapTokenString) error {
119119
if err := json.Unmarshal(b, newbts); err != nil {
120120
return errors.Wrap(err, "expected no unmarshal error, got error")
121121
}
122-
if !reflect.DeepEqual(bts, newbts) {
122+
if diff := cmp.Diff(bts, newbts); diff != "" {
123123
return errors.Errorf(
124-
"expected object: %v\n\t actual: %v",
124+
"expected object: %v\n\t actual: %v\n\t got diff: %v",
125125
bts,
126126
newbts,
127+
diff,
127128
)
128129
}
129130
}

bootstrap/kubeadm/api/v1beta1/kubeadm_types_test.go

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

1919
import (
2020
"encoding/json"
21-
"reflect"
2221
"testing"
2322

23+
"github.com/google/go-cmp/cmp"
2424
. "github.com/onsi/gomega"
2525
"github.com/pkg/errors"
2626
corev1 "k8s.io/api/core/v1"
@@ -177,11 +177,12 @@ func roundtrip(input string, bts *BootstrapTokenString) error {
177177
if err := json.Unmarshal(b, newbts); err != nil {
178178
return errors.Wrap(err, "expected no unmarshal error, got error")
179179
}
180-
if !reflect.DeepEqual(bts, newbts) {
180+
if diff := cmp.Diff(bts, newbts); diff != "" {
181181
return errors.Errorf(
182-
"expected object: %v\n\t actual: %v",
182+
"expected object: %v\n\t actual: %v\n\t got diff: %v",
183183
bts,
184184
newbts,
185+
diff,
185186
)
186187
}
187188
}

bootstrap/kubeadm/types/upstreamv1beta1/bootstraptokenstring_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ package upstreamv1beta1
1818

1919
import (
2020
"encoding/json"
21-
"reflect"
2221
"testing"
2322

23+
"github.com/google/go-cmp/cmp"
2424
. "github.com/onsi/gomega"
2525
"github.com/pkg/errors"
2626
)
@@ -119,11 +119,12 @@ func roundtrip(input string, bts *BootstrapTokenString) error {
119119
if err := json.Unmarshal(b, newbts); err != nil {
120120
return errors.Wrap(err, "expected no unmarshal error, got error")
121121
}
122-
if !reflect.DeepEqual(bts, newbts) {
122+
if diff := cmp.Diff(bts, newbts); diff != "" {
123123
return errors.Errorf(
124-
"expected object: %v\n\t actual: %v",
124+
"expected object: %v\n\t actual: %v\n\t got diff: %v",
125125
bts,
126126
newbts,
127+
diff,
127128
)
128129
}
129130
}

bootstrap/kubeadm/types/upstreamv1beta2/bootstraptokenstring_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ package upstreamv1beta2
1818

1919
import (
2020
"encoding/json"
21-
"reflect"
2221
"testing"
2322

23+
"github.com/google/go-cmp/cmp"
2424
. "github.com/onsi/gomega"
2525
"github.com/pkg/errors"
2626
)
@@ -119,11 +119,12 @@ func roundtrip(input string, bts *BootstrapTokenString) error {
119119
if err := json.Unmarshal(b, newbts); err != nil {
120120
return errors.Wrap(err, "expected no unmarshal error, got error")
121121
}
122-
if !reflect.DeepEqual(bts, newbts) {
122+
if diff := cmp.Diff(bts, newbts); diff != "" {
123123
return errors.Errorf(
124-
"expected object: %v\n\t actual: %v",
124+
"expected object: %v\n\t actual: %v\n\t got diff: %v",
125125
bts,
126126
newbts,
127+
diff,
127128
)
128129
}
129130
}

bootstrap/kubeadm/types/upstreamv1beta3/bootstraptokenstring_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ package upstreamv1beta3
1818

1919
import (
2020
"encoding/json"
21-
"reflect"
2221
"testing"
2322

23+
"github.com/google/go-cmp/cmp"
2424
"github.com/pkg/errors"
2525
)
2626

@@ -71,11 +71,12 @@ func TestUnmarshalJSON(t *testing.T) {
7171
err := json.Unmarshal([]byte(rt.input), newbts)
7272
if (err != nil) != rt.expectedError {
7373
t.Errorf("failed BootstrapTokenString.UnmarshalJSON:\n\texpected error: %t\n\t actual error: %v", rt.expectedError, err)
74-
} else if !reflect.DeepEqual(rt.bts, newbts) {
74+
} else if diff := cmp.Diff(rt.bts, newbts); diff != "" {
7575
t.Errorf(
76-
"failed BootstrapTokenString.UnmarshalJSON:\n\texpected: %v\n\t actual: %v",
76+
"failed BootstrapTokenString.UnmarshalJSON:\n\texpected: %v\n\t actual: %v\n\t diff: %v",
7777
rt.bts,
7878
newbts,
79+
diff,
7980
)
8081
}
8182
})
@@ -125,11 +126,12 @@ func roundtrip(input string, bts *BootstrapTokenString) error {
125126
if err := json.Unmarshal(b, newbts); err != nil {
126127
return errors.Wrap(err, "expected no unmarshal error, got error")
127128
}
128-
if !reflect.DeepEqual(bts, newbts) {
129+
if diff := cmp.Diff(bts, newbts); diff != "" {
129130
return errors.Errorf(
130-
"expected object: %v\n\t actual: %v",
131+
"expected object: %v\n\t actual: %v\n\t got diff: %v",
131132
bts,
132133
newbts,
134+
diff,
133135
)
134136
}
135137
}
@@ -192,12 +194,13 @@ func TestNewBootstrapTokenString(t *testing.T) {
192194
rt.expectedError,
193195
err,
194196
)
195-
} else if !reflect.DeepEqual(actual, rt.bts) {
197+
} else if diff := cmp.Diff(actual, rt.bts); diff != "" {
196198
t.Errorf(
197-
"failed NewBootstrapTokenString for the token %q\n\texpected: %v\n\t actual: %v",
199+
"failed NewBootstrapTokenString for the token %q\n\texpected: %v\n\t actual: %v\n\t diff: %v",
198200
rt.token,
199201
rt.bts,
200202
actual,
203+
diff,
201204
)
202205
}
203206
})
@@ -235,13 +238,14 @@ func TestNewBootstrapTokenStringFromIDAndSecret(t *testing.T) {
235238
rt.expectedError,
236239
err,
237240
)
238-
} else if !reflect.DeepEqual(actual, rt.bts) {
241+
} else if diff := cmp.Diff(actual, rt.bts); diff != "" {
239242
t.Errorf(
240-
"failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected: %v\n\t actual: %v",
243+
"failed NewBootstrapTokenStringFromIDAndSecret for the token with id %q and secret %q\n\texpected: %v\n\t actual: %v\n\t diff: %v",
241244
rt.id,
242245
rt.secret,
243246
rt.bts,
244247
actual,
248+
diff,
245249
)
246250
}
247251
})

0 commit comments

Comments
 (0)