Search in sources :

Example 11 with RuntimeContext

use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project devspaces-images by redhat-developer.

the class WorkspaceRuntimesTest method mockContext.

private RuntimeContext mockContext(RuntimeIdentity identity) throws ValidationException, InfrastructureException {
    RuntimeContext context = mock(RuntimeContext.class);
    InternalEnvironment internalEnvironment = mock(InternalEnvironment.class);
    lenient().doReturn(internalEnvironment).when(testEnvFactory).create(any(Environment.class));
    lenient().doReturn(context).when(infrastructure).prepare(eq(identity), eq(internalEnvironment));
    lenient().when(context.getInfrastructure()).thenReturn(infrastructure);
    lenient().when(context.getIdentity()).thenReturn(identity);
    lenient().when(context.getRuntime()).thenReturn(new TestInternalRuntime(context));
    lenient().when(context.getEnvironment()).thenReturn(internalEnvironment);
    List<Warning> warnings = new ArrayList<>();
    warnings.add(createWarning());
    lenient().when(internalEnvironment.getWarnings()).thenReturn(warnings);
    return context;
}
Also used : Warning(org.eclipse.che.api.core.model.workspace.Warning) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) ArrayList(java.util.ArrayList) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext)

Example 12 with RuntimeContext

use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project devspaces-images by redhat-developer.

the class WorkspaceRuntimesTest method shouldReturnRuntimesIdsOfActiveWorkspacesForGivenOwner.

@Test
public void shouldReturnRuntimesIdsOfActiveWorkspacesForGivenOwner() throws Exception {
    // given
    String ws1 = generate("workspace", 6);
    String ws2 = generate("workspace", 6);
    String ws3 = generate("workspace", 6);
    String owner = generate("user", 6);
    when(statuses.asMap()).thenReturn(ImmutableMap.of(ws1, WorkspaceStatus.STARTING, ws2, WorkspaceStatus.RUNNING, ws3, WorkspaceStatus.STOPPING));
    RuntimeIdentityImpl runtimeIdentity1 = new RuntimeIdentityImpl(ws1, generate("env", 6), owner, generate("infraNamespace", 6));
    RuntimeIdentityImpl runtimeIdentity2 = new RuntimeIdentityImpl(ws2, generate("env", 6), generate("user", 6), generate("infraNamespace", 6));
    RuntimeIdentityImpl runtimeIdentity3 = new RuntimeIdentityImpl(ws3, generate("env", 6), generate("user", 6), generate("infraNamespace", 6));
    mockWorkspaceWithConfig(runtimeIdentity1);
    mockWorkspaceWithConfig(runtimeIdentity2);
    mockWorkspaceWithConfig(runtimeIdentity3);
    RuntimeContext context1 = mockContext(runtimeIdentity1);
    RuntimeContext context2 = mockContext(runtimeIdentity2);
    RuntimeContext context3 = mockContext(runtimeIdentity3);
    when(context1.getRuntime()).thenReturn(new TestInternalRuntime(context1, emptyMap(), WorkspaceStatus.STARTING));
    when(context2.getRuntime()).thenReturn(new TestInternalRuntime(context2, emptyMap(), WorkspaceStatus.RUNNING));
    when(context3.getRuntime()).thenReturn(new TestInternalRuntime(context3, emptyMap(), WorkspaceStatus.STOPPING));
    doReturn(context1).when(infrastructure).prepare(eq(runtimeIdentity1), any());
    doReturn(context2).when(infrastructure).prepare(eq(runtimeIdentity2), any());
    doReturn(context3).when(infrastructure).prepare(eq(runtimeIdentity3), any());
    Set<RuntimeIdentity> identities = ImmutableSet.of(runtimeIdentity1, runtimeIdentity2, runtimeIdentity3);
    doReturn(identities).when(infrastructure).getIdentities();
    InternalEnvironment internalEnvironment = mock(InternalEnvironment.class);
    doReturn(internalEnvironment).when(testEnvFactory).create(any(Environment.class));
    // when
    Set<String> active = runtimes.getActive(owner);
    // then
    assertEquals(active.size(), 1);
    assertTrue(active.containsAll(asList(ws1)));
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Test(org.testng.annotations.Test)

Example 13 with RuntimeContext

use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project devspaces-images by redhat-developer.

the class WorkspaceRuntimesTest method shouldRecoverRuntimeWhenThereIsNotCachedOneDuringInjecting.

@Test
public void shouldRecoverRuntimeWhenThereIsNotCachedOneDuringInjecting() throws Exception {
    // given
    RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", "my-env", "myId", "infraNamespace");
    mockWorkspaceWithConfig(identity);
    when(statuses.get("workspace123")).thenReturn(WorkspaceStatus.STARTING);
    RuntimeContext context = mockContext(identity);
    doReturn(context).when(infrastructure).prepare(eq(identity), any());
    ImmutableMap<String, Machine> machines = ImmutableMap.of("machine", new MachineImpl(emptyMap(), emptyMap(), MachineStatus.STARTING));
    TestInternalRuntime testRuntime = new TestInternalRuntime(context, machines, WorkspaceStatus.STARTING);
    when(context.getRuntime()).thenReturn(testRuntime);
    doReturn(mock(InternalEnvironment.class)).when(testEnvFactory).create(any());
    doReturn(ImmutableSet.of(identity)).when(infrastructure).getIdentities();
    // when
    WorkspaceImpl workspace = new WorkspaceImpl();
    workspace.setId("workspace123");
    runtimes.injectRuntime(workspace);
    // then
    assertEquals(workspace.getStatus(), WorkspaceStatus.STARTING);
    assertEquals(workspace.getRuntime(), asRuntime(testRuntime));
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) MachineImpl(org.eclipse.che.api.workspace.server.model.impl.MachineImpl) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Machine(org.eclipse.che.api.core.model.workspace.runtime.Machine) Test(org.testng.annotations.Test)

Example 14 with RuntimeContext

use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project devspaces-images by redhat-developer.

the class WorkspaceRuntimesTest method stoppingStatusIsSetWhenRuntimeAbnormallyStopping.

@Test
public void stoppingStatusIsSetWhenRuntimeAbnormallyStopping() throws Exception {
    String error = "Some kind of error happened";
    EventService localEventService = new EventService();
    WorkspaceRuntimes localRuntimes = new WorkspaceRuntimes(localEventService, ImmutableMap.of(TEST_ENVIRONMENT_TYPE, testEnvFactory), infrastructure, sharedPool, workspaceDao, dbInitializer, probeScheduler, statuses, lockService, devfileConverter, false);
    localRuntimes.init();
    RuntimeIdentityDto identity = DtoFactory.newDto(RuntimeIdentityDto.class).withWorkspaceId("workspace123").withEnvName("my-env").withOwnerId("myId");
    mockWorkspaceWithConfig(identity);
    RuntimeContext context = mockContext(identity);
    when(context.getRuntime()).thenReturn(new TestInternalRuntime(context));
    RuntimeAbnormalStoppingEvent event = new RuntimeAbnormalStoppingEvent(identity, error);
    localRuntimes.recoverOne(infrastructure, identity);
    // when
    localEventService.publish(event);
    // then
    verify(statuses).replace("workspace123", WorkspaceStatus.STOPPING);
}
Also used : RuntimeAbnormalStoppingEvent(org.eclipse.che.api.workspace.server.event.RuntimeAbnormalStoppingEvent) RuntimeIdentityDto(org.eclipse.che.api.workspace.shared.dto.RuntimeIdentityDto) EventService(org.eclipse.che.api.core.notification.EventService) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) Test(org.testng.annotations.Test)

Example 15 with RuntimeContext

use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project devspaces-images by redhat-developer.

the class WorkspaceRuntimesTest method runtimeRecoveryContinuesThroughException.

@Test
public void runtimeRecoveryContinuesThroughException() throws Exception {
    // Given
    RuntimeIdentityImpl identity1 = new RuntimeIdentityImpl("workspace1", "env1", "owner1", "infraNamespace");
    RuntimeIdentityImpl identity2 = new RuntimeIdentityImpl("workspace2", "env2", "owner2", "infraNamespace");
    RuntimeIdentityImpl identity3 = new RuntimeIdentityImpl("workspace3", "env3", "owner3", "infraNamespace");
    Set<RuntimeIdentity> identities = ImmutableSet.<RuntimeIdentity>builder().add(identity1).add(identity2).add(identity3).build();
    mockWorkspaceWithConfig(identity1);
    mockWorkspaceWithConfig(identity2);
    mockWorkspaceWithConfig(identity3);
    when(statuses.get(anyString())).thenReturn(WorkspaceStatus.STARTING);
    RuntimeContext context1 = mockContext(identity1);
    when(context1.getRuntime()).thenReturn(new TestInternalRuntime(context1, emptyMap(), WorkspaceStatus.STARTING));
    doReturn(context1).when(infrastructure).prepare(eq(identity1), any());
    RuntimeContext context2 = mockContext(identity1);
    RuntimeContext context3 = mockContext(identity1);
    when(context3.getRuntime()).thenReturn(new TestInternalRuntime(context3, emptyMap(), WorkspaceStatus.STARTING));
    doReturn(context3).when(infrastructure).prepare(eq(identity3), any());
    InternalEnvironment internalEnvironment = mock(InternalEnvironment.class);
    doReturn(internalEnvironment).when(testEnvFactory).create(any(Environment.class));
    // Want to fail recovery of identity2
    doThrow(new InfrastructureException("oops!")).when(infrastructure).prepare(eq(identity2), any(InternalEnvironment.class));
    // When
    runtimes.new RecoverRuntimesTask(identities).run();
    // Then
    verify(infrastructure).prepare(identity1, internalEnvironment);
    verify(infrastructure).prepare(identity2, internalEnvironment);
    verify(infrastructure).prepare(identity3, internalEnvironment);
    WorkspaceImpl workspace1 = WorkspaceImpl.builder().setId(identity1.getWorkspaceId()).build();
    runtimes.injectRuntime(workspace1);
    assertNotNull(workspace1.getRuntime());
    assertEquals(workspace1.getStatus(), WorkspaceStatus.STARTING);
    WorkspaceImpl workspace3 = WorkspaceImpl.builder().setId(identity3.getWorkspaceId()).build();
    runtimes.injectRuntime(workspace3);
    assertNotNull(workspace3.getRuntime());
    assertEquals(workspace3.getStatus(), WorkspaceStatus.STARTING);
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Test(org.testng.annotations.Test)

Aggregations

RuntimeContext (org.eclipse.che.api.workspace.server.spi.RuntimeContext)30 Test (org.testng.annotations.Test)22 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)20 RuntimeIdentityImpl (org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl)18 InternalEnvironment (org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment)18 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)16 Environment (org.eclipse.che.api.core.model.workspace.config.Environment)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)10 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)8 ServerException (org.eclipse.che.api.core.ServerException)6 URI (java.net.URI)4 ArrayList (java.util.ArrayList)4 Machine (org.eclipse.che.api.core.model.workspace.runtime.Machine)4 EventService (org.eclipse.che.api.core.notification.EventService)4 MachineImpl (org.eclipse.che.api.workspace.server.model.impl.MachineImpl)4 RuntimeIdentityDto (org.eclipse.che.api.workspace.shared.dto.RuntimeIdentityDto)4 InternalRuntime (org.eclipse.che.api.workspace.server.spi.InternalRuntime)3 Provider (com.google.inject.Provider)2 Config (io.fabric8.kubernetes.client.Config)2 OpenShiftConfig (io.fabric8.openshift.client.OpenShiftConfig)2