Search in sources :

Example 26 with NamespaceResolutionContext

use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project devspaces-images by redhat-developer.

the class KubernetesNamespaceFactoryTest method testEvalNamespaceNameWhenPreparedNamespacesFound.

@Test
public void testEvalNamespaceNameWhenPreparedNamespacesFound() throws InfrastructureException {
    List<Namespace> namespaces = Arrays.asList(new NamespaceBuilder().withNewMetadata().withName("ns1").withAnnotations(Map.of(NAMESPACE_ANNOTATION_NAME, "jondoe")).endMetadata().withNewStatus().withNewPhase("Active").endStatus().build(), new NamespaceBuilder().withNewMetadata().withName("ns2").withAnnotations(Map.of(NAMESPACE_ANNOTATION_NAME, "jondoe")).endMetadata().withNewStatus().withNewPhase("Active").endStatus().build());
    doReturn(namespaces).when(namespaceList).getItems();
    namespaceFactory = new KubernetesNamespaceFactory("<username>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, emptySet(), clientFactory, cheClientFactory, userManager, preferenceManager, pool);
    String namespace = namespaceFactory.evaluateNamespaceName(new NamespaceResolutionContext("workspace123", "user123", "jondoe"));
    assertEquals(namespace, "ns1");
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Namespace(io.fabric8.kubernetes.api.model.Namespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) Test(org.testng.annotations.Test)

Example 27 with NamespaceResolutionContext

use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project devspaces-images by redhat-developer.

the class KubernetesNamespaceFactoryTest method testAllConfiguratorsAreCalledWhenCreatingNamespace.

@Test
public void testAllConfiguratorsAreCalledWhenCreatingNamespace() throws InfrastructureException {
    // given
    String namespaceName = "testNamespaceName";
    NamespaceConfigurator configurator1 = Mockito.mock(NamespaceConfigurator.class);
    NamespaceConfigurator configurator2 = Mockito.mock(NamespaceConfigurator.class);
    Set<NamespaceConfigurator> namespaceConfigurators = Set.of(configurator1, configurator2);
    namespaceFactory = spy(new KubernetesNamespaceFactory("<username>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, namespaceConfigurators, clientFactory, cheClientFactory, userManager, preferenceManager, pool));
    EnvironmentContext.getCurrent().setSubject(new SubjectImpl("jondoe", "123", null, false));
    KubernetesNamespace toReturnNamespace = mock(KubernetesNamespace.class);
    when(toReturnNamespace.getName()).thenReturn(namespaceName);
    RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", null, USER_ID, "old-che");
    doReturn(toReturnNamespace).when(namespaceFactory).get(identity);
    // when
    KubernetesNamespace namespace = namespaceFactory.getOrCreate(identity);
    // then
    NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext("workspace123", "123", "jondoe");
    verify(configurator1).configure(resolutionCtx, namespaceName);
    verify(configurator2).configure(resolutionCtx, namespaceName);
    assertEquals(namespace, toReturnNamespace);
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) NamespaceConfigurator(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.NamespaceConfigurator) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Test(org.testng.annotations.Test)

Example 28 with NamespaceResolutionContext

use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project devspaces-images by redhat-developer.

the class KubernetesNamespaceFactoryTest method shouldFailToProvisionIfNotAbleToFindNamespace.

@Test(expectedExceptions = InfrastructureException.class, expectedExceptionsMessageRegExp = "Not able to find namespace jondoe-cha-cha-cha")
public void shouldFailToProvisionIfNotAbleToFindNamespace() throws InfrastructureException {
    // given
    namespaceFactory = spy(new KubernetesNamespaceFactory("<username>-cha-cha-cha", false, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, emptySet(), clientFactory, cheClientFactory, userManager, preferenceManager, pool));
    KubernetesNamespace toReturnNamespace = mock(KubernetesNamespace.class);
    prepareNamespace(toReturnNamespace);
    when(toReturnNamespace.getName()).thenReturn("jondoe-cha-cha-cha");
    doReturn(toReturnNamespace).when(namespaceFactory).doCreateNamespaceAccess(any(), any());
    KubernetesNamespaceMetaImpl namespaceMeta = new KubernetesNamespaceMetaImpl("jondoe-cha-cha-cha", ImmutableMap.of("phase", "active", "default", "true"));
    doReturn(empty()).when(namespaceFactory).fetchNamespace(eq("jondoe-cha-cha-cha"));
    // when
    NamespaceResolutionContext context = new NamespaceResolutionContext("workspace123", "user123", "jondoe");
    testProvisioning(context);
    // then
    fail("should not reach this point since exception has to be thrown");
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) KubernetesNamespaceMetaImpl(org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl) Test(org.testng.annotations.Test)

Example 29 with NamespaceResolutionContext

use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project devspaces-images by redhat-developer.

the class KubernetesNamespaceServiceTest method shouldProvisionNamespaceWithCorrectContext.

@Test
public void shouldProvisionNamespaceWithCorrectContext() throws Exception {
    // given
    KubernetesNamespaceMetaImpl namespaceMeta = new KubernetesNamespaceMetaImpl("ws-namespace", ImmutableMap.of("phase", "active", "default", "true"));
    when(namespaceProvisioner.provision(any(NamespaceResolutionContext.class))).thenReturn(namespaceMeta);
    // when
    final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).when().post(SECURE_PATH + "/kubernetes/namespace/provision");
    // then
    assertEquals(response.getStatusCode(), 200);
    ArgumentCaptor<NamespaceResolutionContext> captor = ArgumentCaptor.forClass(NamespaceResolutionContext.class);
    verify(namespaceProvisioner).provision(captor.capture());
    NamespaceResolutionContext actualContext = captor.getValue();
    assertEquals(actualContext.getUserId(), SUBJECT.getUserId());
    assertEquals(actualContext.getUserName(), SUBJECT.getUserName());
    Assert.assertNull(actualContext.getWorkspaceId());
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) Response(io.restassured.response.Response) KubernetesNamespaceMetaImpl(org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl) Test(org.testng.annotations.Test)

Example 30 with NamespaceResolutionContext

use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project che-server by eclipse-che.

the class KubernetesNamespaceFactoryTest method testEvalNamespaceSkipsNamespaceFromUserPreferencesIfTemplateChanged.

@Test
public void testEvalNamespaceSkipsNamespaceFromUserPreferencesIfTemplateChanged() throws Exception {
    namespaceFactory = new KubernetesNamespaceFactory("che-<userid>-<username>", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, emptySet(), clientFactory, cheClientFactory, userManager, preferenceManager, pool);
    Map<String, String> prefs = new HashMap<>();
    // returned but ignored
    prefs.put(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "che-123");
    prefs.put(NAMESPACE_TEMPLATE_ATTRIBUTE, "che-<userid>");
    when(preferenceManager.find(anyString())).thenReturn(prefs);
    String namespace = namespaceFactory.evaluateNamespaceName(new NamespaceResolutionContext("workspace123", "user123", "jondoe"));
    assertEquals(namespace, "che-user123-jondoe");
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.testng.annotations.Test)

Aggregations

NamespaceResolutionContext (org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext)56 Test (org.testng.annotations.Test)30 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 KubernetesServer (io.fabric8.kubernetes.client.server.mock.KubernetesServer)14 BeforeMethod (org.testng.annotations.BeforeMethod)14 HashMap (java.util.HashMap)8 KubernetesNamespaceMetaImpl (org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl)8 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)6 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)6 KubernetesNamespaceMeta (org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta)6 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)4 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)4 UserImpl (org.eclipse.che.api.user.server.model.impl.UserImpl)4 RuntimeIdentityImpl (org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl)4 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)4 SubjectImpl (org.eclipse.che.commons.subject.SubjectImpl)4 NamespaceConfigurator (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.NamespaceConfigurator)4 Namespace (io.fabric8.kubernetes.api.model.Namespace)2 NamespaceBuilder (io.fabric8.kubernetes.api.model.NamespaceBuilder)2 Project (io.fabric8.openshift.api.model.Project)2