use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.
the class WorkspaceRuntimesTest method shouldRecoverEachRuntimeOnlyOnce.
@Test
public void shouldRecoverEachRuntimeOnlyOnce() throws Exception {
// Given
Set<RuntimeIdentity> identities = generateRuntimeIdentitySet(200);
doReturn(identities).when(infrastructure).getIdentities();
for (RuntimeIdentity identity : identities) {
mockWorkspaceWithDevfile(identity);
RuntimeContext context = mockContext(identity);
when(context.getRuntime()).thenReturn(new TestInternalRuntime(context, emptyMap(), WorkspaceStatus.STARTING));
doReturn(context).when(infrastructure).prepare(eq(identity), any());
}
when(statuses.get(anyString())).thenReturn(WorkspaceStatus.STARTING);
InternalEnvironment internalEnvironment = mock(InternalEnvironment.class);
doReturn(internalEnvironment).when(testEnvFactory).create(any(Environment.class));
CountDownLatch finishLatch = new CountDownLatch(1);
WorkspaceRuntimes runtimesSpy = spy(runtimes);
// When
WorkspaceRuntimes.RecoverRuntimesTask recoverRuntimesTask = runtimesSpy.new RecoverRuntimesTask(identities);
new Thread(() -> {
recoverRuntimesTask.run();
finishLatch.countDown();
}).start();
// simulate all WorkspaceManager methods that uses WorkspaceManager.normalizeState
new Thread(() -> {
List<RuntimeIdentity> runtimeIdentities = new ArrayList<>(identities);
Collections.shuffle(runtimeIdentities);
for (RuntimeIdentity runtimeIdentity : runtimeIdentities) {
if (finishLatch.getCount() > 0) {
try {
runtimesSpy.injectRuntime(WorkspaceImpl.builder().setId(runtimeIdentity.getWorkspaceId()).build());
} catch (ServerException e) {
fail(e.getMessage());
}
} else {
break;
}
}
}).start();
finishLatch.await();
// Then
verify(runtimesSpy, Mockito.times(identities.size())).recoverOne(any(RuntimeInfrastructure.class), any(RuntimeIdentity.class));
}
use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.
the class WorkspaceRuntimesTest method runtimeIsRecoveredForWorkspaceWithConfig.
@Test
public void runtimeIsRecoveredForWorkspaceWithConfig() throws Exception {
RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", "my-env", "myId", "infraNamespace");
mockWorkspaceWithConfig(identity);
RuntimeContext context = mockContext(identity);
when(context.getRuntime()).thenReturn(new TestInternalRuntime(context, emptyMap(), WorkspaceStatus.STARTING));
doReturn(context).when(infrastructure).prepare(eq(identity), any());
doReturn(mock(InternalEnvironment.class)).when(testEnvFactory).create(any());
when(statuses.get(anyString())).thenReturn(WorkspaceStatus.STARTING);
// try recover
runtimes.recoverOne(infrastructure, identity);
WorkspaceImpl workspace = WorkspaceImpl.builder().setId(identity.getWorkspaceId()).build();
runtimes.injectRuntime(workspace);
assertNotNull(workspace.getRuntime());
assertEquals(workspace.getStatus(), WorkspaceStatus.STARTING);
}
use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.
the class WorkspaceRuntimesTest method runtimeRecoveryContinuesThroughRuntimeException.
@Test
public void runtimeRecoveryContinuesThroughRuntimeException() 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(identity2);
RuntimeContext context3 = mockContext(identity3);
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 RuntimeException("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);
}
use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.
the class WorkspaceRuntimesTest method shouldUseInfraNamespaceAttributeOnStartAsync.
@Test
public void shouldUseInfraNamespaceAttributeOnStartAsync() throws Exception {
EnvironmentContext.getCurrent().setSubject(new SubjectImpl("username", "user123", null, false));
WorkspaceImpl workspace = mockWorkspaceWithDevfile("workspace123", "env");
workspace.getAttributes().put(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "infraNamespace");
RuntimeContext context = mockContext(new RuntimeIdentityImpl("workspace123", "env", "user123", "infraNamespace"));
doReturn(context.getEnvironment()).when(testEnvFactory).create(any());
runtimes.startAsync(workspace, null, emptyMap());
ArgumentCaptor<RuntimeIdentity> runtimeIdCaptor = ArgumentCaptor.forClass(RuntimeIdentity.class);
verify(infrastructure).prepare(runtimeIdCaptor.capture(), any());
RuntimeIdentity runtimeId = runtimeIdCaptor.getValue();
assertEquals(runtimeId.getInfrastructureNamespace(), "infraNamespace");
assertWorkspaceEventFired("workspace123", WorkspaceStatus.STARTING, WorkspaceStatus.STOPPED, null, true, Collections.emptyMap());
}
use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.
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);
}
Aggregations