@@ -257,10 +257,11 @@ var _ = Describe("Collector", Ordered, func() {
257
257
})
258
258
When ("it encounters an error while collecting data" , func () {
259
259
It ("should error on kubernetes client api errors" , func () {
260
- k8sClientReader .ListReturns (errors .New ("there was an error" ))
260
+ expectedError := errors .New ("there was an error getting NodeList" )
261
+ k8sClientReader .ListReturns (expectedError )
261
262
262
263
_ , err := dataCollector .Collect (ctx )
263
- Expect (err ).To (HaveOccurred ( ))
264
+ Expect (err ).To (MatchError ( expectedError ))
264
265
})
265
266
})
266
267
})
@@ -389,17 +390,19 @@ var _ = Describe("Collector", Ordered, func() {
389
390
fakeConfigurationGetter .GetLatestConfigurationReturns (& dataplane.Configuration {})
390
391
})
391
392
It ("should error on nil latest graph" , func () {
393
+ expectedError := errors .New ("latest graph cannot be nil" )
392
394
fakeGraphGetter .GetLatestGraphReturns (nil )
393
395
394
396
_ , err := dataCollector .Collect (ctx )
395
- Expect (err ).To (HaveOccurred ( ))
397
+ Expect (err ).To (MatchError ( expectedError ))
396
398
})
397
399
398
400
It ("should error on nil latest configuration" , func () {
401
+ expectedError := errors .New ("latest configuration cannot be nil" )
399
402
fakeConfigurationGetter .GetLatestConfigurationReturns (nil )
400
403
401
404
_ , err := dataCollector .Collect (ctx )
402
- Expect (err ).To (HaveOccurred ( ))
405
+ Expect (err ).To (MatchError ( expectedError ))
403
406
})
404
407
})
405
408
})
@@ -408,19 +411,27 @@ var _ = Describe("Collector", Ordered, func() {
408
411
Describe ("NGF replica count collector" , func () {
409
412
When ("collecting NGF replica count" , func () {
410
413
When ("it encounters an error while collecting data" , func () {
411
- BeforeEach (func () {
412
- k8sClientReader .GetReturns (nil )
413
- k8sClientReader .GetCalls (createGetCallsFunc ())
414
- })
415
-
416
414
It ("should error if the kubernetes client errored when getting the Pod" , func () {
417
- k8sClientReader .GetReturnsOnCall (0 , errors .New ("there was an error" ))
415
+ expectedErr := errors .New ("there was an error getting the Pod" )
416
+ k8sClientReader .GetCalls (
417
+ func (ctx context.Context , key client.ObjectKey , object client.Object , option ... client.GetOption ) error {
418
+ Expect (option ).To (BeEmpty ())
419
+
420
+ switch typedObj := object .(type ) {
421
+ case * v1.Pod :
422
+ return expectedErr
423
+ default :
424
+ Fail (fmt .Sprintf ("unknown type: %T" , typedObj ))
425
+ }
426
+ return nil
427
+ })
418
428
419
429
_ , err := dataCollector .Collect (ctx )
420
- Expect (err ).To (HaveOccurred ( ))
430
+ Expect (err ).To (MatchError ( expectedErr ))
421
431
})
422
432
423
433
It ("should error if the Pod's owner reference is nil" , func () {
434
+ expectedErr := errors .New ("expected one owner reference of the NGF Pod, got 0" )
424
435
k8sClientReader .GetCalls (
425
436
func (ctx context.Context , key client.ObjectKey , object client.Object , option ... client.GetOption ) error {
426
437
Expect (option ).To (BeEmpty ())
@@ -438,10 +449,11 @@ var _ = Describe("Collector", Ordered, func() {
438
449
})
439
450
440
451
_ , err := dataCollector .Collect (ctx )
441
- Expect (err ).To (HaveOccurred ( ))
452
+ Expect (err ).To (MatchError ( expectedErr ))
442
453
})
443
454
444
455
It ("should error if the Pod has multiple owner references" , func () {
456
+ expectedErr := errors .New ("expected one owner reference of the NGF Pod, got 2" )
445
457
k8sClientReader .GetCalls (
446
458
func (ctx context.Context , key client.ObjectKey , object client.Object , option ... client.GetOption ) error {
447
459
Expect (option ).To (BeEmpty ())
@@ -468,10 +480,11 @@ var _ = Describe("Collector", Ordered, func() {
468
480
})
469
481
470
482
_ , err := dataCollector .Collect (ctx )
471
- Expect (err ).To (HaveOccurred ( ))
483
+ Expect (err ).To (MatchError ( expectedErr ))
472
484
})
473
485
474
486
It ("should error if the Pod's owner reference is not a ReplicaSet" , func () {
487
+ expectedErr := errors .New ("expected pod owner reference to be ReplicaSet, got Deployment" )
475
488
k8sClientReader .GetCalls (
476
489
func (ctx context.Context , key client.ObjectKey , object client.Object , option ... client.GetOption ) error {
477
490
Expect (option ).To (BeEmpty ())
@@ -494,10 +507,11 @@ var _ = Describe("Collector", Ordered, func() {
494
507
})
495
508
496
509
_ , err := dataCollector .Collect (ctx )
497
- Expect (err ).To (HaveOccurred ( ))
510
+ Expect (err ).To (MatchError ( expectedErr ))
498
511
})
499
512
500
513
It ("should error if the replica set's replicas is nil" , func () {
514
+ expectedErr := errors .New ("replica set replicas was nil" )
501
515
k8sClientReader .GetCalls (
502
516
func (ctx context.Context , key client.ObjectKey , object client.Object , option ... client.GetOption ) error {
503
517
Expect (option ).To (BeEmpty ())
@@ -524,14 +538,36 @@ var _ = Describe("Collector", Ordered, func() {
524
538
})
525
539
526
540
_ , err := dataCollector .Collect (ctx )
527
- Expect (err ).To (HaveOccurred ( ))
541
+ Expect (err ).To (MatchError ( expectedErr ))
528
542
})
529
543
530
544
It ("should error if the kubernetes client errored when getting the ReplicaSet" , func () {
531
- k8sClientReader .GetReturnsOnCall (1 , errors .New ("there was an error" ))
545
+ expectedErr := errors .New ("there was an error getting the ReplicaSet" )
546
+ k8sClientReader .GetCalls (
547
+ func (ctx context.Context , key client.ObjectKey , object client.Object , option ... client.GetOption ) error {
548
+ Expect (option ).To (BeEmpty ())
549
+
550
+ switch typedObj := object .(type ) {
551
+ case * v1.Pod :
552
+ typedObj .ObjectMeta = metav1.ObjectMeta {
553
+ Name : "pod1" ,
554
+ OwnerReferences : []metav1.OwnerReference {
555
+ {
556
+ Kind : "ReplicaSet" ,
557
+ Name : "replicaset1" ,
558
+ },
559
+ },
560
+ }
561
+ case * appsv1.ReplicaSet :
562
+ return expectedErr
563
+ default :
564
+ Fail (fmt .Sprintf ("unknown type: %T" , typedObj ))
565
+ }
566
+ return nil
567
+ })
532
568
533
569
_ , err := dataCollector .Collect (ctx )
534
- Expect (err ).To (HaveOccurred ( ))
570
+ Expect (err ).To (MatchError ( expectedErr ))
535
571
})
536
572
})
537
573
})
0 commit comments