Search in sources :

Example 11 with SnapshotImpl

use of org.eclipse.che.api.machine.server.model.impl.SnapshotImpl in project che by eclipse.

the class WorkspaceManagerTest method shouldNotCreateSnapshotIfWorkspaceIsTemporaryAndAutoCreateSnapshotActivated.

@Test
public void shouldNotCreateSnapshotIfWorkspaceIsTemporaryAndAutoCreateSnapshotActivated() throws Exception {
    final WorkspaceImpl workspace = createAndMockWorkspace();
    workspace.getAttributes().put(Constants.AUTO_CREATE_SNAPSHOT, "true");
    mockRuntime(workspace, RUNNING);
    SnapshotImpl oldSnapshot = mock(SnapshotImpl.class);
    when(snapshotDao.getSnapshot(eq(workspace.getId()), eq(workspace.getConfig().getDefaultEnv()), anyString())).thenReturn(oldSnapshot);
    workspace.setTemporary(true);
    workspaceManager.stopWorkspace(workspace.getId());
    captureRunAsyncCallsAndRunSynchronously();
    verify(runtimes, never()).snapshot(workspace.getId());
    verify(runtimes).stop(workspace.getId());
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) Test(org.testng.annotations.Test)

Example 12 with SnapshotImpl

use of org.eclipse.che.api.machine.server.model.impl.SnapshotImpl in project che by eclipse.

the class WorkspaceServiceTest method shouldReturnSnapshotsOnGetSnapshot.

@Test
public void shouldReturnSnapshotsOnGetSnapshot() throws Exception {
    // given
    String workspaceId = "testWsId1";
    SnapshotImpl.SnapshotBuilder snapshotBuilder = SnapshotImpl.builder().setCreationDate(System.currentTimeMillis()).setDescription("description").setDev(true).setEnvName("envName").setId("snap1").setMachineName("machine1").setMachineSource(new MachineSourceImpl("type").setContent("content")).setType("type").setWorkspaceId(workspaceId);
    SnapshotImpl snapshot1 = snapshotBuilder.build();
    SnapshotImpl snapshot2 = snapshotBuilder.setDev(false).build();
    List<SnapshotImpl> originSnapshots = Arrays.asList(snapshot1, snapshot2);
    when(wsManager.getSnapshot(workspaceId)).thenReturn(originSnapshots);
    // when
    final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).when().get(SECURE_PATH + "/workspace/" + workspaceId + "/snapshot");
    // then
    assertEquals(response.getStatusCode(), 200);
    List<SnapshotDto> snapshotDtos = unwrapDtoList(response, SnapshotDto.class);
    List<SnapshotImpl> newSnapshots = snapshotDtos.stream().map(SnapshotImpl::new).collect(Collectors.toList());
    originSnapshots.forEach(snapshot -> snapshot.setMachineSource(null));
    assertEquals(newSnapshots, originSnapshots);
    verify(wsManager).getSnapshot(workspaceId);
}
Also used : Response(com.jayway.restassured.response.Response) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) SnapshotDto(org.eclipse.che.api.machine.shared.dto.SnapshotDto) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 13 with SnapshotImpl

use of org.eclipse.che.api.machine.server.model.impl.SnapshotImpl in project che by eclipse.

the class WorkspaceManagerTest method shouldCreateWorkspaceSnapshotUsingDefaultValueForAutoRestore.

@Test
public void shouldCreateWorkspaceSnapshotUsingDefaultValueForAutoRestore() throws Exception {
    // given
    workspaceManager = new WorkspaceManager(workspaceDao, runtimes, eventService, accountManager, true, false, snapshotDao, sharedPool);
    final WorkspaceImpl workspace = createAndMockWorkspace();
    mockRuntime(workspace, RUNNING);
    SnapshotImpl oldSnapshot = mock(SnapshotImpl.class);
    when(snapshotDao.getSnapshot(eq(workspace.getId()), eq(workspace.getConfig().getDefaultEnv()), anyString())).thenReturn(oldSnapshot);
    // when
    workspaceManager.stopWorkspace(workspace.getId());
    // then
    captureRunAsyncCallsAndRunSynchronously();
    verify(runtimes).snapshot(workspace.getId());
    verify(runtimes).stop(workspace.getId());
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) Test(org.testng.annotations.Test)

Example 14 with SnapshotImpl

use of org.eclipse.che.api.machine.server.model.impl.SnapshotImpl in project che by eclipse.

the class WorkspaceManagerTest method shouldBeAbleToRemoveMachinesSnapshots.

@Test
public void shouldBeAbleToRemoveMachinesSnapshots() throws Exception {
    // given
    String testWsId = "testWsId";
    String testNamespace = "testNamespace";
    WorkspaceImpl workspaceMock = mock(WorkspaceImpl.class);
    when(workspaceDao.get(testWsId)).thenReturn(workspaceMock);
    when(workspaceMock.getNamespace()).thenReturn(testNamespace);
    SnapshotImpl.SnapshotBuilder snapshotBuilder = SnapshotImpl.builder().generateId().setEnvName("env").setDev(true).setMachineName("machine1").setWorkspaceId(testWsId).setType("docker").setMachineSource(new MachineSourceImpl("image"));
    SnapshotImpl snapshot1 = snapshotBuilder.build();
    SnapshotImpl snapshot2 = snapshotBuilder.generateId().setDev(false).setMachineName("machine2").build();
    when(snapshotDao.findSnapshots(testWsId)).thenReturn(asList(snapshot1, snapshot2));
    // when
    workspaceManager.removeSnapshots(testWsId);
    // then
    captureExecuteCallsAndRunSynchronously();
    verify(runtimes).removeBinaries(asList(snapshot1, snapshot2));
    InOrder snapshotDaoInOrder = inOrder(snapshotDao);
    snapshotDaoInOrder.verify(snapshotDao).removeSnapshot(snapshot1.getId());
    snapshotDaoInOrder.verify(snapshotDao).removeSnapshot(snapshot2.getId());
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) InOrder(org.mockito.InOrder) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 15 with SnapshotImpl

use of org.eclipse.che.api.machine.server.model.impl.SnapshotImpl in project che by eclipse.

the class CheEnvironmentEngine method startInstance.

private Instance startInstance(boolean recover, MessageConsumer<MachineLogMessage> environmentLogger, MachineImpl machine, MachineStarter machineStarter) throws ServerException, EnvironmentException {
    LineConsumer machineLogger = null;
    Instance instance = null;
    try {
        addMachine(machine);
        eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.CREATING).withDev(machine.getConfig().isDev()).withMachineName(machine.getConfig().getName()).withMachineId(machine.getId()).withWorkspaceId(machine.getWorkspaceId()));
        machineLogger = getMachineLogger(environmentLogger, machine.getId(), machine.getConfig().getName());
        MachineImpl originMachine = new MachineImpl(machine);
        try {
            MachineSourceImpl machineSource = null;
            if (recover) {
                try {
                    SnapshotImpl snapshot = snapshotDao.getSnapshot(machine.getWorkspaceId(), machine.getEnvName(), machine.getConfig().getName());
                    machineSource = snapshot.getMachineSource();
                    // Snapshot image location has SHA-256 digest which needs to be removed,
                    // otherwise it will be pulled without tag and cause problems
                    String imageName = machineSource.getLocation();
                    if (imageName.contains("@sha256:")) {
                        machineSource.setLocation(imageName.substring(0, imageName.indexOf('@')));
                    }
                } catch (NotFoundException e) {
                    try {
                        machineLogger.writeLine("Failed to boot machine from snapshot: snapshot not found. " + "Machine will be created from origin source.");
                    } catch (IOException ignore) {
                    }
                }
            }
            instance = machineStarter.startMachine(machineLogger, machineSource);
        } catch (SourceNotFoundException e) {
            if (recover) {
                LOG.error("Image of snapshot for machine " + machine.getConfig().getName() + " not found. " + "Machine will be created from origin source.");
                machine = originMachine;
                instance = machineStarter.startMachine(machineLogger, null);
            } else {
                throw e;
            }
        }
        replaceMachine(instance);
        eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.RUNNING).withDev(machine.getConfig().isDev()).withMachineName(machine.getConfig().getName()).withMachineId(instance.getId()).withWorkspaceId(machine.getWorkspaceId()));
        return instance;
    } catch (ApiException | RuntimeException e) {
        boolean interrupted = Thread.interrupted();
        removeMachine(machine.getWorkspaceId(), machine.getId());
        if (instance != null) {
            try {
                instance.destroy();
            } catch (Exception destroyingExc) {
                LOG.error(destroyingExc.getLocalizedMessage(), destroyingExc);
            }
        }
        if (machineLogger != null) {
            try {
                machineLogger.writeLine("[ERROR] " + e.getLocalizedMessage());
            } catch (IOException ioEx) {
                LOG.error(ioEx.getLocalizedMessage(), ioEx);
            }
            try {
                machineLogger.close();
            } catch (IOException ioEx) {
                LOG.error(ioEx.getLocalizedMessage(), ioEx);
            }
        }
        eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.ERROR).withDev(machine.getConfig().isDev()).withMachineName(machine.getConfig().getName()).withMachineId(machine.getId()).withWorkspaceId(machine.getWorkspaceId()));
        if (interrupted) {
            Thread.currentThread().interrupt();
        }
        throw new ServerException(e.getLocalizedMessage(), e);
    }
}
Also used : SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) MachineStatusEvent(org.eclipse.che.api.machine.shared.dto.event.MachineStatusEvent) ServerException(org.eclipse.che.api.core.ServerException) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) Instance(org.eclipse.che.api.machine.server.spi.Instance) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) IOException(java.io.IOException) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) ApiException(org.eclipse.che.api.core.ApiException) ConflictException(org.eclipse.che.api.core.ConflictException) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) IOException(java.io.IOException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ServerException(org.eclipse.che.api.core.ServerException) ConcurrentCompositeLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentCompositeLineConsumer) ConcurrentFileLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentFileLineConsumer) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) ApiException(org.eclipse.che.api.core.ApiException)

Aggregations

SnapshotImpl (org.eclipse.che.api.machine.server.model.impl.SnapshotImpl)31 Test (org.testng.annotations.Test)22 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)13 MachineSourceImpl (org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl)10 NotFoundException (org.eclipse.che.api.core.NotFoundException)6 ServerException (org.eclipse.che.api.core.ServerException)5 SnapshotException (org.eclipse.che.api.machine.server.exception.SnapshotException)4 Instance (org.eclipse.che.api.machine.server.spi.Instance)4 ArrayList (java.util.ArrayList)3 ConflictException (org.eclipse.che.api.core.ConflictException)3 Workspace (org.eclipse.che.api.core.model.workspace.Workspace)3 EnvironmentException (org.eclipse.che.api.environment.server.exception.EnvironmentException)3 EnvironmentNotRunningException (org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException)3 SourceNotFoundException (org.eclipse.che.api.machine.server.exception.SourceNotFoundException)3 SnapshotDao (org.eclipse.che.api.machine.server.spi.SnapshotDao)3 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)3 Matchers.anyString (org.mockito.Matchers.anyString)3 TypeLiteral (com.google.inject.TypeLiteral)2 Transactional (com.google.inject.persist.Transactional)2 IOException (java.io.IOException)2