Skip to content

Commit 6eb8a16

Browse files
n3wscottarschles
authored andcommitted
Apply Event Message Builder controller_broker unit tests (openshift#1497)
* Refactor how we create unit test expected event messages. * Fixing unit tests and some bugs in the message builder. * Expand the comments for MessageBuilder. * Reverting the helper methods in the helper builder, now more clean. * Adding a helper method to shortcut some of the construction of the message builder. * Updating controller_broker to use message builder for unit tests. * Use expectedGot.
1 parent 411831c commit 6eb8a16

File tree

2 files changed

+82
-74
lines changed

2 files changed

+82
-74
lines changed

Diff for: pkg/controller/controller_binding_test.go

+28-32
Original file line numberDiff line numberDiff line change
@@ -926,11 +926,11 @@ func TestReconcileServiceBindingDelete(t *testing.T) {
926926

927927
deleteAction := kubeActions[0].(clientgotesting.DeleteActionImpl)
928928
if e, a := "delete", deleteAction.GetVerb(); e != a {
929-
t.Fatalf("Unexpected verb on kubeActions[1]; expected %v, got %v", e, a)
929+
t.Fatalf("Unexpected verb on kubeActions[1]; %s", expectedGot(e, a))
930930
}
931931

932932
if e, a := binding.Spec.SecretName, deleteAction.Name; e != a {
933-
t.Fatalf("Unexpected name of secret: expected %v, got %v", e, a)
933+
t.Fatalf("Unexpected name of secret: %s", expectedGot(e, a))
934934
}
935935

936936
actions := fakeCatalogClient.Actions()
@@ -1142,7 +1142,7 @@ func TestReconcileServiceBindingDeleteFailedServiceBinding(t *testing.T) {
11421142

11431143
deleteAction := kubeActions[0].(clientgotesting.DeleteActionImpl)
11441144
if e, a := binding.Spec.SecretName, deleteAction.Name; e != a {
1145-
t.Fatalf("Unexpected name of secret: expected %v, got %v", e, a)
1145+
t.Fatalf("Unexpected name of secret: %s", expectedGot(e, a))
11461146
}
11471147

11481148
actions := fakeCatalogClient.Actions()
@@ -1555,7 +1555,7 @@ func TestUpdateServiceBindingCondition(t *testing.T) {
15551555
}
15561556

15571557
if !reflect.DeepEqual(tc.input, inputClone) {
1558-
t.Errorf("%v: updating broker condition mutated input: expected %v, got %v", tc.name, inputClone, tc.input)
1558+
t.Errorf("%v: updating broker condition mutated input: %s", tc.name, expectedGot(inputClone, tc.input))
15591559
continue
15601560
}
15611561

@@ -1581,7 +1581,7 @@ func TestUpdateServiceBindingCondition(t *testing.T) {
15811581
}
15821582

15831583
if e, a := 1, len(updateActionObject.Status.Conditions); e != a {
1584-
t.Errorf("%v: expected %v condition(s), got %v", tc.name, e, a)
1584+
t.Errorf("%v: %s", tc.name, expectedGot(e, a))
15851585
}
15861586

15871587
outputCondition := updateActionObject.Status.Conditions[0]
@@ -1595,11 +1595,11 @@ func TestUpdateServiceBindingCondition(t *testing.T) {
15951595
continue
15961596
}
15971597
if e, a := tc.reason, outputCondition.Reason; e != "" && e != a {
1598-
t.Errorf("%v: condition reasons didn't match; expected %v, got %v", tc.name, e, a)
1598+
t.Errorf("%v: condition reasons didn't match; %s", tc.name, expectedGot(e, a))
15991599
continue
16001600
}
16011601
if e, a := tc.message, outputCondition.Message; e != "" && e != a {
1602-
t.Errorf("%v: condition reasons didn't match; expected %v, got %v", tc.name, e, a)
1602+
t.Errorf("%v: condition reasons didn't match; %s", tc.name, expectedGot(e, a))
16031603
}
16041604
}
16051605
}
@@ -2013,10 +2013,10 @@ func TestReconcileBindingWithSecretConflictFailedAfterFinalRetry(t *testing.T) {
20132013
// second action is a get on the secret
20142014
action := kubeActions[1].(clientgotesting.GetAction)
20152015
if e, a := "get", action.GetVerb(); e != a {
2016-
t.Fatalf("Unexpected verb on action; expected %v, got %v", e, a)
2016+
t.Fatalf("Unexpected verb on action; %s", expectedGot(e, a))
20172017
}
20182018
if e, a := "secrets", action.GetResource().Resource; e != a {
2019-
t.Fatalf("Unexpected resource on action; expected %v, got %v", e, a)
2019+
t.Fatalf("Unexpected resource on action; %s", expectedGot(e, a))
20202020
}
20212021

20222022
events := getRecordedEvents(testController)
@@ -2057,7 +2057,7 @@ func TestReconcileServiceBindingWithStatusUpdateError(t *testing.T) {
20572057
t.Fatalf("expected error from but got none")
20582058
}
20592059
if e, a := "update error", err.Error(); e != a {
2060-
t.Fatalf("unexpected error returned: expected %q, got %q", e, a)
2060+
t.Fatalf("unexpected error returned: %s", expectedGot(e, a))
20612061
}
20622062

20632063
brokerActions := fakeClusterServiceBrokerClient.Actions()
@@ -2195,10 +2195,10 @@ func TestReconcileServiceBindingWithSecretParameters(t *testing.T) {
21952195
t.Fatalf("unexpected type of action: expected a GetAction, got %T", kubeActions[0])
21962196
}
21972197
if e, a := "secrets", action.GetResource().Resource; e != a {
2198-
t.Fatalf("Unexpected resource on action: expected %q, got %q", e, a)
2198+
t.Fatalf("Unexpected resource on action: %s", expectedGot(e, a))
21992199
}
22002200
if e, a := "param-secret-name", action.GetName(); e != a {
2201-
t.Fatalf("Unexpected name of secret fetched: expected %q, got %q", e, a)
2201+
t.Fatalf("Unexpected name of secret fetched: %s", expectedGot(e, a))
22022202
}
22032203

22042204
events := getRecordedEvents(testController)
@@ -2334,10 +2334,10 @@ func TestReconcileBindingWithSetOrphanMitigation(t *testing.T) {
23342334
assertNumberOfActions(t, kubeActions, 1)
23352335
action := kubeActions[0].(clientgotesting.GetAction)
23362336
if e, a := "get", action.GetVerb(); e != a {
2337-
t.Fatalf("Unexpected verb on action; expected %v, got %v", e, a)
2337+
t.Fatalf("Unexpected verb on action; %s", expectedGot(e, a))
23382338
}
23392339
if e, a := "namespaces", action.GetResource().Resource; e != a {
2340-
t.Fatalf("Unexpected resource on action; expected %v, got %v", e, a)
2340+
t.Fatalf("Unexpected resource on action; %s", expectedGot(e, a))
23412341
}
23422342

23432343
actions := fakeCatalogClient.Actions()
@@ -2402,10 +2402,10 @@ func TestReconcileBindingWithOrphanMitigationInProgress(t *testing.T) {
24022402
assertNumberOfActions(t, kubeActions, 1)
24032403
action := kubeActions[0].(clientgotesting.GetAction)
24042404
if e, a := "delete", action.GetVerb(); e != a {
2405-
t.Fatalf("Unexpected verb on action; expected %v, got %v", e, a)
2405+
t.Fatalf("Unexpected verb on action; %s", expectedGot(e, a))
24062406
}
24072407
if e, a := "secrets", action.GetResource().Resource; e != a {
2408-
t.Fatalf("Unexpected resource on action; expected %v, got %v", e, a)
2408+
t.Fatalf("Unexpected resource on action; %s", expectedGot(e, a))
24092409
}
24102410

24112411
brokerActions := fakeServiceBrokerClient.Actions()
@@ -2482,10 +2482,10 @@ func TestReconcileBindingWithOrphanMitigationReconciliationRetryTimeOut(t *testi
24822482
assertNumberOfActions(t, kubeActions, 1)
24832483
action := kubeActions[0].(clientgotesting.GetAction)
24842484
if e, a := "delete", action.GetVerb(); e != a {
2485-
t.Fatalf("Unexpected verb on action; expected %v, got %v", e, a)
2485+
t.Fatalf("Unexpected verb on action; %s", expectedGot(e, a))
24862486
}
24872487
if e, a := "secrets", action.GetResource().Resource; e != a {
2488-
t.Fatalf("Unexpected resource on action; expected %v, got %v", e, a)
2488+
t.Fatalf("Unexpected resource on action; %s", expectedGot(e, a))
24892489
}
24902490

24912491
brokerActions := fakeServiceBrokerClient.Actions()
@@ -2504,18 +2504,15 @@ func TestReconcileBindingWithOrphanMitigationReconciliationRetryTimeOut(t *testi
25042504
assertServiceBindingRequestFailingError(t, updatedServiceBinding, v1beta1.ServiceBindingOperationUnbind, errorUnbindCallReason, "reason-orphan-mitigation-began", binding)
25052505
assertServiceBindingOrphanMitigationSet(t, updatedServiceBinding, false)
25062506

2507+
events := getRecordedEvents(testController)
2508+
25072509
expectedEventPrefixes := []string{
2508-
corev1.EventTypeWarning + " " + errorUnbindCallReason,
2509-
corev1.EventTypeWarning + " " + errorReconciliationRetryTimeoutReason,
2510+
warningEventBuilder(errorUnbindCallReason).String(),
2511+
warningEventBuilder(errorReconciliationRetryTimeoutReason).String(),
25102512
}
2511-
events := getRecordedEvents(testController)
2512-
assertNumEvents(t, events, len(expectedEventPrefixes))
25132513

2514-
for i, e := range expectedEventPrefixes {
2515-
a := events[i]
2516-
if !strings.HasPrefix(a, e) {
2517-
t.Fatalf("Received unexpected event:\n expected prefix: %v\n got: %v", e, a)
2518-
}
2514+
if err := checkEventPrefixes(events, expectedEventPrefixes); err != nil {
2515+
t.Fatal(err)
25192516
}
25202517
}
25212518

@@ -2576,11 +2573,11 @@ func TestReconcileServiceBindingDeleteDuringOngoingOperation(t *testing.T) {
25762573

25772574
deleteAction := kubeActions[0].(clientgotesting.DeleteActionImpl)
25782575
if e, a := "delete", deleteAction.GetVerb(); e != a {
2579-
t.Fatalf("Unexpected verb on kubeActions[1]; expected %v, got %v", e, a)
2576+
t.Fatalf("Unexpected verb on kubeActions[1]; %s", expectedGot(e, a))
25802577
}
25812578

25822579
if e, a := binding.Spec.SecretName, deleteAction.Name; e != a {
2583-
t.Fatalf("Unexpected name of secret: expected %v, got %v", e, a)
2580+
t.Fatalf("Unexpected name of secret: %s", expectedGot(e, a))
25842581
}
25852582

25862583
actions := fakeCatalogClient.Actions()
@@ -2672,11 +2669,11 @@ func TestReconcileServiceBindingDeleteDuringOrphanMitigation(t *testing.T) {
26722669

26732670
deleteAction := kubeActions[0].(clientgotesting.DeleteActionImpl)
26742671
if e, a := "delete", deleteAction.GetVerb(); e != a {
2675-
t.Fatalf("Unexpected verb on kubeActions[1]; expected %v, got %v", e, a)
2672+
t.Fatalf("Unexpected verb on kubeActions[1]; %s", expectedGot(e, a))
26762673
}
26772674

26782675
if e, a := binding.Spec.SecretName, deleteAction.Name; e != a {
2679-
t.Fatalf("Unexpected name of secret: expected %v, got %v", e, a)
2676+
t.Fatalf("Unexpected name of secret: %s", expectedGot(e, a))
26802677
}
26812678

26822679
actions := fakeCatalogClient.Actions()
@@ -2703,7 +2700,6 @@ func TestReconcileServiceBindingDeleteDuringOrphanMitigation(t *testing.T) {
27032700
assertServiceBindingOrphanMitigationSet(t, updatedServiceBinding, false)
27042701

27052702
events := getRecordedEvents(testController)
2706-
assertNumEvents(t, events, 1)
27072703

27082704
expectedEvent := normalEventBuilder(successUnboundReason).msg("This binding was deleted successfully")
27092705
if err := checkEvents(events, expectedEvent.stringArr()); err != nil {

0 commit comments

Comments
 (0)