Skip to content

Commit 3a1d036

Browse files
gnufiedk8s-publishing-bot
authored andcommitted
UPSTREAM: 62827: fix csi data race in csi_attacher_test.go
xref : openshift/origin#19465 Origin-commit: f320c6dae53c0b459ef8420654baf66cbe8eb63a
1 parent 67bc66c commit 3a1d036

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

pkg/volume/csi/csi_attacher_test.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -165,39 +165,26 @@ func TestAttacherAttach(t *testing.T) {
165165
}
166166

167167
func TestAttacherWaitForVolumeAttachment(t *testing.T) {
168-
169-
plug, fakeWatcher, tmpDir := newTestWatchPlugin(t)
170-
defer os.RemoveAll(tmpDir)
171-
172-
attacher, err := plug.NewAttacher()
173-
if err != nil {
174-
t.Fatalf("failed to create new attacher: %v", err)
175-
}
176-
csiAttacher := attacher.(*csiAttacher)
177168
nodeName := "test-node"
178-
179169
testCases := []struct {
180170
name string
181171
initAttached bool
182172
finalAttached bool
183173
trigerWatchEventTime time.Duration
184174
initAttachErr *storage.VolumeError
185175
finalAttachErr *storage.VolumeError
186-
sleepTime time.Duration
187176
timeout time.Duration
188177
shouldFail bool
189178
}{
190179
{
191180
name: "attach success at get",
192181
initAttached: true,
193-
sleepTime: 10 * time.Millisecond,
194182
timeout: 50 * time.Millisecond,
195183
shouldFail: false,
196184
},
197185
{
198186
name: "attachment error ant get",
199187
initAttachErr: &storage.VolumeError{Message: "missing volume"},
200-
sleepTime: 10 * time.Millisecond,
201188
timeout: 30 * time.Millisecond,
202189
shouldFail: true,
203190
},
@@ -207,7 +194,6 @@ func TestAttacherWaitForVolumeAttachment(t *testing.T) {
207194
finalAttached: true,
208195
trigerWatchEventTime: 5 * time.Millisecond,
209196
timeout: 50 * time.Millisecond,
210-
sleepTime: 5 * time.Millisecond,
211197
shouldFail: false,
212198
},
213199
{
@@ -216,7 +202,6 @@ func TestAttacherWaitForVolumeAttachment(t *testing.T) {
216202
finalAttached: false,
217203
finalAttachErr: &storage.VolumeError{Message: "missing volume"},
218204
trigerWatchEventTime: 5 * time.Millisecond,
219-
sleepTime: 10 * time.Millisecond,
220205
timeout: 30 * time.Millisecond,
221206
shouldFail: true,
222207
},
@@ -226,32 +211,41 @@ func TestAttacherWaitForVolumeAttachment(t *testing.T) {
226211
finalAttached: true,
227212
trigerWatchEventTime: 100 * time.Millisecond,
228213
timeout: 50 * time.Millisecond,
229-
sleepTime: 5 * time.Millisecond,
230214
shouldFail: true,
231215
},
232216
}
233217

234218
for i, tc := range testCases {
235-
fakeWatcher.Reset()
219+
plug, fakeWatcher, tmpDir := newTestWatchPlugin(t)
220+
defer os.RemoveAll(tmpDir)
221+
222+
attacher, err := plug.NewAttacher()
223+
if err != nil {
224+
t.Fatalf("failed to create new attacher: %v", err)
225+
}
226+
csiAttacher := attacher.(*csiAttacher)
236227
t.Logf("running test: %v", tc.name)
237228
pvName := fmt.Sprintf("test-pv-%d", i)
238229
volID := fmt.Sprintf("test-vol-%d", i)
239230
attachID := getAttachmentName(volID, testDriver, nodeName)
240231
attachment := makeTestAttachment(attachID, nodeName, pvName)
241232
attachment.Status.Attached = tc.initAttached
242233
attachment.Status.AttachError = tc.initAttachErr
243-
csiAttacher.waitSleepTime = tc.sleepTime
244-
_, err := csiAttacher.k8s.StorageV1beta1().VolumeAttachments().Create(attachment)
234+
_, err = csiAttacher.k8s.StorageV1beta1().VolumeAttachments().Create(attachment)
245235
if err != nil {
246236
t.Fatalf("failed to attach: %v", err)
247237
}
248238

239+
trigerWatchEventTime := tc.trigerWatchEventTime
240+
finalAttached := tc.finalAttached
241+
finalAttachErr := tc.finalAttachErr
249242
// after timeout, fakeWatcher will be closed by csiAttacher.waitForVolumeAttachment
250243
if tc.trigerWatchEventTime > 0 && tc.trigerWatchEventTime < tc.timeout {
251244
go func() {
252-
time.Sleep(tc.trigerWatchEventTime)
253-
attachment.Status.Attached = tc.finalAttached
254-
attachment.Status.AttachError = tc.finalAttachErr
245+
time.Sleep(trigerWatchEventTime)
246+
attachment := makeTestAttachment(attachID, nodeName, pvName)
247+
attachment.Status.Attached = finalAttached
248+
attachment.Status.AttachError = finalAttachErr
255249
fakeWatcher.Modify(attachment)
256250
}()
257251
}
@@ -677,14 +671,14 @@ func TestAttacherUnmountDevice(t *testing.T) {
677671
}
678672

679673
// create a plugin mgr to load plugins and setup a fake client
680-
func newTestWatchPlugin(t *testing.T) (*csiPlugin, *watch.FakeWatcher, string) {
674+
func newTestWatchPlugin(t *testing.T) (*csiPlugin, *watch.RaceFreeFakeWatcher, string) {
681675
tmpDir, err := utiltesting.MkTmpdir("csi-test")
682676
if err != nil {
683677
t.Fatalf("can't create temp dir: %v", err)
684678
}
685679

686680
fakeClient := fakeclient.NewSimpleClientset()
687-
fakeWatcher := watch.NewFake()
681+
fakeWatcher := watch.NewRaceFreeFake()
688682
fakeClient.Fake.PrependWatchReactor("*", core.DefaultWatchReactor(fakeWatcher, nil))
689683
fakeClient.Fake.WatchReactionChain = fakeClient.Fake.WatchReactionChain[:1]
690684
host := volumetest.NewFakeVolumeHost(

0 commit comments

Comments
 (0)