@@ -38,8 +38,10 @@ import (
38
38
"k8s.io/apimachinery/pkg/watch"
39
39
utilfeature "k8s.io/apiserver/pkg/util/feature"
40
40
"k8s.io/client-go/informers"
41
+ storageinformer "k8s.io/client-go/informers/storage/v1"
41
42
clientset "k8s.io/client-go/kubernetes"
42
43
fakeclient "k8s.io/client-go/kubernetes/fake"
44
+ storagelister "k8s.io/client-go/listers/storage/v1"
43
45
core "k8s.io/client-go/testing"
44
46
utiltesting "k8s.io/client-go/util/testing"
45
47
featuregatetesting "k8s.io/component-base/featuregate/testing"
@@ -106,7 +108,9 @@ func markVolumeAttached(t *testing.T, client clientset.Interface, watch *watch.R
106
108
if err != nil {
107
109
t .Error (err )
108
110
}
109
- watch .Modify (attach )
111
+ if watch != nil {
112
+ watch .Modify (attach )
113
+ }
110
114
}
111
115
}
112
116
@@ -197,7 +201,7 @@ func TestAttacherAttach(t *testing.T) {
197
201
for _ , tc := range testCases {
198
202
t .Run (tc .name , func (t * testing.T ) {
199
203
t .Logf ("test case: %s" , tc .name )
200
- plug , fakeWatcher , tmpDir , _ := newTestWatchPlugin (t , nil )
204
+ plug , fakeWatcher , tmpDir , _ := newTestWatchPlugin (t , nil , false )
201
205
defer os .RemoveAll (tmpDir )
202
206
203
207
attacher , err := plug .NewAttacher ()
@@ -281,7 +285,7 @@ func TestAttacherAttachWithInline(t *testing.T) {
281
285
for _ , tc := range testCases {
282
286
t .Run (tc .name , func (t * testing.T ) {
283
287
t .Logf ("test case: %s" , tc .name )
284
- plug , fakeWatcher , tmpDir , _ := newTestWatchPlugin (t , nil )
288
+ plug , fakeWatcher , tmpDir , _ := newTestWatchPlugin (t , nil , false )
285
289
defer os .RemoveAll (tmpDir )
286
290
287
291
attacher , err := plug .NewAttacher ()
@@ -349,7 +353,7 @@ func TestAttacherWithCSIDriver(t *testing.T) {
349
353
getTestCSIDriver ("attachable" , nil , & bTrue , nil ),
350
354
getTestCSIDriver ("nil" , nil , nil , nil ),
351
355
)
352
- plug , fakeWatcher , tmpDir , _ := newTestWatchPlugin (t , fakeClient )
356
+ plug , _ , tmpDir , _ := newTestWatchPlugin (t , fakeClient , true )
353
357
defer os .RemoveAll (tmpDir )
354
358
355
359
attacher , err := plug .NewAttacher ()
@@ -390,7 +394,7 @@ func TestAttacherWithCSIDriver(t *testing.T) {
390
394
status := storage.VolumeAttachmentStatus {
391
395
Attached : true ,
392
396
}
393
- markVolumeAttached (t , csiAttacher .k8s , fakeWatcher , expectedAttachID , status )
397
+ markVolumeAttached (t , csiAttacher .k8s , nil , expectedAttachID , status )
394
398
}
395
399
wg .Wait ()
396
400
})
@@ -515,7 +519,7 @@ func TestAttacherWaitForAttach(t *testing.T) {
515
519
516
520
for _ , test := range tests {
517
521
t .Run (test .name , func (t * testing.T ) {
518
- plug , _ , tmpDir , _ := newTestWatchPlugin (t , nil )
522
+ plug , _ , tmpDir , _ := newTestWatchPlugin (t , nil , true )
519
523
defer os .RemoveAll (tmpDir )
520
524
521
525
attacher , err := plug .NewAttacher ()
@@ -597,7 +601,7 @@ func TestAttacherWaitForAttachWithInline(t *testing.T) {
597
601
598
602
for _ , test := range tests {
599
603
t .Run (test .name , func (t * testing.T ) {
600
- plug , _ , tmpDir , _ := newTestWatchPlugin (t , nil )
604
+ plug , _ , tmpDir , _ := newTestWatchPlugin (t , nil , true )
601
605
defer os .RemoveAll (tmpDir )
602
606
603
607
attacher , err := plug .NewAttacher ()
@@ -684,7 +688,7 @@ func TestAttacherWaitForVolumeAttachment(t *testing.T) {
684
688
685
689
for i , tc := range testCases {
686
690
t .Run (tc .name , func (t * testing.T ) {
687
- plug , fakeWatcher , tmpDir , _ := newTestWatchPlugin (t , nil )
691
+ plug , fakeWatcher , tmpDir , _ := newTestWatchPlugin (t , nil , false )
688
692
defer os .RemoveAll (tmpDir )
689
693
690
694
attacher , err := plug .NewAttacher ()
@@ -941,7 +945,7 @@ func TestAttacherDetach(t *testing.T) {
941
945
for _ , tc := range testCases {
942
946
t .Run (tc .name , func (t * testing.T ) {
943
947
t .Logf ("running test: %v" , tc .name )
944
- plug , fakeWatcher , tmpDir , client := newTestWatchPlugin (t , nil )
948
+ plug , fakeWatcher , tmpDir , client := newTestWatchPlugin (t , nil , false )
945
949
defer os .RemoveAll (tmpDir )
946
950
if tc .reactor != nil {
947
951
client .PrependReactor ("*" , "*" , tc .reactor )
@@ -998,7 +1002,7 @@ func TestAttacherDetach(t *testing.T) {
998
1002
func TestAttacherGetDeviceMountPath (t * testing.T ) {
999
1003
// Setup
1000
1004
// Create a new attacher
1001
- plug , _ , tmpDir , _ := newTestWatchPlugin (t , nil )
1005
+ plug , _ , tmpDir , _ := newTestWatchPlugin (t , nil , true )
1002
1006
defer os .RemoveAll (tmpDir )
1003
1007
attacher , err0 := plug .NewAttacher ()
1004
1008
if err0 != nil {
@@ -1163,7 +1167,7 @@ func TestAttacherMountDevice(t *testing.T) {
1163
1167
1164
1168
// Setup
1165
1169
// Create a new attacher
1166
- plug , fakeWatcher , tmpDir , _ := newTestWatchPlugin (t , nil )
1170
+ plug , fakeWatcher , tmpDir , _ := newTestWatchPlugin (t , nil , false )
1167
1171
defer os .RemoveAll (tmpDir )
1168
1172
attacher , err0 := plug .NewAttacher ()
1169
1173
if err0 != nil {
@@ -1314,7 +1318,7 @@ func TestAttacherMountDeviceWithInline(t *testing.T) {
1314
1318
1315
1319
// Setup
1316
1320
// Create a new attacher
1317
- plug , fakeWatcher , tmpDir , _ := newTestWatchPlugin (t , nil )
1321
+ plug , fakeWatcher , tmpDir , _ := newTestWatchPlugin (t , nil , false )
1318
1322
defer os .RemoveAll (tmpDir )
1319
1323
attacher , err0 := plug .NewAttacher ()
1320
1324
if err0 != nil {
@@ -1442,7 +1446,7 @@ func TestAttacherUnmountDevice(t *testing.T) {
1442
1446
t .Logf ("Running test case: %s" , tc .testName )
1443
1447
// Setup
1444
1448
// Create a new attacher
1445
- plug , _ , tmpDir , _ := newTestWatchPlugin (t , nil )
1449
+ plug , _ , tmpDir , _ := newTestWatchPlugin (t , nil , true )
1446
1450
defer os .RemoveAll (tmpDir )
1447
1451
attacher , err0 := plug .NewAttacher ()
1448
1452
if err0 != nil {
@@ -1529,7 +1533,7 @@ func TestAttacherUnmountDevice(t *testing.T) {
1529
1533
}
1530
1534
1531
1535
// create a plugin mgr to load plugins and setup a fake client
1532
- func newTestWatchPlugin (t * testing.T , fakeClient * fakeclient.Clientset ) (* csiPlugin , * watch.RaceFreeFakeWatcher , string , * fakeclient.Clientset ) {
1536
+ func newTestWatchPlugin (t * testing.T , fakeClient * fakeclient.Clientset , setupInformer bool ) (* csiPlugin , * watch.RaceFreeFakeWatcher , string , * fakeclient.Clientset ) {
1533
1537
tmpDir , err := utiltesting .MkTmpdir ("csi-test" )
1534
1538
if err != nil {
1535
1539
t .Fatalf ("can't create temp dir: %v" , err )
@@ -1545,12 +1549,24 @@ func newTestWatchPlugin(t *testing.T, fakeClient *fakeclient.Clientset) (*csiPlu
1545
1549
Spec : v1.NodeSpec {},
1546
1550
})
1547
1551
fakeWatcher := watch .NewRaceFreeFake ()
1548
- fakeClient .Fake .PrependWatchReactor ("volumeattachments" , core .DefaultWatchReactor (fakeWatcher , nil ))
1552
+ if ! setupInformer {
1553
+ // TODO: In the fakeClient, if default watchReactor is overwritten, the volumeAttachmentInformer
1554
+ // and the csiAttacher.Attach both endup reading from same channel causing hang in Attach().
1555
+ // So, until this is fixed, we don't overwrite default reactor while setting up volumeAttachment informer.
1556
+ fakeClient .Fake .PrependWatchReactor ("volumeattachments" , core .DefaultWatchReactor (fakeWatcher , nil ))
1557
+ }
1549
1558
1550
1559
// Start informer for CSIDrivers.
1551
1560
factory := informers .NewSharedInformerFactory (fakeClient , CsiResyncPeriod )
1552
1561
csiDriverInformer := factory .Storage ().V1 ().CSIDrivers ()
1553
1562
csiDriverLister := csiDriverInformer .Lister ()
1563
+ var volumeAttachmentInformer storageinformer.VolumeAttachmentInformer
1564
+ var volumeAttachmentLister storagelister.VolumeAttachmentLister
1565
+ if setupInformer {
1566
+ volumeAttachmentInformer = factory .Storage ().V1 ().VolumeAttachments ()
1567
+ volumeAttachmentLister = volumeAttachmentInformer .Lister ()
1568
+ }
1569
+
1554
1570
factory .Start (wait .NeverStop )
1555
1571
1556
1572
host := volumetest .NewFakeVolumeHostWithCSINodeName (t ,
@@ -1559,6 +1575,7 @@ func newTestWatchPlugin(t *testing.T, fakeClient *fakeclient.Clientset) (*csiPlu
1559
1575
ProbeVolumePlugins (),
1560
1576
"fakeNode" ,
1561
1577
csiDriverLister ,
1578
+ volumeAttachmentLister ,
1562
1579
)
1563
1580
plugMgr := host .GetPluginMgr ()
1564
1581
@@ -1577,5 +1594,10 @@ func newTestWatchPlugin(t *testing.T, fakeClient *fakeclient.Clientset) (*csiPlu
1577
1594
return csiDriverInformer .Informer ().HasSynced (), nil
1578
1595
})
1579
1596
1597
+ if volumeAttachmentInformer != nil {
1598
+ wait .PollImmediate (TestInformerSyncPeriod , TestInformerSyncTimeout , func () (bool , error ) {
1599
+ return volumeAttachmentInformer .Informer ().HasSynced (), nil
1600
+ })
1601
+ }
1580
1602
return csiPlug , fakeWatcher , tmpDir , fakeClient
1581
1603
}
0 commit comments