1818
1919import com .cloud .alert .AlertManager ;
2020import com .cloud .configuration .Resource ;
21+ import com .cloud .dc .DataCenterVO ;
22+ import com .cloud .dc .dao .DataCenterDao ;
2123import com .cloud .domain .Domain ;
2224import com .cloud .event .ActionEventUtils ;
25+ import com .cloud .event .UsageEventUtils ;
2326import com .cloud .exception .InvalidParameterValueException ;
2427import com .cloud .exception .ResourceAllocationException ;
2528import com .cloud .storage .Volume ;
6972import java .util .HashMap ;
7073import java .util .List ;
7174import java .util .Map ;
75+ import java .util .TimeZone ;
7276
7377import static org .junit .Assert .assertEquals ;
7478import static org .junit .Assert .fail ;
7579import static org .mockito .ArgumentMatchers .any ;
7680import static org .mockito .Mockito .mock ;
7781import static org .mockito .Mockito .times ;
82+ import static org .mockito .Mockito .verify ;
7883import static org .mockito .Mockito .when ;
7984
8085@ RunWith (MockitoJUnitRunner .class )
@@ -116,6 +121,9 @@ public class BackupManagerTest {
116121 @ Mock
117122 BackupDao backupDao ;
118123
124+ @ Mock
125+ DataCenterDao dataCenterDao ;
126+
119127 @ Mock
120128 AlertManager alertManager ;
121129
@@ -364,6 +372,7 @@ public void tryRestoreVMTestRestoreFails() throws NoTransitionException {
364372 }
365373 }
366374 }
375+
367376 private void overrideBackupFrameworkConfigValue () {
368377 ConfigKey configKey = BackupManager .BackupFrameworkEnabled ;
369378 this .configDepotImpl = (ConfigDepotImpl )ReflectionTestUtils .getField (configKey , "s_depot" );
@@ -372,10 +381,59 @@ private void overrideBackupFrameworkConfigValue() {
372381 Mockito .eq (ConfigKey .Scope .Global ), Mockito .isNull ())).thenReturn ("true" );
373382 Mockito .when (configDepot .getConfigStringValue (Mockito .eq (BackupManager .BackupFrameworkEnabled .key ()),
374383 Mockito .eq (ConfigKey .Scope .Zone ), Mockito .anyLong ())).thenReturn ("true" );
384+ Mockito .when (configDepot .getConfigStringValue (Mockito .eq (BackupManager .BackupProviderPlugin .key ()),
385+ Mockito .eq (ConfigKey .Scope .Zone ), Mockito .anyLong ())).thenReturn ("testbackupprovider" );
375386 ReflectionTestUtils .setField (configKey , "s_depot" , configDepot );
376387 updatedConfigKeyDepot = true ;
377388 }
378389
390+ @ Test
391+ public void testConfigureBackupSchedule () {
392+ Long vmId = 1L ;
393+ Long zoneId = 2L ;
394+ Long accountId = 3L ;
395+ Long domainId = 4L ;
396+ Long backupOfferingId = 5L ;
397+
398+ CreateBackupScheduleCmd cmd = Mockito .mock (CreateBackupScheduleCmd .class );
399+ when (cmd .getVmId ()).thenReturn (vmId );
400+ when (cmd .getTimezone ()).thenReturn ("GMT" );
401+ when (cmd .getIntervalType ()).thenReturn (DateUtil .IntervalType .DAILY );
402+ when (cmd .getMaxBackups ()).thenReturn (8 );
403+ when (cmd .getSchedule ()).thenReturn ("00:00:00" );
404+
405+ VMInstanceVO vm = Mockito .mock (VMInstanceVO .class );
406+ when (vmInstanceDao .findById (vmId )).thenReturn (vm );
407+ when (vm .getDataCenterId ()).thenReturn (zoneId );
408+ when (vm .getAccountId ()).thenReturn (accountId );
409+ when (vm .getBackupOfferingId ()).thenReturn (backupOfferingId );
410+
411+ overrideBackupFrameworkConfigValue ();
412+
413+ Account account = Mockito .mock (Account .class );
414+ when (accountManager .getAccount (accountId )).thenReturn (account );
415+ when (account .getDomainId ()).thenReturn (domainId );
416+ Domain domain = Mockito .mock (Domain .class );
417+ when (domainManager .getDomain (domainId )).thenReturn (domain );
418+ when (resourceLimitMgr .findCorrectResourceLimitForAccount (account , Resource .ResourceType .backup , null )).thenReturn (8L );
419+ when (resourceLimitMgr .findCorrectResourceLimitForDomain (domain , Resource .ResourceType .backup , null )).thenReturn (8L );
420+
421+ BackupOfferingVO offering = Mockito .mock (BackupOfferingVO .class );
422+ when (backupOfferingDao .findById (backupOfferingId )).thenReturn (offering );
423+ when (offering .isUserDrivenBackupAllowed ()).thenReturn (true );
424+ when (offering .getProvider ()).thenReturn ("test" );
425+
426+ BackupScheduleVO schedule = mock (BackupScheduleVO .class );
427+ when (backupScheduleDao .findByVMAndIntervalType (vmId , DateUtil .IntervalType .DAILY )).thenReturn (schedule );
428+
429+ backupManager .configureBackupSchedule (cmd );
430+
431+ verify (schedule , times (1 )).setScheduleType ((short ) DateUtil .IntervalType .DAILY .ordinal ());
432+ verify (schedule , times (1 )).setSchedule ("00:00:00" );
433+ verify (schedule , times (1 )).setTimezone (TimeZone .getTimeZone ("GMT" ).getID ());
434+ verify (schedule , times (1 )).setMaxBackups (8 );
435+ }
436+
379437 @ Test
380438 public void testConfigureBackupScheduleLimitReached () {
381439 Long vmId = 1L ;
@@ -522,4 +580,81 @@ public void testCreateBackupLimitReached() throws ResourceAllocationException {
522580 Mockito .verify (alertManager , times (1 )).sendAlert (AlertManager .AlertType .ALERT_TYPE_UPDATE_RESOURCE_COUNT , 0L , 0L , msg , "Backup storage space resource limit exceeded for account id : " + accountId
523581 + ". Failed to create backups; please use updateResourceLimit to increase the limit" );
524582 }
583+
584+ @ Test
585+ public void testBackupSyncTask () {
586+ Long dataCenterId = 1L ;
587+ Long vmId = 2L ;
588+ Long accountId = 3L ;
589+ Long backup2Id = 4L ;
590+ String restorePoint1ExternalId = "1234" ;
591+ Long backup1Size = 1 * Resource .ResourceType .bytesToGiB ;
592+ Long backup2Size = 2 * Resource .ResourceType .bytesToGiB ;
593+ Long newBackupSize = 3 * Resource .ResourceType .bytesToGiB ;
594+ Long metricSize = 4 * Resource .ResourceType .bytesToGiB ;
595+
596+ overrideBackupFrameworkConfigValue ();
597+
598+ DataCenterVO dataCenter = mock (DataCenterVO .class );
599+ when (dataCenter .getId ()).thenReturn (dataCenterId );
600+ when (dataCenterDao .listAllZones ()).thenReturn (List .of (dataCenter ));
601+
602+ BackupProvider backupProvider = mock (BackupProvider .class );
603+ when (backupProvider .getName ()).thenReturn ("testbackupprovider" );
604+ backupManager .setBackupProviders (List .of (backupProvider ));
605+ backupManager .start ();
606+
607+ VMInstanceVO vm = Mockito .mock (VMInstanceVO .class );
608+ when (vm .getId ()).thenReturn (vmId );
609+ when (vm .getAccountId ()).thenReturn (accountId );
610+ when (vmInstanceDao .listByZoneWithBackups (dataCenterId , null )).thenReturn (List .of (vm ));
611+ Backup .Metric metric = new Backup .Metric (null , metricSize );
612+ Map <VirtualMachine , Backup .Metric > metricMap = new HashMap <>();
613+ metricMap .put (vm , metric );
614+ when (backupProvider .getBackupMetrics (Mockito .anyLong (), Mockito .anyList ())).thenReturn (metricMap );
615+
616+ Backup .RestorePoint restorePoint1 = new Backup .RestorePoint (restorePoint1ExternalId , DateUtil .now (), "Root" );
617+ Backup .RestorePoint restorePoint2 = new Backup .RestorePoint ("12345" , DateUtil .now (), "Root" );
618+ List <Backup .RestorePoint > restorePoints = new ArrayList <>(List .of (restorePoint1 , restorePoint2 ));
619+ when (backupProvider .listRestorePoints (vm )).thenReturn (restorePoints );
620+
621+ BackupVO backupInDb1 = new BackupVO ();
622+ backupInDb1 .setProtectedSize (backup1Size );
623+ backupInDb1 .setExternalId (restorePoint1ExternalId );
624+
625+ BackupVO backupInDb2 = new BackupVO ();
626+ backupInDb2 .setProtectedSize (backup2Size );
627+ backupInDb2 .setExternalId (null );
628+ ReflectionTestUtils .setField (backupInDb2 , "id" , backup2Id );
629+ when (backupDao .findById (backup2Id )).thenReturn (backupInDb2 );
630+
631+ when (backupDao .listByVmId (null , vmId )).thenReturn (List .of (backupInDb1 , backupInDb2 ));
632+
633+ BackupVO newBackupEntry = new BackupVO ();
634+ newBackupEntry .setProtectedSize (newBackupSize );
635+ when (backupProvider .createNewBackupEntryForRestorePoint (restorePoint2 , vm ,metric )).thenReturn (newBackupEntry );
636+
637+ try (MockedStatic <ActionEventUtils > ignored = Mockito .mockStatic (ActionEventUtils .class )) {
638+ Mockito .when (ActionEventUtils .onActionEvent (Mockito .anyLong (), Mockito .anyLong (),
639+ Mockito .anyLong (),
640+ Mockito .anyString (), Mockito .anyString (),
641+ Mockito .anyLong (), Mockito .anyString ())).thenReturn (1L );
642+
643+ try (MockedStatic <UsageEventUtils > ignored2 = Mockito .mockStatic (UsageEventUtils .class )) {
644+
645+ BackupManagerImpl .BackupSyncTask backupSyncTask = backupManager .new BackupSyncTask (backupManager );
646+ backupSyncTask .runInContext ();
647+
648+ verify (resourceLimitMgr , times (1 )).decrementResourceCount (accountId , Resource .ResourceType .backup_storage , backup1Size );
649+ verify (resourceLimitMgr , times (1 )).incrementResourceCount (accountId , Resource .ResourceType .backup_storage , metricSize );
650+ Assert .assertEquals (backupInDb1 .getProtectedSize (), metricSize );
651+
652+ verify (resourceLimitMgr , times (1 )).incrementResourceCount (accountId , Resource .ResourceType .backup );
653+ verify (resourceLimitMgr , times (1 )).incrementResourceCount (accountId , Resource .ResourceType .backup_storage , newBackupSize );
654+
655+ verify (resourceLimitMgr , times (1 )).decrementResourceCount (accountId , Resource .ResourceType .backup );
656+ verify (resourceLimitMgr , times (1 )).decrementResourceCount (accountId , Resource .ResourceType .backup_storage , backup2Size );
657+ }
658+ }
659+ }
525660}
0 commit comments