Skip to content

Commit 9268030

Browse files
committed
test: fold the snapshot-copy resource-count test into SnapshotManagerImplTest
Keep a single test suite per class. Move copySnapshotOnPoolChargesPrimaryStorageToTheSnapshotOwnerAccount into SnapshotManagerImplTest and drop the separate SnapshotManagerImplCopyResourceCountTest. The case reuses the mocks already declared on the suite, so the duplicate mock declarations and the separate runner are no longer needed.
1 parent f2dfb46 commit 9268030

2 files changed

Lines changed: 39 additions & 98 deletions

File tree

‎server/src/test/java/com/cloud/storage/snapshot/SnapshotManagerImplCopyResourceCountTest.java‎

Lines changed: 0 additions & 98 deletions
This file was deleted.

‎server/src/test/java/com/cloud/storage/snapshot/SnapshotManagerImplTest.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.cloud.exception.PermissionDeniedException;
2525
import com.cloud.exception.ResourceUnavailableException;
2626
import com.cloud.org.Grouping;
27+
import com.cloud.configuration.Resource.ResourceType;
2728
import com.cloud.storage.DataStoreRole;
2829
import com.cloud.storage.Snapshot;
2930
import com.cloud.storage.Storage.StoragePoolType;
@@ -55,6 +56,7 @@
5556
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo;
5657
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult;
5758
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotService;
59+
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy;
5860
import org.apache.cloudstack.framework.async.AsyncCallFuture;
5961
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
6062
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreDao;
@@ -669,4 +671,41 @@ public void testRemoveClvmPrimarySnapshotStoreRefIfNeeded_NonClvmPool() {
669671
Mockito.verify(snapshotStoreDao, Mockito.never()).removeBySnapshotStore(
670672
Mockito.anyLong(), Mockito.anyLong(), Mockito.any());
671673
}
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+
}
672711
}

0 commit comments

Comments
 (0)