@@ -144,7 +144,7 @@ func prepareForController(t *testing.T) error {
144144}
145145
146146func TestNewIPsecController (t * testing.T ) {
147- tests := []struct {
147+ testCases := []struct {
148148 name string
149149 setupEnv func () func ()
150150 expectedError bool
@@ -172,10 +172,10 @@ func TestNewIPsecController(t *testing.T) {
172172 },
173173 }
174174
175- for _ , tt := range tests {
176- t .Run (tt .name , func (t * testing.T ) {
175+ for _ , test := range testCases {
176+ t .Run (test .name , func (t * testing.T ) {
177177 // Setup environment
178- setupEnv := tt .setupEnv ()
178+ setupEnv := test .setupEnv ()
179179 setupEnv ()
180180
181181 // Setup patches
@@ -197,10 +197,10 @@ func TestNewIPsecController(t *testing.T) {
197197
198198 controller , err := NewIPsecController (k8sClient , mockMap , mockProg )
199199
200- if tt .expectedError {
200+ if test .expectedError {
201201 assert .Error (t , err )
202- if tt .errorContains != "" {
203- assert .Contains (t , err .Error (), tt .errorContains )
202+ if test .errorContains != "" {
203+ assert .Contains (t , err .Error (), test .errorContains )
204204 }
205205 assert .Nil (t , controller )
206206 } else {
@@ -224,16 +224,18 @@ func TestHandleKNIEvents(t *testing.T) {
224224 kmeshClient := fakeKmeshClientset .NewSimpleClientset ()
225225 patches .ApplyFuncReturn (kube .GetKmeshNodeInfoClient , kmeshClient , nil )
226226
227- // Create mock eBPF components
228- mockMap := & ebpf.Map {}
229- mockProg := & ebpf.Program {}
230- controller , err := NewIPsecController (k8sClient , mockMap , mockProg )
231- require .NoError (t , err )
232- require .NotNil (t , controller )
227+ prepare := func (t * testing.T ) * IPSecController {
228+ // Create mock eBPF components
229+ mockMap := & ebpf.Map {}
230+ mockProg := & ebpf.Program {}
231+ controller , err := NewIPsecController (k8sClient , mockMap , mockProg )
232+ assert .NoError (t , err )
233+ assert .NotNil (t , controller )
234+ return controller
235+ }
233236
234237 t .Run ("handleKNIAdd" , func (t * testing.T ) {
235- // Reset queue
236- controller .queue = workqueue .NewTypedRateLimitingQueue (workqueue .DefaultTypedControllerRateLimiter [any ]())
238+ controller := prepare (t )
237239
238240 // Test adding a remote node (should be added to queue)
239241 controller .handleKNIAdd (testRemoteNodeInfo )
@@ -246,7 +248,7 @@ func TestHandleKNIEvents(t *testing.T) {
246248 })
247249
248250 t .Run ("handleKNIUpdate" , func (t * testing.T ) {
249- controller . queue = workqueue . NewTypedRateLimitingQueue ( workqueue . DefaultTypedControllerRateLimiter [ any ]()) // Reset queue
251+ controller := prepare ( t )
250252
251253 // Test updating with same spec (should be ignored)
252254 controller .handleKNIUpdate (testRemoteNodeInfo , testRemoteNodeInfo )
@@ -266,6 +268,7 @@ func TestHandleKNIEvents(t *testing.T) {
266268 t .Run ("handleKNIDelete" , func (t * testing.T ) {
267269 // Test case 1: Normal delete of remote node
268270 t .Run ("Normal delete of remote node" , func (t * testing.T ) {
271+ controller := prepare (t )
269272 // Create patches for network namespace operations
270273 nsPatches := gomonkey .NewPatches ()
271274 defer nsPatches .Reset ()
@@ -307,6 +310,7 @@ func TestHandleKNIEvents(t *testing.T) {
307310
308311 // Test case 2: WithNetNSPath error
309312 t .Run ("network_namespace_error" , func (t * testing.T ) {
313+ controller := prepare (t )
310314 // Create patches for network namespace operations that fail
311315 nsPatches := gomonkey .NewPatches ()
312316 defer nsPatches .Reset ()
@@ -378,31 +382,29 @@ func TestMapOperations(t *testing.T) {
378382 {"invalid_cidr" , "not-a-cidr" , false },
379383 }
380384
381- for _ , tc := range testCases {
382- t .Run (tc .name , func (t * testing.T ) {
383- // Add CIDR to KNI map
384- err := controller .updateKNIMapCIDR (tc .cidr , kniMap )
385- if tc .expected {
386- require .NoError (t , err )
387-
388- // Verify add success
389- key , err := controller .generalKNIMapKey (tc .cidr )
390- require .NoError (t , err )
391- var value uint32
392- err = kniMap .Lookup (key , & value )
393- require .NoError (t , err )
394- assert .Equal (t , uint32 (1 ), value )
395-
396- // Delete CIDR
397- controller .deleteKNIMapCIDR (tc .cidr , kniMap )
398-
399- // Verify delete success
400- err = kniMap .Lookup (key , & value )
401- assert .Error (t , err ) // not found
402- } else {
403- assert .Error (t , err )
404- }
405- })
385+ for _ , tc := range testCases { // not use t.Run to avoid parallel execution issues
386+ // Add CIDR to KNI map
387+ err := controller .updateKNIMapCIDR (tc .cidr , kniMap )
388+ if tc .expected {
389+ require .NoError (t , err )
390+
391+ // Verify add success
392+ key , err := controller .generalKNIMapKey (tc .cidr )
393+ require .NoError (t , err )
394+ var value uint32
395+ err = kniMap .Lookup (key , & value )
396+ require .NoError (t , err )
397+ assert .Equal (t , uint32 (1 ), value )
398+
399+ // Delete CIDR
400+ controller .deleteKNIMapCIDR (tc .cidr , kniMap )
401+
402+ // Verify delete success
403+ err = kniMap .Lookup (key , & value )
404+ assert .Error (t , err ) // not found
405+ } else {
406+ assert .Error (t , err )
407+ }
406408 }
407409}
408410
@@ -420,20 +422,24 @@ func TestNodeOperations(t *testing.T) {
420422 clientPatches .ApplyFuncReturn (kube .GetKmeshNodeInfoClient , kmeshClient , nil )
421423 defer clientPatches .Reset ()
422424
423- // Create mock eBPF components
424- mockMap := & ebpf.Map {}
425- mockProg := & ebpf.Program {}
426-
427- t .Run ("createAndUpdateLocalKmeshNodeInfo" , func (t * testing.T ) {
425+ prepare := func (t * testing.T ) (* IPSecController , chan struct {}) {
426+ // Create mock eBPF components
427+ mockMap := & ebpf.Map {}
428+ mockProg := & ebpf.Program {}
428429 stopCh := make (chan struct {})
429- defer close (stopCh )
430430 controller , err := NewIPsecController (k8sClient , mockMap , mockProg )
431431 assert .NoError (t , err )
432432 assert .NotNil (t , controller )
433433 go controller .informer .Run (stopCh )
434434 if ! cache .WaitForCacheSync (stopCh , controller .informer .HasSynced ) {
435435 t .Fatal ("timed out waiting for caches to sync" )
436436 }
437+ return controller , stopCh
438+ }
439+
440+ t .Run ("createAndUpdateLocalKmeshNodeInfo" , func (t * testing.T ) {
441+ controller , stopCh := prepare (t )
442+ defer close (stopCh )
437443 // No node info
438444 node , err := controller .lister .KmeshNodeInfos ("kmesh-system" ).Get ("test-local-node" )
439445 assert .Error (t , err )
@@ -483,11 +489,8 @@ func TestNodeOperations(t *testing.T) {
483489 })
484490
485491 t .Run ("syncAllNodeInfo" , func (t * testing.T ) {
486- stopCh := make ( chan struct {} )
492+ controller , stopCh := prepare ( t )
487493 defer close (stopCh )
488- controller , err := NewIPsecController (k8sClient , mockMap , mockProg )
489- assert .NoError (t , err )
490- assert .NotNil (t , controller )
491494
492495 // Create remote node
493496 _ , err = kmeshClient .KmeshV1alpha1 ().KmeshNodeInfos ("kmesh-system" ).Create (context .TODO (), testRemoteNodeInfo , metav1.CreateOptions {})
@@ -514,11 +517,8 @@ func TestNodeOperations(t *testing.T) {
514517
515518 // test handle testRemoteNodeInfo
516519 t .Run ("handleOneNodeInfo" , func (t * testing.T ) {
517- stopCh := make ( chan struct {} )
520+ controller , stopCh := prepare ( t )
518521 defer close (stopCh )
519- controller , err := NewIPsecController (k8sClient , mockMap , mockProg )
520- assert .NoError (t , err )
521- assert .NotNil (t , controller )
522522
523523 patches := gomonkey .NewPatches ()
524524 defer patches .Reset ()
@@ -570,21 +570,26 @@ func TestProcessNextItem(t *testing.T) {
570570 clientPatches .ApplyFuncReturn (kube .GetKmeshNodeInfoClient , kmeshClient , nil )
571571 defer clientPatches .Reset ()
572572
573- // Create mock eBPF components
574- mockMap := & ebpf.Map {}
575- mockProg := & ebpf.Program {}
573+ prepare := func (t * testing.T ) (* IPSecController , chan struct {}) {
574+ // Create mock eBPF components
575+ mockMap := & ebpf.Map {}
576+ mockProg := & ebpf.Program {}
576577
577- t .Run ("successful_processing" , func (t * testing.T ) {
578578 controller , err := NewIPsecController (k8sClient , mockMap , mockProg )
579- require .NoError (t , err )
580- require .NotNil (t , controller )
579+ assert .NoError (t , err )
580+ assert .NotNil (t , controller )
581581
582582 stopCh := make (chan struct {})
583- defer close (stopCh )
584583 go controller .informer .Run (stopCh )
585584 if ! cache .WaitForCacheSync (stopCh , controller .informer .HasSynced ) {
586585 t .Fatal ("timed out waiting for caches to sync" )
587586 }
587+ return controller , stopCh
588+ }
589+
590+ t .Run ("successful_processing" , func (t * testing.T ) {
591+ controller , stopCh := prepare (t )
592+ defer close (stopCh )
588593
589594 // Add remote node to queue
590595 controller .queue .Add ("test-remote-node" )
@@ -603,16 +608,8 @@ func TestProcessNextItem(t *testing.T) {
603608 })
604609
605610 t .Run ("non_existent_node" , func (t * testing.T ) {
606- controller , err := NewIPsecController (k8sClient , mockMap , mockProg )
607- require .NoError (t , err )
608- require .NotNil (t , controller )
609-
610- stopCh := make (chan struct {})
611+ controller , stopCh := prepare (t )
611612 defer close (stopCh )
612- go controller .informer .Run (stopCh )
613- if ! cache .WaitForCacheSync (stopCh , controller .informer .HasSynced ) {
614- t .Fatal ("timed out waiting for caches to sync" )
615- }
616613
617614 // Test with non-existent node
618615 controller .queue .Add ("non-existent-node" )
@@ -622,18 +619,12 @@ func TestProcessNextItem(t *testing.T) {
622619 assert .Equal (t , 0 , controller .queue .Len ()) // Item should be removed from queue
623620 })
624621
625- // Test if the error returned by handleOneNodeInfo is not nil, the function should return true, and requeue the item or forget it based on the number of retries
622+ // Test if the error returned by handleOneNodeInfo is not nil, the function should return true, and requeue the item or forget it based on the number of requeues
626623 t .Run ("handleOneNodeInfo_err" , func (t * testing.T ) {
627- controller , err := NewIPsecController (k8sClient , mockMap , mockProg )
628- require .NoError (t , err )
629- require .NotNil (t , controller )
630-
631- stopCh := make (chan struct {})
624+ controller , stopCh := prepare (t )
632625 defer close (stopCh )
633- go controller .informer .Run (stopCh )
634- if ! cache .WaitForCacheSync (stopCh , controller .informer .HasSynced ) {
635- t .Fatal ("timed out waiting for caches to sync" )
636- }
626+
627+ // Add local node to queue
637628 controller .queue .Add ("test-local-node" )
638629 failPatches := gomonkey .NewPatches ()
639630 failPatches .ApplyPrivateMethod (reflect .TypeOf (controller ), "handleOneNodeInfo" , func (_ * IPSecController , _ * v1alpha1.KmeshNodeInfo ) error {
@@ -652,10 +643,13 @@ func TestProcessNextItem(t *testing.T) {
652643 })
653644 assert .NoError (t , err )
654645
655- for i := range MaxRetries {
646+ for exceptRetryCount := 1 ; exceptRetryCount <= MaxRetries + 1 ; exceptRetryCount ++ {
656647 result := controller .processNextItem ()
657- assert .True (t , result ) // Should still return true
658- assert .Equal (t , i + 1 % MaxRetries , controller .queue .NumRequeues ("test-local-node" )) // if i+1==MaxRetries then should be equal to 0, means it has been forgetten
648+ assert .True (t , result ) // Should still return true
649+ // NumRequeues("test-local-node") will return the number of times the item has been requeued
650+ // So, every time to call processNextItem, the number of requeues will increase by 1
651+ // When the number of requeues is greater than MaxRetries, the item will be forgotten
652+ assert .Equal (t , exceptRetryCount % (MaxRetries + 1 ), controller .queue .NumRequeues ("test-local-node" ))
659653 }
660654 })
661655}
0 commit comments