Search in sources :

Example 11 with EnvironmentImpl

use of org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl in project che by eclipse.

the class CheEnvironmentEngineTest method createEnv.

private EnvironmentImpl createEnv() {
    // singletonMap, asList are wrapped into modifiable collections to ease env modifying by tests
    EnvironmentImpl env = new EnvironmentImpl();
    Map<String, ExtendedMachineImpl> machines = new HashMap<>();
    Map<String, ServerConf2Impl> servers = new HashMap<>();
    servers.put("ref1", new ServerConf2Impl("8080/tcp", "proto1", singletonMap("prop1", "propValue")));
    servers.put("ref2", new ServerConf2Impl("8080/udp", "proto1", null));
    servers.put("ref3", new ServerConf2Impl("9090", "proto1", null));
    machines.put("dev-machine", new ExtendedMachineImpl(asList("org.eclipse.che.ws-agent", "someAgent"), servers, singletonMap("memoryLimitBytes", "10000")));
    machines.put("machine2", new ExtendedMachineImpl(asList("someAgent2", "someAgent3"), null, singletonMap("memoryLimitBytes", "10000")));
    String environmentRecipeContent = "services:\n  " + "dev-machine:\n    image: codenvy/ubuntu_jdk8\n    mem_limit: 4294967296\n  " + "machine2:\n    image: codenvy/ubuntu_jdk8\n    mem_limit: 100000";
    env.setRecipe(new EnvironmentRecipeImpl("compose", "application/x-yaml", environmentRecipeContent, null));
    env.setMachines(machines);
    return env;
}
Also used : ServerConf2Impl(org.eclipse.che.api.workspace.server.model.impl.ServerConf2Impl) HashMap(java.util.HashMap) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) 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) EnvironmentRecipeImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentRecipeImpl)

Example 12 with EnvironmentImpl

use of org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl in project che by eclipse.

the class CheEnvironmentEngineTest method shouldNotSetDockerfileContentInsteadOfUrlIfUrlDoesNotPointToCheApiOnEnvironmentStart.

@Test
public void shouldNotSetDockerfileContentInsteadOfUrlIfUrlDoesNotPointToCheApiOnEnvironmentStart() throws Exception {
    // given
    EnvironmentImpl env = createEnv();
    String machineName = "machineWithDockerfileNotFromApi";
    String contextUrl = "http://another-server.com/recipe/12345";
    //prepare CheServicesEnvironmentImpl which should return compose parser
    CheServicesEnvironmentImpl cheServicesEnvironment = createCheServicesEnvByName(machineName);
    cheServicesEnvironment.getServices().get(machineName).withBuild(new CheServiceBuildContextImpl().withContext(contextUrl));
    // when
    startEnv(env, cheServicesEnvironment);
    // then
    ArgumentCaptor<CheServiceImpl> captor = ArgumentCaptor.forClass(CheServiceImpl.class);
    verify(machineProvider).startService(anyString(), anyString(), anyString(), eq(machineName), eq(false), anyString(), captor.capture(), any(LineConsumer.class));
    CheServiceImpl actualService = captor.getValue();
    assertNull(actualService.getBuild().getDockerfilePath());
    assertNull(actualService.getBuild().getDockerfileContent());
    assertEquals(actualService.getBuild().getContext(), contextUrl);
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) CheServiceBuildContextImpl(org.eclipse.che.api.environment.server.model.CheServiceBuildContextImpl) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) 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) Test(org.testng.annotations.Test)

Example 13 with EnvironmentImpl

use of org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl in project che by eclipse.

the class CheEnvironmentEngineTest method shouldBeAbleToStartEnvironment.

@Test
public void shouldBeAbleToStartEnvironment() throws Exception {
    // given
    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;
    });
    when(environmentParser.parse(env)).thenReturn(createCheServicesEnv());
    // when
    List<Instance> machines = engine.start(workspaceId, envName, env, false, messageConsumer, startedHandler);
    // then
    assertEquals(machines, expectedMachines);
    for (Instance expectedMachine : expectedMachines) {
        verify(startedHandler).started(eq(expectedMachine), any(ExtendedMachine.class));
    }
}
Also used : Instance(org.eclipse.che.api.machine.server.spi.Instance) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) ArrayList(java.util.ArrayList) 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) 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 EnvironmentImpl

use of org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl 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 15 with EnvironmentImpl

use of org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl in project che by eclipse.

the class WorkspaceRuntimes method startAsync.

/**
     * Asynchronously starts the environment of the workspace.
     * Before executing start task checks whether all conditions
     * are met and throws appropriate exceptions if not, so
     * there is no way to start the same workspace twice.
     *
     * <p>Note that cancellation of resulting future won't
     * interrupt workspace start, call {@link #stop(String)} directly instead.
     *
     * <p>If starting process is interrupted let's say within call
     * to {@link #stop(String)} method, resulting future will
     * be exceptionally completed(eventually) with an instance of
     * {@link EnvironmentStartInterruptedException}. Note that clients
     * don't have to cleanup runtime resources, the component
     * will do necessary cleanup when interrupted.
     *
     * <p>Implementation notes:
     * if thread which executes the task is interrupted, then the
     * task is also eventually(depends on the environment engine implementation)
     * interrupted as if {@link #stop(String)} is called directly.
     * That helps to shutdown gracefully when thread pool is asked
     * to {@link ExecutorService#shutdownNow()} and also reduces
     * shutdown time when there are starting workspaces.
     *
     * @param workspace
     *         workspace containing target environment
     * @param envName
     *         the name of the environment to start
     * @param recover
     *         whether to recover from the snapshot
     * @return completable future describing the instance of running environment
     * @throws ConflictException
     *         when the workspace is already started
     * @throws ConflictException
     *         when workspaces start refused {@link #refuseWorkspacesStart()} was called
     * @throws ServerException
     *         when any other error occurs
     * @throws IllegalArgumentException
     *         when the workspace doesn't contain the environment
     * @throws NullPointerException
     *         when either {@code workspace} or {@code envName} is null
     */
public CompletableFuture<WorkspaceRuntimeImpl> startAsync(Workspace workspace, String envName, boolean recover) throws ConflictException, ServerException {
    requireNonNull(workspace, "Non-null workspace required");
    requireNonNull(envName, "Non-null environment name required");
    EnvironmentImpl environment = copyEnv(workspace, envName);
    String workspaceId = workspace.getId();
    CompletableFuture<WorkspaceRuntimeImpl> cmpFuture;
    StartTask startTask;
    try (@SuppressWarnings("unused") Unlocker u = locks.writeLock(workspaceId)) {
        checkIsNotTerminated("start the workspace");
        if (isStartRefused.get()) {
            throw new ConflictException(format("Start of the workspace '%s' is rejected by the system, " + "no more workspaces are allowed to start", workspace.getConfig().getName()));
        }
        RuntimeState state = states.get(workspaceId);
        if (state != null) {
            throw new ConflictException(format("Could not start workspace '%s' because its status is '%s'", workspace.getConfig().getName(), state.status));
        }
        startTask = new StartTask(workspaceId, envName, environment, recover, cmpFuture = new CompletableFuture<>());
        states.put(workspaceId, new RuntimeState(WorkspaceStatus.STARTING, envName, startTask, sharedPool.submit(startTask)));
    }
    // publish event synchronously as the task may not be executed by
    // executors service(due to legal cancellation), clients still have
    // to receive STOPPED -> STARTING event
    eventsService.publish(DtoFactory.newDto(WorkspaceStatusEvent.class).withWorkspaceId(workspaceId).withStatus(WorkspaceStatus.STARTING).withEventType(EventType.STARTING).withPrevStatus(WorkspaceStatus.STOPPED));
    // so the start thread is free to go and start the environment
    startTask.unlockStart();
    return cmpFuture;
}
Also used : ConflictException(org.eclipse.che.api.core.ConflictException) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) WorkspaceStatusEvent(org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) WorkspaceRuntimeImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceRuntimeImpl)

Aggregations

EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)40 CheServicesEnvironmentImpl (org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl)27 Test (org.testng.annotations.Test)23 ExtendedMachineImpl (org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl)17 CheServiceImpl (org.eclipse.che.api.environment.server.model.CheServiceImpl)15 Matchers.anyString (org.mockito.Matchers.anyString)14 EnvironmentRecipeImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentRecipeImpl)10 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)7 ServerConf2Impl (org.eclipse.che.api.workspace.server.model.impl.ServerConf2Impl)7 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)6 Instance (org.eclipse.che.api.machine.server.spi.Instance)5 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)5 CheServiceBuildContextImpl (org.eclipse.che.api.environment.server.model.CheServiceBuildContextImpl)4 CommandImpl (org.eclipse.che.api.machine.server.model.impl.CommandImpl)4 NotFoundException (org.eclipse.che.api.core.NotFoundException)3 ServerException (org.eclipse.che.api.core.ServerException)3 Machine (org.eclipse.che.api.core.model.machine.Machine)3 ExtendedMachine (org.eclipse.che.api.core.model.workspace.ExtendedMachine)3