Skip to content

Commit ae6acc1

Browse files
committed
resizevolume: add unit tests
1 parent 2733d10 commit ae6acc1

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

server/src/test/java/com/cloud/storage/StorageManagerImplTest.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package com.cloud.storage;
1818

19+
import java.lang.reflect.Field;
1920
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.HashMap;
@@ -42,6 +43,7 @@
4243

4344
import org.apache.cloudstack.api.ApiConstants;
4445
import org.apache.cloudstack.framework.config.ConfigDepot;
46+
import org.apache.cloudstack.framework.config.ConfigKey;
4547
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
4648
import org.apache.cloudstack.resourcedetail.dao.DiskOfferingDetailsDao;
4749
import org.apache.cloudstack.storage.command.CheckDataStoreStoragePolicyComplainceCommand;
@@ -756,4 +758,81 @@ public void testGetStoragePoolMountFailureReason() {
756758
String failureReason = storageManagerImpl.getStoragePoolMountFailureReason(error);
757759
Assert.assertEquals(failureReason, "An incorrect mount option was specified");
758760
}
761+
762+
private void overrideDefaultConfigValue(final ConfigKey configKey, final String name, final Object o) throws IllegalAccessException, NoSuchFieldException {
763+
Field f = ConfigKey.class.getDeclaredField(name);
764+
f.setAccessible(true);
765+
f.set(configKey, o);
766+
}
767+
768+
private Long testCheckPoolforSpaceForResizeSetup(StoragePoolVO pool, Long allocatedSizeWithTemplate) {
769+
Long poolId = 10L;
770+
Long zoneId = 2L;
771+
772+
Long capacityBytes = (long) (allocatedSizeWithTemplate / Double.valueOf(CapacityManager.StorageAllocatedCapacityDisableThreshold.defaultValue())
773+
/ Double.valueOf(CapacityManager.StorageOverprovisioningFactor.defaultValue()));
774+
Long maxAllocatedSizeForResize = (long) (capacityBytes * Double.valueOf(CapacityManager.StorageOverprovisioningFactor.defaultValue())
775+
* Double.valueOf(CapacityManager.StorageAllocatedCapacityDisableThresholdForVolumeSize.defaultValue()));
776+
777+
System.out.println("maxAllocatedSizeForResize = " + maxAllocatedSizeForResize);
778+
System.out.println("allocatedSizeWithTemplate = " + allocatedSizeWithTemplate);
779+
780+
Mockito.when(pool.getId()).thenReturn(poolId);
781+
Mockito.when(pool.getCapacityBytes()).thenReturn(capacityBytes);
782+
Mockito.when(pool.getDataCenterId()).thenReturn(zoneId);
783+
Mockito.when(storagePoolDao.findById(poolId)).thenReturn(pool);
784+
Mockito.when(pool.getPoolType()).thenReturn(Storage.StoragePoolType.NetworkFilesystem);
785+
786+
return maxAllocatedSizeForResize - allocatedSizeWithTemplate;
787+
}
788+
789+
@Test
790+
public void testCheckPoolforSpaceForResize1() {
791+
StoragePoolVO pool = Mockito.mock(StoragePoolVO.class);
792+
Long allocatedSizeWithTemplate = 100L * 1024 * 1024 * 1024;
793+
794+
Long maxAskingSize = testCheckPoolforSpaceForResizeSetup(pool, allocatedSizeWithTemplate);
795+
Long totalAskingSize = maxAskingSize / 2;
796+
797+
boolean result = storageManagerImpl.checkPoolforSpace(pool, allocatedSizeWithTemplate, totalAskingSize, false);
798+
Assert.assertFalse(result);
799+
}
800+
801+
@Test
802+
public void testCheckPoolforSpaceForResize2() {
803+
StoragePoolVO pool = Mockito.mock(StoragePoolVO.class);
804+
Long allocatedSizeWithTemplate = 100L * 1024 * 1024 * 1024;
805+
806+
Long maxAskingSize = testCheckPoolforSpaceForResizeSetup(pool, allocatedSizeWithTemplate);
807+
Long totalAskingSize = maxAskingSize / 2;
808+
809+
boolean result = storageManagerImpl.checkPoolforSpace(pool, allocatedSizeWithTemplate, totalAskingSize, true);
810+
Assert.assertFalse(result);
811+
}
812+
813+
@Test
814+
public void testCheckPoolforSpaceForResize3() throws NoSuchFieldException, IllegalAccessException {
815+
StoragePoolVO pool = Mockito.mock(StoragePoolVO.class);
816+
Long allocatedSizeWithTemplate = 100L * 1024 * 1024 * 1024;
817+
818+
Long maxAskingSize = testCheckPoolforSpaceForResizeSetup(pool, allocatedSizeWithTemplate);
819+
Long totalAskingSize = maxAskingSize + 1;
820+
overrideDefaultConfigValue(StorageManagerImpl.AllowVolumeReSizeBeyondAllocation, "_defaultValue", "true");
821+
822+
boolean result = storageManagerImpl.checkPoolforSpace(pool, allocatedSizeWithTemplate, totalAskingSize, true);
823+
Assert.assertFalse(result);
824+
}
825+
826+
@Test
827+
public void testCheckPoolforSpaceForResize4() throws NoSuchFieldException, IllegalAccessException {
828+
StoragePoolVO pool = Mockito.mock(StoragePoolVO.class);
829+
Long allocatedSizeWithTemplate = 100L * 1024 * 1024 * 1024;
830+
831+
Long maxAskingSize = testCheckPoolforSpaceForResizeSetup(pool, allocatedSizeWithTemplate);
832+
Long totalAskingSize = maxAskingSize / 2;
833+
overrideDefaultConfigValue(StorageManagerImpl.AllowVolumeReSizeBeyondAllocation, "_defaultValue", "true");
834+
835+
boolean result = storageManagerImpl.checkPoolforSpace(pool, allocatedSizeWithTemplate, totalAskingSize, true);
836+
Assert.assertTrue(result);
837+
}
759838
}

server/src/test/java/com/cloud/storage/VolumeApiServiceImplTest.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.fail;
2121
import static org.mockito.ArgumentMatchers.any;
22+
import static org.mockito.ArgumentMatchers.anyBoolean;
2223
import static org.mockito.ArgumentMatchers.anyLong;
2324
import static org.mockito.ArgumentMatchers.anyString;
2425
import static org.mockito.ArgumentMatchers.eq;
26+
import static org.mockito.ArgumentMatchers.nullable;
2527
import static org.mockito.Mockito.doNothing;
28+
import static org.mockito.Mockito.doReturn;
2629
import static org.mockito.Mockito.doThrow;
2730
import static org.mockito.Mockito.lenient;
2831
import static org.mockito.Mockito.mock;
@@ -38,14 +41,17 @@
3841
import java.util.UUID;
3942
import java.util.concurrent.ExecutionException;
4043

44+
import com.cloud.server.ManagementService;
4145
import org.apache.cloudstack.acl.ControlledEntity;
4246
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
4347
import org.apache.cloudstack.api.command.user.volume.CheckAndRepairVolumeCmd;
4448
import org.apache.cloudstack.api.command.user.volume.CreateVolumeCmd;
4549
import org.apache.cloudstack.api.command.user.volume.DetachVolumeCmd;
4650
import org.apache.cloudstack.api.command.user.volume.MigrateVolumeCmd;
51+
import org.apache.cloudstack.api.command.user.volume.ResizeVolumeCmd;
4752
import org.apache.cloudstack.backup.dao.BackupDao;
4853
import org.apache.cloudstack.context.CallContext;
54+
import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
4955
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
5056
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
5157
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore;
@@ -85,6 +91,7 @@
8591
import org.springframework.test.util.ReflectionTestUtils;
8692

8793
import com.cloud.api.query.dao.ServiceOfferingJoinDao;
94+
import com.cloud.configuration.ConfigurationManager;
8895
import com.cloud.configuration.Resource;
8996
import com.cloud.configuration.Resource.ResourceType;
9097
import com.cloud.dc.DataCenterVO;
@@ -210,6 +217,8 @@ public class VolumeApiServiceImplTest {
210217
private StoragePool storagePoolMock;
211218
private long storagePoolMockId = 1;
212219
@Mock
220+
private DiskOfferingVO diskOfferingMock;
221+
@Mock
213222
private DiskOfferingVO newDiskOfferingMock;
214223

215224
@Mock
@@ -238,10 +247,20 @@ public class VolumeApiServiceImplTest {
238247
@Mock
239248
private StorageManager storageMgr;
240249

250+
@Mock
251+
private ConfigurationManager _configMgr;
252+
253+
@Mock
254+
private VolumeOrchestrationService _volumeMgr;
255+
256+
@Mock
257+
private ManagementService managementService;
258+
241259
private long accountMockId = 456l;
242260
private long volumeMockId = 12313l;
243261
private long vmInstanceMockId = 1123l;
244262
private long volumeSizeMock = 456789921939l;
263+
private long newVolumeSizeMock = 456789930000l;
245264
private static long imageStoreId = 10L;
246265

247266
private String projectMockUuid = "projectUuid";
@@ -250,6 +269,7 @@ public class VolumeApiServiceImplTest {
250269
private long projectMockAccountId = 132329390L;
251270

252271
private long diskOfferingMockId = 100203L;
272+
private long newDiskOfferingMockId = 100204L;
253273

254274
private long offeringMockId = 31902L;
255275

@@ -1820,4 +1840,75 @@ public void testValidationsForCheckVolumeAPIWithInvalidVolumeFormat() {
18201840

18211841
volumeApiServiceImpl.validationsForCheckVolumeOperation(volume);
18221842
}
1843+
1844+
private void testResizeVolumeSetup() throws ExecutionException, InterruptedException {
1845+
Long poolId = 11L;
1846+
1847+
when(volumeDaoMock.findById(volumeMockId)).thenReturn(volumeVoMock);
1848+
when(volumeVoMock.getId()).thenReturn(volumeMockId);
1849+
when(volumeDaoMock.getHypervisorType(volumeMockId)).thenReturn(HypervisorType.KVM);
1850+
when(volumeVoMock.getState()).thenReturn(Volume.State.Ready);
1851+
when(volumeVoMock.getDiskOfferingId()).thenReturn(diskOfferingMockId);
1852+
when(_diskOfferingDao.findById(diskOfferingMockId)).thenReturn(diskOfferingMock);
1853+
when(_diskOfferingDao.findById(newDiskOfferingMockId)).thenReturn(newDiskOfferingMock);
1854+
when(newDiskOfferingMock.getRemoved()).thenReturn(null);
1855+
when(diskOfferingMock.getDiskSizeStrictness()).thenReturn(false);
1856+
when(newDiskOfferingMock.getDiskSizeStrictness()).thenReturn(false);
1857+
when(volumeVoMock.getInstanceId()).thenReturn(null);
1858+
when(volumeVoMock.getVolumeType()).thenReturn(Type.DATADISK);
1859+
when(newDiskOfferingMock.getDiskSize()).thenReturn(newVolumeSizeMock);
1860+
1861+
VolumeInfo volInfo = Mockito.mock(VolumeInfo.class);
1862+
when(volumeDataFactoryMock.getVolume(volumeMockId)).thenReturn(volInfo);
1863+
DataStore dataStore = Mockito.mock(DataStore.class);
1864+
when((volInfo.getDataStore())).thenReturn(dataStore);
1865+
1866+
when(volumeVoMock.getPoolId()).thenReturn(poolId);
1867+
StoragePoolVO storagePool = Mockito.mock(StoragePoolVO.class);
1868+
when(primaryDataStoreDaoMock.findById(poolId)).thenReturn(storagePool);
1869+
1870+
Mockito.lenient().doReturn(asyncCallFutureVolumeapiResultMock).when(volumeServiceMock).resize(any(VolumeInfo.class));
1871+
Mockito.doReturn(Mockito.mock(VolumeApiResult.class)).when(asyncCallFutureVolumeapiResultMock).get();
1872+
}
1873+
1874+
@Test
1875+
public void testResizeVolumeWithEnoughCapacity() throws ResourceAllocationException, ExecutionException, InterruptedException {
1876+
ResizeVolumeCmd cmd = new ResizeVolumeCmd();
1877+
ReflectionTestUtils.setField(cmd, "id", volumeMockId);
1878+
ReflectionTestUtils.setField(cmd, "newDiskOfferingId", newDiskOfferingMockId);
1879+
1880+
testResizeVolumeSetup();
1881+
1882+
when(storageMgr.storagePoolHasEnoughSpaceForResize(any(), nullable(Long.class), nullable(Long.class))).thenReturn(true);
1883+
1884+
try (MockedStatic<UsageEventUtils> ignored = Mockito.mockStatic(UsageEventUtils.class)) {
1885+
volumeApiServiceImpl.resizeVolume(cmd);
1886+
1887+
verify(volumeServiceMock).resize(any(VolumeInfo.class));
1888+
}
1889+
}
1890+
1891+
@Test
1892+
public void testResizeVolumeWithoutEnoughCapacity() throws ResourceAllocationException, ExecutionException, InterruptedException {
1893+
ResizeVolumeCmd cmd = new ResizeVolumeCmd();
1894+
ReflectionTestUtils.setField(cmd, "id", volumeMockId);
1895+
ReflectionTestUtils.setField(cmd, "newDiskOfferingId", newDiskOfferingMockId);
1896+
ReflectionTestUtils.setField(cmd, "autoMigrate", true);
1897+
1898+
testResizeVolumeSetup();
1899+
1900+
when(storageMgr.storagePoolHasEnoughSpaceForResize(any(), nullable(Long.class), nullable(Long.class))).thenReturn(false).thenReturn(true);
1901+
StoragePoolVO suitableStoragePool = Mockito.mock(StoragePoolVO.class);
1902+
Pair<List<? extends StoragePool>, List<? extends StoragePool>> poolsPair = new Pair<>(Arrays.asList(suitableStoragePool), Arrays.asList(suitableStoragePool));
1903+
when(managementService.listStoragePoolsForSystemMigrationOfVolume(anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyBoolean(), anyBoolean())).thenReturn(poolsPair);
1904+
doReturn(volumeInfoMock).when(volumeApiServiceImpl).migrateVolume(any());
1905+
when(volumeInfoMock.getId()).thenReturn(volumeMockId);
1906+
1907+
try (MockedStatic<UsageEventUtils> ignored = Mockito.mockStatic(UsageEventUtils.class)) {
1908+
volumeApiServiceImpl.resizeVolume(cmd);
1909+
1910+
verify(volumeApiServiceImpl).migrateVolume(any());
1911+
verify(volumeServiceMock).resize(any(VolumeInfo.class));
1912+
}
1913+
}
18231914
}

0 commit comments

Comments
 (0)