@@ -165,39 +165,26 @@ func TestAttacherAttach(t *testing.T) {
165
165
}
166
166
167
167
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 )
177
168
nodeName := "test-node"
178
-
179
169
testCases := []struct {
180
170
name string
181
171
initAttached bool
182
172
finalAttached bool
183
173
trigerWatchEventTime time.Duration
184
174
initAttachErr * storage.VolumeError
185
175
finalAttachErr * storage.VolumeError
186
- sleepTime time.Duration
187
176
timeout time.Duration
188
177
shouldFail bool
189
178
}{
190
179
{
191
180
name : "attach success at get" ,
192
181
initAttached : true ,
193
- sleepTime : 10 * time .Millisecond ,
194
182
timeout : 50 * time .Millisecond ,
195
183
shouldFail : false ,
196
184
},
197
185
{
198
186
name : "attachment error ant get" ,
199
187
initAttachErr : & storage.VolumeError {Message : "missing volume" },
200
- sleepTime : 10 * time .Millisecond ,
201
188
timeout : 30 * time .Millisecond ,
202
189
shouldFail : true ,
203
190
},
@@ -207,7 +194,6 @@ func TestAttacherWaitForVolumeAttachment(t *testing.T) {
207
194
finalAttached : true ,
208
195
trigerWatchEventTime : 5 * time .Millisecond ,
209
196
timeout : 50 * time .Millisecond ,
210
- sleepTime : 5 * time .Millisecond ,
211
197
shouldFail : false ,
212
198
},
213
199
{
@@ -216,7 +202,6 @@ func TestAttacherWaitForVolumeAttachment(t *testing.T) {
216
202
finalAttached : false ,
217
203
finalAttachErr : & storage.VolumeError {Message : "missing volume" },
218
204
trigerWatchEventTime : 5 * time .Millisecond ,
219
- sleepTime : 10 * time .Millisecond ,
220
205
timeout : 30 * time .Millisecond ,
221
206
shouldFail : true ,
222
207
},
@@ -226,32 +211,41 @@ func TestAttacherWaitForVolumeAttachment(t *testing.T) {
226
211
finalAttached : true ,
227
212
trigerWatchEventTime : 100 * time .Millisecond ,
228
213
timeout : 50 * time .Millisecond ,
229
- sleepTime : 5 * time .Millisecond ,
230
214
shouldFail : true ,
231
215
},
232
216
}
233
217
234
218
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 )
236
227
t .Logf ("running test: %v" , tc .name )
237
228
pvName := fmt .Sprintf ("test-pv-%d" , i )
238
229
volID := fmt .Sprintf ("test-vol-%d" , i )
239
230
attachID := getAttachmentName (volID , testDriver , nodeName )
240
231
attachment := makeTestAttachment (attachID , nodeName , pvName )
241
232
attachment .Status .Attached = tc .initAttached
242
233
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 )
245
235
if err != nil {
246
236
t .Fatalf ("failed to attach: %v" , err )
247
237
}
248
238
239
+ trigerWatchEventTime := tc .trigerWatchEventTime
240
+ finalAttached := tc .finalAttached
241
+ finalAttachErr := tc .finalAttachErr
249
242
// after timeout, fakeWatcher will be closed by csiAttacher.waitForVolumeAttachment
250
243
if tc .trigerWatchEventTime > 0 && tc .trigerWatchEventTime < tc .timeout {
251
244
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
255
249
fakeWatcher .Modify (attachment )
256
250
}()
257
251
}
@@ -677,14 +671,14 @@ func TestAttacherUnmountDevice(t *testing.T) {
677
671
}
678
672
679
673
// 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 ) {
681
675
tmpDir , err := utiltesting .MkTmpdir ("csi-test" )
682
676
if err != nil {
683
677
t .Fatalf ("can't create temp dir: %v" , err )
684
678
}
685
679
686
680
fakeClient := fakeclient .NewSimpleClientset ()
687
- fakeWatcher := watch .NewFake ()
681
+ fakeWatcher := watch .NewRaceFreeFake ()
688
682
fakeClient .Fake .PrependWatchReactor ("*" , core .DefaultWatchReactor (fakeWatcher , nil ))
689
683
fakeClient .Fake .WatchReactionChain = fakeClient .Fake .WatchReactionChain [:1 ]
690
684
host := volumetest .NewFakeVolumeHost (
0 commit comments