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());
}
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);
}
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());
}
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());
}
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);
}
}
Aggregations