|
37 | 37 | import java.util.Set;
|
38 | 38 | import org.apache.cloudstack.storage.to.SnapshotObjectTO;
|
39 | 39 | import org.apache.cloudstack.storage.to.VolumeObjectTO;
|
| 40 | +import org.apache.cloudstack.utils.qemu.QemuImg; |
| 41 | +import org.apache.cloudstack.utils.qemu.QemuImgException; |
| 42 | +import org.apache.cloudstack.utils.qemu.QemuImgFile; |
40 | 43 | import org.junit.Assert;
|
41 | 44 | import org.junit.Before;
|
42 | 45 | import org.junit.Test;
|
@@ -87,6 +90,9 @@ public class KVMStorageProcessorTest {
|
87 | 90 | @Mock
|
88 | 91 | Connect connectMock;
|
89 | 92 |
|
| 93 | + @Mock |
| 94 | + QemuImg qemuImgMock; |
| 95 | + |
90 | 96 | private static final String directDownloadTemporaryPath = "/var/lib/libvirt/images/dd";
|
91 | 97 | private static final long templateSize = 80000L;
|
92 | 98 |
|
@@ -241,32 +247,53 @@ public void validateTakeVolumeSnapshotSuccessReturnDiskLabel() throws LibvirtExc
|
241 | 247 |
|
242 | 248 | @Test
|
243 | 249 | @PrepareForTest(KVMStorageProcessor.class)
|
244 |
| - public void validateCopySnapshotToPrimaryStorageDirFailToCopyReturnErrorMessage() throws Exception { |
| 250 | + public void convertBaseFileToSnapshotFileInPrimaryStorageDirTestFailToConvertWithQemuImgExceptionReturnErrorMessage() throws Exception { |
245 | 251 | String baseFile = "baseFile";
|
246 | 252 | String snapshotPath = "snapshotPath";
|
247 | 253 | String errorMessage = "error";
|
248 |
| - String expectedResult = String.format("Unable to copy %s snapshot [%s] to [%s] due to [%s].", volumeObjectToMock, baseFile, snapshotPath, errorMessage); |
| 254 | + String expectedResult = String.format("Failed to convert %s snapshot of volume [%s] to [%s] due to [%s].", volumeObjectToMock, baseFile, snapshotPath, errorMessage); |
249 | 255 |
|
250 | 256 | Mockito.doReturn(true).when(kvmStoragePoolMock).createFolder(Mockito.anyString());
|
251 |
| - PowerMockito.mockStatic(Files.class); |
252 |
| - PowerMockito.when(Files.copy(Mockito.any(Path.class), Mockito.any(Path.class), Mockito.any())).thenThrow(new IOException(errorMessage)); |
253 | 257 |
|
254 |
| - String result = storageProcessorSpy.copySnapshotToPrimaryStorageDir(kvmStoragePoolMock, baseFile, snapshotPath, volumeObjectToMock); |
| 258 | + PowerMockito.whenNew(QemuImg.class).withArguments(Mockito.anyInt()).thenReturn(qemuImgMock); |
| 259 | + Mockito.doThrow(new QemuImgException(errorMessage)).when(qemuImgMock).convert(Mockito.any(QemuImgFile.class), Mockito.any(QemuImgFile.class)); |
| 260 | + |
| 261 | + String result = storageProcessorSpy.convertBaseFileToSnapshotFileInPrimaryStorageDir(kvmStoragePoolMock, baseFile, snapshotPath, volumeObjectToMock, 1); |
255 | 262 |
|
256 | 263 | Assert.assertEquals(expectedResult, result);
|
257 | 264 | }
|
258 | 265 |
|
259 | 266 | @Test
|
260 | 267 | @PrepareForTest(KVMStorageProcessor.class)
|
261 |
| - public void validateCopySnapshotToPrimaryStorageDirCopySuccessReturnNull() throws Exception { |
| 268 | + public void convertBaseFileToSnapshotFileInPrimaryStorageDirTestFailToConvertWithLibvirtExceptionReturnErrorMessage() throws Exception { |
262 | 269 | String baseFile = "baseFile";
|
263 | 270 | String snapshotPath = "snapshotPath";
|
| 271 | + String errorMessage = "null"; |
| 272 | + String expectedResult = String.format("Failed to convert %s snapshot of volume [%s] to [%s] due to [%s].", volumeObjectToMock, baseFile, snapshotPath, errorMessage); |
264 | 273 |
|
265 | 274 | Mockito.doReturn(true).when(kvmStoragePoolMock).createFolder(Mockito.anyString());
|
266 |
| - PowerMockito.mockStatic(Files.class); |
267 |
| - PowerMockito.when(Files.copy(Mockito.any(Path.class), Mockito.any(Path.class), Mockito.any())).thenReturn(null); |
268 | 275 |
|
269 |
| - String result = storageProcessorSpy.copySnapshotToPrimaryStorageDir(kvmStoragePoolMock, baseFile, snapshotPath, volumeObjectToMock); |
| 276 | + PowerMockito.whenNew(QemuImg.class).withArguments(Mockito.anyInt()).thenReturn(qemuImgMock); |
| 277 | + Mockito.doThrow(LibvirtException.class).when(qemuImgMock).convert(Mockito.any(QemuImgFile.class), Mockito.any(QemuImgFile.class)); |
| 278 | + |
| 279 | + String result = storageProcessorSpy.convertBaseFileToSnapshotFileInPrimaryStorageDir(kvmStoragePoolMock, baseFile, snapshotPath, volumeObjectToMock, 1); |
| 280 | + |
| 281 | + Assert.assertEquals(expectedResult, result); |
| 282 | + } |
| 283 | + |
| 284 | + |
| 285 | + @Test |
| 286 | + @PrepareForTest(KVMStorageProcessor.class) |
| 287 | + public void convertBaseFileToSnapshotFileInPrimaryStorageDirTestConvertSuccessReturnNull() throws Exception { |
| 288 | + String baseFile = "baseFile"; |
| 289 | + String snapshotPath = "snapshotPath"; |
| 290 | + |
| 291 | + Mockito.doReturn(true).when(kvmStoragePoolMock).createFolder(Mockito.anyString()); |
| 292 | + |
| 293 | + PowerMockito.whenNew(QemuImg.class).withArguments(Mockito.anyInt()).thenReturn(qemuImgMock); |
| 294 | + Mockito.doNothing().when(qemuImgMock).convert(Mockito.any(QemuImgFile.class), Mockito.any(QemuImgFile.class)); |
| 295 | + |
| 296 | + String result = storageProcessorSpy.convertBaseFileToSnapshotFileInPrimaryStorageDir(kvmStoragePoolMock, baseFile, snapshotPath, volumeObjectToMock, 1); |
270 | 297 |
|
271 | 298 | Assert.assertNull(result);
|
272 | 299 | }
|
@@ -311,22 +338,22 @@ public void validateIsAvailablePoolSizeDividedByDiskSizeLesserThanMinRate(){
|
311 | 338 |
|
312 | 339 | @Test
|
313 | 340 | public void validateValidateCopyResultResultIsNullReturn() throws CloudRuntimeException, IOException{
|
314 |
| - storageProcessorSpy.validateCopyResult(null, ""); |
| 341 | + storageProcessorSpy.validateConvertResult(null, ""); |
315 | 342 | }
|
316 | 343 |
|
317 | 344 | @Test (expected = IOException.class)
|
318 | 345 | public void validateValidateCopyResultFailToDeleteThrowIOException() throws CloudRuntimeException, IOException{
|
319 | 346 | PowerMockito.mockStatic(Files.class);
|
320 | 347 | PowerMockito.when(Files.deleteIfExists(Mockito.any())).thenThrow(new IOException(""));
|
321 |
| - storageProcessorSpy.validateCopyResult("", ""); |
| 348 | + storageProcessorSpy.validateConvertResult("", ""); |
322 | 349 | }
|
323 | 350 |
|
324 | 351 | @Test (expected = CloudRuntimeException.class)
|
325 | 352 | @PrepareForTest(KVMStorageProcessor.class)
|
326 | 353 | public void validateValidateCopyResulResultNotNullThrowCloudRuntimeException() throws CloudRuntimeException, IOException{
|
327 | 354 | PowerMockito.mockStatic(Files.class);
|
328 | 355 | PowerMockito.when(Files.deleteIfExists(Mockito.any())).thenReturn(true);
|
329 |
| - storageProcessorSpy.validateCopyResult("", ""); |
| 356 | + storageProcessorSpy.validateConvertResult("", ""); |
330 | 357 | }
|
331 | 358 |
|
332 | 359 | @Test (expected = CloudRuntimeException.class)
|
|
0 commit comments