@@ -620,6 +620,8 @@ func provisionMockServerSetupExpectations(identityServer *driver.MockIdentitySer
620620 }, nil ).Times (1 )
621621}
622622
623+ // provisionFromSnapshotMockServerSetupExpectations mocks plugin and controller capabilities reported
624+ // by a CSI plugin that supports the snapshot feature
623625func provisionFromSnapshotMockServerSetupExpectations (identityServer * driver.MockIdentityServer , controllerServer * driver.MockControllerServer ) {
624626 identityServer .EXPECT ().GetPluginCapabilities (gomock .Any (), gomock .Any ()).Return (& csi.GetPluginCapabilitiesResponse {
625627 Capabilities : []* csi.PluginCapability {
@@ -1166,7 +1168,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
11661168 volOpts controller.VolumeOptions
11671169 restoredVolSizeSmall bool
11681170 wrongDataSource bool
1169- validSnapshotStatus bool
1171+ snapshotStatusReady bool
11701172 expectedPVSpec * pvSpec
11711173 expectErr bool
11721174 }{
@@ -1195,7 +1197,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
11951197 },
11961198 Parameters : map [string ]string {},
11971199 },
1198- validSnapshotStatus : true ,
1200+ snapshotStatusReady : true ,
11991201 expectedPVSpec : & pvSpec {
12001202 Name : "test-testi" ,
12011203 ReclaimPolicy : v1 .PersistentVolumeReclaimDelete ,
@@ -1238,7 +1240,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
12381240 Parameters : map [string ]string {},
12391241 },
12401242 restoredVolSizeSmall : true ,
1241- validSnapshotStatus : true ,
1243+ snapshotStatusReady : true ,
12421244 expectErr : true ,
12431245 },
12441246 "fail empty snapshot name" : {
@@ -1346,7 +1348,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
13461348 },
13471349 Parameters : map [string ]string {},
13481350 },
1349- validSnapshotStatus : false ,
1351+ snapshotStatusReady : false ,
13501352 expectErr : true ,
13511353 },
13521354 }
@@ -1364,7 +1366,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
13641366 client := & fake.Clientset {}
13651367
13661368 client .AddReactor ("get" , "volumesnapshots" , func (action k8stesting.Action ) (handled bool , ret runtime.Object , err error ) {
1367- snap := newSnapshot (snapName , snapClassName , "snapcontent-snapuid" , "snapuid" , "claim" , tc .validSnapshotStatus , nil , metaTimeNowUnix , resource .NewQuantity (requestedBytes , resource .BinarySI ))
1369+ snap := newSnapshot (snapName , snapClassName , "snapcontent-snapuid" , "snapuid" , "claim" , tc .snapshotStatusReady , nil , metaTimeNowUnix , resource .NewQuantity (requestedBytes , resource .BinarySI ))
13681370 return true , snap , nil
13691371 })
13701372
@@ -1382,11 +1384,19 @@ func TestProvisionFromSnapshot(t *testing.T) {
13821384 },
13831385 }
13841386
1385- // Setup mock call expectations.
1387+ // Setup mock call expectations. If tc.wrongDataSource is false, DataSource is valid
1388+ // and the controller will proceed to check whether the plugin supports snapshot.
1389+ // So in this case, we need the plugin to report snapshot support capabilities;
1390+ // Otherwise, the controller will fail the operation so it won't check the capabilities.
13861391 if tc .wrongDataSource == false {
13871392 provisionFromSnapshotMockServerSetupExpectations (identityServer , controllerServer )
13881393 }
1389- if tc .restoredVolSizeSmall == false && tc .wrongDataSource == false && tc .validSnapshotStatus {
1394+ // If tc.restoredVolSizeSmall is true, or tc.wrongDataSource is true, or
1395+ // tc.snapshotStatusReady is false, create volume from snapshot operation will fail
1396+ // early and therefore CreateVolume is not expected to be called.
1397+ // When the following if condition is met, it is a valid create volume from snapshot
1398+ // operation and CreateVolume is expected to be called.
1399+ if tc .restoredVolSizeSmall == false && tc .wrongDataSource == false && tc .snapshotStatusReady {
13901400 controllerServer .EXPECT ().CreateVolume (gomock .Any (), gomock .Any ()).Return (out , nil ).Times (1 )
13911401 }
13921402
0 commit comments