Search in sources :

Example 26 with Instance

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

the class CheEnvironmentEngineTest method machineStopOfNonExistingMachineShouldThrowsException.

@Test(expectedExceptions = NotFoundException.class, expectedExceptionsMessageRegExp = "Machine with ID '.*' is not found in environment of workspace '.*'")
public void machineStopOfNonExistingMachineShouldThrowsException() throws Exception {
    // given
    List<Instance> instances = startEnv();
    Instance instance = instances.get(0);
    // when
    engine.stopMachine(instance.getWorkspaceId(), "idOfNonExistingMachine");
}
Also used : Instance(org.eclipse.che.api.machine.server.spi.Instance) Test(org.testng.annotations.Test)

Example 27 with Instance

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

the class CheEnvironmentEngineTest method shouldDestroyAndRemoveMachineFromEnvironmentIfEventAboutItsOOM.

@Test
public void shouldDestroyAndRemoveMachineFromEnvironmentIfEventAboutItsOOM() 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.OOM));
    // 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 28 with Instance

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

the class MachineProviderImpl method startService.

@Override
public Instance startService(String namespace, String workspaceId, String envName, String machineName, boolean isDev, String networkName, CheServiceImpl service, LineConsumer machineLogger) throws ServerException {
    // copy to not affect/be affected by changes in origin
    service = new CheServiceImpl(service);
    ProgressLineFormatterImpl progressLineFormatter = new ProgressLineFormatterImpl();
    ProgressMonitor progressMonitor = currentProgressStatus -> {
        try {
            machineLogger.writeLine(progressLineFormatter.format(currentProgressStatus));
        } catch (IOException e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    };
    String container = null;
    try {
        String image = prepareImage(machineName, service, progressMonitor);
        container = createContainer(workspaceId, machineName, isDev, image, networkName, service);
        connectContainerToAdditionalNetworks(container, service);
        docker.startContainer(StartContainerParams.create(container));
        readContainerLogsInSeparateThread(container, workspaceId, service.getId(), machineLogger);
        DockerNode node = dockerMachineFactory.createNode(workspaceId, container);
        dockerInstanceStopDetector.startDetection(container, service.getId(), workspaceId);
        final String userId = EnvironmentContext.getCurrent().getSubject().getUserId();
        MachineImpl machine = new MachineImpl(MachineConfigImpl.builder().setDev(isDev).setName(machineName).setType("docker").setLimits(new MachineLimitsImpl((int) Size.parseSizeToMegabytes(service.getMemLimit() + "b"))).setSource(new MachineSourceImpl(service.getBuild() != null ? "context" : "image").setLocation(service.getBuild() != null ? service.getBuild().getContext() : service.getImage())).build(), service.getId(), workspaceId, envName, userId, MachineStatus.RUNNING, null);
        return dockerMachineFactory.createInstance(machine, container, image, node, machineLogger);
    } catch (SourceNotFoundException e) {
        throw e;
    } catch (RuntimeException | ServerException | NotFoundException | IOException e) {
        cleanUpContainer(container);
        throw new ServerException(e.getLocalizedMessage(), e);
    }
}
Also used : ConnectContainer(org.eclipse.che.plugin.docker.client.json.network.ConnectContainer) RemoveContainerParams(org.eclipse.che.plugin.docker.client.params.RemoveContainerParams) Arrays(java.util.Arrays) MachineStatus(org.eclipse.che.api.core.model.machine.MachineStatus) HostConfig(org.eclipse.che.plugin.docker.client.json.HostConfig) DockerConnectorProvider(org.eclipse.che.plugin.docker.client.DockerConnectorProvider) Volume(org.eclipse.che.plugin.docker.client.json.Volume) MachineLimitsImpl(org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl) NewNetwork(org.eclipse.che.plugin.docker.client.json.network.NewNetwork) BuildImageParams(org.eclipse.che.plugin.docker.client.params.BuildImageParams) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Thread.sleep(java.lang.Thread.sleep) NetworkingConfig(org.eclipse.che.plugin.docker.client.json.container.NetworkingConfig) LoggingUncaughtExceptionHandler(org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler) FileCleaner(org.eclipse.che.api.core.util.FileCleaner) Collectors.toSet(java.util.stream.Collectors.toSet) ProgressLineFormatterImpl(org.eclipse.che.plugin.docker.client.ProgressLineFormatterImpl) GetContainerLogsParams(org.eclipse.che.plugin.docker.client.params.GetContainerLogsParams) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) Set(java.util.Set) Nullable(org.eclipse.che.commons.annotation.Nullable) Executors(java.util.concurrent.Executors) String.format(java.lang.String.format) WindowsPathEscaper(org.eclipse.che.commons.lang.os.WindowsPathEscaper) ConnectContainerToNetworkParams(org.eclipse.che.plugin.docker.client.params.network.ConnectContainerToNetworkParams) List(java.util.List) ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) TagParams(org.eclipse.che.plugin.docker.client.params.TagParams) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) Pattern(java.util.regex.Pattern) UserSpecificDockerRegistryCredentialsProvider(org.eclipse.che.plugin.docker.client.UserSpecificDockerRegistryCredentialsProvider) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) PullParams(org.eclipse.che.plugin.docker.client.params.PullParams) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) EndpointConfig(org.eclipse.che.plugin.docker.client.json.network.EndpointConfig) ImageNotFoundException(org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException) Size(org.eclipse.che.commons.lang.Size) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) StartContainerParams(org.eclipse.che.plugin.docker.client.params.StartContainerParams) ArrayList(java.util.ArrayList) RemoveImageParams(org.eclipse.che.plugin.docker.client.params.RemoveImageParams) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ProgressMonitor(org.eclipse.che.plugin.docker.client.ProgressMonitor) LATEST_TAG(org.eclipse.che.plugin.docker.machine.DockerInstance.LATEST_TAG) CreateNetworkParams(org.eclipse.che.plugin.docker.client.params.network.CreateNetworkParams) PortBinding(org.eclipse.che.plugin.docker.client.json.PortBinding) SocketTimeoutException(java.net.SocketTimeoutException) MachineInstanceProvider(org.eclipse.che.api.environment.server.MachineInstanceProvider) Collections.singletonMap(java.util.Collections.singletonMap) Named(javax.inject.Named) Instance(org.eclipse.che.api.machine.server.spi.Instance) ExecutorService(java.util.concurrent.ExecutorService) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) Collections.emptyMap(java.util.Collections.emptyMap) Logger(org.slf4j.Logger) DockerNode(org.eclipse.che.plugin.docker.machine.node.DockerNode) Files(java.nio.file.Files) NetworkNotFoundException(org.eclipse.che.plugin.docker.client.exception.NetworkNotFoundException) FileWriter(java.io.FileWriter) MoreObjects(com.google.common.base.MoreObjects) ServerConf(org.eclipse.che.api.core.model.machine.ServerConf) IOException(java.io.IOException) SystemInfo(org.eclipse.che.api.core.util.SystemInfo) RemoveNetworkParams(org.eclipse.che.plugin.docker.client.params.RemoveNetworkParams) NotFoundException(org.eclipse.che.api.core.NotFoundException) File(java.io.File) ContainerNotFoundException(org.eclipse.che.plugin.docker.client.exception.ContainerNotFoundException) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) ServerException(org.eclipse.che.api.core.ServerException) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) DockerConnector(org.eclipse.che.plugin.docker.client.DockerConnector) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) DockerNode(org.eclipse.che.plugin.docker.machine.node.DockerNode) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) ServerException(org.eclipse.che.api.core.ServerException) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) ProgressLineFormatterImpl(org.eclipse.che.plugin.docker.client.ProgressLineFormatterImpl) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) ImageNotFoundException(org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException) NetworkNotFoundException(org.eclipse.che.plugin.docker.client.exception.NetworkNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ContainerNotFoundException(org.eclipse.che.plugin.docker.client.exception.ContainerNotFoundException) MachineLimitsImpl(org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl) IOException(java.io.IOException) ProgressMonitor(org.eclipse.che.plugin.docker.client.ProgressMonitor)

Example 29 with Instance

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

the class CheEnvironmentEngineTest method shouldSetDefaultRamToMachineWithoutRamOnMachineStart.

@Test
public void shouldSetDefaultRamToMachineWithoutRamOnMachineStart() throws Exception {
    // given
    List<Instance> instances = startEnv();
    String workspaceId = instances.get(0).getWorkspaceId();
    when(engine.generateMachineId()).thenReturn("newMachineId");
    Instance newMachine = mock(Instance.class);
    when(newMachine.getId()).thenReturn("newMachineId");
    when(newMachine.getWorkspaceId()).thenReturn(workspaceId);
    when(machineProvider.startService(anyString(), anyString(), anyString(), anyString(), anyBoolean(), anyString(), any(CheServiceImpl.class), any(LineConsumer.class))).thenReturn(newMachine);
    MachineConfigImpl config = createConfig(false);
    String machineName = "extraMachine";
    config.setName(machineName);
    config.setLimits(null);
    // when
    engine.startMachine(workspaceId, config, emptyList());
    // 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();
    assertEquals((long) actualService.getMemLimit(), DEFAULT_MACHINE_MEM_LIMIT_MB * 1024L * 1024L);
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) Instance(org.eclipse.che.api.machine.server.spi.Instance) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 30 with Instance

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

the class CheEnvironmentEngineTest method devMachineStopShouldThrowException.

@Test(expectedExceptions = ConflictException.class, expectedExceptionsMessageRegExp = "Stop of dev machine is not allowed. Please, stop whole environment")
public void devMachineStopShouldThrowException() throws Exception {
    // given
    List<Instance> instances = startEnv();
    Optional<Instance> instanceOpt = instances.stream().filter(machine -> machine.getConfig().isDev()).findAny();
    assertTrue(instanceOpt.isPresent(), "Required for test dev machine is not found");
    Instance instance = instanceOpt.get();
    // when
    engine.stopMachine(instance.getWorkspaceId(), instance.getId());
}
Also used : Agent(org.eclipse.che.api.agent.shared.model.Agent) MachineStatusEvent(org.eclipse.che.api.machine.shared.dto.event.MachineStatusEvent) Listeners(org.testng.annotations.Listeners) MachineStatus(org.eclipse.che.api.core.model.machine.MachineStatus) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) MachineLimitsImpl(org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl) Test(org.testng.annotations.Test) WorkspaceSharedPool(org.eclipse.che.api.workspace.server.WorkspaceSharedPool) AfterMethod(org.testng.annotations.AfterMethod) ServerConf2Impl(org.eclipse.che.api.workspace.server.model.impl.ServerConf2Impl) Collections.singletonList(java.util.Collections.singletonList) Mockito.doThrow(org.mockito.Mockito.doThrow) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Arrays.asList(java.util.Arrays.asList) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) MessageConsumer(org.eclipse.che.api.core.util.MessageConsumer) CheServiceBuildContextImpl(org.eclipse.che.api.environment.server.model.CheServiceBuildContextImpl) Mockito.doReturn(org.mockito.Mockito.doReturn) EventService(org.eclipse.che.api.core.notification.EventService) Assert.assertNotEquals(org.testng.Assert.assertNotEquals) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) EventSubscriber(org.eclipse.che.api.core.notification.EventSubscriber) Collections.emptyList(java.util.Collections.emptyList) BeforeMethod(org.testng.annotations.BeforeMethod) DtoFactory.newDto(org.eclipse.che.dto.server.DtoFactory.newDto) UUID(java.util.UUID) String.format(java.lang.String.format) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Mockito.inOrder(org.mockito.Mockito.inOrder) Optional(java.util.Optional) AgentKey(org.eclipse.che.api.agent.shared.model.AgentKey) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) AgentRegistry(org.eclipse.che.api.agent.server.AgentRegistry) Mockito.mock(org.mockito.Mockito.mock) MachineInstanceProviders(org.eclipse.che.api.machine.server.MachineInstanceProviders) Assert.assertNull(org.testng.Assert.assertNull) Size(org.eclipse.che.commons.lang.Size) Mock(org.mockito.Mock) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) Mockito.spy(org.mockito.Mockito.spy) Captor(org.mockito.Captor) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) Matchers.anyString(org.mockito.Matchers.anyString) ArrayList(java.util.ArrayList) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) InstanceProvider(org.eclipse.che.api.machine.server.spi.InstanceProvider) ArgumentCaptor(org.mockito.ArgumentCaptor) ServerConf2(org.eclipse.che.api.core.model.workspace.ServerConf2) ConflictException(org.eclipse.che.api.core.ConflictException) Collections.singletonMap(java.util.Collections.singletonMap) InstanceStateEvent(org.eclipse.che.api.machine.server.event.InstanceStateEvent) Instance(org.eclipse.che.api.machine.server.spi.Instance) MachineRuntimeInfoImpl(org.eclipse.che.api.machine.server.model.impl.MachineRuntimeInfoImpl) SnapshotDao(org.eclipse.che.api.machine.server.spi.SnapshotDao) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) Collections.emptyMap(java.util.Collections.emptyMap) EnvironmentRecipeImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentRecipeImpl) InOrder(org.mockito.InOrder) ExtendedMachine(org.eclipse.che.api.core.model.workspace.ExtendedMachine) Assert.fail(org.testng.Assert.fail) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) MachineLogMessage(org.eclipse.che.api.core.model.machine.MachineLogMessage) NotFoundException(org.eclipse.che.api.core.NotFoundException) Mockito.verify(org.mockito.Mockito.verify) Machine(org.eclipse.che.api.core.model.machine.Machine) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) Mockito.never(org.mockito.Mockito.never) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) RecipeDownloader(org.eclipse.che.api.machine.server.util.RecipeDownloader) ServerException(org.eclipse.che.api.core.ServerException) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) Instance(org.eclipse.che.api.machine.server.spi.Instance) 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