@@ -675,6 +675,13 @@ func createFakePVC(requestBytes int64) *v1.PersistentVolumeClaim {
675
675
}
676
676
}
677
677
678
+ // createFakePVCWithVolumeMode returns PVC with VolumeMode
679
+ func createFakePVCWithVolumeMode (requestBytes int64 , volumeMode v1.PersistentVolumeMode ) * v1.PersistentVolumeClaim {
680
+ claim := createFakePVC (requestBytes )
681
+ claim .Spec .VolumeMode = & volumeMode
682
+ return claim
683
+ }
684
+
678
685
func TestGetSecretReference (t * testing.T ) {
679
686
testcases := map [string ]struct {
680
687
nameKey string
@@ -798,13 +805,67 @@ func TestGetSecretReference(t *testing.T) {
798
805
}
799
806
}
800
807
808
+ func TestSupportsBlock (t * testing.T ) {
809
+ var (
810
+ volCapTrue = csi.ValidateVolumeCapabilitiesResponse {
811
+ Supported : true ,
812
+ }
813
+ volCapFalse = csi.ValidateVolumeCapabilitiesResponse {
814
+ Supported : false ,
815
+ }
816
+ generalError = fmt .Errorf ("" )
817
+ )
818
+
819
+ testcases := []struct {
820
+ name string
821
+ volCapResp csi.ValidateVolumeCapabilitiesResponse
822
+ volCapErr error
823
+ expectSupportsBlock bool
824
+ }{
825
+ {"ValidateVolumeCapabilities returns (true, nil): expects true" , volCapTrue , nil , true },
826
+ {"ValidateVolumeCapabilities returns (false, nil): expects false" , volCapFalse , nil , false },
827
+ {"ValidateVolumeCapabilities returns (true, error): expects false" , volCapTrue , generalError , false }, // This won't happen.
828
+ {"ValidateVolumeCapabilities returns (false, error): expects false" , volCapFalse , generalError , false },
829
+ }
830
+
831
+ mockController , driver , _ , controllerServer , csiConn , err := createMockServer (t )
832
+ if err != nil {
833
+ t .Fatal (err )
834
+ }
835
+ defer mockController .Finish ()
836
+ defer driver .Stop ()
837
+
838
+ for _ , tc := range testcases {
839
+ var clientSet kubernetes.Interface
840
+
841
+ clientSet = fakeclientset .NewSimpleClientset ()
842
+ csiProvisioner := NewCSIProvisioner (clientSet , driver .Address (), 5 * time .Second , "test-provisioner" , "test" , 5 , csiConn .conn , nil )
843
+
844
+ controllerServer .EXPECT ().ValidateVolumeCapabilities (gomock .Any (), gomock .Any ()).Return (& tc .volCapResp , tc .volCapErr ).Times (1 )
845
+
846
+ if csiBlockProvisioner , ok := csiProvisioner .(controller.BlockProvisioner ); ok {
847
+ supportsBlock := csiBlockProvisioner .SupportsBlock ()
848
+ if tc .expectSupportsBlock != supportsBlock {
849
+ t .Errorf ("test %q: Expected %v got %v" , tc .name , tc .expectSupportsBlock , supportsBlock )
850
+ }
851
+ } else {
852
+ t .Errorf ("test %q: Expected csiProvisioner implements BlockProvisioner interface got %v" , tc .name , ok )
853
+ }
854
+ }
855
+ }
856
+
801
857
func TestProvision (t * testing.T ) {
802
- var requestedBytes int64 = 100
858
+ var (
859
+ requestedBytes int64 = 100
860
+ volumeModeFileSystem = v1 .PersistentVolumeFilesystem
861
+ volumeModeBlock = v1 .PersistentVolumeBlock
862
+ )
803
863
804
864
type pvSpec struct {
805
865
Name string
806
866
ReclaimPolicy v1.PersistentVolumeReclaimPolicy
807
867
AccessModes []v1.PersistentVolumeAccessMode
868
+ VolumeMode * v1.PersistentVolumeMode
808
869
Capacity v1.ResourceList
809
870
CSIPVS * v1.CSIPersistentVolumeSource
810
871
}
@@ -917,6 +978,49 @@ func TestProvision(t *testing.T) {
917
978
},
918
979
},
919
980
},
981
+ "provision with volume mode(Filesystem)" : {
982
+ volOpts : controller.VolumeOptions {
983
+ PVName : "test-name" ,
984
+ PVC : createFakePVCWithVolumeMode (requestedBytes , volumeModeFileSystem ),
985
+ Parameters : map [string ]string {},
986
+ },
987
+ expectedPVSpec : & pvSpec {
988
+ Name : "test-testi" ,
989
+ Capacity : v1.ResourceList {
990
+ v1 .ResourceName (v1 .ResourceStorage ): bytesToGiQuantity (requestedBytes ),
991
+ },
992
+ VolumeMode : & volumeModeFileSystem ,
993
+ CSIPVS : & v1.CSIPersistentVolumeSource {
994
+ Driver : "test-driver" ,
995
+ VolumeHandle : "test-volume-id" ,
996
+ FSType : "ext4" ,
997
+ VolumeAttributes : map [string ]string {
998
+ "storage.kubernetes.io/csiProvisionerIdentity" : "test-provisioner" ,
999
+ },
1000
+ },
1001
+ },
1002
+ },
1003
+ "provision with volume mode(Block)" : {
1004
+ volOpts : controller.VolumeOptions {
1005
+ PVName : "test-name" ,
1006
+ PVC : createFakePVCWithVolumeMode (requestedBytes , volumeModeBlock ),
1007
+ Parameters : map [string ]string {},
1008
+ },
1009
+ expectedPVSpec : & pvSpec {
1010
+ Name : "test-testi" ,
1011
+ Capacity : v1.ResourceList {
1012
+ v1 .ResourceName (v1 .ResourceStorage ): bytesToGiQuantity (requestedBytes ),
1013
+ },
1014
+ VolumeMode : & volumeModeBlock ,
1015
+ CSIPVS : & v1.CSIPersistentVolumeSource {
1016
+ Driver : "test-driver" ,
1017
+ VolumeHandle : "test-volume-id" ,
1018
+ VolumeAttributes : map [string ]string {
1019
+ "storage.kubernetes.io/csiProvisionerIdentity" : "test-provisioner" ,
1020
+ },
1021
+ },
1022
+ },
1023
+ },
920
1024
"fail to get secret reference" : {
921
1025
volOpts : controller.VolumeOptions {
922
1026
PVName : "test-name" ,
@@ -1065,6 +1169,10 @@ func TestProvision(t *testing.T) {
1065
1169
t .Errorf ("test %q: expected access modes: %v, got: %v" , k , tc .expectedPVSpec .AccessModes , pv .Spec .AccessModes )
1066
1170
}
1067
1171
1172
+ if ! reflect .DeepEqual (pv .Spec .VolumeMode , tc .expectedPVSpec .VolumeMode ) {
1173
+ t .Errorf ("test %q: expected volumeMode: %v, got: %v" , k , tc .expectedPVSpec .VolumeMode , pv .Spec .VolumeMode )
1174
+ }
1175
+
1068
1176
if ! reflect .DeepEqual (pv .Spec .Capacity , tc .expectedPVSpec .Capacity ) {
1069
1177
t .Errorf ("test %q: expected capacity: %v, got: %v" , k , tc .expectedPVSpec .Capacity , pv .Spec .Capacity )
1070
1178
}
0 commit comments