Search in sources :

Example 11 with Instance

use of org.eclipse.che.api.machine.server.spi.Instance in project che by eclipse.

the class CheEnvironmentEngineTest method envStartShouldThrowsExceptionIfSameEnvironmentExists.

@Test(expectedExceptions = ConflictException.class, expectedExceptionsMessageRegExp = "Environment of workspace '.*' already exists")
public void envStartShouldThrowsExceptionIfSameEnvironmentExists() throws Exception {
    // given
    List<Instance> instances = startEnv();
    Instance instance = instances.get(0);
    EnvironmentImpl env = createEnv();
    String envName = "env-1";
    // when
    engine.start(instance.getWorkspaceId(), envName, env, false, messageConsumer);
}
Also used : Instance(org.eclipse.che.api.machine.server.spi.Instance) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 12 with Instance

use of org.eclipse.che.api.machine.server.spi.Instance in project che by eclipse.

the class CheEnvironmentEngineTest method shouldDestroyAndRemoveMachineFromEnvironmentIfEventAboutItsDeath.

@Test
public void shouldDestroyAndRemoveMachineFromEnvironmentIfEventAboutItsDeath() throws Exception {
    // given
    List<Instance> instances = startEnv();
    Instance instance = instances.get(0);
    String machineId = instance.getId();
    String workspaceId = instance.getWorkspaceId();
    when(instance.getLogger()).thenReturn(LineConsumer.DEV_NULL);
    engine.init();
    verify(eventService).subscribe(eventServiceSubscriberCaptor.capture());
    EventSubscriber<InstanceStateEvent> subscriber = eventServiceSubscriberCaptor.getValue();
    ArgumentCaptor<Runnable> runnableArgumentCaptor = ArgumentCaptor.forClass(Runnable.class);
    // when
    subscriber.onEvent(new InstanceStateEvent(machineId, workspaceId, InstanceStateEvent.Type.DIE));
    // catch event actor
    verify(sharedPool).execute(runnableArgumentCaptor.capture());
    Runnable eventActor = runnableArgumentCaptor.getValue();
    // run event actor to verify its behavior
    eventActor.run();
    // then
    for (Instance instance1 : instances) {
        // other machines are not destroyed
        if (instance1.equals(instance)) {
            verify(instance1).destroy();
        } else {
            verify(instance1, never()).destroy();
        }
    }
    for (Instance instance1 : engine.getMachines(workspaceId)) {
        assertNotEquals(instance1.getId(), machineId);
    }
}
Also used : Instance(org.eclipse.che.api.machine.server.spi.Instance) Matchers.anyString(org.mockito.Matchers.anyString) InstanceStateEvent(org.eclipse.che.api.machine.server.event.InstanceStateEvent) Test(org.testng.annotations.Test)

Example 13 with Instance

use of org.eclipse.che.api.machine.server.spi.Instance in project che by eclipse.

the class CheEnvironmentEngineTest method shouldBeAbleToStartEnvironmentWhenRecoverFailed.

@Test
public void shouldBeAbleToStartEnvironmentWhenRecoverFailed() throws Exception {
    // given
    String machineImage = "che/ubuntu_jdk";
    when(snapshotDao.getSnapshot(anyString(), anyString(), anyString())).thenThrow(new NotFoundException("Snapshot not found"));
    EnvironmentImpl env = createEnv();
    String envName = "env-1";
    String workspaceId = "wsId";
    List<Instance> expectedMachines = new ArrayList<>();
    when(machineProvider.startService(anyString(), eq(workspaceId), eq(envName), anyString(), anyBoolean(), anyString(), any(CheServiceImpl.class), any(LineConsumer.class))).thenAnswer(invocationOnMock -> {
        Object[] arguments = invocationOnMock.getArguments();
        String machineName = (String) arguments[3];
        boolean isDev = (boolean) arguments[4];
        CheServiceImpl service = (CheServiceImpl) arguments[6];
        Machine machine = createMachine(workspaceId, envName, service, machineName, isDev);
        NoOpMachineInstance instance = spy(new NoOpMachineInstance(machine));
        expectedMachines.add(instance);
        return instance;
    });
    CheServicesEnvironmentImpl servicesEnvironment = createCheServicesEnv();
    for (CheServiceImpl service : servicesEnvironment.getServices().values()) {
        service.setImage(machineImage);
    }
    when(environmentParser.parse(env)).thenReturn(servicesEnvironment);
    // when
    List<Instance> machines = engine.start(workspaceId, envName, env, true, messageConsumer);
    // then
    assertEquals(machines, expectedMachines);
    ArgumentCaptor<CheServiceImpl> captor = ArgumentCaptor.forClass(CheServiceImpl.class);
    verify(machineProvider).startService(anyString(), anyString(), anyString(), anyString(), eq(false), anyString(), captor.capture(), any(LineConsumer.class));
    CheServiceImpl actualService = captor.getValue();
    assertEquals(actualService.getImage(), machineImage);
}
Also used : Instance(org.eclipse.che.api.machine.server.spi.Instance) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) ArrayList(java.util.ArrayList) NotFoundException(org.eclipse.che.api.core.NotFoundException) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl) Matchers.anyString(org.mockito.Matchers.anyString) ExtendedMachine(org.eclipse.che.api.core.model.workspace.ExtendedMachine) Machine(org.eclipse.che.api.core.model.machine.Machine) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) Test(org.testng.annotations.Test)

Example 14 with Instance

use of org.eclipse.che.api.machine.server.spi.Instance in project che by eclipse.

the class CheEnvironmentEngineTest method stopsTheEnvironmentWhileStartOfMachineIsInterrupted.

@Test
public void stopsTheEnvironmentWhileStartOfMachineIsInterrupted() throws Exception {
    // given
    EnvironmentImpl env = createEnv();
    String envName = "env-1";
    String workspaceId = "wsId";
    int[] counter = new int[] { env.getMachines().size() };
    ArrayList<Instance> created = new ArrayList<>();
    when(machineProvider.startService(anyString(), eq(workspaceId), eq(envName), anyString(), anyBoolean(), anyString(), any(CheServiceImpl.class), any(LineConsumer.class))).thenAnswer(invocationOnMock -> {
        if (--counter[0] == 0) {
            Thread.currentThread().interrupt();
            throw new ServerException("interrupted!");
        }
        Object[] arguments = invocationOnMock.getArguments();
        NoOpMachineInstance instance = spy(new NoOpMachineInstance(createMachine(workspaceId, envName, (CheServiceImpl) arguments[6], (String) arguments[3], (boolean) arguments[4])));
        created.add(instance);
        return instance;
    });
    when(environmentParser.parse(env)).thenReturn(createCheServicesEnv());
    // when, then
    try {
        engine.start(workspaceId, envName, env, false, messageConsumer, startedHandler);
        fail("environment must not be running");
    } catch (EnvironmentStartInterruptedException x) {
        assertEquals(x.getMessage(), format("Start of environment '%s' in workspace '%s' is interrupted", envName, workspaceId));
    }
    // environment must not be running
    try {
        engine.getMachines(workspaceId);
        fail("environment must not be running");
    } catch (EnvironmentNotRunningException x) {
        assertEquals(x.getMessage(), format("Environment with ID '%s' is not found", workspaceId));
    }
    // all the machines expect of the last one must be destroyed
    for (Instance instance : created) {
        verify(instance).destroy();
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) Instance(org.eclipse.che.api.machine.server.spi.Instance) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) ArrayList(java.util.ArrayList) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl) Matchers.anyString(org.mockito.Matchers.anyString) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) Test(org.testng.annotations.Test)

Example 15 with Instance

use of org.eclipse.che.api.machine.server.spi.Instance in project che by eclipse.

the class CheEnvironmentEngineTest method shouldBeAbleToGetMachinesOfEnv.

@Test
public void shouldBeAbleToGetMachinesOfEnv() throws Exception {
    // given
    List<Instance> instances = startEnv();
    String workspaceId = instances.get(0).getWorkspaceId();
    // when
    List<Instance> actualMachines = engine.getMachines(workspaceId);
    // then
    assertEquals(actualMachines, instances);
}
Also used : Instance(org.eclipse.che.api.machine.server.spi.Instance) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Aggregations

Instance (org.eclipse.che.api.machine.server.spi.Instance)47 Test (org.testng.annotations.Test)36 Matchers.anyString (org.mockito.Matchers.anyString)22 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)19 CheServiceImpl (org.eclipse.che.api.environment.server.model.CheServiceImpl)17 MachineConfigImpl (org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl)17 NotFoundException (org.eclipse.che.api.core.NotFoundException)13 MachineSourceImpl (org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl)11 ArrayList (java.util.ArrayList)10 ServerException (org.eclipse.che.api.core.ServerException)10 Machine (org.eclipse.che.api.core.model.machine.Machine)10 ExtendedMachine (org.eclipse.che.api.core.model.workspace.ExtendedMachine)10 EnvironmentNotRunningException (org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException)10 CheServicesEnvironmentImpl (org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl)10 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)9 ConflictException (org.eclipse.che.api.core.ConflictException)8 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)7 MachineImpl (org.eclipse.che.api.machine.server.model.impl.MachineImpl)7 SnapshotImpl (org.eclipse.che.api.machine.server.model.impl.SnapshotImpl)7 ExtendedMachineImpl (org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl)7