@@ -880,6 +880,7 @@ func TestPluginCanAttach(t *testing.T) {
880
880
driverName string
881
881
spec * volume.Spec
882
882
canAttach bool
883
+ shouldFail bool
883
884
}{
884
885
{
885
886
name : "non-attachable inline" ,
@@ -893,6 +894,19 @@ func TestPluginCanAttach(t *testing.T) {
893
894
spec : volume .NewSpecFromPersistentVolume (makeTestPV ("test-vol" , 20 , "attachable-pv" , testVol ), true ),
894
895
canAttach : true ,
895
896
},
897
+ {
898
+ name : "incomplete spec" ,
899
+ driverName : "attachable-pv" ,
900
+ spec : & volume.Spec {ReadOnly : true },
901
+ canAttach : false ,
902
+ shouldFail : true ,
903
+ },
904
+ {
905
+ name : "nil spec" ,
906
+ driverName : "attachable-pv" ,
907
+ canAttach : false ,
908
+ shouldFail : true ,
909
+ },
896
910
}
897
911
898
912
for _ , test := range tests {
@@ -902,10 +916,80 @@ func TestPluginCanAttach(t *testing.T) {
902
916
plug , tmpDir := newTestPlugin (t , fakeCSIClient )
903
917
defer os .RemoveAll (tmpDir )
904
918
905
- pluginCanAttach := plug .CanAttach (test .spec )
919
+ pluginCanAttach , err := plug .CanAttach (test .spec )
920
+ if err != nil && ! test .shouldFail {
921
+ t .Fatalf ("unexected plugin.CanAttach error: %s" , err )
922
+ }
906
923
if pluginCanAttach != test .canAttach {
907
924
t .Fatalf ("expecting plugin.CanAttach %t got %t" , test .canAttach , pluginCanAttach )
908
- return
925
+ }
926
+ })
927
+ }
928
+ }
929
+
930
+ func TestPluginFindAttachablePlugin (t * testing.T ) {
931
+ defer utilfeaturetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .CSIInlineVolume , true )()
932
+ tests := []struct {
933
+ name string
934
+ driverName string
935
+ spec * volume.Spec
936
+ canAttach bool
937
+ shouldFail bool
938
+ }{
939
+ {
940
+ name : "non-attachable inline" ,
941
+ driverName : "attachable-inline" ,
942
+ spec : volume .NewSpecFromVolume (makeTestVol ("test-vol" , "attachable-inline" )),
943
+ canAttach : false ,
944
+ },
945
+ {
946
+ name : "attachable PV" ,
947
+ driverName : "attachable-pv" ,
948
+ spec : volume .NewSpecFromPersistentVolume (makeTestPV ("test-vol" , 20 , "attachable-pv" , testVol ), true ),
949
+ canAttach : true ,
950
+ },
951
+ {
952
+ name : "incomplete spec" ,
953
+ driverName : "attachable-pv" ,
954
+ spec : & volume.Spec {ReadOnly : true },
955
+ canAttach : false ,
956
+ shouldFail : true ,
957
+ },
958
+ {
959
+ name : "nil spec" ,
960
+ driverName : "attachable-pv" ,
961
+ canAttach : false ,
962
+ shouldFail : true ,
963
+ },
964
+ }
965
+
966
+ for _ , test := range tests {
967
+ t .Run (test .name , func (t * testing.T ) {
968
+ tmpDir , err := utiltesting .MkTmpdir ("csi-test" )
969
+ if err != nil {
970
+ t .Fatalf ("can't create temp dir: %v" , err )
971
+ }
972
+ defer os .RemoveAll (tmpDir )
973
+
974
+ client := fakeclient .NewSimpleClientset (getCSIDriver (test .driverName , nil , & test .canAttach ))
975
+ factory := informers .NewSharedInformerFactory (client , csiResyncPeriod )
976
+ host := volumetest .NewFakeVolumeHostWithCSINodeName (
977
+ tmpDir ,
978
+ client ,
979
+ nil ,
980
+ "fakeNode" ,
981
+ factory .Storage ().V1beta1 ().CSIDrivers ().Lister (),
982
+ )
983
+
984
+ plugMgr := & volume.VolumePluginMgr {}
985
+ plugMgr .InitPlugins (ProbeVolumePlugins (), nil /* prober */ , host )
986
+
987
+ plugin , err := plugMgr .FindAttachablePluginBySpec (test .spec )
988
+ if err != nil && ! test .shouldFail {
989
+ t .Fatalf ("unexected error calling pluginMgr.FindAttachablePluginBySpec: %s" , err )
990
+ }
991
+ if (plugin != nil ) != test .canAttach {
992
+ t .Fatal ("expecting attachable plugin, but got nil" )
909
993
}
910
994
})
911
995
}
0 commit comments