44 "context"
55 "errors"
66 "fmt"
7+ "reflect"
78
89 . "github.com/onsi/ginkgo/v2"
910 . "github.com/onsi/gomega"
@@ -40,34 +41,23 @@ func createListCallsFunc(nodes []v1.Node) func(
4041 }
4142}
4243
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 ,
4849) 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 {
5051 Expect (option ).To (BeEmpty ())
5152
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
6257 }
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 ))
7058 }
59+
60+ Fail (fmt .Sprintf ("unknown type: %T" , object ))
7161 return nil
7262 }
7363}
@@ -82,11 +72,37 @@ var _ = Describe("Collector", Ordered, func() {
8272 expData telemetry.Data
8373 ctx context.Context
8474 podNSName types.NamespacedName
75+ ngfPod * v1.Pod
76+ ngfReplicaSet * appsv1.ReplicaSet
8577 )
8678
8779 BeforeAll (func () {
8880 ctx = context .Background ()
8981 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+ }
90106 })
91107
92108 BeforeEach (func () {
@@ -100,10 +116,6 @@ var _ = Describe("Collector", Ordered, func() {
100116 k8sClientReader = & eventsfakes.FakeReader {}
101117 fakeGraphGetter = & telemetryfakes.FakeGraphGetter {}
102118 fakeConfigurationGetter = & telemetryfakes.FakeConfigurationGetter {}
103- podNSName = types.NamespacedName {
104- Namespace : "nginx-gateway" ,
105- Name : "ngf-pod" ,
106- }
107119
108120 fakeGraphGetter .GetLatestGraphReturns (& graph.Graph {})
109121 fakeConfigurationGetter .GetLatestConfigurationReturns (& dataplane.Configuration {})
@@ -115,7 +127,7 @@ var _ = Describe("Collector", Ordered, func() {
115127 Version : version ,
116128 PodNSName : podNSName ,
117129 })
118- k8sClientReader .GetCalls (createGetCallsFunc ())
130+ k8sClientReader .GetCalls (createGetCallsFunc (ngfPod , ngfReplicaSet ))
119131 })
120132
121133 Describe ("Normal case" , func () {
@@ -432,110 +444,83 @@ var _ = Describe("Collector", Ordered, func() {
432444
433445 It ("should error if the Pod's owner reference is nil" , func () {
434446 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+ ))
450455
451456 _ , err := dataCollector .Collect (ctx )
452457 Expect (err ).To (MatchError (expectedErr ))
453458 })
454459
455460 It ("should error if the Pod has multiple owner references" , func () {
456461 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" ,
474470 },
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+ ))
481479
482480 _ , err := dataCollector .Collect (ctx )
483481 Expect (err ).To (MatchError (expectedErr ))
484482 })
485483
486484 It ("should error if the Pod's owner reference is not a ReplicaSet" , func () {
487485 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" ,
501494 },
502- }
503- default :
504- Fail (fmt .Sprintf ("unknown type: %T" , typedObj ))
505- }
506- return nil
507- })
495+ },
496+ },
497+ },
498+ ))
508499
509500 _ , err := dataCollector .Collect (ctx )
510501 Expect (err ).To (MatchError (expectedErr ))
511502 })
512503
513504 It ("should error if the replica set's replicas is nil" , func () {
514505 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" ,
528514 },
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+ ))
539524
540525 _ , err := dataCollector .Collect (ctx )
541526 Expect (err ).To (MatchError (expectedErr ))
0 commit comments