use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.
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)));
}
use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project che-server by eclipse-che.
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 che-server by eclipse-che.
the class WorkspaceLinksGenerator method addRuntimeLinks.
private void addRuntimeLinks(Map<String, String> links, String workspaceId, ServiceContext serviceContext) throws ServerException {
URI uri = serviceContext.getServiceUriBuilder().build();
links.put(LINK_REL_ENVIRONMENT_STATUS_CHANNEL, UriBuilder.fromUri(cheWebsocketEndpoint).scheme(uri.getScheme().equals("https") ? "wss" : "ws").host(uri.getHost()).port(uri.getPort()).build().toString());
Optional<RuntimeContext> ctxOpt = workspaceRuntimes.getRuntimeContext(workspaceId);
if (ctxOpt.isPresent()) {
try {
links.put(LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL, ctxOpt.get().getOutputChannel().toString());
} catch (UnsupportedOperationException e) {
// Do not include output channel to links since it is not supported by context
} catch (InfrastructureException x) {
throw new ServerException(x.getMessage(), x);
}
}
}
use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project devspaces-images by redhat-developer.
the class KeycloakProviderConfigFactory method buildConfig.
/**
* Builds the OpenShift {@link Config} object based on a default {@link Config} object and an
* optional workspace Id.
*/
public Config buildConfig(Config defaultConfig, @Nullable String workspaceId) throws InfrastructureException {
Subject subject = EnvironmentContext.getCurrent().getSubject();
if (oauthIdentityProvider == null) {
LOG.debug("OAuth Provider is not configured, default config is used.");
return defaultConfig;
}
if (subject == Subject.ANONYMOUS) {
LOG.debug("OAuth Provider is configured but default subject is anonymous, default config is used.");
return defaultConfig;
}
if (workspaceId == null) {
LOG.debug("OAuth Provider is configured and this request is not related to any workspace. OAuth token will be retrieved.");
return personalizeConfig(defaultConfig);
}
Optional<RuntimeContext> context = workspaceRuntimeProvider.get().getRuntimeContext(workspaceId);
if (!context.isPresent()) {
// there is no cached info for this workspace in workspace API.
// it means that it's not started yet and it's initial call for preparing context
LOG.debug("There is no runtime context for the specified workspace '%s'. It's the first workspace " + "related call, so context is personalized with OAuth token.");
return personalizeConfig(defaultConfig);
}
String workspaceOwnerId = context.map(c -> c.getIdentity().getOwnerId()).orElse(null);
boolean isRuntimeOwner = subject.getUserId().equals(workspaceOwnerId);
if (!isRuntimeOwner) {
LOG.debug("OAuth Provider is configured, but current subject is not runtime owner, default config is used." + "Subject user id: '{}'. Runtime owner id: '{}'", subject.getUserId(), workspaceOwnerId);
return defaultConfig;
}
LOG.debug("OAuth Provider is configured and current subject is runtime owner. OAuth token will be retrieved.");
return personalizeConfig(defaultConfig);
}
use of org.eclipse.che.api.workspace.server.spi.RuntimeContext in project devspaces-images by redhat-developer.
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);
}
Aggregations