|
24 | 24 | import com.cloud.exception.PermissionDeniedException; |
25 | 25 | import com.cloud.exception.ResourceUnavailableException; |
26 | 26 | import com.cloud.org.Grouping; |
| 27 | +import com.cloud.configuration.Resource.ResourceType; |
27 | 28 | import com.cloud.storage.DataStoreRole; |
28 | 29 | import com.cloud.storage.Snapshot; |
29 | 30 | import com.cloud.storage.Storage.StoragePoolType; |
|
55 | 56 | import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo; |
56 | 57 | import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult; |
57 | 58 | import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotService; |
| 59 | +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy; |
58 | 60 | import org.apache.cloudstack.framework.async.AsyncCallFuture; |
59 | 61 | import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; |
60 | 62 | import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreDao; |
@@ -669,4 +671,41 @@ public void testRemoveClvmPrimarySnapshotStoreRefIfNeeded_NonClvmPool() { |
669 | 671 | Mockito.verify(snapshotStoreDao, Mockito.never()).removeBySnapshotStore( |
670 | 672 | Mockito.anyLong(), Mockito.anyLong(), Mockito.any()); |
671 | 673 | } |
| 674 | + |
| 675 | + @Test |
| 676 | + @SuppressWarnings("unchecked") |
| 677 | + public void copySnapshotOnPoolChargesPrimaryStorageToTheSnapshotOwnerAccount() throws Exception { |
| 678 | + long ownerAccountId = 42L; |
| 679 | + long snapshotSize = 1024L; |
| 680 | + Long poolId = 5L; |
| 681 | + |
| 682 | + SnapshotInfo snapshot = Mockito.mock(SnapshotInfo.class); |
| 683 | + SnapshotInfo snapshotOnStore = Mockito.mock(SnapshotInfo.class); |
| 684 | + SnapshotStrategy strategy = Mockito.mock(SnapshotStrategy.class); |
| 685 | + DataStore store = Mockito.mock(DataStore.class); |
| 686 | + AsyncCallFuture<SnapshotResult> future = Mockito.mock(AsyncCallFuture.class); |
| 687 | + SnapshotResult result = Mockito.mock(SnapshotResult.class); |
| 688 | + |
| 689 | + Mockito.when(snapshot.getId()).thenReturn(100L); |
| 690 | + Mockito.when(snapshot.getSize()).thenReturn(snapshotSize); |
| 691 | + Mockito.when(snapshot.getAccountId()).thenReturn(ownerAccountId); |
| 692 | + Mockito.when(dataStoreManager.getDataStore(poolId, DataStoreRole.Primary)).thenReturn(store); |
| 693 | + Mockito.when(store.create(snapshot)).thenReturn(snapshotOnStore); |
| 694 | + Mockito.when(snapshotOnStore.getDataCenterId()).thenReturn(2L); |
| 695 | + Mockito.when(snapshotService.copySnapshot(snapshot, snapshotOnStore, strategy)).thenReturn(future); |
| 696 | + Mockito.when(future.get()).thenReturn(result); |
| 697 | + Mockito.when(result.isFailed()).thenReturn(false); |
| 698 | + |
| 699 | + try (MockedStatic<CallContext> ctxMock = Mockito.mockStatic(CallContext.class)) { |
| 700 | + CallContext ctx = Mockito.mock(CallContext.class); |
| 701 | + ctxMock.when(CallContext::current).thenReturn(ctx); |
| 702 | + // A SYSTEM calling account skips the usage-event branch; this test only covers which account is charged. |
| 703 | + Mockito.when(ctx.getCallingAccountId()).thenReturn(Account.ACCOUNT_ID_SYSTEM); |
| 704 | + |
| 705 | + snapshotManager.copySnapshotOnPool(snapshot, strategy, poolId); |
| 706 | + } |
| 707 | + |
| 708 | + Mockito.verify(resourceLimitService).incrementResourceCount( |
| 709 | + Mockito.eq(ownerAccountId), Mockito.eq(ResourceType.primary_storage), Mockito.eq(snapshotSize)); |
| 710 | + } |
672 | 711 | } |
0 commit comments