8000 Make the resource comparison more informative in case of an error (#2… · nginx/kubernetes-ingress@98239fc · GitHub
[go: up one dir, main page]

Skip to content

Commit 98239fc

Browse files
authored
Make the resource comparison more informative in case of an error (#2273)
1 parent c4a38b1 commit 98239fc

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

internal/k8s/handlers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,12 @@ func areResourcesDifferent(oldresource, resource *unstructured.Unstructured) (bo
533533
return false, err
534534
}
535535
spec, found, err := unstructured.NestedMap(resource.Object, "spec")
536-
if !found {
537-
return false, fmt.Errorf("Error, spec has unexpected format")
538-
}
539536
if err != nil {
540537
return false, err
541538
}
539+
if !found {
540+
return false, fmt.Errorf("Error, spec has unexpected format")
541+
}
542542
eq := reflect.DeepEqual(oldSpec, spec)
543543
if eq {
544544
glog.V(3).Infof("New spec of %v same as old spec", oldresource.GetName())

internal/k8s/handlers_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package k8s
22

33
import (
4+
"errors"
45
"testing"
56

67
v1 "k8s.io/api/core/v1"
@@ -158,9 +159,10 @@ func TestHasServicePortChanges(t *testing.T) {
158159

159160
func TestAreResourcesDifferent(t *testing.T) {
160161
tests := []struct {
161-
oldR, newR *unstructured.Unstructured
162-
expected, expectErr bool
163-
msg string
162+
oldR, newR *unstructured.Unstructured
163+
expected bool
164+
expectErr error
165+
msg string
164166
}{
165167
{
166168
oldR: &unstructured.Unstructured{
@@ -174,7 +176,7 @@ func TestAreResourcesDifferent(t *testing.T) {
174176
},
175177
},
176178
expected: false,
177-
expectErr: true,
179+
expectErr: errors.New(`.spec accessor error: true is of the type bool, expected map[string]interface{}`),
178180
msg: "invalid old resource",
179181
},
180182
{
@@ -189,7 +191,7 @@ func TestAreResourcesDifferent(t *testing.T) {
189191
},
190192
},
191193
expected: false,
192-
expectErr: true,
194+
expectErr: errors.New(`.spec accessor error: true is of the type bool, expected map[string]interface{}`),
193195
msg: "invalid new resource",
194196
},
195197
{
@@ -202,7 +204,7 @@ func TestAreResourcesDifferent(t *testing.T) {
202204
Object: map[string]interface{}{},
203205
},
204206
expected: false,
205-
expectErr: true,
207+
expectErr: errors.New(`Error, spec has unexpected format`),
206208
msg: "new resource with missing spec",
207209
},
208210
{
@@ -221,7 +223,7 @@ func TestAreResourcesDifferent(t *testing.T) {
221223
},
222224
},
223225
expected: false,
224-
expectErr: false,
226+
expectErr: nil,
225227
msg: "equal resources",
226228
},
227229
{
@@ -240,7 +242,7 @@ func TestAreResourcesDifferent(t *testing.T) {
240242
},
241243
},
242244
expected: true,
243-
expectErr: false,
245+
expectErr: nil,
244246
msg: "not equal resources",
245247
},
246248
{
@@ -255,7 +257,7 @@ func TestAreResourcesDifferent(t *testing.T) {
255257
},
256258
},
257259
expected: true,
258-
expectErr: false,
260+
expectErr: nil,
259261
msg: "not equal resources with with first resource missing spec",
260262
},
261263
}
@@ -265,10 +267,14 @@ func TestAreResourcesDifferent(t *testing.T) {
265267
if result != test.expected {
266268
t.Errorf("areResourcesDifferent() returned %v but expected %v for the case of %s", result, test.expected, test.msg)
267269
}
268-
if test.expectErr && err == nil {
269-
t.Errorf("areResourcesDifferent() returned no error for the case of %s", test.msg)
270+
if test.expectErr != nil {
271+
if err == nil {
272+
t.Errorf("areResourcesDifferent() returned no error for the case of %s", test.msg)
273+
} else if test.expectErr.Error() != err.Error() {
274+
t.Errorf("areResourcesDifferent() returned an unexpected error '%v' for the case of %s", err, test.msg)
275+
}
270276
}
271-
if !test.expectErr && err != nil {
277+
if test.expectErr == nil && err != nil {
272278
t.Errorf("areResourcesDifferent() returned unexpected error %v for the case of %s", err, test.msg)
273279
}
274280
}

0 commit comments

Comments
 (0)
0