Skip to content

Commit 0885edb

Browse files
n3wscottpmorie
authored andcommitted
Adding expectedGot function and using it. (#1440)
1 parent a7d582e commit 0885edb

File tree

2 files changed

+64
-30
lines changed

2 files changed

+64
-30
lines changed

pkg/controller/controller_binding_test.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ func TestReconcileServiceBindingNonExistingServiceInstance(t *testing.T) {
8383
assertNumEvents(t, events, 1)
8484

8585
expectedEvent := corev1.EventTypeWarning + " " + errorNonexistentServiceInstanceReason + " " + "References a non-existent ServiceInstance \"/nothere\""
86-
if e, a := expectedEvent, events[0]; e != a {
87-
t.Fatalf("Received unexpected event: %v", a)
86+
if err := checkEvents(events, []string{expectedEvent}); err != nil {
87+
t.Fatal(err)
8888
}
8989
}
9090

@@ -125,7 +125,7 @@ func TestReconcileServiceBindingUnresolvedClusterServiceClassReference(t *testin
125125
t.Fatal("serviceclassref was nil and reconcile should return an error")
126126
}
127127
if !strings.Contains(err.Error(), "not been resolved yet") {
128-
t.Fatalf("Did not get the expected error %q : got %q", "not been resolved yet", err)
128+
t.Fatalf("Did not get the expected error: %s", expectedGot("not been resolved yet", err))
129129
}
130130

131131
brokerActions := fakeClusterServiceBrokerClient.Actions()
@@ -174,8 +174,8 @@ func TestReconcileServiceBindingUnresolvedClusterServicePlanReference(t *testing
174174
t.Fatal("serviceclass nothere was found and it should not be found")
175175
}
176176

177-
if !strings.Contains(err.Error(), "not been resolved yet") {
178-
t.Fatalf("Did not get the expected error %q : got %q", "not been resolved yet", err)
177+
if err := checkEventContains(err.Error(), "not been resolved yet"); err != nil {
178+
t.Fatal(err)
179179
}
180180

181181
brokerActions := fakeClusterServiceBrokerClient.Actions()
@@ -242,8 +242,8 @@ func TestReconcileServiceBindingNonExistingClusterServiceClass(t *testing.T) {
242242
assertNumEvents(t, events, 1)
243243

244244
expectedEvent := corev1.EventTypeWarning + " " + errorNonexistentClusterServiceClassMessage + " " + "References a non-existent ClusterServiceClass (K8S: \"nosuchclassid\" ExternalName: \"" + testNonExistentClusterServiceClassName + "\")"
245-
if e, a := expectedEvent, events[0]; e != a {
246-
t.Fatalf("Received unexpected event expected: %v got: %v", e, a)
245+
if err := checkEvents(events, []string{expectedEvent}); err != nil {
246+
t.Fatal(err)
247247
}
248248
}
249249

@@ -327,18 +327,19 @@ func TestReconcileServiceBindingWithSecretConflict(t *testing.T) {
327327
// second action is a get on the secret
328328
action := kubeActions[1].(clientgotesting.GetAction)
329329
if e, a := "get", action.GetVerb(); e != a {
330-
t.Fatalf("Unexpected verb on action; expected %v, got %v", e, a)
330+
t.Fatalf("Unexpected verb on action; %s", expectedGot(e, a))
331331
}
332332
if e, a := "secrets", action.GetResource().Resource; e != a {
333-
t.Fatalf("Unexpected resource on action; expected %v, got %v", e, a)
333+
t.Fatalf("Unexpected resource on action; %s", expectedGot(e, a))
334334
}
335335

336336
events := getRecordedEvents(testController)
337337
assertNumEvents(t, events, 1)
338338

339339
expectedEvent := corev1.EventTypeWarning + " " + errorInjectingBindResultReason
340-
if e, a := expectedEvent, events[0]; !strings.HasPrefix(a, e) {
341-
t.Fatalf("Received unexpected event: %v", a)
340+
341+
if err := checkEventPrefixes(events, []string{expectedEvent}); err != nil {
342+
t.Fatal(err)
342343
}
343344
}
344345

@@ -441,10 +442,10 @@ func TestReconcileServiceBindingWithParameters(t *testing.T) {
441442
// second action is a get on the secret
442443
action := kubeActions[2].(clientgotesting.CreateAction)
443444
if e, a := "create", action.GetVerb(); e != a {
444-
t.Fatalf("Unexpected verb on action; expected %v, got %v", e, a)
445+
t.Fatalf("Unexpected verb on action; %s", expectedGot(e, a))
445446
}
446447
if e, a := "secrets", action.GetResource().Resource; e != a {
447-
t.Fatalf("Unexpected resource on action; expected %v, got %v", e, a)
448+
t.Fatalf("Unexpected resource on action; %s", expectedGot(e, a))
448449
}
449450
actionSecret, ok := action.GetObject().(*corev1.Secret)
450451
if !ok {
@@ -458,29 +459,29 @@ func TestReconcileServiceBindingWithParameters(t *testing.T) {
458459
t.Fatal("Secret is not owned by the ServiceBinding")
459460
}
460461
if e, a := testServiceBindingSecretName, actionSecret.Name; e != a {
461-
t.Fatalf("Unexpected name of secret; expected %v, got %v", e, a)
462+
t.Fatalf("Unexpected name of secret; %s", expectedGot(e, a))
462463
}
463464
value, ok := actionSecret.Data["a"]
464465
if !ok {
465466
t.Fatal("Didn't find secret key 'a' in created secret")
466467
}
467468
if e, a := "b", string(value); e != a {
468-
t.Fatalf("Unexpected value of key 'a' in created secret; expected %v got %v", e, a)
469+
t.Fatalf("Unexpected value of key 'a' in created secret; %s", expectedGot(e, a))
469470
}
470471
value, ok = actionSecret.Data["c"]
471472
if !ok {
472473
t.Fatal("Didn't find secret key 'a' in created secret")
473474
}
474475
if e, a := "d", string(value); e != a {
475-
t.Fatalf("Unexpected value of key 'c' in created secret; expected %v got %v", e, a)
476+
t.Fatalf("Unexpected value of key 'c' in created secret; %s", expectedGot(e, a))
476477
}
477478

478479
events := getRecordedEvents(testController)
479480
assertNumEvents(t, events, 1)
480481

481482
expectedEvent := corev1.EventTypeNormal + " " + successInjectedBindResultReason + " " + successInjectedBindResultMessage
482-
if e, a := expectedEvent, events[0]; e != a {
483-
t.Fatalf("Received unexpected event: %v", a)
483+
if err := checkEvents(events, []string{expectedEvent}); err != nil {
484+
t.Fatal(err)
484485
}
485486
}
486487

@@ -527,8 +528,8 @@ func TestReconcileServiceBindingNonbindableClusterServiceClass(t *testing.T) {
527528
assertNumEvents(t, events, 1)
528529

529530
expectedEvent := corev1.EventTypeWarning + " " + errorNonbindableClusterServiceClassReason + ` References a non-bindable ClusterServiceClass (K8S: "UNBINDABLE-SERVICE" ExternalName: "test-unbindable-serviceclass") and Plan ("test-unbindable-plan") combination`
530-
if e, a := expectedEvent, events[0]; e != a {
531-
t.Fatalf("Received unexpected event: %v", a)
531+
if err := checkEvents(events, []string{expectedEvent}); err != nil {
532+
t.Fatal(err)
532533
}
533534
}
534535

@@ -615,31 +616,31 @@ func TestReconcileServiceBindingNonbindableClusterServiceClassBindablePlan(t *te
615616
// second action is a get on the secret
616617
action := kubeActions[2].(clientgotesting.CreateAction)
617618
if e, a := "create", action.GetVerb(); e != a {
618-
t.Fatalf("Unexpected verb on action; expected %v, got %v", e, a)
619+
t.Fatalf("Unexpected verb on action; %s", expectedGot(e, a))
619620
}
620621
if e, a := "secrets", action.GetResource().Resource; e != a {
621-
t.Fatalf("Unexpected resource on action; expected %v, got %v", e, a)
622+
t.Fatalf("Unexpected resource on action; %s", expectedGot(e, a))
622623
}
623624
actionSecret, ok := action.GetObject().(*corev1.Secret)
624625
if !ok {
625626
t.Fatal("couldn't convert secret into a corev1.Secret")
626627
}
627628
if e, a := testServiceBindingSecretName, actionSecret.Name; e != a {
628-
t.Fatalf("Unexpected name of secret; expected %v, got %v", e, a)
629+
t.Fatalf("Unexpected name of secret; %s", expectedGot(e, a))
629630
}
630631
value, ok := actionSecret.Data["a"]
631632
if !ok {
632633
t.Fatal("Didn't find secret key 'a' in created secret")
633634
}
634635
if e, a := "b", string(value); e != a {
635-
t.Fatalf("Unexpected value of key 'a' in created secret; expected %v got %v", e, a)
636+
t.Fatalf("Unexpected value of key 'a' in created secret; %s", expectedGot(e, a))
636637
}
637638
value, ok = actionSecret.Data["c"]
638639
if !ok {
639640
t.Fatal("Didn't find secret key 'a' in created secret")
640641
}
641642
if e, a := "d", string(value); e != a {
642-
t.Fatalf("Unexpected value of key 'c' in created secret; expected %v got %v", e, a)
643+
t.Fatalf("Unexpected value of key 'c' in created secret; %s", expectedGot(e, a))
643644
}
644645

645646
events := getRecordedEvents(testController)
@@ -689,8 +690,8 @@ func TestReconcileServiceBindingBindableClusterServiceClassNonbindablePlan(t *te
689690
assertNumEvents(t, events, 1)
690691

691692
expectedEvent := corev1.EventTypeWarning + " " + errorNonbindableClusterServiceClassReason + ` References a non-bindable ClusterServiceClass (K8S: "SCGUID" ExternalName: "test-serviceclass") and Plan ("test-unbindable-plan") combination`
692-
if e, a := expectedEvent, events[0]; e != a {
693-
t.Fatalf("Received unexpected event: %v", a)
693+
if err := checkEvents(events, []string{expectedEvent}); err != nil {
694+
t.Fatal(err)
694695
}
695696
}
696697

@@ -722,8 +723,8 @@ func TestReconcileServiceBindingFailsWithServiceInstanceAsyncOngoing(t *testing.
722723
t.Fatalf("reconcileServiceBinding did not fail with async operation ongoing")
723724
}
724725

725-
if !strings.Contains(err.Error(), "Ongoing Asynchronous") {
726-
t.Fatalf("Did not get the expected error %q : got %q", "Ongoing Asynchronous", err)
726+
if err := checkEventContains(err.Error(), "Ongoing Asynchronous"); err != nil {
727+
t.Fatal(err)
727728
}
728729

729730
brokerActions := fakeClusterServiceBrokerClient.Actions()

pkg/controller/controller_events_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,49 @@ package controller
1818

1919
import (
2020
"fmt"
21+
"strings"
2122
)
2223

23-
func checkEvents(actual, expected []string) error {
24+
func checkEventCounts(actual, expected []string) error {
2425
if len(actual) != len(expected) {
2526
return fmt.Errorf("expected %d events, got %d", len(expected), len(actual))
2627
}
28+
return nil
29+
}
30+
31+
func checkEvents(actual, expected []string) error {
32+
if err := checkEventCounts(actual, expected); err != nil {
33+
return err
34+
}
2735
for i, actualEvt := range actual {
2836
if expectedEvt := expected[i]; actualEvt != expectedEvt {
2937
return fmt.Errorf("event %d: expected '%s', got '%s'", i, expectedEvt, actualEvt)
3038
}
3139
}
3240
return nil
3341
}
42+
43+
func checkEventPrefixes(actual, expected []string) error {
44+
if err := checkEventCounts(actual, expected); err != nil {
45+
return err
46+
}
47+
for i, e := range expected {
48+
a := actual[i]
49+
if !strings.HasPrefix(a, e) {
50+
return fmt.Errorf("received unexpected event prefix:\n %s", expectedGot(e, a))
51+
}
52+
}
53+
return nil
54+
}
55+
56+
func checkEventContains(actual, expected string) error {
57+
if !strings.Contains(actual, expected) {
58+
return fmt.Errorf("received unexpected event (contains):\n %s", expectedGot(expected, actual))
59+
}
60+
61+
return nil
62+
}
63+
64+
func expectedGot(a ...interface{}) string {
65+
return fmt.Sprintf("\nexpected:\n\t '%v',\ngot:\n\t '%v'", a...)
66+
}

0 commit comments

Comments
 (0)