@@ -408,14 +408,138 @@ var _ = Describe("controller", func() {
408408 // TODO(community): write this test
409409 })
410410
411+ Context ("prometheus metric reconcile_total" , func () {
412+ var reconcileTotal dto.Metric
413+
414+ BeforeEach (func () {
415+ ctrlmetrics .ReconcileTotal .Reset ()
416+ reconcileTotal .Reset ()
417+ })
418+
419+ It ("should get updated on successful reconciliation" , func (done Done ) {
420+ Expect (func () error {
421+ ctrlmetrics .ReconcileTotal .WithLabelValues (ctrl .Name , "success" ).Write (& reconcileTotal )
422+ if reconcileTotal .GetCounter ().GetValue () != 0.0 {
423+ return fmt .Errorf ("metric reconcile total not reset" )
424+ }
425+ return nil
426+ }()).Should (Succeed ())
427+
428+ go func () {
429+ defer GinkgoRecover ()
430+ Expect (ctrl .Start (stop )).NotTo (HaveOccurred ())
431+ }()
432+ By ("Invoking Reconciler which will succeed" )
433+ ctrl .Queue .Add (request )
434+
435+ Expect (<- reconciled ).To (Equal (request ))
436+ Eventually (func () error {
437+ ctrlmetrics .ReconcileTotal .WithLabelValues (ctrl .Name , "success" ).Write (& reconcileTotal )
438+ if actual := reconcileTotal .GetCounter ().GetValue (); actual != 1.0 {
439+ return fmt .Errorf ("metric reconcile total expected: %v and got: %v" , 1.0 , actual )
440+ }
441+ return nil
442+ }, 2.0 ).Should (Succeed ())
443+
444+ close (done )
445+ }, 2.0 )
446+
447+ It ("should get updated on reconcile errors" , func (done Done ) {
448+ Expect (func () error {
449+ ctrlmetrics .ReconcileTotal .WithLabelValues (ctrl .Name , "error" ).Write (& reconcileTotal )
450+ if reconcileTotal .GetCounter ().GetValue () != 0.0 {
451+ return fmt .Errorf ("metric reconcile total not reset" )
452+ }
453+ return nil
454+ }()).Should (Succeed ())
455+
456+ fakeReconcile .Err = fmt .Errorf ("expected error: reconcile" )
457+ go func () {
458+ defer GinkgoRecover ()
459+ Expect (ctrl .Start (stop )).NotTo (HaveOccurred ())
460+ }()
461+ By ("Invoking Reconciler which will give an error" )
462+ ctrl .Queue .Add (request )
463+
464+ Expect (<- reconciled ).To (Equal (request ))
465+ Eventually (func () error {
466+ ctrlmetrics .ReconcileTotal .WithLabelValues (ctrl .Name , "error" ).Write (& reconcileTotal )
467+ if actual := reconcileTotal .GetCounter ().GetValue (); actual != 1.0 {
468+ return fmt .Errorf ("metric reconcile total expected: %v and got: %v" , 1.0 , actual )
469+ }
470+ return nil
471+ }, 2.0 ).Should (Succeed ())
472+
473+ close (done )
474+ }, 2.0 )
475+
476+ It ("should get updated when reconcile returns with retry enabled" , func (done Done ) {
477+ Expect (func () error {
478+ ctrlmetrics .ReconcileTotal .WithLabelValues (ctrl .Name , "retry" ).Write (& reconcileTotal )
479+ if reconcileTotal .GetCounter ().GetValue () != 0.0 {
480+ return fmt .Errorf ("metric reconcile total not reset" )
481+ }
482+ return nil
483+ }()).Should (Succeed ())
484+
485+ fakeReconcile .Result .Requeue = true
486+ go func () {
487+ defer GinkgoRecover ()
488+ Expect (ctrl .Start (stop )).NotTo (HaveOccurred ())
489+ }()
490+ By ("Invoking Reconciler which will return result with Requeue enabled" )
491+ ctrl .Queue .Add (request )
492+
493+ Expect (<- reconciled ).To (Equal (request ))
494+ Eventually (func () error {
495+ ctrlmetrics .ReconcileTotal .WithLabelValues (ctrl .Name , "requeue" ).Write (& reconcileTotal )
496+ if actual := reconcileTotal .GetCounter ().GetValue (); actual != 1.0 {
497+ return fmt .Errorf ("metric reconcile total expected: %v and got: %v" , 1.0 , actual )
498+ }
499+ return nil
500+ }, 2.0 ).Should (Succeed ())
501+
502+ close (done )
503+ }, 2.0 )
504+
505+ It ("should get updated when reconcile returns with retryAfter enabled" , func (done Done ) {
506+ Expect (func () error {
507+ ctrlmetrics .ReconcileTotal .WithLabelValues (ctrl .Name , "retry_after" ).Write (& reconcileTotal )
508+ if reconcileTotal .GetCounter ().GetValue () != 0.0 {
509+ return fmt .Errorf ("metric reconcile total not reset" )
510+ }
511+ return nil
512+ }()).Should (Succeed ())
513+
514+ fakeReconcile .Result .RequeueAfter = 5 * time .Hour
515+ go func () {
516+ defer GinkgoRecover ()
517+ Expect (ctrl .Start (stop )).NotTo (HaveOccurred ())
518+ }()
519+ By ("Invoking Reconciler which will return result with requeueAfter enabled" )
520+ ctrl .Queue .Add (request )
521+
522+ Expect (<- reconciled ).To (Equal (request ))
523+ Eventually (func () error {
524+ ctrlmetrics .ReconcileTotal .WithLabelValues (ctrl .Name , "requeue_after" ).Write (& reconcileTotal )
525+ if actual := reconcileTotal .GetCounter ().GetValue (); actual != 1.0 {
526+ return fmt .Errorf ("metric reconcile total expected: %v and got: %v" , 1.0 , actual )
527+ }
528+ return nil
529+ }, 2.0 ).Should (Succeed ())
530+
531+ close (done )
532+ }, 2.0 )
533+ })
534+
411535 Context ("should update prometheus metrics" , func () {
412536 It ("should requeue a Request if there is an error and continue processing items" , func (done Done ) {
413537 var queueLength , reconcileErrs dto.Metric
414538 ctrlmetrics .QueueLength .Reset ()
415539 Expect (func () error {
416540 ctrlmetrics .QueueLength .WithLabelValues (ctrl .Name ).Write (& queueLength )
417541 if queueLength .GetGauge ().GetValue () != 0.0 {
418- return fmt .Errorf ("metrics not reset" )
542+ return fmt .Errorf ("metric queue length not reset" )
419543 }
420544 return nil
421545 }()).Should (Succeed ())
@@ -424,7 +548,7 @@ var _ = Describe("controller", func() {
424548 Expect (func () error {
425549 ctrlmetrics .ReconcileErrors .WithLabelValues (ctrl .Name ).Write (& reconcileErrs )
426550 if reconcileErrs .GetCounter ().GetValue () != 0.0 {
427- return fmt .Errorf ("metrics not reset" )
551+ return fmt .Errorf ("metric reconcile errors not reset" )
428552 }
429553 return nil
430554 }()).Should (Succeed ())
@@ -444,7 +568,7 @@ var _ = Describe("controller", func() {
444568 Eventually (func () error {
445569 ctrlmetrics .QueueLength .WithLabelValues (ctrl .Name ).Write (& queueLength )
446570 if queueLength .GetGauge ().GetValue () != 1.0 {
447- return fmt .Errorf ("metrics not updated" )
571+ return fmt .Errorf ("metric queue length not updated" )
448572 }
449573 return nil
450574 }, 2.0 ).Should (Succeed ())
0 commit comments