use of com.thoughtworks.go.server.domain.ServerBackup in project gocd by gocd.
the class BackupServiceTest method shouldReturnTheLatestBackupTime.
@Test
public void shouldReturnTheLatestBackupTime() {
ServerBackupRepository repo = mock(ServerBackupRepository.class);
Date serverBackupTime = new Date();
when(repo.lastBackup()).thenReturn(new ServerBackup("file_path", serverBackupTime, "user"));
BackupService backupService = new BackupService(null, null, mock(GoConfigService.class), null, repo, systemEnvironment, serverVersion, configRepo, databaseStrategy);
backupService.initialize();
Date date = backupService.lastBackupTime();
assertThat(date, is(serverBackupTime));
}
use of com.thoughtworks.go.server.domain.ServerBackup in project gocd by gocd.
the class BackupServiceTest method shouldReturnTheUserThatTriggeredTheLastBackup.
@Test
public void shouldReturnTheUserThatTriggeredTheLastBackup() {
ServerBackupRepository repo = mock(ServerBackupRepository.class);
when(repo.lastBackup()).thenReturn(new ServerBackup("file_path", new Date(), "loser"));
BackupService backupService = new BackupService(null, null, mock(GoConfigService.class), null, repo, systemEnvironment, serverVersion, configRepo, databaseStrategy);
backupService.initialize();
String username = backupService.lastBackupUser();
assertThat(username, is("loser"));
}
use of com.thoughtworks.go.server.domain.ServerBackup in project gocd by gocd.
the class BackupsControllerDelegate method create.
public String create(Request request, Response response) throws IOException {
HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
ServerBackup backup = backupService.startBackup(currentUsername(), result);
if (result.isSuccessful()) {
return writerForTopLevelObject(request, response, outputWriter -> BackupRepresenter.toJSON(outputWriter, backup));
}
return renderHTTPOperationResult(result, request, response, localizer);
}
use of com.thoughtworks.go.server.domain.ServerBackup in project gocd by gocd.
the class DirectoryStructureWalker method startBackup.
public ServerBackup startBackup(Username username, HttpLocalizedOperationResult result) {
if (!goConfigService.isUserAdmin(username)) {
result.unauthorized(LocalizedMessage.string("UNAUTHORIZED_TO_BACKUP"), HealthStateType.unauthorised());
return null;
}
synchronized (BACKUP_MUTEX) {
DateTime now = timeProvider.currentDateTime();
final File destDir = new File(backupLocation(), BACKUP + now.toString("YYYYMMdd-HHmmss"));
if (!destDir.mkdirs()) {
result.badRequest(LocalizedMessage.string("BACKUP_UNSUCCESSFUL", "Could not create the backup directory."));
return null;
}
try {
backupRunningSince = now;
backupStartedBy = username.getUsername().toString();
backupVersion(destDir);
backupConfig(destDir);
configRepository.doLocked(new VoidThrowingFn<IOException>() {
@Override
public void run() throws IOException {
backupConfigRepository(destDir);
}
});
backupDb(destDir);
ServerBackup serverBackup = new ServerBackup(destDir.getAbsolutePath(), now.toDate(), username.getUsername().toString());
serverBackupRepository.save(serverBackup);
mailSender.send(EmailMessageDrafter.backupSuccessfullyCompletedMessage(destDir.getAbsolutePath(), goConfigService.adminEmail(), username));
result.setMessage(LocalizedMessage.string("BACKUP_COMPLETED_SUCCESSFULLY"));
return serverBackup;
} catch (Exception e) {
FileUtils.deleteQuietly(destDir);
result.badRequest(LocalizedMessage.string("BACKUP_UNSUCCESSFUL", e.getMessage()));
LOGGER.error("[Backup] Failed to backup Go.", e);
mailSender.send(EmailMessageDrafter.backupFailedMessage(e.getMessage(), goConfigService.adminEmail()));
} finally {
backupRunningSince = null;
backupStartedBy = null;
}
return null;
}
}
use of com.thoughtworks.go.server.domain.ServerBackup in project gocd by gocd.
the class ServerBackupRepositoryTest method shouldReturnTheLastBackupTime.
@Test
public void shouldReturnTheLastBackupTime() {
Date time = new Date();
repository.save(new ServerBackup("file_path", time, "user"));
Date latest = new Date(time.getTime() + 10000);
repository.save(new ServerBackup("file_path", latest, "user"));
ServerBackup serverBackup = repository.lastBackup();
assertEquals(new ServerBackup("file_path", latest, "user"), serverBackup);
}
Aggregations