Skip to content

Commit 43b40ab

Browse files
Jay Boydpmorie
authored andcommitted
controller_broker unit test bullet-proofing #1077 (#1099)
1 parent bb596b8 commit 43b40ab

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-16
lines changed

pkg/controller/controller_broker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ func (c *controller) reconcileServiceClassFromBrokerCatalog(broker *v1alpha1.Bro
389389
return nil
390390
}
391391

392-
// updateBrokerReadyCondition updates the ready condition for the given Broker
392+
// updateBrokerCondition updates the ready condition for the given Broker
393393
// with the given status, reason, and message.
394394
func (c *controller) updateBrokerCondition(broker *v1alpha1.Broker, conditionType v1alpha1.BrokerConditionType, status v1alpha1.ConditionStatus, reason, message string) error {
395395
clone, err := api.Scheme.DeepCopy(broker)

pkg/controller/controller_broker_test.go

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,25 @@ import (
3131
"k8s.io/apimachinery/pkg/runtime"
3232
"k8s.io/apimachinery/pkg/util/diff"
3333

34+
"strings"
35+
3436
"k8s.io/client-go/pkg/api"
3537
"k8s.io/client-go/pkg/api/v1"
3638
clientgotesting "k8s.io/client-go/testing"
37-
"strings"
3839
)
3940

41+
// TestShouldReconcileBroker ensures that with the expected conditions the
42+
// reconciler is reported as needing to run.
43+
//
44+
// The test cases are proving:
45+
// - broker without ready condition will reconcile
46+
// - broker with deletion timestamp set will reconcile
47+
// - broker without ready condition, with status will reconcile
48+
// - broker without ready condition, without status will reconcile
49+
// - broker with status/ready, past relist interval will reconcile
50+
// - broker with status/ready, within relist interval will NOT reconcile
51+
// - broker with status/ready/checksum, will reconcile
4052
func TestShouldReconcileBroker(t *testing.T) {
41-
// The test cases here are testing shouldReconcileBroker to ensure that
42-
// with the expected conditions the reconciler is reported as needing
43-
// to run.
44-
// The test cases are proving:
45-
// - broker without ready condition will reconcile
46-
// - broker with deletion timestamp set will reconcile
47-
// - broker without ready condition, with status will reconcile
48-
// - broker without ready condition, without status will reconcile
49-
// - broker with status/ready, past relist interval will reconcile
50-
// - broker with status/ready, within relist interval will NOT reconcile
51-
// - broker with status/ready/checksum, will reconcile
52-
//
5353
// Anonymous struct fields:
5454
// name: short description of the test
5555
// broker: broker object to test
@@ -156,6 +156,11 @@ func TestShouldReconcileBroker(t *testing.T) {
156156
}
157157
}
158158

159+
// TestReconcileBrokerExistingServiceClass verifies a simple, successful run
160+
// of reconcileBroker(). This test will cause reconcileBroker() to fetch the
161+
// catalog from the Broker, create a Service Class for the single service that
162+
// it lists and reconcile the service class ensuring the name and id of the
163+
// relisted service matches the existing entry and updates the service catalog.
159164
func TestReconcileBrokerExistingServiceClass(t *testing.T) {
160165
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, getTestCatalogConfig())
161166

@@ -185,6 +190,9 @@ func TestReconcileBrokerExistingServiceClass(t *testing.T) {
185190
assertNumberOfActions(t, kubeActions, 0)
186191
}
187192

193+
// TestReconcileBrokerExistingServiceClassDifferentExternalID simulates catalog
194+
// refresh where broker lists an existing service but there is a mismatch on the
195+
// service class ID which should result in an error
188196
func TestReconcileBrokerExistingServiceClassDifferentExternalID(t *testing.T) {
189197
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, getTestCatalogConfig())
190198

@@ -219,6 +227,9 @@ func TestReconcileBrokerExistingServiceClassDifferentExternalID(t *testing.T) {
219227
}
220228
}
221229

230+
// TestReconcileBrokerExistingServiceClassDifferentBroker simulates catalog
231+
// refresh where broker lists a service which matches an existing, already
232+
// cataloged service but the service points to a different Broker. Results in an error.
222233
func TestReconcileBrokerExistingServiceClassDifferentBroker(t *testing.T) {
223234
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, getTestCatalogConfig())
224235

@@ -253,6 +264,8 @@ func TestReconcileBrokerExistingServiceClassDifferentBroker(t *testing.T) {
253264
}
254265
}
255266

267+
// TestReconcileBrokerDelete simulates a broker reconciliation where broker was marked for deletion.
268+
// Results in service class and broker both being deleted.
256269
func TestReconcileBrokerDelete(t *testing.T) {
257270
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, sharedInformers := newTestController(t, getTestCatalogConfig())
258271

@@ -279,7 +292,7 @@ func TestReconcileBrokerDelete(t *testing.T) {
279292
assertNumberOfActions(t, kubeActions, 0)
280293

281294
actions := fakeCatalogClient.Actions()
282-
// The three actions should be:
295+
// The four actions should be:
283296
// 0. Deleting the associated ServiceClass
284297
// 1. Updating the ready condition
285298
// 2. Getting the broker
@@ -305,6 +318,9 @@ func TestReconcileBrokerDelete(t *testing.T) {
305318
}
306319
}
307320

321+
// TestReconcileBrokerErrorFetchingCatalog simulates broker reconciliation where
322+
// OSB client responds with an error for getting the catalog which in turn causes
323+
// reconcileBroker() to return an error.
308324
func TestReconcileBrokerErrorFetchingCatalog(t *testing.T) {
309325
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, _ := newTestController(t, fakeosb.FakeClientConfiguration{
310326
CatalogReaction: &fakeosb.CatalogReaction{
@@ -339,6 +355,9 @@ func TestReconcileBrokerErrorFetchingCatalog(t *testing.T) {
339355
}
340356
}
341357

358+
// TestReconcileBrokerZeroServices simulates broker reconciliation where
359+
// OSB client responds with zero services which causes reconcileBroker()
360+
// to return an error
342361
func TestReconcileBrokerZeroServices(t *testing.T) {
343362
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, _ := newTestController(t, fakeosb.FakeClientConfiguration{
344363
CatalogReaction: &fakeosb.CatalogReaction{
@@ -534,6 +553,9 @@ func testReconcileBrokerWithAuth(t *testing.T, authInfo *v1alpha1.BrokerAuthInfo
534553
}
535554
}
536555

556+
// TestReconcileBrokerWithReconcileError simulates broker reconciliation where
557+
// creation of a service class causes an error which causes ReconcileBroker to
558+
// return an error
537559
func TestReconcileBrokerWithReconcileError(t *testing.T) {
538560
fakeKubeClient, fakeCatalogClient, fakeBrokerClient, testController, _ := newTestController(t, getTestCatalogConfig())
539561

@@ -577,7 +599,25 @@ func TestReconcileBrokerWithReconcileError(t *testing.T) {
577599
}
578600
}
579601

602+
// TestUpdateBrokerCondition ensures that with specific conditions
603+
// the broker correctly reflects the changes during updateBrokerCondition().
604+
//
605+
// The test cases are proving:
606+
// - broker transitions from unset status to not ready results in status change and new time
607+
// - broker transitions from not ready to not ready results in no changes
608+
// - broker transitions from not ready to not ready and with reason & msg updates results in no time change, but reflects new reason & msg
609+
// - broker transitions from not ready to ready results in status change & new time
610+
// - broker transitions from ready to ready results in no status change
611+
// - broker transitions from ready to not ready results in status change & new time
612+
// - condition reason & message should always be updated
580613
func TestUpdateBrokerCondition(t *testing.T) {
614+
// Anonymous struct fields:
615+
// name: short description of the test
616+
// input: broker object to test
617+
// status: new condition status
618+
// reason: condition reason
619+
// message: condition message
620+
// transitionTimeChanged: true if the test conditions should result in transition time change
581621
cases := []struct {
582622
name string
583623
input *v1alpha1.Broker
@@ -689,7 +729,7 @@ func TestUpdateBrokerCondition(t *testing.T) {
689729
continue
690730
}
691731
if e, a := tc.message, outputCondition.Message; e != "" && e != a {
692-
t.Errorf("%v: condition reasons didn't match; expected %v, got %v", tc.name, e, a)
732+
t.Errorf("%v: condition message didn't match; expected %v, got %v", tc.name, e, a)
693733
}
694734
}
695735
}

0 commit comments

Comments
 (0)