@@ -1603,6 +1603,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
1603
1603
snapshotStatusReady bool
1604
1604
expectedPVSpec * pvSpec
1605
1605
expectErr bool
1606
+ notPopulated bool
1606
1607
}{
1607
1608
"provision with volume snapshot data source" : {
1608
1609
volOpts : controller.ProvisionOptions {
@@ -1795,6 +1796,36 @@ func TestProvisionFromSnapshot(t *testing.T) {
1795
1796
snapshotStatusReady : false ,
1796
1797
expectErr : true ,
1797
1798
},
1799
+ "fail not populated volume content source" : {
1800
+ volOpts : controller.ProvisionOptions {
1801
+ StorageClass : & storagev1.StorageClass {
1802
+ Parameters : map [string ]string {},
1803
+ },
1804
+ PVName : "test-name" ,
1805
+ PVC : & v1.PersistentVolumeClaim {
1806
+ ObjectMeta : metav1.ObjectMeta {
1807
+ UID : "testid" ,
1808
+ },
1809
+ Spec : v1.PersistentVolumeClaimSpec {
1810
+ Selector : nil ,
1811
+ Resources : v1.ResourceRequirements {
1812
+ Requests : v1.ResourceList {
1813
+ v1 .ResourceName (v1 .ResourceStorage ): resource .MustParse (strconv .FormatInt (requestedBytes , 10 )),
1814
+ },
1815
+ },
1816
+ AccessModes : []v1.PersistentVolumeAccessMode {v1 .ReadWriteOnce },
1817
+ DataSource : & v1.TypedLocalObjectReference {
1818
+ Name : snapName ,
1819
+ Kind : "VolumeSnapshot" ,
1820
+ APIGroup : & apiGrp ,
1821
+ },
1822
+ },
1823
+ },
1824
+ },
1825
+ snapshotStatusReady : true ,
1826
+ expectErr : true ,
1827
+ notPopulated : true ,
1828
+ },
1798
1829
}
1799
1830
1800
1831
tmpdir := tempDir (t )
@@ -1838,7 +1869,23 @@ func TestProvisionFromSnapshot(t *testing.T) {
1838
1869
// When the following if condition is met, it is a valid create volume from snapshot
1839
1870
// operation and CreateVolume is expected to be called.
1840
1871
if tc .restoredVolSizeSmall == false && tc .wrongDataSource == false && tc .snapshotStatusReady {
1841
- controllerServer .EXPECT ().CreateVolume (gomock .Any (), gomock .Any ()).Return (out , nil ).Times (1 )
1872
+ if tc .notPopulated {
1873
+ out .Volume .ContentSource = nil
1874
+ controllerServer .EXPECT ().CreateVolume (gomock .Any (), gomock .Any ()).Return (out , nil ).Times (1 )
1875
+ controllerServer .EXPECT ().DeleteVolume (gomock .Any (), & csi.DeleteVolumeRequest {
1876
+ VolumeId : "test-volume-id" ,
1877
+ }).Return (& csi.DeleteVolumeResponse {}, nil ).Times (1 )
1878
+ } else {
1879
+ snapshotSource := csi.VolumeContentSource_Snapshot {
1880
+ Snapshot : & csi.VolumeContentSource_SnapshotSource {
1881
+ SnapshotId : "sid" ,
1882
+ },
1883
+ }
1884
+ out .Volume .ContentSource = & csi.VolumeContentSource {
1885
+ Type : & snapshotSource ,
1886
+ }
1887
+ controllerServer .EXPECT ().CreateVolume (gomock .Any (), gomock .Any ()).Return (out , nil ).Times (1 )
1888
+ }
1842
1889
}
1843
1890
1844
1891
pv , err := csiProvisioner .Provision (tc .volOpts )
@@ -1851,17 +1898,19 @@ func TestProvisionFromSnapshot(t *testing.T) {
1851
1898
}
1852
1899
1853
1900
if tc .expectedPVSpec != nil {
1854
- if pv .Name != tc .expectedPVSpec .Name {
1855
- t .Errorf ("test %q: expected PV name: %q, got: %q" , k , tc .expectedPVSpec .Name , pv .Name )
1856
- }
1901
+ if pv != nil {
1902
+ if pv .Name != tc .expectedPVSpec .Name {
1903
+ t .Errorf ("test %q: expected PV name: %q, got: %q" , k , tc .expectedPVSpec .Name , pv .Name )
1904
+ }
1857
1905
1858
- if ! reflect .DeepEqual (pv .Spec .Capacity , tc .expectedPVSpec .Capacity ) {
1859
- t .Errorf ("test %q: expected capacity: %v, got: %v" , k , tc .expectedPVSpec .Capacity , pv .Spec .Capacity )
1860
- }
1906
+ if ! reflect .DeepEqual (pv .Spec .Capacity , tc .expectedPVSpec .Capacity ) {
1907
+ t .Errorf ("test %q: expected capacity: %v, got: %v" , k , tc .expectedPVSpec .Capacity , pv .Spec .Capacity )
1908
+ }
1861
1909
1862
- if tc .expectedPVSpec .CSIPVS != nil {
1863
- if ! reflect .DeepEqual (pv .Spec .PersistentVolumeSource .CSI , tc .expectedPVSpec .CSIPVS ) {
1864
- t .Errorf ("test %q: expected PV: %v, got: %v" , k , tc .expectedPVSpec .CSIPVS , pv .Spec .PersistentVolumeSource .CSI )
1910
+ if tc .expectedPVSpec .CSIPVS != nil {
1911
+ if ! reflect .DeepEqual (pv .Spec .PersistentVolumeSource .CSI , tc .expectedPVSpec .CSIPVS ) {
1912
+ t .Errorf ("test %q: expected PV: %v, got: %v" , k , tc .expectedPVSpec .CSIPVS , pv .Spec .PersistentVolumeSource .CSI )
1913
+ }
1865
1914
}
1866
1915
}
1867
1916
}
@@ -2572,8 +2621,15 @@ func TestProvisionFromPVC(t *testing.T) {
2572
2621
2573
2622
}
2574
2623
if ! tc .expectErr {
2624
+ volumeSource := csi.VolumeContentSource_Volume {
2625
+ Volume : & csi.VolumeContentSource_VolumeSource {
2626
+ VolumeId : tc .volOpts .PVC .Spec .DataSource .Name ,
2627
+ },
2628
+ }
2629
+ out .Volume .ContentSource = & csi.VolumeContentSource {
2630
+ Type : & volumeSource ,
2631
+ }
2575
2632
controllerServer .EXPECT ().CreateVolume (gomock .Any (), gomock .Any ()).Return (out , nil ).Times (1 )
2576
-
2577
2633
}
2578
2634
csiProvisioner := NewCSIProvisioner (clientSet , 5 * time .Second , "test-provisioner" , "test" , 5 , csiConn .conn , nil , driverName , pluginCaps , controllerCaps , "" , false )
2579
2635
@@ -2587,17 +2643,19 @@ func TestProvisionFromPVC(t *testing.T) {
2587
2643
}
2588
2644
2589
2645
if tc .expectedPVSpec != nil {
2590
- if pv .Name != tc .expectedPVSpec .Name {
2591
- t .Errorf ("test %q: expected PV name: %q, got: %q" , k , tc .expectedPVSpec .Name , pv .Name )
2592
- }
2646
+ if pv != nil {
2647
+ if pv .Name != tc .expectedPVSpec .Name {
2648
+ t .Errorf ("test %q: expected PV name: %q, got: %q" , k , tc .expectedPVSpec .Name , pv .Name )
2649
+ }
2593
2650
2594
- if ! reflect .DeepEqual (pv .Spec .Capacity , tc .expectedPVSpec .Capacity ) {
2595
- t .Errorf ("test %q: expected capacity: %v, got: %v" , k , tc .expectedPVSpec .Capacity , pv .Spec .Capacity )
2596
- }
2651
+ if ! reflect .DeepEqual (pv .Spec .Capacity , tc .expectedPVSpec .Capacity ) {
2652
+ t .Errorf ("test %q: expected capacity: %v, got: %v" , k , tc .expectedPVSpec .Capacity , pv .Spec .Capacity )
2653
+ }
2597
2654
2598
- if tc .expectedPVSpec .CSIPVS != nil {
2599
- if ! reflect .DeepEqual (pv .Spec .PersistentVolumeSource .CSI , tc .expectedPVSpec .CSIPVS ) {
2600
- t .Errorf ("test %q: expected PV: %v, got: %v" , k , tc .expectedPVSpec .CSIPVS , pv .Spec .PersistentVolumeSource .CSI )
2655
+ if tc .expectedPVSpec .CSIPVS != nil {
2656
+ if ! reflect .DeepEqual (pv .Spec .PersistentVolumeSource .CSI , tc .expectedPVSpec .CSIPVS ) {
2657
+ t .Errorf ("test %q: expected PV: %v, got: %v" , k , tc .expectedPVSpec .CSIPVS , pv .Spec .PersistentVolumeSource .CSI )
2658
+ }
2601
2659
}
2602
2660
}
2603
2661
}
0 commit comments