4
4
"context"
5
5
"errors"
6
6
"fmt"
7
+ "reflect"
7
8
8
9
. "github.com/onsi/ginkgo/v2"
9
10
. "github.com/onsi/gomega"
@@ -40,34 +41,23 @@ func createListCallsFunc(nodes []v1.Node) func(
40
41
}
41
42
}
42
43
43
- func createGetCallsFunc () func (
44
- ctx context.Context ,
45
- key client. ObjectKey ,
46
- object client.Object ,
47
- option ... client.GetOption ,
44
+ func createGetCallsFunc (objects ... client. Object ) func (
45
+ context.Context ,
46
+ types. NamespacedName ,
47
+ client.Object ,
48
+ ... client.GetOption ,
48
49
) error {
49
- return func (_ context.Context , _ client. ObjectKey , object client.Object , option ... client.GetOption ) error {
50
+ return func (_ context.Context , _ types. NamespacedName , object client.Object , option ... client.GetOption ) error {
50
51
Expect (option ).To (BeEmpty ())
51
52
52
- switch typedObj := object .(type ) {
53
- case * v1.Pod :
54
- typedObj .ObjectMeta = metav1.ObjectMeta {
55
- Name : "pod1" ,
56
- OwnerReferences : []metav1.OwnerReference {
57
- {
58
- Kind : "ReplicaSet" ,
59
- Name : "replicaset1" ,
60
- },
61
- },
53
+ for _ , obj := range objects {
54
+ if reflect .TypeOf (obj ) == reflect .TypeOf (object ) {
55
+ reflect .ValueOf (object ).Elem ().Set (reflect .ValueOf (obj ).Elem ())
56
+ return nil
62
57
}
63
- case * appsv1.ReplicaSet :
64
- replicas := int32 (1 )
65
- typedObj .Spec = appsv1.ReplicaSetSpec {
66
- Replicas : & replicas ,
67
- }
68
- default :
69
- Fail (fmt .Sprintf ("unknown type: %T" , typedObj ))
70
58
}
59
+
60
+ Fail (fmt .Sprintf ("unknown type: %T" , object ))
71
61
return nil
72
62
}
73
63
}
@@ -82,11 +72,37 @@ var _ = Describe("Collector", Ordered, func() {
82
72
expData telemetry.Data
83
73
ctx context.Context
84
74
podNSName types.NamespacedName
75
+ ngfPod * v1.Pod
76
+ ngfReplicaSet * appsv1.ReplicaSet
85
77
)
86
78
87
79
BeforeAll (func () {
88
80
ctx = context .Background ()
89
81
version = "1.1"
82
+
83
+ ngfPod = & v1.Pod {
84
+ ObjectMeta : metav1.ObjectMeta {
85
+ Name : "pod1" ,
86
+ OwnerReferences : []metav1.OwnerReference {
87
+ {
88
+ Kind : "ReplicaSet" ,
89
+ Name : "replicaset1" ,
90
+ },
91
+ },
92
+ },
93
+ }
94
+
95
+ replicas := int32 (1 )
96
+ ngfReplicaSet = & appsv1.ReplicaSet {
97
+ Spec : appsv1.ReplicaSetSpec {
98
+ Replicas : & replicas ,
99
+ },
100
+ }
101
+
102
+ podNSName = types.NamespacedName {
103
+ Namespace : "nginx-gateway" ,
104
+ Name : "ngf-pod" ,
105
+ }
90
106
})
91
107
92
108
BeforeEach (func () {
@@ -100,10 +116,6 @@ var _ = Describe("Collector", Ordered, func() {
100
116
k8sClientReader = & eventsfakes.FakeReader {}
101
117
fakeGraphGetter = & telemetryfakes.FakeGraphGetter {}
102
118
fakeConfigurationGetter = & telemetryfakes.FakeConfigurationGetter {}
103
- podNSName = types.NamespacedName {
104
- Namespace : "nginx-gateway" ,
105
- Name : "ngf-pod" ,
106
- }
107
119
108
120
fakeGraphGetter .GetLatestGraphReturns (& graph.Graph {})
109
121
fakeConfigurationGetter .GetLatestConfigurationReturns (& dataplane.Configuration {})
@@ -115,7 +127,7 @@ var _ = Describe("Collector", Ordered, func() {
115
127
Version : version ,
116
128
PodNSName : podNSName ,
117
129
})
118
- k8sClientReader .GetCalls (createGetCallsFunc ())
130
+ k8sClientReader .GetCalls (createGetCallsFunc (ngfPod , ngfReplicaSet ))
119
131
})
120
132
121
133
Describe ("Normal case" , func () {
@@ -432,110 +444,83 @@ var _ = Describe("Collector", Ordered, func() {
432
444
433
445
It ("should error if the Pod's owner reference is nil" , func () {
434
446
expectedErr := errors .New ("expected one owner reference of the NGF Pod, got 0" )
435
- k8sClientReader .GetCalls (
436
- func (_ context.Context , _ client.ObjectKey , object client.Object , option ... client.GetOption ) error {
437
- Expect (option ).To (BeEmpty ())
438
-
439
- switch typedObj := object .(type ) {
440
- case * v1.Pod :
441
- typedObj .ObjectMeta = metav1.ObjectMeta {
442
- Name : "pod1" ,
443
- OwnerReferences : nil ,
444
- }
445
- default :
446
- Fail (fmt .Sprintf ("unknown type: %T" , typedObj ))
447
- }
448
- return nil
449
- })
447
+ k8sClientReader .GetCalls (createGetCallsFunc (
448
+ & v1.Pod {
449
+ ObjectMeta : metav1.ObjectMeta {
450
+ Name : "pod1" ,
451
+ OwnerReferences : nil ,
452
+ },
453
+ },
454
+ ))
450
455
451
456
_ , err := dataCollector .Collect (ctx )
452
457
Expect (err ).To (MatchError (expectedErr ))
453
458
})
454
459
455
460
It ("should error if the Pod has multiple owner references" , func () {
456
461
expectedErr := errors .New ("expected one owner reference of the NGF Pod, got 2" )
457
- k8sClientReader .GetCalls (
458
- func (_ context.Context , _ client.ObjectKey , object client.Object , option ... client.GetOption ) error {
459
- Expect (option ).To (BeEmpty ())
460
-
461
- switch typedObj := object .(type ) {
462
- case * v1.Pod :
463
- typedObj .ObjectMeta = metav1.ObjectMeta {
464
- Name : "pod1" ,
465
- OwnerReferences : []metav1.OwnerReference {
466
- {
467
- Kind : "ReplicaSet" ,
468
- Name : "replicaset1" ,
469
- },
470
- {
471
- Kind : "ReplicaSet" ,
472
- Name : "replicaset2" ,
473
- },
462
+ k8sClientReader .GetCalls (createGetCallsFunc (
463
+ & v1.Pod {
464
+ ObjectMeta : metav1.ObjectMeta {
465
+ Name : "pod1" ,
466
+ OwnerReferences : []metav1.OwnerReference {
467
+ {
468
+ Kind : "ReplicaSet" ,
469
+ Name : "replicaset1" ,
474
470
},
475
- }
476
- default :
477
- Fail (fmt .Sprintf ("unknown type: %T" , typedObj ))
478
- }
479
- return nil
480
- })
471
+ {
472
+ Kind : "ReplicaSet" ,
473
+ Name : "replicaset2" ,
474
+ },
475
+ },
476
+ },
477
+ },
478
+ ))
481
479
482
480
_ , err := dataCollector .Collect (ctx )
483
481
Expect (err ).To (MatchError (expectedErr ))
484
482
})
485
483
486
484
It ("should error if the Pod's owner reference is not a ReplicaSet" , func () {
487
485
expectedErr := errors .New ("expected pod owner reference to be ReplicaSet, got Deployment" )
488
- k8sClientReader .GetCalls (
489
- func (_ context.Context , _ client.ObjectKey , object client.Object , option ... client.GetOption ) error {
490
- Expect (option ).To (BeEmpty ())
491
-
492
- switch typedObj := object .(type ) {
493
- case * v1.Pod :
494
- typedObj .ObjectMeta = metav1.ObjectMeta {
495
- Name : "pod1" ,
496
- OwnerReferences : []metav1.OwnerReference {
497
- {
498
- Kind : "Deployment" ,
499
- Name : "deployment1" ,
500
- },
486
+ k8sClientReader .GetCalls (createGetCallsFunc (
487
+ & v1.Pod {
488
+ ObjectMeta : metav1.ObjectMeta {
489
+ Name : "pod1" ,
490
+ OwnerReferences : []metav1.OwnerReference {
491
+ {
492
+ Kind : "Deployment" ,
493
+ Name : "deployment1" ,
501
494
},
502
- }
503
- default :
504
- Fail (fmt .Sprintf ("unknown type: %T" , typedObj ))
505
- }
506
- return nil
507
- })
495
+ },
496
+ },
497
+ },
498
+ ))
508
499
509
500
_ , err := dataCollector .Collect (ctx )
510
501
Expect (err ).To (MatchError (expectedErr ))
511
502
})
512
503
513
504
It ("should error if the replica set's replicas is nil" , func () {
514
505
expectedErr := errors .New ("replica set replicas was nil" )
515
- k8sClientReader .GetCalls (
516
- func (_ context.Context , _ client.ObjectKey , object client.Object , option ... client.GetOption ) error {
517
- Expect (option ).To (BeEmpty ())
518
-
519
- switch typedObj := object .(type ) {
520
- case * v1.Pod :
521
- typedObj .ObjectMeta = metav1.ObjectMeta {
522
- Name : "pod1" ,
523
- OwnerReferences : []metav1.OwnerReference {
524
- {
525
- Kind : "ReplicaSet" ,
526
- Name : "replicaset1" ,
527
- },
506
+ k8sClientReader .GetCalls (createGetCallsFunc (
507
+ & v1.Pod {
508
+ ObjectMeta : metav1.ObjectMeta {
509
+ Name : "pod1" ,
510
+ OwnerReferences : []metav1.OwnerReference {
511
+ {
512
+ Kind : "ReplicaSet" ,
513
+ Name : "replicaset1" ,
528
514
},
529
- }
530
- case * appsv1.ReplicaSet :
531
- typedObj .Spec = appsv1.ReplicaSetSpec {
532
- Replicas : nil ,
533
- }
534
- default :
535
- Fail (fmt .Sprintf ("unknown type: %T" , typedObj ))
536
- }
537
- return nil
538
- })
515
+ },
516
+ },
517
+ },
518
+ & appsv1.ReplicaSet {
519
+ Spec : appsv1.ReplicaSetSpec {
520
+ Replicas : nil ,
521
+ },
522
+ },
523
+ ))
539
524
540
525
_ , err := dataCollector .Collect (ctx )
541
526
Expect (err ).To (MatchError (expectedErr ))
0 commit comments