Skip to content

Commit fcbc5a5

Browse files
committed
Added more UT to ResourceLimitManagerImplTest
1 parent cfc8121 commit fcbc5a5

File tree

2 files changed

+53
-30
lines changed

2 files changed

+53
-30
lines changed

server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -527,15 +527,6 @@ public boolean deleteBackupSchedule(Long vmId) {
527527
return backupScheduleDao.remove(schedule.getId());
528528
}
529529

530-
private boolean deleteAllVMBackupSchedules(long vmId) {
531-
List<BackupScheduleVO> vmBackupSchedules = backupScheduleDao.listByVM(vmId);
532-
boolean success = true;
533-
for (BackupScheduleVO vmBackupSchedule : vmBackupSchedules) {
534-
success = success && backupScheduleDao.remove(vmBackupSchedule.getId());
535-
}
536-
return success;
537-
}
538-
539530
private void postCreateScheduledBackup(Backup.Type backupType, Long vmId) {
540531
DateUtil.IntervalType intervalType = DateUtil.IntervalType.valueOf(backupType.name());
541532
final BackupScheduleVO schedule = backupScheduleDao.findByVMAndIntervalType(vmId, intervalType);

server/src/test/java/com/cloud/resourcelimit/ResourceLimitManagerImplTest.java

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -473,24 +473,26 @@ public void testFindCorrectResourceLimitForAccountProjects() {
473473

474474
@Test
475475
public void testFindCorrectResourceLimitForAccountId1() {
476-
// long accountId = 1L;
477-
// Mockito.when(accountManager.isRootAdmin(accountId)).thenReturn(true);
478-
// long result = resourceLimitManager.findCorrectResourceLimitForAccount(accountId, null, Resource.ResourceType.cpu);
479-
// Assert.assertEquals(Resource.RESOURCE_UNLIMITED, result);
480-
//
481-
// accountId = 2L;
482-
// Mockito.when(accountManager.isRootAdmin(accountId)).thenReturn(false);
483-
// Long limit = 100L;
484-
// long result = resourceLimitManager.findCorrectResourceLimitForAccount(accountId, limit, Resource.ResourceType.cpu);
485-
// Assert.assertEquals(limit.longValue(), result);
486-
//
487-
// long defaultAccountCpuMax = 25L;
488-
// Mockito.when(accountManager.isRootAdmin(accountId)).thenReturn(false);
489-
// Map<String, Long> accountResourceLimitMap = new HashMap<>();
490-
// accountResourceLimitMap.put(Resource.ResourceType.cpu.name(), defaultAccountCpuMax);
491-
// resourceLimitManager.accountResourceLimitMap = accountResourceLimitMap;
492-
// result = resourceLimitManager.findCorrectResourceLimitForAccount(accountId, null, Resource.ResourceType.cpu);
493-
// Assert.assertEquals(defaultAccountCpuMax, result);
476+
long accountId = 1L;
477+
Mockito.when(accountManager.isRootAdmin(accountId)).thenReturn(true);
478+
long result = resourceLimitManager.findCorrectResourceLimitForAccount(accountId, null, Resource.ResourceType.cpu);
479+
Assert.assertEquals(Resource.RESOURCE_UNLIMITED, result);
480+
481+
accountId = 2L;
482+
AccountVO account = mock(AccountVO.class);
483+
Mockito.when(accountManager.isRootAdmin(accountId)).thenReturn(false);
484+
Mockito.when(accountDao.findById(accountId)).thenReturn(account);
485+
Long limit = 100L;
486+
result = resourceLimitManager.findCorrectResourceLimitForAccount(accountId, limit, Resource.ResourceType.cpu);
487+
Assert.assertEquals(limit.longValue(), result);
488+
489+
long defaultAccountCpuMax = 25L;
490+
Mockito.when(accountManager.isRootAdmin(accountId)).thenReturn(false);
491+
Map<String, Long> accountResourceLimitMap = new HashMap<>();
492+
accountResourceLimitMap.put(Resource.ResourceType.cpu.name(), defaultAccountCpuMax);
493+
resourceLimitManager.accountResourceLimitMap = accountResourceLimitMap;
494+
result = resourceLimitManager.findCorrectResourceLimitForAccount(accountId, null, Resource.ResourceType.cpu);
495+
Assert.assertEquals(defaultAccountCpuMax, result);
494496
}
495497

496498
@Test
@@ -1253,9 +1255,8 @@ public void testDecrementVmMemoryResourceCount() {
12531255
}
12541256

12551257
@Test
1256-
public void testUpdateResourceLimit() {
1258+
public void testUpdateResourceLimitForAccount() {
12571259
Long accountId = 1L;
1258-
Long domainId = 2L;
12591260
Long resourceLimitId = 3L;
12601261
Integer typeId = 13;
12611262
Long maxGB = 10L;
@@ -1273,7 +1274,7 @@ public void testUpdateResourceLimit() {
12731274
Mockito.anyString(), Mockito.anyString(),
12741275
Mockito.anyLong(), Mockito.anyString())).thenReturn(1L);
12751276

1276-
resourceLimitManager.updateResourceLimit(accountId, domainId, typeId, maxGB, null);
1277+
resourceLimitManager.updateResourceLimit(accountId, null, typeId, maxGB, null);
12771278

12781279
Mockito.verify(resourceLimitDao, Mockito.times(1)).update(resourceLimitId, maxBytes);
12791280
Mockito.verify(resourceLimitDao, Mockito.never()).persist(Mockito.any());
@@ -1282,4 +1283,35 @@ public void testUpdateResourceLimit() {
12821283
accountId, ApiCommandResourceType.Account.toString()));
12831284
}
12841285
}
1286+
1287+
@Test
1288+
public void testUpdateResourceLimitForDomain() {
1289+
Long domainId = 2L;
1290+
Long resourceLimitId = 3L;
1291+
Integer typeId = 13;
1292+
Long maxGB = 10L;
1293+
Long maxBytes = maxGB * Resource.ResourceType.bytesToGiB;
1294+
1295+
Domain domain = mock(Domain.class);
1296+
when(domain.getParent()).thenReturn(null);
1297+
when(entityManager.findById(Domain.class, domainId)).thenReturn(domain);
1298+
ResourceLimitVO resourceLimitVO = mock(ResourceLimitVO.class);
1299+
when(resourceLimitVO.getId()).thenReturn(resourceLimitId);
1300+
when(resourceLimitDao.findByOwnerIdAndTypeAndTag(domainId, Resource.ResourceOwnerType.Domain, Resource.ResourceType.backup_storage, null)).thenReturn(resourceLimitVO);
1301+
1302+
try (MockedStatic<ActionEventUtils> actionEventUtilsMockedStatic = Mockito.mockStatic(ActionEventUtils.class)) {
1303+
Mockito.when(ActionEventUtils.onActionEvent(Mockito.anyLong(), Mockito.anyLong(),
1304+
Mockito.anyLong(),
1305+
Mockito.anyString(), Mockito.anyString(),
1306+
Mockito.anyLong(), Mockito.anyString())).thenReturn(1L);
1307+
1308+
resourceLimitManager.updateResourceLimit(null, domainId, typeId, maxGB, null);
1309+
1310+
Mockito.verify(resourceLimitDao, Mockito.times(1)).update(resourceLimitId, maxBytes);
1311+
Mockito.verify(resourceLimitDao, Mockito.never()).persist(Mockito.any());
1312+
actionEventUtilsMockedStatic.verify(() -> ActionEventUtils.onActionEvent(0L, 0L, 0L, EventTypes.EVENT_RESOURCE_LIMIT_UPDATE,
1313+
"Resource limit updated. Resource Type: " + Resource.ResourceType.backup_storage.toString() + ", New Value: " + maxBytes,
1314+
domainId, ApiCommandResourceType.Domain.toString()));
1315+
}
1316+
}
12851317
}

0 commit comments

Comments
 (0)