Search in sources :

Example 76 with InfrastructureException

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);
}
Also used : ServiceError(org.eclipse.che.api.core.rest.shared.dto.ServiceError) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Test(org.testng.annotations.Test)

Example 77 with InfrastructureException

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();
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Test(org.testng.annotations.Test)

Example 78 with InfrastructureException

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);
}
Also used : KeycloakServiceClient(org.eclipse.che.multiuser.keycloak.server.KeycloakServiceClient) CLIENT_ID_SETTING(org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.CLIENT_ID_SETTING) LoggerFactory(org.slf4j.LoggerFactory) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) Subject(org.eclipse.che.commons.subject.Subject) URI(java.net.URI) Named(javax.inject.Named) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) KubernetesClientConfigFactory(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesClientConfigFactory) KeycloakTokenResponse(org.eclipse.che.multiuser.keycloak.shared.dto.KeycloakTokenResponse) REALM_SETTING(org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.REALM_SETTING) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) Logger(org.slf4j.Logger) OpenShiftConfigBuilder(io.fabric8.openshift.client.OpenShiftConfigBuilder) Nullable(org.eclipse.che.commons.annotation.Nullable) Config(io.fabric8.kubernetes.client.Config) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Provider(com.google.inject.Provider) OpenShiftConfig(io.fabric8.openshift.client.OpenShiftConfig) URLEncoder(java.net.URLEncoder) BadRequestException(org.eclipse.che.api.core.BadRequestException) UriBuilder(jakarta.ws.rs.core.UriBuilder) WorkspaceRuntimes(org.eclipse.che.api.workspace.server.WorkspaceRuntimes) Optional(java.util.Optional) KeycloakSettings(org.eclipse.che.multiuser.keycloak.server.KeycloakSettings) AUTH_SERVER_URL_SETTING(org.eclipse.che.multiuser.oidc.OIDCInfoProvider.AUTH_SERVER_URL_SETTING) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RuntimeContext(org.eclipse.che.api.workspace.server.spi.RuntimeContext) Subject(org.eclipse.che.commons.subject.Subject)

Example 79 with InfrastructureException

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());
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Test(org.testng.annotations.Test)

Example 80 with InfrastructureException

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);
}
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

InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)242 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)64 Test (org.testng.annotations.Test)56 KubernetesInfrastructureException (org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException)44 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)42 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)38 CompletableFuture (java.util.concurrent.CompletableFuture)36 ExecutionException (java.util.concurrent.ExecutionException)36 TimeoutException (java.util.concurrent.TimeoutException)32 ServerException (org.eclipse.che.api.core.ServerException)32 Pod (io.fabric8.kubernetes.api.model.Pod)30 Map (java.util.Map)26 ValidationException (org.eclipse.che.api.core.ValidationException)22 Traced (org.eclipse.che.commons.annotation.Traced)20 Container (io.fabric8.kubernetes.api.model.Container)18 List (java.util.List)18 Set (java.util.Set)18 Inject (javax.inject.Inject)18 RuntimeStartInterruptedException (org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException)18 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)18