diff --git a/pkg/aaq-controller/additional-cluster-quota-controllers/aacrq-controller/aacrq-controller.go b/pkg/aaq-controller/additional-cluster-quota-controllers/aacrq-controller/aacrq-controller.go index a2d552f0f..bba83f519 100644 --- a/pkg/aaq-controller/additional-cluster-quota-controllers/aacrq-controller/aacrq-controller.go +++ b/pkg/aaq-controller/additional-cluster-quota-controllers/aacrq-controller/aacrq-controller.go @@ -60,6 +60,7 @@ func NewAacrqController(aaqCli client.AAQClient, AddFunc: ctrl.addAcrq, }) if err != nil { + panic(fmt.Errorf("failed to register ApplicationAwareClusterResourceQuota event handler: %w", err)) } return &ctrl @@ -167,7 +168,7 @@ func (ctrl *AacrqController) Execute() bool { err, enqueueState := ctrl.execute(key.(string)) if err != nil { - log.Log.Infof(fmt.Sprintf("AacrqController: Error with key: %v err: %v", key, err)) + log.Log.Infof("AacrqController: Error with key: %v err: %v", key, err) } switch enqueueState { case BackOff: diff --git a/pkg/aaq-controller/additional-cluster-quota-controllers/aacrq-controller/aacrq-controller_constructor_test.go b/pkg/aaq-controller/additional-cluster-quota-controllers/aacrq-controller/aacrq-controller_constructor_test.go new file mode 100644 index 000000000..69960aa11 --- /dev/null +++ b/pkg/aaq-controller/additional-cluster-quota-controllers/aacrq-controller/aacrq-controller_constructor_test.go @@ -0,0 +1,24 @@ +package aacrq_controller + +import ( + "errors" + "testing" + + testsutils "kubevirt.io/application-aware-quota/pkg/tests-utils" +) + +func TestNewAacrqControllerPanicsOnAcrqHandlerRegistrationError(t *testing.T) { + t.Helper() + + aacrqInformer := testsutils.NewFakeSharedIndexInformer(nil) + acrqInformer := testsutils.NewFakeSharedIndexInformer(nil) + acrqInformer.AddEventHandlerErr = errors.New("boom") + + defer func() { + if recover() == nil { + t.Fatal("expected panic when acrq informer handler registration fails") + } + }() + + NewAacrqController(nil, aacrqInformer, acrqInformer, make(chan struct{})) +} diff --git a/pkg/aaq-controller/additional-cluster-quota-controllers/crq-controller/crq-controller.go b/pkg/aaq-controller/additional-cluster-quota-controllers/crq-controller/crq-controller.go index 2bf85b9ca..ca0f52fdc 100644 --- a/pkg/aaq-controller/additional-cluster-quota-controllers/crq-controller/crq-controller.go +++ b/pkg/aaq-controller/additional-cluster-quota-controllers/crq-controller/crq-controller.go @@ -67,6 +67,7 @@ func NewCRQController(aaqCli client.AAQClient, AddFunc: ctrl.addAcrq, }) if err != nil { + panic(fmt.Errorf("failed to register ApplicationAwareClusterResourceQuota event handler: %w", err)) } return &ctrl @@ -154,7 +155,7 @@ func (ctrl *CRQController) Execute() bool { err, enqueueState := ctrl.execute(key.(string)) if err != nil { - log.Log.Infof(fmt.Sprintf("CRQController: Error with key: %v err: %v", key, err)) + log.Log.Infof("CRQController: Error with key: %v err: %v", key, err) } switch enqueueState { case BackOff: diff --git a/pkg/aaq-controller/additional-cluster-quota-controllers/crq-controller/crq-controller_constructor_test.go b/pkg/aaq-controller/additional-cluster-quota-controllers/crq-controller/crq-controller_constructor_test.go new file mode 100644 index 000000000..a4979f6a6 --- /dev/null +++ b/pkg/aaq-controller/additional-cluster-quota-controllers/crq-controller/crq-controller_constructor_test.go @@ -0,0 +1,24 @@ +package crq_controller + +import ( + "errors" + "testing" + + testsutils "kubevirt.io/application-aware-quota/pkg/tests-utils" +) + +func TestNewCRQControllerPanicsOnAcrqHandlerRegistrationError(t *testing.T) { + t.Helper() + + crqInformer := testsutils.NewFakeSharedIndexInformer(nil) + acrqInformer := testsutils.NewFakeSharedIndexInformer(nil) + acrqInformer.AddEventHandlerErr = errors.New("boom") + + defer func() { + if recover() == nil { + t.Fatal("expected panic when acrq informer handler registration fails") + } + }() + + NewCRQController(nil, crqInformer, acrqInformer, make(chan struct{})) +} diff --git a/pkg/tests-utils/fake-informers.go b/pkg/tests-utils/fake-informers.go index 21ccece17..00ac7ca0a 100644 --- a/pkg/tests-utils/fake-informers.go +++ b/pkg/tests-utils/fake-informers.go @@ -13,6 +13,7 @@ import ( type FakeSharedIndexInformer struct { indexer cache.Indexer InternalGetIndexer func(cache.Indexer) cache.Indexer + AddEventHandlerErr error } func (i FakeSharedIndexInformer) AddEventHandlerWithOptions(handler cache.ResourceEventHandler, options cache.HandlerOptions) (cache.ResourceEventHandlerRegistration, error) { @@ -53,10 +54,10 @@ func (i FakeSharedIndexInformer) GetIndexer() cache.Indexer { } func (i FakeSharedIndexInformer) AddEventHandler(handler cache.ResourceEventHandler) (cache.ResourceEventHandlerRegistration, error) { - return nil, nil + return nil, i.AddEventHandlerErr } func (i FakeSharedIndexInformer) AddEventHandlerWithResyncPeriod(handler cache.ResourceEventHandler, resyncPeriod time.Duration) (cache.ResourceEventHandlerRegistration, error) { - return nil, nil + return nil, i.AddEventHandlerErr } func (i FakeSharedIndexInformer) GetStore() cache.Store { return nil } func (i FakeSharedIndexInformer) GetController() cache.Controller { return nil }