Skip to content

Commit

Permalink
Fixup
Browse files Browse the repository at this point in the history
Signed-off-by: Sergii Kabashniuk <skabashniuk@redhat.com>
  • Loading branch information
skabashnyuk committed Oct 20, 2020
1 parent 5fa7800 commit d20bfdf
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ public final class Warnings {
public static final String NOT_ABLE_TO_PROVISION_SSH_KEYS_MESSAGE =
"Not able to provision SSH Keys. Message: '%s'";

public static final int NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT = 4200;
public static final String NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT_MESSAGE =
"Not able to find workspace attributes for %s. Reason %s";

public static final int NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT_LABELS_OR_ANNOTATIONS = 4250;
public static final String
NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT_LABELS_OR_ANNOTATIONS_MESSAGE =
"Not able to provision workspace deployment labels or annotations. Message: '%s'";
"Not able to provision workspace %s deployment labels or annotations because of invalid configuration. Reason: '%s'";

private Warnings() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
package org.eclipse.che.workspace.infrastructure.kubernetes.provision;

import static java.lang.String.format;
import static org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT;
import static org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT_LABELS_OR_ANNOTATIONS;
import static org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT_LABELS_OR_ANNOTATIONS_MESSAGE;
import static org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT_MESSAGE;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putAnnotations;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putLabels;

Expand Down Expand Up @@ -72,14 +74,16 @@ public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity)
} catch (NotFoundException | ServerException e) {
String message =
format(
"Not able to find workspace attributes for %s. Reason %s",
identity.getWorkspaceId(), e.getMessage());
NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT_MESSAGE,
identity.getWorkspaceId(),
e.getMessage());
LOG.warn(message);
k8sEnv.addWarning(new WarningImpl(4200, message));
k8sEnv.addWarning(new WarningImpl(NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT, message));
} catch (IllegalArgumentException e) {
String message =
format(
NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT_LABELS_OR_ANNOTATIONS_MESSAGE,
identity.getWorkspaceId(),
e.getMessage());
LOG.warn(message);
k8sEnv.addWarning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import static org.eclipse.che.dto.server.DtoFactory.newDto;
import static org.eclipse.che.workspace.infrastructure.kubernetes.Annotations.CREATE_IN_CHE_INSTALLATION_NAMESPACE;
import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.CHE_ORIGINAL_NAME_LABEL;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putAnnotations;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putLabels;
import static org.eclipse.che.workspace.infrastructure.kubernetes.server.external.MultiHostExternalServiceExposureStrategy.MULTI_HOST_STRATEGY;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
Expand Down Expand Up @@ -1068,6 +1070,49 @@ public void testMultipleMachinesRequiringSamePodInjectionResultInOnePodInjected(
new HashSet<>(asList(CONTAINER_NAME_1, CONTAINER_NAME_2, "injectedContainer")));
}

@Test
public void testDeploymentLabelsAndAnnotations() throws Exception {
// given
Map<String, Pod> injectedPods =
ImmutableMap.of("injected", mockPod(singletonList(mockContainer("injectedContainer"))));

doReturn(ImmutableMap.of(M1_NAME, injectedPods)).when(k8sEnv).getInjectablePodsCopy();

doReturn(emptyMap()).when(k8sEnv).getPodsCopy();
Deployment deployment = mockDeployment(singletonList(mockContainer(CONTAINER_NAME_1)));

putLabels(deployment.getMetadata(), ImmutableMap.of("k1", "v2", "k3", "v4"));
putAnnotations(deployment.getMetadata(), ImmutableMap.of("ak1", "av2", "ak3", "av4"));
doReturn(ImmutableMap.of(WORKSPACE_POD_NAME, deployment)).when(k8sEnv).getDeploymentsCopy();

doReturn(
ImmutableMap.of(
M1_NAME,
mock(InternalMachineConfig.class),
WORKSPACE_POD_NAME + "/injectedContainer",
mock(InternalMachineConfig.class)))
.when(k8sEnv)
.getMachines();

// when
internalRuntime.start(emptyMap());

// then
ArgumentCaptor<Deployment> deploymentCaptor = ArgumentCaptor.forClass(Deployment.class);

verify(deployments).deploy(deploymentCaptor.capture());
assertTrue(deployment.getMetadata().getLabels().containsKey("k1"));
assertTrue(deployment.getMetadata().getLabels().containsKey("k3"));
assertEquals(
deployment.getMetadata().getLabels(),
deploymentCaptor.getValue().getMetadata().getLabels());
assertTrue(deployment.getMetadata().getAnnotations().containsKey("ak1"));
assertTrue(deployment.getMetadata().getAnnotations().containsKey("ak3"));
assertEquals(
deployment.getMetadata().getAnnotations(),
deploymentCaptor.getValue().getMetadata().getAnnotations());
}

@Test
public void testInjectablePodsMergedIntoRuntimeDeployments() throws Exception {
// given
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,218 @@
import static org.testng.Assert.*;
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.workspace.infrastructure.kubernetes.provision;

import static org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT;
import static org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT_LABELS_OR_ANNOTATIONS;
import static org.eclipse.che.workspace.infrastructure.kubernetes.provision.DeploymentMetadataProvisioner.WS_DEPLOYMENT_ANNOTATIONS_ATTR_NAME;
import static org.eclipse.che.workspace.infrastructure.kubernetes.provision.DeploymentMetadataProvisioner.WS_DEPLOYMENT_LABELS_ATTR_NAME;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

import com.google.common.collect.ImmutableMap;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import java.util.Collections;
import java.util.Map;
import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
import org.eclipse.che.api.workspace.server.WorkspaceManager;
import org.eclipse.che.api.workspace.server.model.impl.WarningImpl;
import org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(MockitoTestNGListener.class)
public class DeploymentMetadataProvisionerTest {

}
static final String WS_ID = "ws-123";
static final String DEPLOYMENT_NAME = "dep-324";
@Mock KubernetesEnvironment k8sEnv;
@Mock RuntimeIdentity identity;
@Mock WorkspaceManager workspaceManager;
@Mock WorkspaceImpl workspace;
@InjectMocks DeploymentMetadataProvisioner provisioner;

@BeforeMethod
public void setup() {
when(identity.getWorkspaceId()).thenReturn(WS_ID);
}

@Test
public void shouldProvisionWorkspaceDeploymentLabels()
throws NotFoundException, ServerException, InfrastructureException {
// given
when(workspace.getAttributes())
.thenReturn(ImmutableMap.of(WS_DEPLOYMENT_LABELS_ATTR_NAME, "L1=V1"));
when(workspaceManager.getWorkspace(eq(WS_ID))).thenReturn(workspace);

Deployment deployment = newDeployment();
doReturn(ImmutableMap.of(DEPLOYMENT_NAME, deployment)).when(k8sEnv).getDeploymentsCopy();
// when
provisioner.provision(k8sEnv, identity);
// then
Map<String, String> labels = deployment.getMetadata().getLabels();
assertEquals(1, labels.size());
assertEquals(labels.get("L1"), "V1");
}

@Test
public void shouldProvisionWorkspaceDeploymentAnnotations()
throws NotFoundException, ServerException, InfrastructureException {
// given
when(workspace.getAttributes())
.thenReturn(ImmutableMap.of(WS_DEPLOYMENT_ANNOTATIONS_ATTR_NAME, "A1=V1"));
when(workspaceManager.getWorkspace(eq(WS_ID))).thenReturn(workspace);

Deployment deployment = newDeployment();
doReturn(ImmutableMap.of(DEPLOYMENT_NAME, deployment)).when(k8sEnv).getDeploymentsCopy();
// when
provisioner.provision(k8sEnv, identity);
// then
Map<String, String> labels = deployment.getMetadata().getAnnotations();
assertEquals(1, labels.size());
assertEquals(labels.get("A1"), "V1");
}

@Test
public void shouldDoNothingWithDeploymentIfNoAttributesIsSet()
throws NotFoundException, ServerException, InfrastructureException {
// given
when(workspace.getAttributes()).thenReturn(Collections.emptyMap());
when(workspaceManager.getWorkspace(eq(WS_ID))).thenReturn(workspace);

Deployment deployment = mock(Deployment.class);
doReturn(ImmutableMap.of(DEPLOYMENT_NAME, deployment)).when(k8sEnv).getDeploymentsCopy();
// when
provisioner.provision(k8sEnv, identity);
// then
verifyZeroInteractions(deployment);
}

@Test
public void shouldAddWarningIfNotAbleToGetWorkspaceAttributes()
throws NotFoundException, ServerException, InfrastructureException {
// given
doThrow(new NotFoundException("Element not found"))
.when(workspaceManager)
.getWorkspace(eq(WS_ID));
// when
provisioner.provision(k8sEnv, identity);
// then
ArgumentCaptor<WarningImpl> argumentCaptor = ArgumentCaptor.forClass(WarningImpl.class);
verify(k8sEnv).addWarning(argumentCaptor.capture());
assertEquals(argumentCaptor.getAllValues().size(), 1);
assertEquals(
argumentCaptor.getValue(),
new WarningImpl(
4200,
"Not able to find workspace attributes for " + WS_ID + ". Reason Element not found"));
}

@Test
public void shouldAddWarningIfNotAbleToGetWorkspaceAttributesServerException()
throws NotFoundException, ServerException, InfrastructureException {
// given
doThrow(new ServerException("Connection problem"))
.when(workspaceManager)
.getWorkspace(eq(WS_ID));
// when
provisioner.provision(k8sEnv, identity);
// then
ArgumentCaptor<WarningImpl> argumentCaptor = ArgumentCaptor.forClass(WarningImpl.class);
verify(k8sEnv).addWarning(argumentCaptor.capture());
assertEquals(argumentCaptor.getAllValues().size(), 1);
assertEquals(
argumentCaptor.getValue(),
new WarningImpl(
NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT,
"Not able to find workspace attributes for " + WS_ID + ". Reason Connection problem"));
}

@Test
public void shouldAddWarningIfLabelAttributeInInvalidFormat()
throws NotFoundException, ServerException, InfrastructureException {
// given
when(workspace.getAttributes())
.thenReturn(ImmutableMap.of(WS_DEPLOYMENT_LABELS_ATTR_NAME, "L1~V1"));
when(workspaceManager.getWorkspace(eq(WS_ID))).thenReturn(workspace);

Deployment deployment = newDeployment();
doReturn(ImmutableMap.of(DEPLOYMENT_NAME, deployment)).when(k8sEnv).getDeploymentsCopy();
// when
provisioner.provision(k8sEnv, identity);
// then
ArgumentCaptor<WarningImpl> argumentCaptor = ArgumentCaptor.forClass(WarningImpl.class);
verify(k8sEnv).addWarning(argumentCaptor.capture());
assertEquals(argumentCaptor.getAllValues().size(), 1);
assertEquals(
argumentCaptor.getValue(),
new WarningImpl(
NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT_LABELS_OR_ANNOTATIONS,
"Not able to provision workspace "
+ WS_ID
+ " deployment labels or annotations because of invalid configuration. Reason: 'Chunk [L1~V1] is not a valid entry'"));
}

@Test
public void shouldAddWarningIfAnnotationAttributeInInvalidFormat()
throws NotFoundException, ServerException, InfrastructureException {
// given
when(workspace.getAttributes())
.thenReturn(ImmutableMap.of(WS_DEPLOYMENT_ANNOTATIONS_ATTR_NAME, "A1~V1"));
when(workspaceManager.getWorkspace(eq(WS_ID))).thenReturn(workspace);

Deployment deployment = newDeployment();
doReturn(ImmutableMap.of(DEPLOYMENT_NAME, deployment)).when(k8sEnv).getDeploymentsCopy();
// when
provisioner.provision(k8sEnv, identity);
// then
ArgumentCaptor<WarningImpl> argumentCaptor = ArgumentCaptor.forClass(WarningImpl.class);
verify(k8sEnv).addWarning(argumentCaptor.capture());
assertEquals(argumentCaptor.getAllValues().size(), 1);
assertEquals(
argumentCaptor.getValue(),
new WarningImpl(
NOT_ABLE_TO_PROVISION_WORKSPACE_DEPLOYMENT_LABELS_OR_ANNOTATIONS,
"Not able to provision workspace "
+ WS_ID
+ " deployment labels or annotations because of invalid configuration. Reason: 'Chunk [A1~V1] is not a valid entry'"));
}

private static Deployment newDeployment() {
return new DeploymentBuilder()
.withNewMetadata()
.withName(DEPLOYMENT_NAME)
.endMetadata()
.withNewSpec()
.withNewTemplate()
.withNewMetadata()
.withName("POD_NAME")
.endMetadata()
.endTemplate()
.endSpec()
.build();
}
}

0 comments on commit d20bfdf

Please sign in to comment.