Skip to content

Commit 915bbfc

Browse files
committed
fix data race bug in TestProcessNextItem
Signed-off-by: aicee <hhbin2000@foxmail.com>
1 parent 198a32c commit 915bbfc

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

pkg/controller/encryption/ipsec/ipsec_controller_test.go

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func TestHandleKNIEvents(t *testing.T) {
290290
deleteMapCallCount := 0
291291
deleteMapPatches := gomonkey.NewPatches()
292292
defer deleteMapPatches.Reset()
293-
deleteMapPatches.ApplyPrivateMethod(reflect.TypeOf(&IPSecController{}), "deleteKNIMapCIDR", func(c *IPSecController, remoteCIDR string, mapfd *ebpf.Map) {
293+
deleteMapPatches.ApplyPrivateMethod(reflect.TypeOf(controller), "deleteKNIMapCIDR", func(c *IPSecController, remoteCIDR string, mapfd *ebpf.Map) {
294294
deleteMapCallCount++
295295
// Verify that the remoteCIDR is one of the remote node's PodCIDRs
296296
assert.Contains(t, testRemoteNodeInfo.Spec.PodCIDRs, remoteCIDR)
@@ -319,7 +319,7 @@ func TestHandleKNIEvents(t *testing.T) {
319319
deleteMapCalled := false
320320
deleteMapPatches := gomonkey.NewPatches()
321321
defer deleteMapPatches.Reset()
322-
deleteMapPatches.ApplyPrivateMethod(reflect.TypeOf(&IPSecController{}), "deleteKNIMapCIDR", func(c *IPSecController, remoteCIDR string, mapfd *ebpf.Map) {
322+
deleteMapPatches.ApplyPrivateMethod(reflect.TypeOf(controller), "deleteKNIMapCIDR", func(c *IPSecController, remoteCIDR string, mapfd *ebpf.Map) {
323323
deleteMapCalled = true
324324
})
325325

@@ -495,7 +495,7 @@ func TestNodeOperations(t *testing.T) {
495495

496496
// Patch handleOneNodeInfo
497497
syncPatch := gomonkey.NewPatches()
498-
syncPatch.ApplyPrivateMethod(reflect.TypeOf(&IPSecController{}), "handleOneNodeInfo", func(c *IPSecController, node *v1alpha1.KmeshNodeInfo) error {
498+
syncPatch.ApplyPrivateMethod(reflect.TypeOf(controller), "handleOneNodeInfo", func(c *IPSecController, node *v1alpha1.KmeshNodeInfo) error {
499499
if node.Name == testRemoteNodeInfo.Name { // test if get remote node info and ignore local node info
500500
return nil
501501
}
@@ -527,7 +527,7 @@ func TestNodeOperations(t *testing.T) {
527527
for _, podCIDR := range testRemoteNodeInfo.Spec.PodCIDRs {
528528
podCIDRSet[podCIDR] = true
529529
}
530-
patches.ApplyPrivateMethod(&IpSecHandler{}, "CreateXfrmRule", func(_ *IpSecHandler, localNode *v1alpha1.KmeshNodeInfo, remoteNode *v1alpha1.KmeshNodeInfo) error {
530+
patches.ApplyPrivateMethod(reflect.TypeOf(controller.ipsecHandler), "CreateXfrmRule", func(_ *IpSecHandler, localNode *v1alpha1.KmeshNodeInfo, remoteNode *v1alpha1.KmeshNodeInfo) error {
531531
// mock, remote node info should be same with testRemoteNodeInfo
532532
for _, podCIDR := range remoteNode.Spec.PodCIDRs {
533533
if !podCIDRSet[podCIDR] {
@@ -536,7 +536,7 @@ func TestNodeOperations(t *testing.T) {
536536
}
537537
return nil
538538
})
539-
patches.ApplyPrivateMethod(&IPSecController{}, "updateKNIMapCIDR", func(c *IPSecController, remoteCIDR string, mapfd *ebpf.Map) error {
539+
patches.ApplyPrivateMethod(reflect.TypeOf(controller), "updateKNIMapCIDR", func(c *IPSecController, remoteCIDR string, mapfd *ebpf.Map) error {
540540
// mock, remote node info should be same with testRemoteNodeInfo
541541
if !podCIDRSet[remoteCIDR] {
542542
return fmt.Errorf("remote node info podCIDRs is not equal")
@@ -565,10 +565,6 @@ func TestProcessNextItem(t *testing.T) {
565565
k8sClient := fake.NewSimpleClientset(testK8sNode)
566566
kmeshClient := fakeKmeshClientset.NewSimpleClientset()
567567

568-
// Create the remote node in the client
569-
_, err = kmeshClient.KmeshV1alpha1().KmeshNodeInfos("kmesh-system").Create(context.TODO(), testRemoteNodeInfo, metav1.CreateOptions{})
570-
require.NoError(t, err)
571-
572568
// Apply patches for kube.GetKmeshNodeInfoClient to return our fake client
573569
clientPatches := gomonkey.NewPatches()
574570
clientPatches.ApplyFuncReturn(kube.GetKmeshNodeInfoClient, kmeshClient, nil)
@@ -578,40 +574,45 @@ func TestProcessNextItem(t *testing.T) {
578574
mockMap := &ebpf.Map{}
579575
mockProg := &ebpf.Program{}
580576

581-
controller, err := NewIPsecController(k8sClient, mockMap, mockProg)
582-
require.NoError(t, err)
583-
require.NotNil(t, controller)
584-
585-
stopCh := make(chan struct{})
586-
defer close(stopCh)
587-
go controller.informer.Run(stopCh)
588-
if !cache.WaitForCacheSync(stopCh, controller.informer.HasSynced) {
589-
t.Fatal("timed out waiting for caches to sync")
590-
}
591577
t.Run("successful_processing", func(t *testing.T) {
592-
// Reset queue
593-
controller.queue = workqueue.NewTypedRateLimitingQueue(workqueue.DefaultTypedControllerRateLimiter[any]())
578+
controller, err := NewIPsecController(k8sClient, mockMap, mockProg)
579+
require.NoError(t, err)
580+
require.NotNil(t, controller)
581+
582+
stopCh := make(chan struct{})
583+
defer close(stopCh)
584+
go controller.informer.Run(stopCh)
585+
if !cache.WaitForCacheSync(stopCh, controller.informer.HasSynced) {
586+
t.Fatal("timed out waiting for caches to sync")
587+
}
594588

595589
// Add remote node to queue
596590
controller.queue.Add("test-remote-node")
597591

598592
// Mock handleOneNodeInfo to avoid complex IPsec operations
599593
handlerPatches := gomonkey.NewPatches()
600-
handlerPatches.ApplyPrivateMethod(reflect.TypeOf(&IPSecController{}), "handleOneNodeInfo", func(_ *IPSecController, _ *v1alpha1.KmeshNodeInfo) error {
594+
handlerPatches.ApplyPrivateMethod(reflect.TypeOf(controller), "handleOneNodeInfo", func(_ *IPSecController, _ *v1alpha1.KmeshNodeInfo) error {
601595
return nil // Simulate success
602596
})
603597
defer handlerPatches.Reset()
604598

605599
// Process the item
606600
result := controller.processNextItem()
607-
controller.queue.Done("test-remote-node")
608601
assert.True(t, result) // Should return true indicating continue processing
609602
assert.Equal(t, 0, controller.queue.Len()) // Item should be removed from queue
610603
})
611604

612605
t.Run("non_existent_node", func(t *testing.T) {
613-
// Reset queue
614-
controller.queue = workqueue.NewTypedRateLimitingQueue(workqueue.DefaultTypedControllerRateLimiter[any]())
606+
controller, err := NewIPsecController(k8sClient, mockMap, mockProg)
607+
require.NoError(t, err)
608+
require.NotNil(t, controller)
609+
610+
stopCh := make(chan struct{})
611+
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+
}
615616

616617
// Test with non-existent node
617618
controller.queue.Add("non-existent-node")
@@ -623,12 +624,19 @@ func TestProcessNextItem(t *testing.T) {
623624

624625
// 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
625626
t.Run("handleOneNodeInfo_err", func(t *testing.T) {
626-
// Reset queue
627-
controller.queue = workqueue.NewTypedRateLimitingQueue(workqueue.DefaultTypedControllerRateLimiter[any]())
627+
controller, err := NewIPsecController(k8sClient, mockMap, mockProg)
628+
require.NoError(t, err)
629+
require.NotNil(t, controller)
628630

631+
stopCh := make(chan struct{})
632+
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+
}
629637
controller.queue.Add("test-local-node")
630638
failPatches := gomonkey.NewPatches()
631-
failPatches.ApplyPrivateMethod(reflect.TypeOf(&IPSecController{}), "handleOneNodeInfo", func(_ *IPSecController, _ *v1alpha1.KmeshNodeInfo) error {
639+
failPatches.ApplyPrivateMethod(reflect.TypeOf(controller), "handleOneNodeInfo", func(_ *IPSecController, _ *v1alpha1.KmeshNodeInfo) error {
632640
return fmt.Errorf("test error") // Simulate failure
633641
})
634642
defer failPatches.Reset()

0 commit comments

Comments
 (0)