Search in sources :

Example 36 with Instance

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

the class CheEnvironmentEngineTest method shouldBeAbleToStartNonDockerMachine.

@Test
public void shouldBeAbleToStartNonDockerMachine() throws Exception {
    // given
    ServerConf2 serverConf2 = mock(ServerConf2.class);
    when(serverConf2.getPort()).thenReturn("1111/tcp");
    when(serverConf2.getProtocol()).thenReturn("http");
    when(serverConf2.getProperties()).thenReturn(singletonMap("path", "some path"));
    when(agent.getServers()).thenAnswer(invocation -> singletonMap("ssh", serverConf2));
    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(machineInstanceProviders.getProvider("anotherType")).thenReturn(instanceProvider);
    doReturn(newMachine).when(instanceProvider).createInstance(any(Machine.class), any(LineConsumer.class));
    MachineConfigImpl config = MachineConfigImpl.builder().fromConfig(createConfig(false)).setType("anotherType").build();
    // when
    Instance actualInstance = engine.startMachine(workspaceId, config, singletonList("agent"));
    // then
    assertEquals(actualInstance, newMachine);
    ArgumentCaptor<Machine> argumentCaptor = ArgumentCaptor.forClass(Machine.class);
    verify(instanceProvider).createInstance(argumentCaptor.capture(), any(LineConsumer.class));
    MachineConfigImpl newConfig = new MachineConfigImpl(config);
    newConfig.setServers(singletonList(new ServerConfImpl("ssh", "1111/tcp", "http", "some path")));
    assertEquals(argumentCaptor.getValue().getConfig(), newConfig);
}
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) Matchers.anyString(org.mockito.Matchers.anyString) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) ServerConf2(org.eclipse.che.api.core.model.workspace.ServerConf2) ExtendedMachine(org.eclipse.che.api.core.model.workspace.ExtendedMachine) Machine(org.eclipse.che.api.core.model.machine.Machine) Test(org.testng.annotations.Test)

Example 37 with Instance

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

the class CheEnvironmentEngineTest method machineStartShouldPublishEvents.

@Test
public void machineStartShouldPublishEvents() throws Exception {
    // given
    List<Instance> instances = startEnv();
    Instance instance = instances.get(0);
    MachineConfigImpl config = createConfig(false);
    when(engine.generateMachineId()).thenReturn("newMachineId");
    // when
    engine.startMachine(instance.getWorkspaceId(), config, emptyList());
    // then
    verify(eventService).publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.CREATING).withDev(config.isDev()).withMachineName(config.getName()).withMachineId("newMachineId").withWorkspaceId(instance.getWorkspaceId()));
    verify(eventService).publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.RUNNING).withDev(config.isDev()).withMachineName(config.getName()).withMachineId("newMachineId").withWorkspaceId(instance.getWorkspaceId()));
}
Also used : MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) Instance(org.eclipse.che.api.machine.server.spi.Instance) Test(org.testng.annotations.Test)

Example 38 with Instance

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

the class CheEnvironmentEngineTest method envStartShouldFireEvents.

@Test
public void envStartShouldFireEvents() throws Exception {
    // when
    List<Instance> instances = startEnv();
    assertTrue(instances.size() > 1, "This test requires at least 2 instances in environment");
    // then
    for (Instance instance : instances) {
        verify(eventService).publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.CREATING).withDev(instance.getConfig().isDev()).withMachineName(instance.getConfig().getName()).withMachineId(instance.getId()).withWorkspaceId(instance.getWorkspaceId()));
        verify(eventService).publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.RUNNING).withDev(instance.getConfig().isDev()).withMachineName(instance.getConfig().getName()).withMachineId(instance.getId()).withWorkspaceId(instance.getWorkspaceId()));
    }
}
Also used : Instance(org.eclipse.che.api.machine.server.spi.Instance) Test(org.testng.annotations.Test)

Example 39 with Instance

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

the class CheEnvironmentEngineTest method shouldBeAbleToStartEnvironmentWithRecover.

@Test
public void shouldBeAbleToStartEnvironmentWithRecover() throws Exception {
    // given
    SnapshotImpl snapshot = mock(SnapshotImpl.class);
    MachineSourceImpl machineSource = new MachineSourceImpl("image", "registry.com/snapshot123:latest@sha256:abc1234567890", null);
    when(snapshotDao.getSnapshot(anyString(), anyString(), anyString())).thenReturn(snapshot);
    when(snapshot.getMachineSource()).thenReturn(machineSource);
    // 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, 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(), "registry.com/snapshot123:latest");
}
Also used : SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) Instance(org.eclipse.che.api.machine.server.spi.Instance) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) ArrayList(java.util.ArrayList) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) 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 40 with Instance

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

the class CheEnvironmentEngineTest method machineStartShouldThrowExceptionIfMachineWithTheSameNameAlreadyExistsInEnvironment.

@Test(expectedExceptions = ConflictException.class, expectedExceptionsMessageRegExp = "Machine with name '.*' already exists in environment of workspace '.*'")
public void machineStartShouldThrowExceptionIfMachineWithTheSameNameAlreadyExistsInEnvironment() throws Exception {
    // given
    List<Instance> instances = startEnv();
    Instance instance = instances.get(0);
    MachineConfigImpl config = createConfig(false);
    config.setName(instance.getConfig().getName());
    // when
    engine.startMachine(instance.getWorkspaceId(), config, emptyList());
}
Also used : MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) 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