Search in sources :

Example 61 with MachineConfigImpl

use of org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl in project devspaces-images by redhat-developer.

the class KubernetesComponentToWorkspaceApplierTest method shouldProvisionMachinesMapWithComponentAttributePreSet.

@Test
public void shouldProvisionMachinesMapWithComponentAttributePreSet() throws Exception {
    @SuppressWarnings("unchecked") ArgumentCaptor<Map<String, MachineConfigImpl>> mapCaptor = ArgumentCaptor.forClass(Map.class);
    // 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(), any(), mapCaptor.capture());
    Map<String, MachineConfigImpl> machines = mapCaptor.getValue();
    assertTrue(machines.values().stream().allMatch(config -> config.getAttributes().get(DEVFILE_COMPONENT_ALIAS_ATTRIBUTE).equals(component.getAlias())));
}
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) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 62 with MachineConfigImpl

use of org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl in project devspaces-images by redhat-developer.

the class KubernetesComponentToWorkspaceApplierTest method serverMustHaveRequireSubdomainWhenNonSinglehostDevfileExpose.

@Test
public void serverMustHaveRequireSubdomainWhenNonSinglehostDevfileExpose() throws DevfileException, IOException, ValidationException, InfrastructureException {
    applier = new KubernetesComponentToWorkspaceApplier(k8sRecipeParser, k8sEnvProvisioner, envVars, PROJECT_MOUNT_PATH, "1Gi", "ReadWriteOnce", "", "Always", MULTI_HOST_STRATEGY, k8sBasedComponents);
    String yamlRecipeContent = getResource("devfile/petclinic.yaml");
    doReturn(toK8SList(yamlRecipeContent).getItems()).when(k8sRecipeParser).parse(anyString());
    // given
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    component.setEndpoints(Arrays.asList(new EndpointImpl("e1", 1111, emptyMap()), new EndpointImpl("e2", 2222, emptyMap())));
    // when
    applier.apply(workspaceConfig, component, s -> yamlRecipeContent);
    // then
    @SuppressWarnings("unchecked") ArgumentCaptor<Map<String, MachineConfigImpl>> objectsCaptor = ArgumentCaptor.forClass(Map.class);
    verify(k8sEnvProvisioner).provision(any(), any(), any(), objectsCaptor.capture());
    Map<String, MachineConfigImpl> machineConfigs = objectsCaptor.getValue();
    assertEquals(machineConfigs.size(), 4);
    machineConfigs.values().forEach(machineConfig -> {
        assertEquals(machineConfig.getServers().size(), 2);
        assertTrue(ServerConfig.isRequireSubdomain(machineConfig.getServers().get("e1").getAttributes()));
        assertTrue(ServerConfig.isRequireSubdomain(machineConfig.getServers().get("e2").getAttributes()));
    });
}
Also used : MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) EndpointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) Test(org.testng.annotations.Test)

Example 63 with MachineConfigImpl

use of org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl in project devspaces-images by redhat-developer.

the class KubernetesComponentToWorkspaceApplierTest method shouldProvisionEndpointWithAttributes.

@Test
public void shouldProvisionEndpointWithAttributes() throws IOException, ValidationException, InfrastructureException, DevfileException {
    // given
    String endpointName = "petclinic-endpoint";
    Integer endpointPort = 8081;
    String endpointProtocol = "tcp";
    String endpointPath = "path";
    String endpointPublic = "false";
    String endpointSecure = "false";
    String yamlRecipeContent = getResource("devfile/petclinicPods.yaml");
    doReturn(toK8SList(yamlRecipeContent).getItems()).when(k8sRecipeParser).parse(anyString());
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    component.setEndpoints(singletonList(new EndpointImpl(endpointName, endpointPort, ImmutableMap.of("protocol", endpointProtocol, "path", endpointPath, "public", endpointPublic, "secure", endpointSecure))));
    // when
    applier.apply(workspaceConfig, component, s -> yamlRecipeContent);
    // then
    @SuppressWarnings("unchecked") ArgumentCaptor<Map<String, MachineConfigImpl>> objectsCaptor = ArgumentCaptor.forClass(Map.class);
    verify(k8sEnvProvisioner).provision(any(), any(), any(), objectsCaptor.capture());
    Map<String, MachineConfigImpl> configs = objectsCaptor.getValue();
    assertEquals(configs.size(), 3);
    configs.values().forEach(machineConfig -> {
        Map<String, ServerConfigImpl> serverConfigs = machineConfig.getServers();
        assertEquals(serverConfigs.size(), 1);
        assertTrue(serverConfigs.containsKey(endpointName));
        assertEquals(serverConfigs.get(endpointName).getPort(), endpointPort.toString());
        assertEquals(serverConfigs.get(endpointName).getPath(), endpointPath);
        assertEquals(serverConfigs.get(endpointName).getProtocol(), endpointProtocol);
        assertEquals(serverConfigs.get(endpointName).getAttributes().get(REQUIRE_SUBDOMAIN), "true");
        assertEquals(serverConfigs.get(endpointName).isSecure(), Boolean.parseBoolean(endpointSecure));
        assertEquals(serverConfigs.get(endpointName).isInternal(), !Boolean.parseBoolean(endpointPublic));
    });
}
Also used : MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) EndpointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) Test(org.testng.annotations.Test)

Example 64 with MachineConfigImpl

use of org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl in project devspaces-images by redhat-developer.

the class KubernetesComponentToWorkspaceApplier method prepareMachineConfigs.

/**
 * Creates map of machine names and corresponding {@link MachineConfigImpl} with component alias
 * attribute set.
 */
private Map<String, MachineConfigImpl> prepareMachineConfigs(List<PodData> podsData, ComponentImpl component) throws DevfileException {
    Map<String, MachineConfigImpl> machineConfigs = new HashMap<>();
    for (PodData podData : podsData) {
        List<Container> containers = new ArrayList<>();
        containers.addAll(podData.getSpec().getContainers());
        containers.addAll(podData.getSpec().getInitContainers());
        for (Container container : containers) {
            String machineName = machineName(podData, container);
            MachineConfigImpl config = new MachineConfigImpl();
            if (!isNullOrEmpty(component.getAlias())) {
                config.getAttributes().put(DEVFILE_COMPONENT_ALIAS_ATTRIBUTE, component.getAlias());
            }
            provisionVolumes(component, container, config);
            provisionEndpoints(component, config);
            machineConfigs.put(machineName, config);
        }
    }
    return machineConfigs;
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Container(io.fabric8.kubernetes.api.model.Container) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 65 with MachineConfigImpl

use of org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl in project devspaces-images by redhat-developer.

the class OpenshiftComponentToWorkspaceApplierTest method serverCantHaveRequireSubdomainWhenSinglehostDevfileExpose.

@Test
public void serverCantHaveRequireSubdomainWhenSinglehostDevfileExpose() throws DevfileException, IOException, ValidationException, InfrastructureException {
    Set<String> openshiftBasedComponents = new HashSet<>();
    openshiftBasedComponents.add(OPENSHIFT_COMPONENT_TYPE);
    applier = new OpenshiftComponentToWorkspaceApplier(k8sRecipeParser, k8sEnvProvisioner, envVars, "/projects", "1Gi", "ReadWriteOnce", "", "Always", SINGLE_HOST_STRATEGY, openshiftBasedComponents);
    String yamlRecipeContent = getResource("devfile/petclinic.yaml");
    doReturn(toK8SList(yamlRecipeContent).getItems()).when(k8sRecipeParser).parse(anyString());
    // given
    ComponentImpl component = new ComponentImpl();
    component.setType(OPENSHIFT_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    component.setEndpoints(Arrays.asList(new EndpointImpl("e1", 1111, emptyMap()), new EndpointImpl("e2", 2222, emptyMap())));
    // when
    applier.apply(workspaceConfig, component, s -> yamlRecipeContent);
    // then
    @SuppressWarnings("unchecked") ArgumentCaptor<Map<String, MachineConfigImpl>> objectsCaptor = ArgumentCaptor.forClass(Map.class);
    verify(k8sEnvProvisioner).provision(any(), any(), any(), objectsCaptor.capture());
    Map<String, MachineConfigImpl> machineConfigs = objectsCaptor.getValue();
    assertEquals(machineConfigs.size(), 4);
    machineConfigs.values().forEach(machineConfig -> {
        assertEquals(machineConfig.getServers().size(), 2);
        assertFalse(ServerConfig.isRequireSubdomain(machineConfig.getServers().get("e1").getAttributes()));
        assertFalse(ServerConfig.isRequireSubdomain(machineConfig.getServers().get("e2").getAttributes()));
    });
}
Also used : MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) EndpointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

MachineConfigImpl (org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl)70 ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)44 Test (org.testng.annotations.Test)44 ServerConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl)26 EndpointImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl)26 EnvironmentImpl (org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl)22 Map (java.util.Map)20 ArrayList (java.util.ArrayList)18 RecipeImpl (org.eclipse.che.api.workspace.server.model.impl.RecipeImpl)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)18 ImmutableMap (com.google.common.collect.ImmutableMap)16 Container (io.fabric8.kubernetes.api.model.Container)16 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)16 Collections.emptyMap (java.util.Collections.emptyMap)16 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)16 Collections.singletonMap (java.util.Collections.singletonMap)14 CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)12 ArgumentMatchers.anyMap (org.mockito.ArgumentMatchers.anyMap)12 HashMap (java.util.HashMap)10 HashSet (java.util.HashSet)10