Search in sources :

Example 21 with CheServiceImpl

use of org.eclipse.che.api.environment.server.model.CheServiceImpl 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 22 with CheServiceImpl

use of org.eclipse.che.api.environment.server.model.CheServiceImpl 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 23 with CheServiceImpl

use of org.eclipse.che.api.environment.server.model.CheServiceImpl in project che by eclipse.

the class CheEnvironmentValidatorTest method createCheService.

private static CheServiceImpl createCheService(String suffix, long memLimitBytes, List<String> links, List<String> dependsOn, List<String> volumesFrom) {
    CheServiceImpl service = new CheServiceImpl();
    service.setMemLimit(memLimitBytes);
    service.setImage("image_repo/image" + suffix);
    service.setEnvironment(new HashMap<>(singletonMap("env" + suffix, "val" + suffix)));
    service.setCommand(new ArrayList<>(asList("this", "is", "command" + suffix)));
    service.setContainerName("containerName" + suffix);
    service.setEntrypoint(new ArrayList<>(asList("this", "is", "entrypoint" + suffix)));
    service.setExpose(new ArrayList<>(asList("8080", "9090/tcp", "7070/udp")));
    service.setLabels(new HashMap<>(singletonMap("label" + suffix, "value" + suffix)));
    if (links != null) {
        service.setLinks(new ArrayList<>(links));
    }
    if (dependsOn != null) {
        service.setDependsOn(new ArrayList<>(dependsOn));
    }
    if (volumesFrom != null) {
        service.setVolumesFrom(new ArrayList<>(volumesFrom));
    }
    return service;
}
Also used : CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl)

Example 24 with CheServiceImpl

use of org.eclipse.che.api.environment.server.model.CheServiceImpl in project che by eclipse.

the class EnvironmentParserTest method getEntryForDockerimageEnv.

private static List<Object> getEntryForDockerimageEnv(Map<String, ServerConf2Impl> servers, List<String> expectedExpose, Map<String, String> expectedLabels) {
    EnvironmentImpl environmentConfig = createDockerimageEnvConfig();
    ExtendedMachineImpl extendedMachine = getMachine(environmentConfig);
    extendedMachine.setServers(servers);
    CheServicesEnvironmentImpl parsedCheEnv = new CheServicesEnvironmentImpl();
    parsedCheEnv.getServices().put(DEFAULT_MACHINE_NAME, new CheServiceImpl().withImage(DEFAULT_DOCKER_IMAGE));
    return asList(environmentConfig, createExpectedEnvFromImage(expectedExpose, expectedLabels), parsedCheEnv);
}
Also used : CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) 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)

Example 25 with CheServiceImpl

use of org.eclipse.che.api.environment.server.model.CheServiceImpl in project che by eclipse.

the class EnvironmentParserTest method getEntryForDockerfileEnv.

private static List<Object> getEntryForDockerfileEnv(Map<String, ServerConf2Impl> servers, List<String> expectedExpose, Map<String, String> expectedLabels) {
    EnvironmentImpl environmentConfig = createDockerfileEnvConfig();
    ExtendedMachineImpl extendedMachine = getMachine(environmentConfig);
    extendedMachine.setServers(servers);
    CheServicesEnvironmentImpl parsedCheEnv = new CheServicesEnvironmentImpl();
    CheServiceBuildContextImpl buildContext = new CheServiceBuildContextImpl().withDockerfileContent(DEFAULT_DOCKERFILE);
    parsedCheEnv.getServices().put(DEFAULT_MACHINE_NAME, new CheServiceImpl().withBuild(buildContext));
    return asList(environmentConfig, createExpectedEnvFromDockerfile(expectedExpose, expectedLabels), parsedCheEnv);
}
Also used : CheServiceBuildContextImpl(org.eclipse.che.api.environment.server.model.CheServiceBuildContextImpl) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) 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)

Aggregations

CheServiceImpl (org.eclipse.che.api.environment.server.model.CheServiceImpl)93 Test (org.testng.annotations.Test)63 CheServicesEnvironmentImpl (org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl)46 Matchers.anyString (org.mockito.Matchers.anyString)32 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)28 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)24 Map (java.util.Map)18 Machine (org.eclipse.che.api.core.model.machine.Machine)17 CreateContainerParams (org.eclipse.che.plugin.docker.client.params.CreateContainerParams)17 ServerException (org.eclipse.che.api.core.ServerException)16 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)16 IOException (java.io.IOException)14 List (java.util.List)14 ServerConf (org.eclipse.che.api.core.model.machine.ServerConf)14 Collections.singletonMap (java.util.Collections.singletonMap)13 Set (java.util.Set)13 CheServiceBuildContextImpl (org.eclipse.che.api.environment.server.model.CheServiceBuildContextImpl)13 Instance (org.eclipse.che.api.machine.server.spi.Instance)13 EnvironmentContext (org.eclipse.che.commons.env.EnvironmentContext)13