|
26 | 26 | import com.cloud.exception.ResourceUnavailableException; |
27 | 27 | import com.cloud.hypervisor.Hypervisor.HypervisorType; |
28 | 28 | import com.cloud.org.Grouping; |
| 29 | +import com.cloud.configuration.Resource.ResourceType; |
29 | 30 | import com.cloud.storage.DataStoreRole; |
30 | 31 | import com.cloud.storage.ScopeType; |
31 | 32 | import com.cloud.storage.Snapshot; |
|
59 | 60 | import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo; |
60 | 61 | import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult; |
61 | 62 | import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotService; |
| 63 | +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy; |
62 | 64 | import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory; |
63 | 65 | import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; |
64 | 66 | import org.apache.cloudstack.framework.async.AsyncCallFuture; |
@@ -822,4 +824,41 @@ public void testEndCurrentChainIfNeededStopsOnMissingParentRef() { |
822 | 824 |
|
823 | 825 | Mockito.verify(snapshotStoreDao, Mockito.never()).update(Mockito.anyLong(), Mockito.any(SnapshotDataStoreVO.class)); |
824 | 826 | } |
| 827 | + |
| 828 | + @Test |
| 829 | + @SuppressWarnings("unchecked") |
| 830 | + public void copySnapshotOnPoolChargesPrimaryStorageToTheSnapshotOwnerAccount() throws Exception { |
| 831 | + long ownerAccountId = 42L; |
| 832 | + long snapshotSize = 1024L; |
| 833 | + Long poolId = 5L; |
| 834 | + |
| 835 | + SnapshotInfo snapshot = Mockito.mock(SnapshotInfo.class); |
| 836 | + SnapshotInfo snapshotOnStore = Mockito.mock(SnapshotInfo.class); |
| 837 | + SnapshotStrategy strategy = Mockito.mock(SnapshotStrategy.class); |
| 838 | + DataStore store = Mockito.mock(DataStore.class); |
| 839 | + AsyncCallFuture<SnapshotResult> future = Mockito.mock(AsyncCallFuture.class); |
| 840 | + SnapshotResult result = Mockito.mock(SnapshotResult.class); |
| 841 | + |
| 842 | + Mockito.when(snapshot.getId()).thenReturn(100L); |
| 843 | + Mockito.when(snapshot.getSize()).thenReturn(snapshotSize); |
| 844 | + Mockito.when(snapshot.getAccountId()).thenReturn(ownerAccountId); |
| 845 | + Mockito.when(dataStoreManager.getDataStore(poolId, DataStoreRole.Primary)).thenReturn(store); |
| 846 | + Mockito.when(store.create(snapshot)).thenReturn(snapshotOnStore); |
| 847 | + Mockito.when(snapshotOnStore.getDataCenterId()).thenReturn(2L); |
| 848 | + Mockito.when(snapshotService.copySnapshot(snapshot, snapshotOnStore, strategy)).thenReturn(future); |
| 849 | + Mockito.when(future.get()).thenReturn(result); |
| 850 | + Mockito.when(result.isFailed()).thenReturn(false); |
| 851 | + |
| 852 | + try (MockedStatic<CallContext> ctxMock = Mockito.mockStatic(CallContext.class)) { |
| 853 | + CallContext ctx = Mockito.mock(CallContext.class); |
| 854 | + ctxMock.when(CallContext::current).thenReturn(ctx); |
| 855 | + // A SYSTEM calling account skips the usage-event branch; this test only covers which account is charged. |
| 856 | + Mockito.when(ctx.getCallingAccountId()).thenReturn(Account.ACCOUNT_ID_SYSTEM); |
| 857 | + |
| 858 | + snapshotManager.copySnapshotOnPool(snapshot, strategy, poolId); |
| 859 | + } |
| 860 | + |
| 861 | + Mockito.verify(resourceLimitService).incrementResourceCount( |
| 862 | + Mockito.eq(ownerAccountId), Mockito.eq(ResourceType.primary_storage), Mockito.eq(snapshotSize)); |
| 863 | + } |
825 | 864 | } |
0 commit comments