Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1820,7 +1820,7 @@ private void copyNewSnapshotToZonesOnPrimary(CreateSnapshotPayload payload, Snap
}
}

private boolean copySnapshotOnPool(SnapshotInfo snapshot, SnapshotStrategy snapshotStrategy, Long storagePoolId) {
boolean copySnapshotOnPool(SnapshotInfo snapshot, SnapshotStrategy snapshotStrategy, Long storagePoolId) {
DataStore store = dataStoreMgr.getDataStore(storagePoolId, DataStoreRole.Primary);
SnapshotInfo snapshotOnStore = (SnapshotInfo) store.create(snapshot);

Expand All @@ -1832,10 +1832,10 @@ private boolean copySnapshotOnPool(SnapshotInfo snapshot, SnapshotStrategy snaps
return false;
}
snapshotZoneDao.addSnapshotToZone(snapshot.getId(), snapshotOnStore.getDataCenterId());
_resourceLimitMgr.incrementResourceCount(CallContext.current().getCallingUserId(), ResourceType.primary_storage, snapshot.getSize());
if (CallContext.current().getCallingUserId() != Account.ACCOUNT_ID_SYSTEM) {
_resourceLimitMgr.incrementResourceCount(snapshot.getAccountId(), ResourceType.primary_storage, snapshot.getSize());
if (CallContext.current().getCallingAccountId() != Account.ACCOUNT_ID_SYSTEM) {
SnapshotVO snapshotVO = _snapshotDao.findByIdIncludingRemoved(snapshot.getSnapshotId());
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_SNAPSHOT_COPY, CallContext.current().getCallingAccountId(), snapshotOnStore.getDataCenterId(), snapshotVO.getId(), null, null, null, snapshotVO.getSize(),
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_SNAPSHOT_COPY, snapshot.getAccountId(), snapshotOnStore.getDataCenterId(), snapshotVO.getId(), null, null, null, snapshotVO.getSize(),
snapshotVO.getSize(), snapshotVO.getClass().getName(), snapshotVO.getUuid());
}
} catch (InterruptedException | ExecutionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.event.ActionEventUtils;
import com.cloud.event.EventTypes;
import com.cloud.event.UsageEventUtils;
import com.cloud.hypervisor.Hypervisor;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.org.Grouping;
import com.cloud.configuration.Resource.ResourceType;
import com.cloud.storage.DataStoreRole;
import com.cloud.storage.ScopeType;
import com.cloud.storage.Snapshot;
Expand Down Expand Up @@ -59,6 +62,7 @@
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotService;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
import org.apache.cloudstack.framework.async.AsyncCallFuture;
Expand Down Expand Up @@ -822,4 +826,71 @@ public void testEndCurrentChainIfNeededStopsOnMissingParentRef() {

Mockito.verify(snapshotStoreDao, Mockito.never()).update(Mockito.anyLong(), Mockito.any(SnapshotDataStoreVO.class));
}

private SnapshotInfo mockSnapshotCopiedToPool(long ownerAccountId, long snapshotSize, Long poolId, SnapshotStrategy strategy) throws Exception {
SnapshotInfo snapshot = Mockito.mock(SnapshotInfo.class);
SnapshotInfo snapshotOnStore = Mockito.mock(SnapshotInfo.class);
DataStore store = Mockito.mock(DataStore.class);
AsyncCallFuture<SnapshotResult> future = Mockito.mock(AsyncCallFuture.class);
SnapshotResult result = Mockito.mock(SnapshotResult.class);

Mockito.when(snapshot.getId()).thenReturn(100L);
Mockito.when(snapshot.getSize()).thenReturn(snapshotSize);
Mockito.when(snapshot.getAccountId()).thenReturn(ownerAccountId);
Mockito.when(dataStoreManager.getDataStore(poolId, DataStoreRole.Primary)).thenReturn(store);
Mockito.when(store.create(snapshot)).thenReturn(snapshotOnStore);
Mockito.when(snapshotOnStore.getDataCenterId()).thenReturn(2L);
Mockito.when(snapshotService.copySnapshot(snapshot, snapshotOnStore, strategy)).thenReturn(future);
Mockito.when(future.get()).thenReturn(result);
Mockito.when(result.isFailed()).thenReturn(false);
return snapshot;
}

@Test
public void copySnapshotOnPoolChargesPrimaryStorageToTheSnapshotOwnerAccount() throws Exception {
long ownerAccountId = 42L;
long snapshotSize = 1024L;
Long poolId = 5L;
SnapshotStrategy strategy = Mockito.mock(SnapshotStrategy.class);
SnapshotInfo snapshot = mockSnapshotCopiedToPool(ownerAccountId, snapshotSize, poolId, strategy);

try (MockedStatic<CallContext> ctxMock = Mockito.mockStatic(CallContext.class)) {
CallContext ctx = Mockito.mock(CallContext.class);
ctxMock.when(CallContext::current).thenReturn(ctx);
Mockito.when(ctx.getCallingAccountId()).thenReturn(Account.ACCOUNT_ID_SYSTEM);

snapshotManager.copySnapshotOnPool(snapshot, strategy, poolId);
}

Mockito.verify(resourceLimitService).incrementResourceCount(
Mockito.eq(ownerAccountId), Mockito.eq(ResourceType.primary_storage), Mockito.eq(snapshotSize));
}

@Test
public void copySnapshotOnPoolPublishesTheUsageEventForTheSnapshotOwnerAccount() throws Exception {
long ownerAccountId = 42L;
long snapshotSize = 1024L;
Long poolId = 5L;
SnapshotStrategy strategy = Mockito.mock(SnapshotStrategy.class);
SnapshotInfo snapshot = mockSnapshotCopiedToPool(ownerAccountId, snapshotSize, poolId, strategy);
Mockito.when(snapshot.getSnapshotId()).thenReturn(7L);
SnapshotVO snapshotVO = Mockito.mock(SnapshotVO.class);
Mockito.when(snapshotVO.getId()).thenReturn(7L);
Mockito.when(snapshotVO.getSize()).thenReturn(snapshotSize);
Mockito.when(snapshotVO.getUuid()).thenReturn("snapshot-uuid");
Mockito.when(snapshotDao.findByIdIncludingRemoved(7L)).thenReturn(snapshotVO);

try (MockedStatic<CallContext> ctxMock = Mockito.mockStatic(CallContext.class);
MockedStatic<UsageEventUtils> usageEvents = Mockito.mockStatic(UsageEventUtils.class)) {
CallContext ctx = Mockito.mock(CallContext.class);
ctxMock.when(CallContext::current).thenReturn(ctx);
Mockito.when(ctx.getCallingAccountId()).thenReturn(2L);

snapshotManager.copySnapshotOnPool(snapshot, strategy, poolId);

usageEvents.verify(() -> UsageEventUtils.publishUsageEvent(Mockito.eq(EventTypes.EVENT_SNAPSHOT_COPY), Mockito.eq(ownerAccountId),
Mockito.eq(2L), Mockito.eq(7L), Mockito.<String>isNull(), Mockito.<Long>isNull(), Mockito.<Long>isNull(),
Mockito.eq(snapshotSize), Mockito.eq(snapshotSize), Mockito.anyString(), Mockito.eq("snapshot-uuid")));
}
}
}
Loading