Skip to content

Commit 5e268ce

Browse files
author
GutoVeronezi
committed
Fix code smells
1 parent 8b210cb commit 5e268ce

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

engine/storage/snapshot/src/main/java/org/apache/cloudstack/storage/snapshot/DefaultSnapshotStrategy.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -346,17 +346,8 @@ protected Boolean deleteSnapshotInfo(SnapshotInfo snapshotInfo, String storage,
346346

347347
snapshotObject.processEvent(Snapshot.Event.OperationSucceeded);
348348
return true;
349-
} else {
350-
try {
351-
if (snapshotSvr.deleteSnapshot(snapshotInfo)) {
352-
snapshotObject.processEvent(Snapshot.Event.OperationSucceeded);
353-
s_logger.debug(String.format("%s was deleted on %s. We will mark the snapshot as destroyed.", snapshotVo, storageToString));
354-
return true;
355-
}
356-
} catch (CloudRuntimeException ex) {
357-
s_logger.warn(String.format("Unable do delete snapshot %s on %s due to [%s]. The reference will be marked as 'Destroying' for future garbage collecting.",
358-
snapshotVo, storageToString, ex.getMessage()), ex);
359-
}
349+
} else if (deleteSnapshotInPrimaryStorage(snapshotInfo, snapshotVo, storageToString, snapshotObject)) {
350+
return true;
360351
}
361352

362353
s_logger.debug(String.format("Failed to delete %s on %s.", snapshotVo, storageToString));
@@ -368,6 +359,20 @@ protected Boolean deleteSnapshotInfo(SnapshotInfo snapshotInfo, String storage,
368359
return false;
369360
}
370361

362+
protected boolean deleteSnapshotInPrimaryStorage(SnapshotInfo snapshotInfo, SnapshotVO snapshotVo, String storageToString, SnapshotObject snapshotObject) throws NoTransitionException {
363+
try {
364+
if (snapshotSvr.deleteSnapshot(snapshotInfo)) {
365+
snapshotObject.processEvent(Snapshot.Event.OperationSucceeded);
366+
s_logger.debug(String.format("%s was deleted on %s. We will mark the snapshot as destroyed.", snapshotVo, storageToString));
367+
return true;
368+
}
369+
} catch (CloudRuntimeException ex) {
370+
s_logger.warn(String.format("Unable do delete snapshot %s on %s due to [%s]. The reference will be marked as 'Destroying' for future garbage collecting.",
371+
snapshotVo, storageToString, ex.getMessage()), ex);
372+
}
373+
return false;
374+
}
375+
371376
protected void verifyIfTheSnapshotIsBeingUsedByAnyVolume(SnapshotObject snapshotObject) throws NoTransitionException {
372377
List<VolumeDetailVO> volumesFromSnapshot = _volumeDetailsDaoImpl.findDetails("SNAPSHOT_ID", String.valueOf(snapshotObject.getSnapshotId()), null);
373378
if (CollectionUtils.isEmpty(volumesFromSnapshot)) {

engine/storage/snapshot/src/test/java/org/apache/cloudstack/storage/snapshot/DefaultSnapshotStrategyTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,23 @@ public void verifyIfTheSnapshotIsBeingUsedByAnyVolumeTestDetailsIsNotEmptyThrowC
226226
Mockito.doReturn(List.of(new VolumeDetailVO())).when(volumeDetailsDaoMock).findDetails(Mockito.any(), Mockito.any(), Mockito.any());
227227
defaultSnapshotStrategySpy.verifyIfTheSnapshotIsBeingUsedByAnyVolume(snapshotObjectMock);
228228
}
229+
230+
@Test
231+
public void deleteSnapshotInPrimaryStorageTestReturnTrueIfDeleteReturnsTrue() throws NoTransitionException {
232+
Mockito.doReturn(true).when(snapshotServiceMock).deleteSnapshot(Mockito.any());
233+
Mockito.doNothing().when(snapshotObjectMock).processEvent(Mockito.any(Snapshot.Event.class));
234+
Assert.assertTrue(defaultSnapshotStrategySpy.deleteSnapshotInPrimaryStorage(null, null, null, snapshotObjectMock));
235+
}
236+
237+
@Test
238+
public void deleteSnapshotInPrimaryStorageTestReturnFalseIfDeleteReturnsFalse() throws NoTransitionException {
239+
Mockito.doReturn(false).when(snapshotServiceMock).deleteSnapshot(Mockito.any());
240+
Assert.assertFalse(defaultSnapshotStrategySpy.deleteSnapshotInPrimaryStorage(null, null, null, null));
241+
}
242+
243+
@Test
244+
public void deleteSnapshotInPrimaryStorageTestReturnFalseIfDeleteThrowsException() throws NoTransitionException {
245+
Mockito.doThrow(CloudRuntimeException.class).when(snapshotServiceMock).deleteSnapshot(Mockito.any());
246+
Assert.assertFalse(defaultSnapshotStrategySpy.deleteSnapshotInPrimaryStorage(null, null, null, null));
247+
}
229248
}

0 commit comments

Comments
 (0)