BackupVerificationTask extends Task { @Override public void execute() throws Exception { if (backupRestoreConfig.getSnapshotMetaServiceCronExpression().equals("-1")) { logger.info("Skipping backup verification. V2 backups are not enabled."); return; } if (instanceState.getRestoreStatus() != null && instanceState.getRestoreStatus().getStatus() != null && instanceState.getRestoreStatus().getStatus() == Status.STARTED) { logger.info("Skipping backup verification. Priam is in restore mode."); return; } Instant now = DateUtil.getInstant(); Instant slo = now.minus(backupRestoreConfig.getBackupVerificationSLOInHours(), ChronoUnit.HOURS); DateRange dateRange = new DateRange(slo, now); List<BackupVerificationResult> verificationResults = backupVerification.verifyAllBackups(BackupVersion.SNAPSHOT_META_SERVICE, dateRange); verificationResults.forEach( result -> { logger.info( "Sending {} message for backup: {}", AbstractBackupPath.BackupFileType.SNAPSHOT_VERIFIED, result.snapshotInstant); backupNotificationMgr.notify(result); }); if (!backupVerification .verifyBackup(BackupVersion.SNAPSHOT_META_SERVICE, false , dateRange) .isPresent()) { logger.error( "Not able to find any snapshot which is valid in our SLO window: {} hours", backupRestoreConfig.getBackupVerificationSLOInHours()); backupMetrics.incrementBackupVerificationFailure(); } } @Inject  BackupVerificationTask(
            IConfiguration configuration,
            IBackupRestoreConfig backupRestoreConfig,
            BackupVerification backupVerification,
            BackupMetrics backupMetrics,
            InstanceState instanceState,
            BackupNotificationMgr backupNotificationMgr); @Override void execute(); static TaskTimer getTimer(IBackupRestoreConfig backupRestoreConfig); @Override String getName(); static final String JOBNAME; }
@Test public void noBackups() throws Exception { MockBackupVerification.shouldThrow(false); MockBackupVerification.setResults(); backupVerificationService.execute(); Truth.assertThat(badVerifications.count()).isEqualTo(1); new Verifications() { { backupNotificationMgr.notify((BackupVerificationResult) any); maxTimes = 0; } }; }
@Test public void testRestoreMode(@Mocked InstanceState state) throws Exception { new Expectations() { { state.getRestoreStatus().getStatus(); result = Status.STARTED; } }; backupVerificationService.execute(); Truth.assertThat(badVerifications.count()).isEqualTo(0); new Verifications() { { backupVerification.verifyBackup((BackupVersion) any, anyBoolean, (DateRange) any); maxTimes = 0; } { backupVerification.verifyAllBackups((BackupVersion) any, (DateRange) any); maxTimes = 0; } { backupNotificationMgr.notify((BackupVerificationResult) any); maxTimes = 0; } }; }
@Test public void throwError() { MockBackupVerification.shouldThrow(true); Assertions.assertThrows( IllegalArgumentException.class, () -> backupVerificationService.execute()); }
@Test public void validBackups() throws Exception { MockBackupVerification.shouldThrow(false); MockBackupVerification.setResults(getValidBackupVerificationResult()); backupVerificationService.execute(); Truth.assertThat(badVerifications.count()).isEqualTo(0); new Verifications() { { backupNotificationMgr.notify((BackupVerificationResult) any); times = 1; } }; }
@Test public void invalidBackups() throws Exception { MockBackupVerification.shouldThrow(false); MockBackupVerification.setResults(getInvalidBackupVerificationResult()); backupVerificationService.execute(); Truth.assertThat(badVerifications.count()).isEqualTo(0); new Verifications() { { backupNotificationMgr.notify((BackupVerificationResult) any); times = 1; } }; }