Skip to content

Commit a671dee

Browse files
committed
Fix UT
1 parent 4532248 commit a671dee

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ private boolean replaceBlockDeviceWithBackup(KVMStoragePoolManager storagePoolMg
283283
KVMPhysicalDisk disk = null;
284284
if (createTargetVolume) {
285285
if (Storage.StoragePoolType.Linstor.equals(volumePool.getPoolType())) {
286+
if (size == null) {
287+
throw new CloudRuntimeException("Restore volume size is required for Linstor pool when creating target volume");
288+
}
286289
disk = volumeStoragePool.createPhysicalDisk(volumeUuid, QemuImg.PhysicalDiskFormat.RAW, Storage.ProvisioningType.THIN, size, null);
287290
}
288291
} else {
@@ -293,7 +296,9 @@ private boolean replaceBlockDeviceWithBackup(KVMStoragePoolManager storagePoolMg
293296
}
294297
qemu.setSkipTargetVolumeCreation(true);
295298
}
296-
logger.debug("Restoring volume: {}", disk.toString());
299+
if (disk != null) {
300+
logger.debug("Restoring volume: {}", disk.toString());
301+
}
297302
} catch (LibvirtException ex) {
298303
throw new CloudRuntimeException("Failed to create qemu-img command to restore RBD volume with backup", ex);
299304
}

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.cloud.agent.api.Answer;
2020
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
21+
import com.cloud.storage.Storage;
2122
import com.cloud.utils.script.Script;
2223
import com.cloud.vm.VirtualMachine;
2324
import org.apache.cloudstack.backup.BackupAnswer;
@@ -66,7 +67,10 @@ public void testExecuteWithVmExistsNull() throws Exception {
6667
when(command.getMountOptions()).thenReturn("rw");
6768
when(command.isVmExists()).thenReturn(null);
6869
when(command.getDiskType()).thenReturn("root");
70+
when(command.getRestoreVolumeSizes()).thenReturn(Arrays.asList(1024L));
71+
when(command.getWait()).thenReturn(60);
6972
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
73+
when(primaryDataStore.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
7074
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
7175
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
7276
when(command.getBackupFiles()).thenReturn(Arrays.asList("volume-123"));
@@ -109,6 +113,7 @@ public void testExecuteWithVmExistsTrue() throws Exception {
109113
when(command.isVmExists()).thenReturn(true);
110114
when(command.getDiskType()).thenReturn("root");
111115
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
116+
when(primaryDataStore.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
112117
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
113118
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
114119
when(command.getBackupVolumesUUIDs()).thenReturn(Arrays.asList("volume-123"));
@@ -148,6 +153,7 @@ public void testExecuteWithVmExistsFalse() throws Exception {
148153
when(command.isVmExists()).thenReturn(false);
149154
when(command.getDiskType()).thenReturn("root");
150155
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
156+
when(primaryDataStore.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
151157
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
152158
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
153159
when(command.getBackupFiles()).thenReturn(Arrays.asList("volume-123"));
@@ -185,7 +191,10 @@ public void testExecuteWithCifsMountType() throws Exception {
185191
when(command.getMountOptions()).thenReturn("username=user,password=pass");
186192
when(command.isVmExists()).thenReturn(null);
187193
when(command.getDiskType()).thenReturn("root");
194+
when(command.getRestoreVolumeSizes()).thenReturn(Arrays.asList(1024L));
195+
when(command.getWait()).thenReturn(60);
188196
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
197+
when(primaryDataStore.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
189198
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
190199
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
191200
when(command.getBackupFiles()).thenReturn(Arrays.asList("volume-123"));
@@ -226,7 +235,10 @@ public void testExecuteWithMountFailure() throws Exception {
226235
lenient().when(command.getMountOptions()).thenReturn("rw");
227236
lenient().when(command.isVmExists()).thenReturn(null);
228237
lenient().when(command.getDiskType()).thenReturn("root");
238+
lenient().when(command.getRestoreVolumeSizes()).thenReturn(Arrays.asList(1024L));
239+
lenient().when(command.getWait()).thenReturn(60);
229240
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
241+
lenient().when(primaryDataStore.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
230242
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
231243
lenient().when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
232244
when(command.getBackupFiles()).thenReturn(Arrays.asList("volume-123"));
@@ -262,7 +274,10 @@ public void testExecuteWithBackupFileNotFound() throws Exception {
262274
when(command.getMountOptions()).thenReturn("rw");
263275
when(command.isVmExists()).thenReturn(null);
264276
when(command.getDiskType()).thenReturn("root");
277+
when(command.getRestoreVolumeSizes()).thenReturn(Arrays.asList(1024L));
278+
when(command.getWait()).thenReturn(60);
265279
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
280+
when(primaryDataStore.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
266281
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
267282
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
268283
when(command.getBackupFiles()).thenReturn(Arrays.asList("volume-123"));
@@ -308,7 +323,10 @@ public void testExecuteWithCorruptBackupFile() throws Exception {
308323
when(command.getMountOptions()).thenReturn("rw");
309324
when(command.isVmExists()).thenReturn(null);
310325
when(command.getDiskType()).thenReturn("root");
326+
when(command.getRestoreVolumeSizes()).thenReturn(Arrays.asList(1024L));
327+
when(command.getWait()).thenReturn(60);
311328
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
329+
when(primaryDataStore.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
312330
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
313331
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
314332
when(command.getBackupFiles()).thenReturn(Arrays.asList("volume-123"));
@@ -356,7 +374,10 @@ public void testExecuteWithRsyncFailure() throws Exception {
356374
when(command.getMountOptions()).thenReturn("rw");
357375
when(command.isVmExists()).thenReturn(null);
358376
when(command.getDiskType()).thenReturn("root");
377+
when(command.getRestoreVolumeSizes()).thenReturn(Arrays.asList(1024L));
378+
when(command.getWait()).thenReturn(60);
359379
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
380+
when(primaryDataStore.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
360381
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
361382
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
362383
when(command.getBackupFiles()).thenReturn(Arrays.asList("volume-123"));
@@ -406,7 +427,10 @@ public void testExecuteWithAttachVolumeFailure() throws Exception {
406427
when(command.getMountOptions()).thenReturn("rw");
407428
when(command.isVmExists()).thenReturn(null);
408429
when(command.getDiskType()).thenReturn("root");
430+
when(command.getRestoreVolumeSizes()).thenReturn(Arrays.asList(1024L));
431+
when(command.getWait()).thenReturn(60);
409432
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
433+
when(primaryDataStore.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
410434
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
411435
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
412436
when(command.getBackupFiles()).thenReturn(Arrays.asList("volume-123"));
@@ -460,7 +484,10 @@ public void testExecuteWithTempDirectoryCreationFailure() throws Exception {
460484
lenient().when(command.getMountOptions()).thenReturn("rw");
461485
lenient().when(command.isVmExists()).thenReturn(null);
462486
lenient().when(command.getDiskType()).thenReturn("root");
487+
lenient().when(command.getRestoreVolumeSizes()).thenReturn(Arrays.asList(1024L));
488+
lenient().when(command.getWait()).thenReturn(60);
463489
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
490+
lenient().when(primaryDataStore.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
464491
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
465492
lenient().when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
466493
when(command.getBackupFiles()).thenReturn(Arrays.asList("volume-123"));
@@ -492,6 +519,8 @@ public void testExecuteWithMultipleVolumes() throws Exception {
492519
when(command.getDiskType()).thenReturn("root");
493520
PrimaryDataStoreTO primaryDataStore1 = Mockito.mock(PrimaryDataStoreTO.class);
494521
PrimaryDataStoreTO primaryDataStore2 = Mockito.mock(PrimaryDataStoreTO.class);
522+
when(primaryDataStore1.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
523+
when(primaryDataStore2.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
495524
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(
496525
primaryDataStore1,
497526
primaryDataStore2

0 commit comments

Comments
 (0)