@@ -528,6 +528,13 @@ func createFakePVC(requestBytes int64) *v1.PersistentVolumeClaim {
528
528
}
529
529
}
530
530
531
+ // createFakePVCWithVolumeMode returns PVC with VolumeMode
532
+ func createFakePVCWithVolumeMode (requestBytes int64 , volumeMode v1.PersistentVolumeMode ) * v1.PersistentVolumeClaim {
533
+ claim := createFakePVC (requestBytes )
534
+ claim .Spec .VolumeMode = & volumeMode
535
+ return claim
536
+ }
537
+
531
538
func TestGetSecretReference (t * testing.T ) {
532
539
testcases := map [string ]struct {
533
540
nameKey string
@@ -651,13 +658,67 @@ func TestGetSecretReference(t *testing.T) {
651
658
}
652
659
}
653
660
661
+ func TestSupportsBlock (t * testing.T ) {
662
+ var (
663
+ volCapTrue = csi.ValidateVolumeCapabilitiesResponse {
664
+ Supported : true ,
665
+ }
666
+ volCapFalse = csi.ValidateVolumeCapabilitiesResponse {
667
+ Supported : false ,
668
+ }
669
+ generalError = fmt .Errorf ("" )
670
+ )
671
+
672
+ testcases := []struct {
673
+ name string
674
+ volCapResp csi.ValidateVolumeCapabilitiesResponse
675
+ volCapErr error
676
+ expectSupportsBlock bool
677
+ }{
678
+ {"ValidateVolumeCapabilities returns (true, nil): expects true" , volCapTrue , nil , true },
679
+ {"ValidateVolumeCapabilities returns (false, nil): expects false" , volCapFalse , nil , false },
680
+ {"ValidateVolumeCapabilities returns (true, error): expects false" , volCapTrue , generalError , false }, // This won't happen.
681
+ {"ValidateVolumeCapabilities returns (false, error): expects false" , volCapFalse , generalError , false },
682
+ }
683
+
684
+ mockController , driver , _ , controllerServer , csiConn , err := createMockServer (t )
685
+ if err != nil {
686
+ t .Fatal (err )
687
+ }
688
+ defer mockController .Finish ()
689
+ defer driver .Stop ()
690
+
691
+ for _ , tc := range testcases {
692
+ var clientSet kubernetes.Interface
693
+
694
+ clientSet = fakeclientset .NewSimpleClientset ()
695
+ csiProvisioner := NewCSIProvisioner (clientSet , driver .Address (), 5 * time .Second , "test-provisioner" , "test" , 5 , csiConn .conn )
696
+
697
+ controllerServer .EXPECT ().ValidateVolumeCapabilities (gomock .Any (), gomock .Any ()).Return (& tc .volCapResp , tc .volCapErr ).Times (1 )
698
+
699
+ if csiBlockProvisioner , ok := csiProvisioner .(controller.BlockProvisioner ); ok {
700
+ supportsBlock := csiBlockProvisioner .SupportsBlock ()
701
+ if tc .expectSupportsBlock != supportsBlock {
702
+ t .Errorf ("test %q: Expected %v got %v" , tc .name , tc .expectSupportsBlock , supportsBlock )
703
+ }
704
+ } else {
705
+ t .Errorf ("test %q: Expected csiProvisioner implements BlockProvisioner interface got %v" , tc .name , ok )
706
+ }
707
+ }
708
+ }
709
+
654
710
func TestProvision (t * testing.T ) {
655
- var requestedBytes int64 = 100
711
+ var (
712
+ requestedBytes int64 = 100
713
+ volumeModeFileSystem = v1 .PersistentVolumeFilesystem
714
+ volumeModeBlock = v1 .PersistentVolumeBlock
715
+ )
656
716
657
717
type pvSpec struct {
658
718
Name string
659
719
ReclaimPolicy v1.PersistentVolumeReclaimPolicy
660
720
AccessModes []v1.PersistentVolumeAccessMode
721
+ VolumeMode * v1.PersistentVolumeMode
661
722
Capacity v1.ResourceList
662
723
CSIPVS * v1.CSIPersistentVolumeSource
663
724
}
@@ -770,6 +831,50 @@ func TestProvision(t *testing.T) {
770
831
},
771
832
},
772
833
},
834
+ "provision with volume mode(Filesystem)" : {
835
+ volOpts : controller.VolumeOptions {
836
+ PVName : "test-name" ,
837
+ PVC : createFakePVCWithVolumeMode (requestedBytes , volumeModeFileSystem ),
838
+ Parameters : map [string ]string {},
839
+ },
840
+ expectedPVSpec : & pvSpec {
841
+ Name : "test-testi" ,
842
+ Capacity : v1.ResourceList {
843
+ v1 .ResourceName (v1 .ResourceStorage ): bytesToGiQuantity (requestedBytes ),
844
+ },
845
+ VolumeMode : & volumeModeFileSystem ,
846
+ CSIPVS : & v1.CSIPersistentVolumeSource {
847
+ Driver : "test-driver" ,
848
+ VolumeHandle : "test-volume-id" ,
849
+ FSType : "ext4" ,
850
+ VolumeAttributes : map [string ]string {
851
+ "storage.kubernetes.io/csiProvisionerIdentity" : "test-provisioner" ,
852
+ },
853
+ },
854
+ },
855
+ },
856
+ "provision with volume mode(Block)" : {
857
+ volOpts : controller.VolumeOptions {
858
+ PVName : "test-name" ,
859
+ PVC : createFakePVCWithVolumeMode (requestedBytes , volumeModeBlock ),
860
+ Parameters : map [string ]string {},
861
+ },
862
+ expectedPVSpec : & pvSpec {
863
+ Name : "test-testi" ,
864
+ Capacity : v1.ResourceList {
865
+ v1 .ResourceName (v1 .ResourceStorage ): bytesToGiQuantity (requestedBytes ),
866
+ },
867
+ VolumeMode : & volumeModeBlock ,
868
+ CSIPVS : & v1.CSIPersistentVolumeSource {
869
+ Driver : "test-driver" ,
870
+ VolumeHandle : "test-volume-id" ,
871
+ FSType : "ext4" ,
872
+ VolumeAttributes : map [string ]string {
873
+ "storage.kubernetes.io/csiProvisionerIdentity" : "test-provisioner" ,
874
+ },
875
+ },
876
+ },
877
+ },
773
878
"fail to get secret reference" : {
774
879
volOpts : controller.VolumeOptions {
775
880
PVName : "test-name" ,
@@ -918,6 +1023,10 @@ func TestProvision(t *testing.T) {
918
1023
t .Errorf ("test %q: expected access modes: %v, got: %v" , k , tc .expectedPVSpec .AccessModes , pv .Spec .AccessModes )
919
1024
}
920
1025
1026
+ if ! reflect .DeepEqual (pv .Spec .VolumeMode , tc .expectedPVSpec .VolumeMode ) {
1027
+ t .Errorf ("test %q: expected volumeMode: %v, got: %v" , k , tc .expectedPVSpec .VolumeMode , pv .Spec .VolumeMode )
1028
+ }
1029
+
921
1030
if ! reflect .DeepEqual (pv .Spec .Capacity , tc .expectedPVSpec .Capacity ) {
922
1031
t .Errorf ("test %q: expected capacity: %v, got: %v" , k , tc .expectedPVSpec .Capacity , pv .Spec .Capacity )
923
1032
}
0 commit comments