use of com.thoughtworks.go.server.database.Database in project gocd by gocd.
the class BackupServiceIntegrationTest method shouldReturnIfBackupIsInProgress.
@Test
public void shouldReturnIfBackupIsInProgress() throws InterruptedException {
final Semaphore waitForBackupToBegin = new Semaphore(1);
final Semaphore waitForAssertion_whichHasToHappen_whileBackupIsRunning = new Semaphore(1);
Database databaseStrategyMock = mock(Database.class);
doAnswer((Answer<Object>) invocationOnMock -> {
waitForBackupToBegin.release();
waitForAssertion_whichHasToHappen_whileBackupIsRunning.acquire();
return null;
}).when(databaseStrategyMock).backup(any(File.class));
final BackupService backupService = new BackupService(artifactsDirHolder, goConfigService, new TimeProvider(), backupInfoRepository, systemEnvSpy, configRepository, databaseStrategyMock, null);
waitForBackupToBegin.acquire();
Thread thd = new Thread(() -> backupService.startBackup(admin));
thd.start();
waitForAssertion_whichHasToHappen_whileBackupIsRunning.acquire();
waitForBackupToBegin.acquire();
assertThat(backupService.isBackingUp(), is(true));
waitForAssertion_whichHasToHappen_whileBackupIsRunning.release();
thd.join();
}
use of com.thoughtworks.go.server.database.Database in project gocd by gocd.
the class BackupServiceIntegrationTest method shouldSendEmailToAdminWhenTheBackupFails.
@Test
public void shouldSendEmailToAdminWhenTheBackupFails() throws Exception {
GoConfigService configService = mock(GoConfigService.class);
ServerConfig serverConfig = new ServerConfig();
serverConfig.setBackupConfig(new BackupConfig().setEmailOnFailure(true));
when(configService.serverConfig()).thenReturn(serverConfig);
when(configService.adminEmail()).thenReturn("mail@admin.com");
GoMailSender goMailSender = mock(GoMailSender.class);
when(configService.getMailSender()).thenReturn(goMailSender);
when(configService.isUserAdmin(admin)).thenReturn(true);
DateTime now = new DateTime();
TimeProvider timeProvider = mock(TimeProvider.class);
when(timeProvider.currentDateTime()).thenReturn(now);
Database databaseStrategyMock = mock(Database.class);
doThrow(new RuntimeException("Oh no!")).when(databaseStrategyMock).backup(any(File.class));
BackupService service = new BackupService(artifactsDirHolder, configService, timeProvider, backupInfoRepository, systemEnvSpy, configRepository, databaseStrategyMock, null);
ServerBackup backup = service.startBackup(admin);
String ipAddress = SystemUtil.getFirstLocalNonLoopbackIpAddress();
String body = String.format("Backup of the Go server at '%s' has failed. The reason is: %s", ipAddress, "Oh no!");
assertThat(backup.isSuccessful(), is(false));
assertThat(backup.getMessage(), is("Failed to perform backup. Reason: Oh no!"));
verify(goMailSender).send(new SendEmailMessage("Server Backup Failed", body, "mail@admin.com"));
verifyNoMoreInteractions(goMailSender);
assertThat(FileUtils.listFiles(backupsDirectory, TrueFileFilter.TRUE, TrueFileFilter.TRUE).isEmpty(), is(true));
}
use of com.thoughtworks.go.server.database.Database in project gocd by gocd.
the class BackupServiceIntegrationTest method shouldNotSendEmailToAdminWhenTheBackupFailsAndEmailConfigIsNotSet.
@Test
public void shouldNotSendEmailToAdminWhenTheBackupFailsAndEmailConfigIsNotSet() throws Exception {
GoConfigService configService = mock(GoConfigService.class);
ServerConfig serverConfig = new ServerConfig();
serverConfig.setBackupConfig(new BackupConfig());
when(configService.serverConfig()).thenReturn(serverConfig);
when(configService.adminEmail()).thenReturn("mail@admin.com");
GoMailSender goMailSender = mock(GoMailSender.class);
when(configService.getMailSender()).thenReturn(goMailSender);
when(configService.isUserAdmin(admin)).thenReturn(true);
DateTime now = new DateTime();
TimeProvider timeProvider = mock(TimeProvider.class);
when(timeProvider.currentDateTime()).thenReturn(now);
Database databaseStrategyMock = mock(Database.class);
doThrow(new RuntimeException("Oh no!")).when(databaseStrategyMock).backup(any(File.class));
BackupService service = new BackupService(artifactsDirHolder, configService, timeProvider, backupInfoRepository, systemEnvSpy, configRepository, databaseStrategyMock, null);
ServerBackup backup = service.startBackup(admin);
assertThat(backup.isSuccessful(), is(false));
assertThat(backup.getMessage(), is("Failed to perform backup. Reason: Oh no!"));
verifyNoMoreInteractions(goMailSender);
assertThat(FileUtils.listFiles(backupsDirectory, TrueFileFilter.TRUE, TrueFileFilter.TRUE).isEmpty(), is(true));
}
Aggregations