|
39 | 39 | import java.util.Set; |
40 | 40 | import org.apache.cloudstack.storage.to.SnapshotObjectTO; |
41 | 41 | import org.apache.cloudstack.storage.to.VolumeObjectTO; |
| 42 | +import org.apache.cloudstack.utils.qemu.QemuImg; |
| 43 | +import org.apache.cloudstack.utils.qemu.QemuImgException; |
| 44 | +import org.apache.cloudstack.utils.qemu.QemuImgFile; |
42 | 45 | import org.junit.Assert; |
43 | 46 | import org.junit.Before; |
44 | 47 | import org.junit.Test; |
@@ -91,6 +94,9 @@ public class KVMStorageProcessorTest { |
91 | 94 | @Mock |
92 | 95 | Connect connectMock; |
93 | 96 |
|
| 97 | + @Mock |
| 98 | + QemuImg qemuImgMock; |
| 99 | + |
94 | 100 | @Mock |
95 | 101 | LibvirtDomainXMLParser libvirtDomainXMLParserMock; |
96 | 102 | @Mock |
@@ -251,32 +257,53 @@ public void validateTakeVolumeSnapshotSuccessReturnDiskLabel() throws LibvirtExc |
251 | 257 |
|
252 | 258 | @Test |
253 | 259 | @PrepareForTest(KVMStorageProcessor.class) |
254 | | - public void validateCopySnapshotToPrimaryStorageDirFailToCopyReturnErrorMessage() throws Exception { |
| 260 | + public void convertBaseFileToSnapshotFileInPrimaryStorageDirTestFailToConvertWithQemuImgExceptionReturnErrorMessage() throws Exception { |
255 | 261 | String baseFile = "baseFile"; |
256 | 262 | String snapshotPath = "snapshotPath"; |
257 | 263 | String errorMessage = "error"; |
258 | | - String expectedResult = String.format("Unable to copy %s snapshot [%s] to [%s] due to [%s].", volumeObjectToMock, baseFile, snapshotPath, errorMessage); |
| 264 | + String expectedResult = String.format("Failed to convert %s snapshot of volume [%s] to [%s] due to [%s].", volumeObjectToMock, baseFile, snapshotPath, errorMessage); |
259 | 265 |
|
260 | 266 | Mockito.doReturn(true).when(kvmStoragePoolMock).createFolder(Mockito.anyString()); |
261 | | - PowerMockito.mockStatic(Files.class); |
262 | | - PowerMockito.when(Files.copy(Mockito.any(Path.class), Mockito.any(Path.class), Mockito.any())).thenThrow(new IOException(errorMessage)); |
263 | 267 |
|
264 | | - String result = storageProcessorSpy.copySnapshotToPrimaryStorageDir(kvmStoragePoolMock, baseFile, snapshotPath, volumeObjectToMock); |
| 268 | + PowerMockito.whenNew(QemuImg.class).withArguments(Mockito.anyInt()).thenReturn(qemuImgMock); |
| 269 | + Mockito.doThrow(new QemuImgException(errorMessage)).when(qemuImgMock).convert(Mockito.any(QemuImgFile.class), Mockito.any(QemuImgFile.class)); |
| 270 | + |
| 271 | + String result = storageProcessorSpy.convertBaseFileToSnapshotFileInPrimaryStorageDir(kvmStoragePoolMock, baseFile, snapshotPath, volumeObjectToMock, 1); |
265 | 272 |
|
266 | 273 | Assert.assertEquals(expectedResult, result); |
267 | 274 | } |
268 | 275 |
|
269 | 276 | @Test |
270 | 277 | @PrepareForTest(KVMStorageProcessor.class) |
271 | | - public void validateCopySnapshotToPrimaryStorageDirCopySuccessReturnNull() throws Exception { |
| 278 | + public void convertBaseFileToSnapshotFileInPrimaryStorageDirTestFailToConvertWithLibvirtExceptionReturnErrorMessage() throws Exception { |
272 | 279 | String baseFile = "baseFile"; |
273 | 280 | String snapshotPath = "snapshotPath"; |
| 281 | + String errorMessage = "null"; |
| 282 | + String expectedResult = String.format("Failed to convert %s snapshot of volume [%s] to [%s] due to [%s].", volumeObjectToMock, baseFile, snapshotPath, errorMessage); |
274 | 283 |
|
275 | 284 | Mockito.doReturn(true).when(kvmStoragePoolMock).createFolder(Mockito.anyString()); |
276 | | - PowerMockito.mockStatic(Files.class); |
277 | | - PowerMockito.when(Files.copy(Mockito.any(Path.class), Mockito.any(Path.class), Mockito.any())).thenReturn(null); |
278 | 285 |
|
279 | | - String result = storageProcessorSpy.copySnapshotToPrimaryStorageDir(kvmStoragePoolMock, baseFile, snapshotPath, volumeObjectToMock); |
| 286 | + PowerMockito.whenNew(QemuImg.class).withArguments(Mockito.anyInt()).thenReturn(qemuImgMock); |
| 287 | + Mockito.doThrow(LibvirtException.class).when(qemuImgMock).convert(Mockito.any(QemuImgFile.class), Mockito.any(QemuImgFile.class)); |
| 288 | + |
| 289 | + String result = storageProcessorSpy.convertBaseFileToSnapshotFileInPrimaryStorageDir(kvmStoragePoolMock, baseFile, snapshotPath, volumeObjectToMock, 1); |
| 290 | + |
| 291 | + Assert.assertEquals(expectedResult, result); |
| 292 | + } |
| 293 | + |
| 294 | + |
| 295 | + @Test |
| 296 | + @PrepareForTest(KVMStorageProcessor.class) |
| 297 | + public void convertBaseFileToSnapshotFileInPrimaryStorageDirTestConvertSuccessReturnNull() throws Exception { |
| 298 | + String baseFile = "baseFile"; |
| 299 | + String snapshotPath = "snapshotPath"; |
| 300 | + |
| 301 | + Mockito.doReturn(true).when(kvmStoragePoolMock).createFolder(Mockito.anyString()); |
| 302 | + |
| 303 | + PowerMockito.whenNew(QemuImg.class).withArguments(Mockito.anyInt()).thenReturn(qemuImgMock); |
| 304 | + Mockito.doNothing().when(qemuImgMock).convert(Mockito.any(QemuImgFile.class), Mockito.any(QemuImgFile.class)); |
| 305 | + |
| 306 | + String result = storageProcessorSpy.convertBaseFileToSnapshotFileInPrimaryStorageDir(kvmStoragePoolMock, baseFile, snapshotPath, volumeObjectToMock, 1); |
280 | 307 |
|
281 | 308 | Assert.assertNull(result); |
282 | 309 | } |
@@ -321,22 +348,22 @@ public void validateIsAvailablePoolSizeDividedByDiskSizeLesserThanMinRate(){ |
321 | 348 |
|
322 | 349 | @Test |
323 | 350 | public void validateValidateCopyResultResultIsNullReturn() throws CloudRuntimeException, IOException{ |
324 | | - storageProcessorSpy.validateCopyResult(null, ""); |
| 351 | + storageProcessorSpy.validateConvertResult(null, ""); |
325 | 352 | } |
326 | 353 |
|
327 | 354 | @Test (expected = IOException.class) |
328 | 355 | public void validateValidateCopyResultFailToDeleteThrowIOException() throws CloudRuntimeException, IOException{ |
329 | 356 | PowerMockito.mockStatic(Files.class); |
330 | 357 | PowerMockito.when(Files.deleteIfExists(Mockito.any())).thenThrow(new IOException("")); |
331 | | - storageProcessorSpy.validateCopyResult("", ""); |
| 358 | + storageProcessorSpy.validateConvertResult("", ""); |
332 | 359 | } |
333 | 360 |
|
334 | 361 | @Test (expected = CloudRuntimeException.class) |
335 | 362 | @PrepareForTest(KVMStorageProcessor.class) |
336 | 363 | public void validateValidateCopyResulResultNotNullThrowCloudRuntimeException() throws CloudRuntimeException, IOException{ |
337 | 364 | PowerMockito.mockStatic(Files.class); |
338 | 365 | PowerMockito.when(Files.deleteIfExists(Mockito.any())).thenReturn(true); |
339 | | - storageProcessorSpy.validateCopyResult("", ""); |
| 366 | + storageProcessorSpy.validateConvertResult("", ""); |
340 | 367 | } |
341 | 368 |
|
342 | 369 | @Test (expected = CloudRuntimeException.class) |
|
0 commit comments