|
122 | 122 | import com.cloud.storage.Volume.Type; |
123 | 123 | import com.cloud.storage.dao.DiskOfferingDao; |
124 | 124 | import com.cloud.storage.dao.SnapshotDao; |
| 125 | +import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreDao; |
| 126 | +import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO; |
125 | 127 | import com.cloud.storage.dao.StoragePoolTagsDao; |
126 | 128 | import com.cloud.storage.dao.VMTemplateDao; |
127 | 129 | import com.cloud.storage.dao.VolumeDao; |
@@ -253,6 +255,9 @@ public class VolumeApiServiceImplTest { |
253 | 255 | @Mock |
254 | 256 | private SnapshotDao snapshotDaoMock; |
255 | 257 |
|
| 258 | + @Mock |
| 259 | + private SnapshotDataStoreDao snapshotDataStoreDaoMock; |
| 260 | + |
256 | 261 | @Mock |
257 | 262 | private SnapshotPolicyDetailsDao snapshotPolicyDetailsDao; |
258 | 263 |
|
@@ -2288,4 +2293,63 @@ private List<VMSnapshotVO> generateVmSnapshotVoList(VMSnapshot.Type t1, VMSnapsh |
2288 | 2293 | Mockito.doReturn(1L).when(mock2).getId(); |
2289 | 2294 | return List.of(mock1, mock2); |
2290 | 2295 | } |
| 2296 | + |
| 2297 | + @Test |
| 2298 | + public void testCleanupSnapshotRecordsInPrimaryStorageOnly() { |
| 2299 | + VolumeVO volume = Mockito.mock(VolumeVO.class); |
| 2300 | + Mockito.when(volume.getId()).thenReturn(1L); |
| 2301 | + |
| 2302 | + SnapshotVO snapshot = Mockito.mock(SnapshotVO.class); |
| 2303 | + Mockito.when(snapshot.getId()).thenReturn(10L); |
| 2304 | + Mockito.when(snapshot.getState()).thenReturn(Snapshot.State.BackedUp); |
| 2305 | + Mockito.when(snapshotDaoMock.listByVolumeId(1L)).thenReturn(List.of(snapshot)); |
| 2306 | + |
| 2307 | + SnapshotDataStoreVO primaryRef = Mockito.mock(SnapshotDataStoreVO.class); |
| 2308 | + Mockito.when(primaryRef.getId()).thenReturn(100L); |
| 2309 | + Mockito.when(snapshotDataStoreDaoMock.listBySnapshotAndDataStoreRole(10L, DataStoreRole.Primary)).thenReturn(List.of(primaryRef)); |
| 2310 | + Mockito.when(snapshotDataStoreDaoMock.listBySnapshotAndDataStoreRole(10L, DataStoreRole.Image)).thenReturn(Collections.emptyList()); |
| 2311 | + |
| 2312 | + volumeApiServiceImpl.cleanupSnapshotRecordsInPrimaryStorageOnly(volume); |
| 2313 | + |
| 2314 | + Mockito.verify(snapshotDataStoreDaoMock).expunge(100L); |
| 2315 | + Mockito.verify(snapshot).setState(Snapshot.State.Destroyed); |
| 2316 | + Mockito.verify(snapshotDaoMock).update(10L, snapshot); |
| 2317 | + } |
| 2318 | + |
| 2319 | + @Test |
| 2320 | + public void testCleanupSnapshotRecordsInPrimaryStorageOnlySkipsWhenSecondaryExists() { |
| 2321 | + VolumeVO volume = Mockito.mock(VolumeVO.class); |
| 2322 | + Mockito.when(volume.getId()).thenReturn(1L); |
| 2323 | + |
| 2324 | + SnapshotVO snapshot = Mockito.mock(SnapshotVO.class); |
| 2325 | + Mockito.when(snapshot.getId()).thenReturn(10L); |
| 2326 | + Mockito.when(snapshot.getState()).thenReturn(Snapshot.State.BackedUp); |
| 2327 | + Mockito.when(snapshotDaoMock.listByVolumeId(1L)).thenReturn(List.of(snapshot)); |
| 2328 | + |
| 2329 | + SnapshotDataStoreVO primaryRef = Mockito.mock(SnapshotDataStoreVO.class); |
| 2330 | + SnapshotDataStoreVO secondaryRef = Mockito.mock(SnapshotDataStoreVO.class); |
| 2331 | + Mockito.when(snapshotDataStoreDaoMock.listBySnapshotAndDataStoreRole(10L, DataStoreRole.Primary)).thenReturn(List.of(primaryRef)); |
| 2332 | + Mockito.when(snapshotDataStoreDaoMock.listBySnapshotAndDataStoreRole(10L, DataStoreRole.Image)).thenReturn(List.of(secondaryRef)); |
| 2333 | + |
| 2334 | + volumeApiServiceImpl.cleanupSnapshotRecordsInPrimaryStorageOnly(volume); |
| 2335 | + |
| 2336 | + Mockito.verify(snapshotDataStoreDaoMock, Mockito.never()).expunge(Mockito.anyLong()); |
| 2337 | + Mockito.verify(snapshotDaoMock, Mockito.never()).update(Mockito.anyLong(), Mockito.any(SnapshotVO.class)); |
| 2338 | + } |
| 2339 | + |
| 2340 | + @Test |
| 2341 | + public void testCleanupSnapshotRecordsInPrimaryStorageOnlySkipsDestroyedSnapshots() { |
| 2342 | + VolumeVO volume = Mockito.mock(VolumeVO.class); |
| 2343 | + Mockito.when(volume.getId()).thenReturn(1L); |
| 2344 | + |
| 2345 | + SnapshotVO snapshot = Mockito.mock(SnapshotVO.class); |
| 2346 | + Mockito.when(snapshot.getId()).thenReturn(10L); |
| 2347 | + Mockito.when(snapshot.getState()).thenReturn(Snapshot.State.Destroyed); |
| 2348 | + Mockito.when(snapshotDaoMock.listByVolumeId(1L)).thenReturn(List.of(snapshot)); |
| 2349 | + |
| 2350 | + volumeApiServiceImpl.cleanupSnapshotRecordsInPrimaryStorageOnly(volume); |
| 2351 | + |
| 2352 | + Mockito.verify(snapshotDataStoreDaoMock, Mockito.never()).listBySnapshotAndDataStoreRole(Mockito.anyLong(), Mockito.any()); |
| 2353 | + Mockito.verify(snapshotDataStoreDaoMock, Mockito.never()).expunge(Mockito.anyLong()); |
| 2354 | + } |
2291 | 2355 | } |
0 commit comments