Search in sources :

Example 31 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project che-server by eclipse-che.

the class KubernetesComponentIntegrityValidatorTest method shouldApplyEntrypoint.

@Test
public void shouldApplyEntrypoint() throws Exception {
    // given
    when(kubernetesRecipeParser.parse(any(String.class))).thenReturn(Arrays.asList(new PodBuilder().withNewSpec().addNewContainer().withName("container_a").endContainer().endSpec().build(), new PodBuilder().withNewSpec().addNewContainer().withName("container_b").endContainer().endSpec().build()));
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReferenceContent("content");
    component.setReference("ref");
    EntrypointImpl entrypoint = new EntrypointImpl();
    entrypoint.setContainerName("container_a");
    component.setEntrypoints(Collections.singletonList(entrypoint));
    // when
    validator.validateComponent(component, __ -> "");
// then no exception is thrown
}
Also used : EntrypointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EntrypointImpl) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 32 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project che-server by eclipse-che.

the class KubernetesComponentToWorkspaceApplierTest method shouldThrowExceptionWhenRecipeContentIsNotAValidYaml.

@Test(expectedExceptions = DevfileException.class, expectedExceptionsMessageRegExp = "Error occurred during parsing list from file " + REFERENCE_FILENAME + " for component '" + COMPONENT_NAME + "': .*")
public void shouldThrowExceptionWhenRecipeContentIsNotAValidYaml() throws Exception {
    // given
    doThrow(new ValidationException("non valid")).when(k8sRecipeParser).parse(anyString());
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    // when
    applier.apply(workspaceConfig, component, s -> "some_non_yaml_content");
}
Also used : ValidationException(org.eclipse.che.api.core.ValidationException) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 33 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project che-server by eclipse-che.

the class KubernetesComponentToWorkspaceApplierTest method shouldThrowExceptionWhenDevfileVolumeNameExists.

@Test(expectedExceptions = DevfileException.class, expectedExceptionsMessageRegExp = "Conflicting volume with same name \\('foo_volume'\\) but different path \\('/foo1'\\) found for component 'foo' and its container 'server'.")
public void shouldThrowExceptionWhenDevfileVolumeNameExists() throws Exception {
    // given
    String yamlRecipeContent = getResource("devfile/petclinic.yaml");
    List<HasMetadata> k8sList = toK8SList(yamlRecipeContent).getItems();
    doReturn(k8sList).when(k8sRecipeParser).parse(anyString());
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    component.setVolumes(Collections.singletonList(new VolumeImpl("foo_volume", "/foo1")));
    // when
    applier.apply(workspaceConfig, component, s -> yamlRecipeContent);
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) VolumeImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.VolumeImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 34 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project che-server by eclipse-che.

the class KubernetesComponentToWorkspaceApplierTest method shouldThrowExceptionWhenExceptionHappensOnContentProvider.

@Test(expectedExceptions = DevfileException.class, expectedExceptionsMessageRegExp = "Error during recipe content retrieval for component 'foo' with type 'kubernetes': fetch failed")
public void shouldThrowExceptionWhenExceptionHappensOnContentProvider() throws Exception {
    // given
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    // when
    applier.apply(workspaceConfig, component, e -> {
        throw new IOException("fetch failed");
    });
}
Also used : IOException(java.io.IOException) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 35 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project che-server by eclipse-che.

the class KubernetesComponentToWorkspaceApplierTest method shouldProvisionProjectVolumesIfSpecifiedIntoK8SList.

@Test
public void shouldProvisionProjectVolumesIfSpecifiedIntoK8SList() throws Exception {
    // given
    String yamlRecipeContent = getResource("devfile/petclinic.yaml");
    List<HasMetadata> k8sList = toK8SList(yamlRecipeContent).getItems();
    doReturn(k8sList).when(k8sRecipeParser).parse(anyString());
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    component.setMountSources(true);
    // when
    applier.apply(workspaceConfig, component, s -> yamlRecipeContent);
    // then
    verify(k8sEnvProvisioner).provision(any(), any(), objectsCaptor.capture(), any());
    List<HasMetadata> list = objectsCaptor.getValue();
    // Make sure PVC is created
    assertTrue(list.stream().filter(hasMeta -> hasMeta instanceof PersistentVolumeClaim).map(o -> (PersistentVolumeClaim) o).anyMatch(claim -> claim.getMetadata().getName().equals(PROJECTS_VOLUME_NAME)));
    for (HasMetadata o : list) {
        if (o instanceof Pod) {
            Pod p = (Pod) o;
            // ignore pods that don't have containers
            if (p.getSpec() == null) {
                continue;
            }
            // Make sure volume is created
            assertTrue(p.getSpec().getVolumes().stream().anyMatch(v -> v.getName().equals(PROJECTS_VOLUME_NAME) && v.getPersistentVolumeClaim().getClaimName().equals(PROJECTS_VOLUME_NAME)));
            for (Container c : p.getSpec().getContainers()) {
                assertEquals(c.getImagePullPolicy(), "Always");
                assertTrue(c.getVolumeMounts().stream().anyMatch(vm -> vm.getName().equals(PROJECTS_VOLUME_NAME) && vm.getMountPath().equals(PROJECT_MOUNT_PATH)));
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) Listeners(org.testng.annotations.Listeners) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MachineConfig(org.eclipse.che.api.core.model.workspace.config.MachineConfig) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) EnvVars(org.eclipse.che.workspace.infrastructure.kubernetes.util.EnvVars) Test(org.testng.annotations.Test) Files(org.testng.reporters.Files) Collections.singletonList(java.util.Collections.singletonList) Mockito.doThrow(org.mockito.Mockito.doThrow) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) REQUIRE_SUBDOMAIN(org.eclipse.che.api.core.model.workspace.config.ServerConfig.REQUIRE_SUBDOMAIN) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertFalse(org.testng.Assert.assertFalse) Serialization.unmarshal(io.fabric8.kubernetes.client.utils.Serialization.unmarshal) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ImmutableMap(com.google.common.collect.ImmutableMap) MULTI_HOST_STRATEGY(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.MultiHostExternalServiceExposureStrategy.MULTI_HOST_STRATEGY) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) EndpointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl) ServerConfig(org.eclipse.che.api.core.model.workspace.config.ServerConfig) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) Stream(java.util.stream.Stream) PROJECTS_VOLUME_NAME(org.eclipse.che.api.workspace.shared.Constants.PROJECTS_VOLUME_NAME) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) KUBERNETES_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.KUBERNETES_COMPONENT_TYPE) Assert.assertNull(org.testng.Assert.assertNull) Container(io.fabric8.kubernetes.api.model.Container) DEVFILE_COMPONENT_ALIAS_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.MachineConfig.DEVFILE_COMPONENT_ALIAS_ATTRIBUTE) Mock(org.mockito.Mock) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) Assert.assertEquals(org.testng.Assert.assertEquals) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Captor(org.mockito.Captor) ValidationException(org.eclipse.che.api.core.ValidationException) ArrayList(java.util.ArrayList) VolumeImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.VolumeImpl) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) MACHINE_NAME_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.MACHINE_NAME_ATTRIBUTE) Collections.singletonMap(java.util.Collections.singletonMap) COMPONENT_ALIAS_COMMAND_ATTRIBUTE(org.eclipse.che.api.workspace.server.devfile.Constants.COMPONENT_ALIAS_COMMAND_ATTRIBUTE) EnvImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EnvImpl) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) Collections.emptyMap(java.util.Collections.emptyMap) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) URLFileContentProvider(org.eclipse.che.api.workspace.server.devfile.URLFileContentProvider) OPENSHIFT_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.OPENSHIFT_COMPONENT_TYPE) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) Mockito.verify(org.mockito.Mockito.verify) EntrypointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EntrypointImpl) SINGLE_HOST_STRATEGY(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.SingleHostExternalServiceExposureStrategy.SINGLE_HOST_STRATEGY) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Assert.assertTrue(org.testng.Assert.assertTrue) KubernetesRecipeParser(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesRecipeParser) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Container(io.fabric8.kubernetes.api.model.Container) Pod(io.fabric8.kubernetes.api.model.Pod) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Aggregations

ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)218 Test (org.testng.annotations.Test)184 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)76 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)56 Container (io.fabric8.kubernetes.api.model.Container)48 MachineConfigImpl (org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl)44 EndpointImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl)42 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)38 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)36 CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)26 EntrypointImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EntrypointImpl)26 Map (java.util.Map)24 EnvImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EnvImpl)24 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)22 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)20 Collections.emptyMap (java.util.Collections.emptyMap)18 DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)18 ImmutableMap (com.google.common.collect.ImmutableMap)16 ArrayList (java.util.ArrayList)14 List (java.util.List)14