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