Skip to content

Commit 65de49c

Browse files
Jeff Peelerpmorie
authored andcommitted
Comments for unit test bullet proofing (#1139)
* Broker binding tests cleanup Comments every test and documents various testing combinations when present. (#1077) * Modify test helper to be more clear getTestInstanceNonbindableServiceBindablePlan has been rewritten to call getTestInstance directly rather than calling another helper function to do so. This is a small change, but I think it more readable and consistent with nearby helper functions.
1 parent f32eec2 commit 65de49c

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

pkg/controller/controller_binding.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,14 @@ func (c *controller) ejectBinding(binding *v1alpha1.Binding) error {
528528
return nil
529529
}
530530

531+
// setBindingCondition sets a single condition on a Binding's status: if
532+
// the condition already exists in the status, it is mutated; if the condition
533+
// does not already exist in the status, it is added. Other conditions in the
534+
// status are not altered. If the condition exists and its status changes,
535+
// the LastTransitionTime field is updated.
536+
//
537+
// Note: objects coming from informers should never be mutated; always pass a
538+
// deep copy as the binding parameter.
531539
func (c *controller) setBindingCondition(toUpdate *v1alpha1.Binding,
532540
conditionType v1alpha1.BindingConditionType,
533541
status v1alpha1.ConditionStatus,
@@ -536,6 +544,8 @@ func (c *controller) setBindingCondition(toUpdate *v1alpha1.Binding,
536544
setBindingConditionInternal(toUpdate, conditionType, status, reason, message, metav1.Now())
537545
}
538546

547+
// setBindingConditionInternal is setBindingCondition but allows the time to
548+
// be parameterized for testing.
539549
func setBindingConditionInternal(toUpdate *v1alpha1.Binding,
540550
conditionType v1alpha1.BindingConditionType,
541551
status v1alpha1.ConditionStatus,

pkg/controller/controller_binding_test.go

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import (
4040
clientgotesting "k8s.io/client-go/testing"
4141
)
4242

43+
// TestReconcileBindingNonExistingInstance tests reconcileBinding to ensure a
44+
// binding fails as expected when an instance to bind to doesn't exist.
4345
func TestReconcileBindingNonExistingInstance(t *testing.T) {
4446
_, fakeCatalogClient, fakeBrokerClient, testController, _ := newTestController(t, noFakeActions())
4547

@@ -79,6 +81,8 @@ func TestReconcileBindingNonExistingInstance(t *testing.T) {
7981
}
8082
}
8183

84+
// TestReconcileBindingNonExistingServiceClass tests reconcileBinding to ensure a
85+
// binding fails as expected when a serviceclass does not exist.
8286
func TestReconcileBindingNonExistingServiceClass(t *testing.T) {
8387
_, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, noFakeActions())
8488

@@ -126,6 +130,8 @@ func TestReconcileBindingNonExistingServiceClass(t *testing.T) {
126130
}
127131
}
128132

133+
// TestReconcileBindingWithSecretConflict tests reconcileBinding to ensure a
134+
// binding with an existing secret not owned by the bindings fails as expected.
129135
func TestReconcileBindingWithSecretConflict(t *testing.T) {
130136
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, fakeosb.FakeClientConfiguration{
131137
BindReaction: &fakeosb.BindReaction{
@@ -185,7 +191,6 @@ func TestReconcileBindingWithSecretConflict(t *testing.T) {
185191

186192
// first action is a get on the namespace
187193
// second action is a get on the secret
188-
189194
action := kubeActions[1].(clientgotesting.GetAction)
190195
if e, a := "get", action.GetVerb(); e != a {
191196
t.Fatalf("Unexpected verb on action; expected %v, got %v", e, a)
@@ -203,6 +208,8 @@ func TestReconcileBindingWithSecretConflict(t *testing.T) {
203208
}
204209
}
205210

211+
// TestReconcileBindingWithParameters tests reconcileBinding to ensure a
212+
// binding with parameters will be passed to the broker properly.
206213
func TestReconcileBindingWithParameters(t *testing.T) {
207214
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, fakeosb.FakeClientConfiguration{
208215
BindReaction: &fakeosb.BindReaction{
@@ -275,7 +282,6 @@ func TestReconcileBindingWithParameters(t *testing.T) {
275282

276283
// first action is a get on the namespace
277284
// second action is a get on the secret
278-
279285
action := kubeActions[2].(clientgotesting.CreateAction)
280286
if e, a := "create", action.GetVerb(); e != a {
281287
t.Fatalf("Unexpected verb on action; expected %v, got %v", e, a)
@@ -321,6 +327,9 @@ func TestReconcileBindingWithParameters(t *testing.T) {
321327
}
322328
}
323329

330+
// TestReconcileBindingNonbindableServiceClass tests reconcileBinding to ensure a
331+
// binding for an instance that references a non-bindable service class and a
332+
// non-bindable plan fails as expected.
324333
func TestReconcileBindingNonbindableServiceClass(t *testing.T) {
325334
_, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, noFakeActions())
326335

@@ -360,6 +369,9 @@ func TestReconcileBindingNonbindableServiceClass(t *testing.T) {
360369
}
361370
}
362371

372+
// TestReconcileBindingNonbindableServiceClassBindablePlan tests reconcileBinding
373+
// to ensure a binding for an instance that references a non-bindable service
374+
// class and a bindable plan fails as expected.
363375
func TestReconcileBindingNonbindableServiceClassBindablePlan(t *testing.T) {
364376
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, fakeosb.FakeClientConfiguration{
365377
BindReaction: &fakeosb.BindReaction{
@@ -427,7 +439,6 @@ func TestReconcileBindingNonbindableServiceClassBindablePlan(t *testing.T) {
427439

428440
// first action is a get on the namespace
429441
// second action is a get on the secret
430-
431442
action := kubeActions[2].(clientgotesting.CreateAction)
432443
if e, a := "create", action.GetVerb(); e != a {
433444
t.Fatalf("Unexpected verb on action; expected %v, got %v", e, a)
@@ -461,6 +472,9 @@ func TestReconcileBindingNonbindableServiceClassBindablePlan(t *testing.T) {
461472
assertNumEvents(t, events, 1)
462473
}
463474

475+
// TestReconcileBindingBindableServiceClassNonbindablePlan tests reconcileBinding
476+
// to ensure a binding for an instance that references a bindable service class
477+
// and a non-bindable plan fails as expected.
464478
func TestReconcileBindingBindableServiceClassNonbindablePlan(t *testing.T) {
465479
_, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, noFakeActions())
466480

@@ -500,6 +514,9 @@ func TestReconcileBindingBindableServiceClassNonbindablePlan(t *testing.T) {
500514
}
501515
}
502516

517+
// TestReconcileBindingFailsWithInstanceAsyncOngoing tests reconcileBinding
518+
// to ensure a binding that references an instance that has the
519+
// AsyncOpInProgreset flag set to true fails as expected.
503520
func TestReconcileBindingFailsWithInstanceAsyncOngoing(t *testing.T) {
504521
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, noFakeActions())
505522

@@ -553,6 +570,8 @@ func TestReconcileBindingFailsWithInstanceAsyncOngoing(t *testing.T) {
553570
}
554571
}
555572

573+
// TestReconcileBindingInstanceNotReady tests reconcileBinding to ensure a
574+
// binding for an instance with a ready condition set to false fails as expected.
556575
func TestReconcileBindingInstanceNotReady(t *testing.T) {
557576
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, noFakeActions())
558577

@@ -594,6 +613,8 @@ func TestReconcileBindingInstanceNotReady(t *testing.T) {
594613
}
595614
}
596615

616+
// TestReconcileBindingNamespaceError tests reconcileBinding to ensure a binding
617+
// with an invalid namespace fails as expected.
597618
func TestReconcileBindingNamespaceError(t *testing.T) {
598619
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, noFakeActions())
599620

@@ -635,6 +656,8 @@ func TestReconcileBindingNamespaceError(t *testing.T) {
635656
}
636657
}
637658

659+
// TestReconcileBindingDelete tests reconcileBinding to ensure a binding
660+
// deletion works as expected.
638661
func TestReconcileBindingDelete(t *testing.T) {
639662
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, fakeosb.FakeClientConfiguration{
640663
UnbindReaction: &fakeosb.UnbindReaction{},
@@ -677,8 +700,7 @@ func TestReconcileBindingDelete(t *testing.T) {
677700
})
678701

679702
kubeActions := fakeKubeClient.Actions()
680-
// The two actions should be:
681-
// 0. Deleting the secret
703+
// The action should be deleting the secret
682704
assertNumberOfActions(t, kubeActions, 1)
683705

684706
deleteAction := kubeActions[0].(clientgotesting.DeleteActionImpl)
@@ -925,6 +947,8 @@ func TestReconcileBindingDeleteFailedBinding(t *testing.T) {
925947
}
926948
}
927949

950+
// TestReconcileBindingWithBrokerError tests reconcileBinding to ensure a
951+
// binding request response that contains a broker error fails as expected.
928952
func TestReconcileBindingWithBrokerError(t *testing.T) {
929953
_, _, _, testController, sharedInformers := newTestController(t, fakeosb.FakeClientConfiguration{
930954
BindReaction: &fakeosb.BindReaction{
@@ -966,6 +990,8 @@ func TestReconcileBindingWithBrokerError(t *testing.T) {
966990
}
967991
}
968992

993+
// TestReconcileBindingWithBrokerHTTPError tests reconcileBindings to ensure a
994+
// binding request response that contains a broker HTTP error fails as expected.
969995
func TestReconcileBindingWithBrokerHTTPError(t *testing.T) {
970996
_, _, _, testController, sharedInformers := newTestController(t, fakeosb.FakeClientConfiguration{
971997
BindReaction: &fakeosb.BindReaction{
@@ -1155,6 +1181,22 @@ func TestReconcileBindingWithBindingFailure(t *testing.T) {
11551181
}
11561182
}
11571183

1184+
// TestUpdateBindingCondition tests updateBindingCondition to ensure all status
1185+
// condition transitions on a binding work as expected.
1186+
//
1187+
// The test cases are proving:
1188+
// - a binding with no status that has status condition set to false will update
1189+
// the transition time
1190+
// - a binding with condition false set to condition false will not update the
1191+
// transition time
1192+
// - a binding with condition false set to condition false with a new message and
1193+
// reason will not update the transition time
1194+
// - a binding with condition false set to condition true will update the
1195+
// transition time
1196+
// - a binding with condition status true set to true will not update the
1197+
// transition time
1198+
// - a binding with condition status true set to false will update the transition
1199+
// time
11581200
func TestUpdateBindingCondition(t *testing.T) {
11591201
getTestBindingWithStatus := func(status v1alpha1.ConditionStatus) *v1alpha1.Binding {
11601202
instance := getTestBinding()
@@ -1170,6 +1212,13 @@ func TestUpdateBindingCondition(t *testing.T) {
11701212
return instance
11711213
}
11721214

1215+
// Anonymous struct fields:
1216+
// name: short description of the test
1217+
// input: the binding to test
1218+
// status: condition status to set for binding condition
1219+
// reason: reason to set for binding condition
1220+
// message: message to set for binding condition
1221+
// transitionTimeChanged: toggle for verifying transition time was updated
11731222
cases := []struct {
11741223
name string
11751224
input *v1alpha1.Binding
@@ -1285,6 +1334,8 @@ func TestUpdateBindingCondition(t *testing.T) {
12851334
}
12861335
}
12871336

1337+
// TestReconcileUnbindingWithBrokerError tests reconcileBinding to ensure an
1338+
// unbinding request response that contains a broker error fails as expected.
12881339
func TestReconcileUnbindingWithBrokerError(t *testing.T) {
12891340
_, _, _, testController, sharedInformers := newTestController(t, fakeosb.FakeClientConfiguration{
12901341
UnbindReaction: &fakeosb.UnbindReaction{
@@ -1327,6 +1378,9 @@ func TestReconcileUnbindingWithBrokerError(t *testing.T) {
13271378
}
13281379
}
13291380

1381+
// TestReconcileUnbindingWithBrokerHTTPError tests reconcileBinding to ensure an
1382+
// unbinding request response that contains a broker HTTP error fails as
1383+
// expected.
13301384
func TestReconcileUnbindingWithBrokerHTTPError(t *testing.T) {
13311385
_, _, _, testController, sharedInformers := newTestController(t, fakeosb.FakeClientConfiguration{
13321386
UnbindReaction: &fakeosb.UnbindReaction{

pkg/controller/controller_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ func getTestNonbindableInstance() *v1alpha1.Instance {
416416

417417
// an instance referencing the result of getTestNonbindableServiceClass, on the bindable plan.
418418
func getTestInstanceNonbindableServiceBindablePlan() *v1alpha1.Instance {
419-
i := getTestNonbindableInstance()
419+
i := getTestInstance()
420+
i.Spec.ServiceClassName = testNonbindableServiceClassName
420421
i.Spec.PlanName = testPlanName
421422

422423
return i

0 commit comments

Comments
 (0)