From 0bb911f90a62be9647b23b37d86770f79dcc16f6 Mon Sep 17 00:00:00 2001 From: pacoxu Date: Sat, 13 Mar 2021 22:52:24 +0800 Subject: [PATCH] fix two data race in volume reconciler ut 99815 --- .../volumemanager/reconciler/reconciler_test.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/kubelet/volumemanager/reconciler/reconciler_test.go b/pkg/kubelet/volumemanager/reconciler/reconciler_test.go index bfe5516a169..10bb3080610 100644 --- a/pkg/kubelet/volumemanager/reconciler/reconciler_test.go +++ b/pkg/kubelet/volumemanager/reconciler/reconciler_test.go @@ -1094,6 +1094,8 @@ func Test_Run_Positive_VolumeFSResizeControllerAttachEnabled(t *testing.T) { }, } + // deep copy before reconciler runs to avoid data race. + pvWithSize := pv.DeepCopy() volumePluginMgr, fakePlugin := volumetesting.GetTestKubeletVolumePluginMgr(t) dsw := cache.NewDesiredStateOfWorld(volumePluginMgr) asw := cache.NewActualStateOfWorld(nodeName, volumePluginMgr) @@ -1138,8 +1140,8 @@ func Test_Run_Positive_VolumeFSResizeControllerAttachEnabled(t *testing.T) { // Start the reconciler to fill ASW. stopChan, stoppedChan := make(chan struct{}), make(chan struct{}) go func() { + defer close(stoppedChan) reconciler.Run(stopChan) - close(stoppedChan) }() waitForMount(t, fakePlugin, volumeName, asw) // Stop the reconciler. @@ -1147,8 +1149,8 @@ func Test_Run_Positive_VolumeFSResizeControllerAttachEnabled(t *testing.T) { <-stoppedChan // Simulate what DSOWP does - pv.Spec.Capacity[v1.ResourceStorage] = tc.newPVSize - volumeSpec = &volume.Spec{PersistentVolume: pv} + pvWithSize.Spec.Capacity[v1.ResourceStorage] = tc.newPVSize + volumeSpec = &volume.Spec{PersistentVolume: pvWithSize} dsw.AddPodToVolume(podName, pod, volumeSpec, volumeSpec.Name(), "" /* volumeGidValue */) // mark volume as resize required asw.MarkFSResizeRequired(volumeName, podName) @@ -1806,6 +1808,8 @@ func Test_Run_Positive_VolumeMountControllerAttachEnabledRace(t *testing.T) { // 1. Add a volume to DSW and wait until it's mounted volumeSpec := &volume.Spec{Volume: &pod.Spec.Volumes[0]} + // copy before reconciler runs to avoid data race. + volumeSpecCopy := &volume.Spec{Volume: &pod.Spec.Volumes[0]} podName := util.GetUniquePodName(pod) generatedVolumeName, err := dsw.AddPodToVolume( podName, pod, volumeSpec, volumeSpec.Name(), "" /* volumeGidValue */) @@ -1832,7 +1836,7 @@ func Test_Run_Positive_VolumeMountControllerAttachEnabledRace(t *testing.T) { // 3. While a volume is being unmounted, add it back to the desired state of world t.Logf("UnmountDevice called") generatedVolumeName, err = dsw.AddPodToVolume( - podName, pod, volumeSpec, volumeSpec.Name(), "" /* volumeGidValue */) + podName, pod, volumeSpecCopy, volumeSpec.Name(), "" /* volumeGidValue */) dsw.MarkVolumesReportedInUse([]v1.UniqueVolumeName{generatedVolumeName}) return nil }