Search in sources :

Example 1 with Database

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();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) BackupProgressStatus(com.thoughtworks.go.server.domain.BackupProgressStatus) Autowired(org.springframework.beans.factory.annotation.Autowired) ServerBackup(com.thoughtworks.go.server.domain.ServerBackup) GitMaterial(com.thoughtworks.go.config.materials.git.GitMaterial) Database(com.thoughtworks.go.server.database.Database) StringRevision(com.thoughtworks.go.domain.materials.mercurial.StringRevision) ConfigRepository(com.thoughtworks.go.service.ConfigRepository) DESCipherProvider(com.thoughtworks.go.security.DESCipherProvider) Username(com.thoughtworks.go.server.domain.Username) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AESCipherProvider(com.thoughtworks.go.security.AESCipherProvider) ServerBackupRepository(com.thoughtworks.go.server.persistence.ServerBackupRepository) Modification(com.thoughtworks.go.domain.materials.Modification) TrueFileFilter(org.apache.commons.io.filefilter.TrueFileFilter) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) ISODateTimeFormat(org.joda.time.format.ISODateTimeFormat) InMemoryStreamConsumer(com.thoughtworks.go.util.command.InMemoryStreamConsumer) DatabaseAccessHelper(com.thoughtworks.go.server.dao.DatabaseAccessHelper) Test(org.junit.jupiter.api.Test) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Hex.encodeHexString(org.apache.commons.codec.binary.Hex.encodeHexString) ServerBackupQueue(com.thoughtworks.go.server.messaging.ServerBackupQueue) TempDir(org.junit.jupiter.api.io.TempDir) Optional(java.util.Optional) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) FilenameUtils(org.apache.commons.io.FilenameUtils) ZipInputStream(java.util.zip.ZipInputStream) RevisionContext(com.thoughtworks.go.domain.materials.RevisionContext) SubprocessExecutionContext(com.thoughtworks.go.config.materials.SubprocessExecutionContext) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) NameFileFilter(org.apache.commons.io.filefilter.NameFileFilter) CurrentGoCDVersion(com.thoughtworks.go.CurrentGoCDVersion) DataSource(javax.sql.DataSource) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) SpringExtension(org.springframework.test.context.junit.jupiter.SpringExtension) Semaphore(java.util.concurrent.Semaphore) UTF_8(java.nio.charset.StandardCharsets.UTF_8) DateTime(org.joda.time.DateTime) FileUtils(org.apache.commons.io.FileUtils) com.thoughtworks.go.config(com.thoughtworks.go.config) Mockito(org.mockito.Mockito) com.thoughtworks.go.util(com.thoughtworks.go.util) AfterEach(org.junit.jupiter.api.AfterEach) java.io(java.io) SendEmailMessage(com.thoughtworks.go.server.messaging.SendEmailMessage) ContextConfiguration(org.springframework.test.context.ContextConfiguration) BackupUpdateListener(com.thoughtworks.go.server.service.backup.BackupUpdateListener) Database(com.thoughtworks.go.server.database.Database) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.jupiter.api.Test)

Example 2 with Database

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));
}
Also used : Hex.encodeHexString(org.apache.commons.codec.binary.Hex.encodeHexString) DateTime(org.joda.time.DateTime) ServerBackup(com.thoughtworks.go.server.domain.ServerBackup) SendEmailMessage(com.thoughtworks.go.server.messaging.SendEmailMessage) Database(com.thoughtworks.go.server.database.Database) Test(org.junit.jupiter.api.Test)

Example 3 with Database

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));
}
Also used : ServerBackup(com.thoughtworks.go.server.domain.ServerBackup) Database(com.thoughtworks.go.server.database.Database) DateTime(org.joda.time.DateTime) Test(org.junit.jupiter.api.Test)

Aggregations

Database (com.thoughtworks.go.server.database.Database)3 ServerBackup (com.thoughtworks.go.server.domain.ServerBackup)3 SendEmailMessage (com.thoughtworks.go.server.messaging.SendEmailMessage)2 Hex.encodeHexString (org.apache.commons.codec.binary.Hex.encodeHexString)2 DateTime (org.joda.time.DateTime)2 Test (org.junit.jupiter.api.Test)2 CurrentGoCDVersion (com.thoughtworks.go.CurrentGoCDVersion)1 com.thoughtworks.go.config (com.thoughtworks.go.config)1 SubprocessExecutionContext (com.thoughtworks.go.config.materials.SubprocessExecutionContext)1 GitMaterial (com.thoughtworks.go.config.materials.git.GitMaterial)1 Modification (com.thoughtworks.go.domain.materials.Modification)1 RevisionContext (com.thoughtworks.go.domain.materials.RevisionContext)1 StringRevision (com.thoughtworks.go.domain.materials.mercurial.StringRevision)1 AESCipherProvider (com.thoughtworks.go.security.AESCipherProvider)1 DESCipherProvider (com.thoughtworks.go.security.DESCipherProvider)1 DatabaseAccessHelper (com.thoughtworks.go.server.dao.DatabaseAccessHelper)1 BackupProgressStatus (com.thoughtworks.go.server.domain.BackupProgressStatus)1 Username (com.thoughtworks.go.server.domain.Username)1 ServerBackupQueue (com.thoughtworks.go.server.messaging.ServerBackupQueue)1 ServerBackupRepository (com.thoughtworks.go.server.persistence.ServerBackupRepository)1