Skip to content

Commit

Permalink
[JBIDE-24184] adding tests for ResourceUtils#getDeploymentConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
adietish committed Jul 10, 2017
1 parent dc9a38e commit e2ee493
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -742,20 +742,20 @@ public static boolean areRelated(final IBuildConfig config, final IDeploymentCon
}

/**
* Checks whether the service and deployment config are related.
* Returns {@code true} if the given service and given deployment config are related given the existing pods.
* @param service the service to match
* @param dc the deployment config to match
* @return true if they are related
*/
public static boolean areRelated(final IService service, IDeploymentConfig dc, Collection<IPod> allPods) {
if (dc == null) {
return false;
}
String dcName = dc.getName();
return allPods.stream()
if (dc == null) {
return false;
}
String dcName = dc.getName();
return allPods.stream()
.filter(pod -> dcName.equals(pod.getAnnotation(OpenShiftAPIAnnotations.DEPLOYMENT_CONFIG_NAME)))
.filter(pod -> containsAll(service.getSelector(), pod.getLabels()))
.count() > 0;
.filter(pod -> areRelated(pod, service))
.count() > 0;
}

/**
Expand Down Expand Up @@ -805,7 +805,8 @@ public static String getDeploymentConfigNameFor(List<IPod> pods) {
}

return pods.stream()
.filter(pod -> pod.getLabels().containsKey(DEPLOYMENT_CONFIG))
.filter(pod ->
!StringUtils.isBlank(getSelectorMatchingAny(pod.getLabels(), OpenShiftResourceSelectors.DEPLOYMENT_CONFIG)))
.findFirst()
.map(pod -> getDeploymentConfigNameFor(pod))
.orElse(null);
Expand Down Expand Up @@ -874,22 +875,23 @@ public static IDeploymentConfig getDeploymentConfigFor(IResource resource, Conne
return dc;
}

public static IDeploymentConfig getDeploymentConfigFor(IService service, Connection connection) throws CoreException {
private static IDeploymentConfig getDeploymentConfigFor(IService service, Connection connection) throws CoreException {
if (service == null) {
return null;
}
String dcName = getDeploymentConfigNameFor(service);
if (dcName != null) {
return getDeploymentConfigByName(dcName, service, connection);
} else {
String namespace = service.getNamespace();
IReplicationController rc = getReplicationControllerFor(
service, connection.getResources(ResourceKind.REPLICATION_CONTROLLER, service.getNamespace()));
service, connection.getResources(ResourceKind.REPLICATION_CONTROLLER, namespace));
if (rc == null) {
throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind(
"Could not find a deployment config for service {0}: no replication controller was found.",
service.getName())));
}
List<IPod> allPods = connection.getResources(ResourceKind.POD, rc.getNamespace());
List<IPod> allPods = connection.getResources(ResourceKind.POD, namespace);
List<IPod> pods = allPods.stream()
.filter(pod -> ResourceUtils.areRelated((IPod) pod, rc))
.collect(Collectors.toList());
Expand All @@ -900,7 +902,7 @@ public static IDeploymentConfig getDeploymentConfigFor(IService service, Connect
+ "You may have to wait for your build to finish and create your pods.",
rc.getName())));
}
List<IDeploymentConfig> dcs = connection.getResources(ResourceKind.DEPLOYMENT_CONFIG, service.getNamespace());
List<IDeploymentConfig> dcs = connection.getResources(ResourceKind.DEPLOYMENT_CONFIG, namespace);
return dcs.stream()
.filter(dc -> ResourceUtils.areRelated((IService) service, (IDeploymentConfig) dc, pods))
.findFirst()
Expand Down Expand Up @@ -953,7 +955,7 @@ private static String getSelectorMatchingAny(Map<String, String> selectors, Stri
}

private static String getDeploymentConfigNameFor(IPod pod) {
return pod.getLabels().get(DEPLOYMENT_CONFIG);
return getSelectorMatchingAny(pod.getLabels(), OpenShiftResourceSelectors.DEPLOYMENT_CONFIG);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.jboss.tools.openshift.core.OpenShiftAPIAnnotations;
import org.jboss.tools.openshift.core.connection.Connection;
import org.jboss.tools.openshift.internal.core.util.ResourceUtils;
import org.jboss.tools.openshift.test.util.ResourceMocks;
import org.junit.Before;
Expand All @@ -59,6 +61,7 @@
import com.openshift.restclient.images.DockerImageURI;
import com.openshift.restclient.model.IBuild;
import com.openshift.restclient.model.IBuildConfig;
import com.openshift.restclient.model.IDeploymentConfig;
import com.openshift.restclient.model.IObjectReference;
import com.openshift.restclient.model.IPod;
import com.openshift.restclient.model.IReplicationController;
Expand Down Expand Up @@ -652,7 +655,40 @@ public void podListWithSeveralDeploymentConfigKeyShouldReturnFirstDeploymentConf
// then
assertThat(name).isEqualTo("hooolahoo");
}


@Test
public void shouldReturnDeploymentConfigForServiceByNameGivenServiceHasDCNameSelector() throws CoreException {
// given
Connection connection = ResourceMocks.create3ProjectsConnection();
IService service = ResourceMocks.PROJECT2_SERVICES[1];
// when
IDeploymentConfig dc = ResourceUtils.getDeploymentConfigFor(service, connection);
// then
assertThat(dc).isEqualTo(ResourceMocks.PROJECT2_DEPLOYMENTCONFIGS[2]);
}

@Test
public void shouldReturnDeploymentConfigForServiceByRCAndPodGivenServiceHasNoDCNameSelector() throws CoreException {
// given
Connection connection = ResourceMocks.create3ProjectsConnection();
IService service = ResourceMocks.PROJECT2_SERVICES[2];
// when
IDeploymentConfig dc = ResourceUtils.getDeploymentConfigFor(service, connection);
// then
assertThat(dc).isEqualTo(ResourceMocks.PROJECT2_DEPLOYMENTCONFIGS[0]);
}

@Test(expected=CoreException.class)
public void shouldThrowCoreExceptionGivenNoDCNameSelectorNorRCisFoundForService() throws CoreException {
// given
Connection connection = ResourceMocks.create3ProjectsConnection();
IService service = ResourceMocks.PROJECT2_SERVICES[0];
// when
ResourceUtils.getDeploymentConfigFor(service, connection);
// then
// exception expected
}

@Test
public void shouldReturnNullIfSelectReplicationControllerByDeploymentConfigOnEmptyList() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -40,12 +41,15 @@
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.team.internal.core.TeamPlugin;
import org.jboss.tools.openshift.core.OpenShiftAPIAnnotations;
import org.jboss.tools.openshift.core.OpenShiftResourceSelectors;
import org.jboss.tools.openshift.core.connection.Connection;
import org.jboss.tools.openshift.core.server.OpenShiftServerUtils;
import org.jboss.tools.openshift.internal.core.util.ResourceUtils;
import org.jboss.tools.openshift.internal.ui.treeitem.ObservableTreeItem;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import com.openshift.restclient.ResourceKind;
import com.openshift.restclient.images.DockerImageURI;
Expand Down Expand Up @@ -90,12 +94,13 @@ public class ResourceMocks {

public static final IService[] PROJECT2_SERVICES = new IService[] {
// selectors need to match pod labels
createService("project2-app1", PROJECT2, new HashMap<String, String>() {{ put("key1", "42"); put("key2", "24"); put("key3", "48");
put(OpenShiftResourceSelectors.DEPLOYMENT_CONFIG, PROJECT2_DEPLOYMENTCONFIGS[2].getName()); }}),
createService("project2-app2", PROJECT2, new HashMap<String, String>() {{ put("key1", "84"); put("key2", "48");
createService("project2-app1", PROJECT2, new HashMap<String, String>() {{
put("key1", "42"); put("key2", "24"); put("key3", "48"); }}),
createService("project2-app2", PROJECT2, new HashMap<String, String>() {{
put("key1", "84"); put("key2", "48");
put(OpenShiftResourceSelectors.DEPLOYMENT_CONFIG, PROJECT2_DEPLOYMENTCONFIGS[2].getName());}}),
createService("project2-app3", PROJECT2, new HashMap<String, String>() {{ put("key1", "42"); put("key2", "24");
put(OpenShiftResourceSelectors.DEPLOYMENT_CONFIG, PROJECT2_DEPLOYMENTCONFIGS[2].getName());}})
createService("project2-app3", PROJECT2, new HashMap<String, String>() {{
put("key1", "42"); put("key2", "24"); }})
};

public static final String PROJECT2_BUILDCONFIG2_BUILD_SOURCEURI = "git@gitrepo.io/somegroup/someproject.git";
Expand All @@ -119,14 +124,28 @@ public class ResourceMocks {
createRoute("project2-app3-route4", PROJECT2, "project2-app3")
};

public static final IReplicationController[] PROJECT2_REPLICATION_CONTROLLERS = new IReplicationController[] {
// labels need to match rc replica selectors
createReplicationController("project2-app2-rc", PROJECT2, new HashMap<String, String>() {{
put("key1", "84"); put("key2", "48"); }}),
createReplicationController("project2-app3-rc", PROJECT2, new HashMap<String, String>() {{
put("key1", "42"); put("key2", "24"); }})
};

public static final IPod[] PROJECT2_PODS = new IPod[] {
// labels need to match service selectors and contain dc name
createPod("project2-app1", PROJECT2, new HashMap<String, String>() {{
put("key1", "42"); put("key2", "24"); }}),
createPod("project2-app2", PROJECT2, new HashMap<String, String>() {{
put("key1", "84"); put("key2", "48"); put(ResourceUtils.DEPLOYMENT_CONFIG, PROJECT2_DEPLOYMENTCONFIGS[2].getName());}}),
createPod("project2-app3", PROJECT2, new HashMap<String, String>() {{
put("key1", "84"); put("key2", "48"); }})
/**
* labels need to match service selectors and contain dc name.
*
* @See ResourceUtils#areRelated(IService, IDeploymentConfig, Collection<IPod>)
*/
createPod("project2-app1", PROJECT2,
new HashMap<String, String>() {{ put("key1", "42"); put("key2", "24"); }},
new HashMap<String, String>() {{ put(OpenShiftAPIAnnotations.DEPLOYMENT_CONFIG_NAME, PROJECT2_DEPLOYMENTCONFIGS[0].getName());}}),
createPod("project2-app2", PROJECT2,
new HashMap<String, String>() {{ put("key1", "84"); put("key2", "48"); }},
new HashMap<String, String>() {{ put(OpenShiftAPIAnnotations.DEPLOYMENT_CONFIG_NAME, PROJECT2_DEPLOYMENTCONFIGS[2].getName());}}),
createPod("project2-app3", PROJECT2,
new HashMap<String, String>() {{ put("key1", "24"); put("key2", "48"); }})
};

public static final IService[] PROJECT3_SERVICES = new IService[] {
Expand Down Expand Up @@ -199,6 +218,8 @@ public static Connection create3ProjectsConnection() {
when(PROJECT2.getResources(ResourceKind.ROUTE)).thenReturn(Arrays.asList(PROJECT2_ROUTES));
when(connection.getResources(ResourceKind.BUILD_CONFIG, PROJECT2.getName())).thenReturn(Arrays.asList(PROJECT2_BUILDCONFIGS));
when(connection.getResources(ResourceKind.POD, PROJECT2.getName())).thenReturn(Arrays.asList(PROJECT2_PODS));
when(connection.getResources(ResourceKind.REPLICATION_CONTROLLER, PROJECT2.getName()))
.thenReturn(Arrays.asList(PROJECT2_REPLICATION_CONTROLLERS));
when(connection.getResources(ResourceKind.DEPLOYMENT_CONFIG, PROJECT2.getName())).thenReturn(Arrays.asList(PROJECT2_DEPLOYMENTCONFIGS));
mockConnectionGetResource(PROJECT2_DEPLOYMENTCONFIGS, ResourceKind.DEPLOYMENT_CONFIG, connection);

Expand Down Expand Up @@ -319,10 +340,22 @@ public static IService createService(String name, IProject project, Map<String,
}

public static IPod createPod(String name, IProject project, Map<String, String> labels) {
return createPod(name, project, labels, Collections.emptyMap());
}

public static IPod createPod(final String name, final IProject project, final Map<String, String> labels, final Map<String, String> annotations) {
return createResource(IPod.class, ResourceKind.POD,
pod -> {
mockGetResourceProperties(name, project, pod);
when(pod.getLabels()).thenReturn(labels);
when(pod.getAnnotations()).thenReturn(annotations);
// return value in mocked map for requested key
doAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return annotations.get(invocation.getArguments()[0]);
}
}).when(pod).getAnnotation(anyString());
});
}

Expand All @@ -331,6 +364,15 @@ public static IDeploymentConfig createDeploymentConfig(String name, IProject pro
dc -> mockGetResourceProperties(name, project, dc));
}

public static IReplicationController createReplicationController(String name, IProject project, Map<String, String> labels) {
return createResource(IReplicationController.class, ResourceKind.REPLICATION_CONTROLLER,
rc -> {
mockGetResourceProperties(name, project, rc);
when(rc.getReplicaSelector()).thenReturn(labels); // match pod
when(rc.getTemplateLabels()).thenReturn(labels); // match service
});
}

public static <R extends IResource> List<R> createResources(int numOf, Class<R> clazz, String kind) {
return createResources(numOf, clazz, kind, null);
}
Expand Down

0 comments on commit e2ee493

Please sign in to comment.