use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project devspaces-images by redhat-developer.
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 devspaces-images by redhat-developer.
the class WorkspaceRuntimesTest method attributesIsSetWhenRuntimeAbnormallyStopped.
@Test
public void attributesIsSetWhenRuntimeAbnormallyStopped() 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));
when(statuses.remove(anyString())).thenReturn(WorkspaceStatus.RUNNING);
RuntimeAbnormalStoppedEvent event = new RuntimeAbnormalStoppedEvent(identity, error);
localRuntimes.recoverOne(infrastructure, identity);
ArgumentCaptor<WorkspaceImpl> captor = ArgumentCaptor.forClass(WorkspaceImpl.class);
// when
localEventService.publish(event);
// then
verify(workspaceDao, atLeastOnce()).update(captor.capture());
WorkspaceImpl ws = captor.getAllValues().get(captor.getAllValues().size() - 1);
assertNotNull(ws.getAttributes().get(STOPPED_ATTRIBUTE_NAME));
assertTrue(Boolean.valueOf(ws.getAttributes().get(STOPPED_ABNORMALLY_ATTRIBUTE_NAME)));
assertEquals(ws.getAttributes().get(ERROR_MESSAGE_ATTRIBUTE_NAME), error);
}
use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project devspaces-images by redhat-developer.
the class WorkspaceRuntimesTest method runtimeIsRecoveredForWorkspaceWithDevfile.
@Test
public void runtimeIsRecoveredForWorkspaceWithDevfile() throws Exception {
RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", "default", "myId", "infraNamespace");
WorkspaceImpl workspaceMock = mockWorkspaceWithDevfile(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);
verify(devfileConverter).convert(workspaceMock.getDevfile());
}
use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project devspaces-images by redhat-developer.
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 devspaces-images by redhat-developer.
the class WorkspaceRuntimesTest method shouldInjectRuntime.
@Test
public void shouldInjectRuntime() throws Exception {
// given
WorkspaceImpl workspace = new WorkspaceImpl();
workspace.setId("ws123");
when(statuses.get("ws123")).thenReturn(WorkspaceStatus.RUNNING);
ImmutableMap<String, Machine> machines = ImmutableMap.of("machine", new MachineImpl(emptyMap(), emptyMap(), MachineStatus.STARTING));
RuntimeIdentity identity = new RuntimeIdentityImpl("ws123", "my-env", "myId", "infraNamespace");
RuntimeContext context = mockContext(identity);
ConcurrentHashMap<String, InternalRuntime<?>> runtimesStorage = new ConcurrentHashMap<>();
TestInternalRuntime testRuntime = new TestInternalRuntime(context, machines, WorkspaceStatus.STARTING);
runtimesStorage.put("ws123", testRuntime);
WorkspaceRuntimes localRuntimes = new WorkspaceRuntimes(runtimesStorage, eventService, ImmutableMap.of(TEST_ENVIRONMENT_TYPE, testEnvFactory), infrastructure, sharedPool, workspaceDao, dbInitializer, probeScheduler, statuses, lockService, devfileConverter, false);
// when
localRuntimes.injectRuntime(workspace);
// then
assertEquals(workspace.getStatus(), WorkspaceStatus.RUNNING);
assertEquals(workspace.getRuntime(), asRuntime(testRuntime));
}
Aggregations