use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project devspaces-images by redhat-developer.
the class KeycloakProviderConfigFactoryTest method testRethrowOnUnauthorizedException.
@Test
public void testRethrowOnUnauthorizedException() throws Exception {
doThrow(new UnauthorizedException(DtoFactory.newDto(ServiceError.class).withMessage("Any other message"))).when(keycloakServiceClient).getIdentityProviderToken(anyString());
try {
configBuilder.buildConfig(defaultConfig, A_WORKSPACE_ID);
} catch (InfrastructureException e) {
assertEquals(e.getMessage(), SHOULD_LINK_ERROR_MESSAGE, "The exception message is wrong");
return;
}
fail("Should have thrown an exception with the following message: " + SHOULD_LINK_ERROR_MESSAGE);
}
use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project devspaces-images by redhat-developer.
the class OpenShiftProjectTest method testOpenShiftProjectCleaningUpIfExceptionsOccurs.
@Test
public void testOpenShiftProjectCleaningUpIfExceptionsOccurs() throws Exception {
doThrow(new InfrastructureException("err1.")).when(services).delete();
doThrow(new InfrastructureException("err2.")).when(deployments).delete();
InfrastructureException error = null;
// when
try {
openShiftProject.cleanUp();
} catch (InfrastructureException e) {
error = e;
}
// then
assertNotNull(error);
String message = error.getMessage();
assertEquals(message, "Error(s) occurs while cleaning up the namespace. err1. err2.");
verify(routes).delete();
}
use of org.eclipse.che.api.workspace.server.spi.InfrastructureException 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.InfrastructureException in project devspaces-images by redhat-developer.
the class WorkspaceRuntimesTest method shouldNotInjectRuntimeIfExceptionOccurredOnRuntimeFetching.
@Test
public void shouldNotInjectRuntimeIfExceptionOccurredOnRuntimeFetching() throws Exception {
// given
RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", "my-env", "myId", "infraNamespace");
mockWorkspaceWithConfig(identity);
when(statuses.get("workspace123")).thenReturn(WorkspaceStatus.STARTING);
mockContext(identity);
doThrow(new InfrastructureException("error")).when(infrastructure).prepare(eq(identity), any());
doReturn(ImmutableSet.of(identity)).when(infrastructure).getIdentities();
// when
WorkspaceImpl workspace = new WorkspaceImpl();
workspace.setId("workspace123");
runtimes.injectRuntime(workspace);
// then
verify(statuses).remove(eq(identity.getWorkspaceId()));
assertEquals(workspace.getStatus(), WorkspaceStatus.STOPPED);
assertNull(workspace.getRuntime());
}
use of org.eclipse.che.api.workspace.server.spi.InfrastructureException 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);
}
Aggregations