Search in sources :

Example 41 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 shouldSetMachineNameAttributeToCommandConfiguredInOpenShiftComponentWithOneContainer.

@Test(dependsOnMethods = "shouldFilterRecipeWithGivenSelectors", enabled = false)
public void shouldSetMachineNameAttributeToCommandConfiguredInOpenShiftComponentWithOneContainer() throws Exception {
    // given
    String yamlRecipeContent = getResource("devfile/petclinic.yaml");
    doReturn(toK8SList(yamlRecipeContent).getItems()).when(k8sRecipeParser).parse(anyString());
    final Map<String, String> selector = singletonMap("app.kubernetes.io/component", "webapp");
    ComponentImpl component = new ComponentImpl();
    component.setType(OPENSHIFT_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    component.setSelector(selector);
    CommandImpl command = new CommandImpl();
    command.getAttributes().put(COMPONENT_ALIAS_COMMAND_ATTRIBUTE, COMPONENT_NAME);
    workspaceConfig.getCommands().add(command);
    // when
    applier.apply(workspaceConfig, component, s -> yamlRecipeContent);
    // then
    CommandImpl actualCommand = workspaceConfig.getCommands().get(0);
    assertEquals(actualCommand.getAttributes().get(MACHINE_NAME_ATTRIBUTE), "petclinic/server");
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 42 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 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 43 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 shouldProvisionEnvIntoK8SList.

@Test
public void shouldProvisionEnvIntoK8SList() throws Exception {
    // given
    List<HasMetadata> k8sList = new ArrayList<>();
    Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").endMetadata().withNewSpec().endSpec().build();
    Pod pod2 = new PodBuilder().withNewMetadata().withName("pod2").endMetadata().withNewSpec().endSpec().build();
    k8sList.add(pod1);
    k8sList.add(pod2);
    doReturn(k8sList).when(k8sRecipeParser).parse(anyString());
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    List<EnvImpl> envToApply = singletonList(new EnvImpl("TEST_ENV", "anyValue"));
    component.setEnv(envToApply);
    // when
    applier.apply(workspaceConfig, component, s -> "content");
    // then
    envVars.apply(new PodData(pod1), envToApply);
    envVars.apply(new PodData(pod2), envToApply);
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) ArrayList(java.util.ArrayList) EnvImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EnvImpl) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 44 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 shouldChangeEntrypointsOnMatchingContainers.

@Test
public void shouldChangeEntrypointsOnMatchingContainers() throws Exception {
    // given
    String yamlRecipeContent = getResource("devfile/petclinic.yaml");
    doReturn(toK8SList(yamlRecipeContent).getItems()).when(k8sRecipeParser).parse(anyString());
    List<String> command = asList("teh", "command");
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    EntrypointImpl entrypoint = new EntrypointImpl();
    entrypoint.setParentName("petclinic");
    entrypoint.setCommand(command);
    component.setEntrypoints(singletonList(entrypoint));
    // when
    applier.apply(workspaceConfig, component, s -> yamlRecipeContent);
    // then
    verify(k8sEnvProvisioner).provision(any(), any(), objectsCaptor.capture(), any());
    List<HasMetadata> list = objectsCaptor.getValue();
    for (HasMetadata o : list) {
        if (o instanceof Pod) {
            Pod p = (Pod) o;
            // ignore pods that don't have containers
            if (p.getSpec() == null) {
                continue;
            }
            Container c = p.getSpec().getContainers().get(0);
            if (o.getMetadata().getName().equals("petclinic")) {
                assertEquals(c.getCommand(), command);
            } else {
                assertTrue(c.getCommand() == null || c.getCommand().isEmpty());
            }
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Container(io.fabric8.kubernetes.api.model.Container) EntrypointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EntrypointImpl) Pod(io.fabric8.kubernetes.api.model.Pod) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 45 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 shouldBeAbleToOverrideImagePullPolicy.

@Test
public void shouldBeAbleToOverrideImagePullPolicy() throws Exception {
    // given
    applier = new KubernetesComponentToWorkspaceApplier(k8sRecipeParser, k8sEnvProvisioner, envVars, PROJECT_MOUNT_PATH, "1Gi", "ReadWriteOnce", "", "Never", MULTI_HOST_STRATEGY, k8sBasedComponents);
    String yamlRecipeContent = getResource("devfile/petclinic.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);
    // when
    applier.apply(workspaceConfig, component, s -> yamlRecipeContent);
    // then
    verify(k8sEnvProvisioner).provision(any(), any(), objectsCaptor.capture(), any());
    List<HasMetadata> list = objectsCaptor.getValue();
    for (HasMetadata o : list) {
        if (o instanceof Pod) {
            Pod p = (Pod) o;
            // ignore pods that don't have containers
            if (p.getSpec() == null) {
                continue;
            }
            List<Container> containers = new ArrayList<>();
            containers.addAll(p.getSpec().getContainers());
            containers.addAll(p.getSpec().getInitContainers());
            for (Container con : containers) {
                assertEquals(con.getImagePullPolicy(), "Never");
            }
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Container(io.fabric8.kubernetes.api.model.Container) Pod(io.fabric8.kubernetes.api.model.Pod) ArrayList(java.util.ArrayList) 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