From a55884d9f77a9f12fd737a84b5eeb16daa1e9afa Mon Sep 17 00:00:00 2001 From: Jeff Cantrill Date: Thu, 18 Feb 2016 13:41:53 -0500 Subject: [PATCH] [JBIDE-21715] fix deploy when missing a resource controller --- .../resources/DeployCapability.java | 32 +++++++++++++------ .../resources/DeployCapabilityTest.java | 17 ++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/openshift/internal/restclient/capability/resources/DeployCapability.java b/src/main/java/com/openshift/internal/restclient/capability/resources/DeployCapability.java index 3bbca5e5..989ab8ce 100644 --- a/src/main/java/com/openshift/internal/restclient/capability/resources/DeployCapability.java +++ b/src/main/java/com/openshift/internal/restclient/capability/resources/DeployCapability.java @@ -18,8 +18,11 @@ import org.slf4j.LoggerFactory; import com.openshift.restclient.IClient; +import com.openshift.restclient.NotFoundException; +import com.openshift.restclient.OpenShiftException; import com.openshift.restclient.ResourceKind; import com.openshift.restclient.capability.resources.IDeployCapability; +import com.openshift.restclient.http.IHttpConstants; import com.openshift.restclient.model.IDeploymentConfig; import com.openshift.restclient.model.IReplicationController; @@ -50,18 +53,27 @@ public String getName() { @Override public void deploy() { - final String deploymentName = getLatestDeploymentName(); - LOG.debug("Attempting to deploy latest deployment for config '%s'. Loading deployment: '%s'", config.getName(), deploymentName); - IReplicationController deployment = client.get(ResourceKind.REPLICATION_CONTROLLER, deploymentName, config.getNamespace()); - final String status = getStatusFor(deployment); - if(COMPLETED_STATES.contains(status)) { - int version = config.getLatestVersionNumber(); - config.setLatestVersionNumber(++version); - client.update(config); - }else { - LOG.debug("Skipping deployment because deployment status '%s' for '%s' is not in %s", new Object [] {status, deploymentName, COMPLETED_STATES}); + try { + final String deploymentName = getLatestDeploymentName(); + LOG.debug("Attempting to deploy latest deployment for config '%s'. Loading deployment: '%s'", config.getName(), deploymentName); + IReplicationController deployment = client.get(ResourceKind.REPLICATION_CONTROLLER, deploymentName, config.getNamespace()); + final String status = getStatusFor(deployment); + if(!COMPLETED_STATES.contains(status)) { + LOG.debug("Skipping deployment because deployment status '%s' for '%s' is not in %s", new Object [] {status, deploymentName, COMPLETED_STATES}); + return; + } + }catch(OpenShiftException e) { + if(e.getStatus() == null || e.getStatus().getCode() != IHttpConstants.STATUS_NOT_FOUND) { + //swallow exception like cli + throw e; + } } + //bumping as currently not supporting 'retry' + int version = config.getLatestVersionNumber(); + config.setLatestVersionNumber(++version); + client.update(config); + } diff --git a/src/test/java/com/openshift/internal/restclient/capability/resources/DeployCapabilityTest.java b/src/test/java/com/openshift/internal/restclient/capability/resources/DeployCapabilityTest.java index e18a5681..129d1049 100644 --- a/src/test/java/com/openshift/internal/restclient/capability/resources/DeployCapabilityTest.java +++ b/src/test/java/com/openshift/internal/restclient/capability/resources/DeployCapabilityTest.java @@ -23,8 +23,10 @@ import com.openshift.restclient.IClient; import com.openshift.restclient.OpenShiftException; import com.openshift.restclient.capability.resources.IDeployCapability; +import com.openshift.restclient.http.IHttpConstants; import com.openshift.restclient.model.IDeploymentConfig; import com.openshift.restclient.model.IReplicationController; +import com.openshift.restclient.model.IStatus; @RunWith(MockitoJUnitRunner.class) public class DeployCapabilityTest { @@ -58,6 +60,14 @@ public void testThrowsErrorWhenUnableToFindLatestDeployment() { when(client.get(anyString(), anyString(), anyString())).thenThrow(OpenShiftException.class); whenDeploying(); } + + @Test + public void testWhenLatestDeploymentNotFound() { + givenTheLatestDeploymentIsNotFound(); + whenDeploying(); + thenVersionShouldIncrease(config); + thenResourceShouldBeUpdated(client, config); + } @Test public void testConfigNotUpdatedWhenAlreadyInProgress() { @@ -107,4 +117,11 @@ private void givenDeploymentStatusIs(String status) { private void givenTheDeploymentIsRetrieved() { when(client.get(anyString(),anyString(),anyString())).thenReturn(deployment); } + + private void givenTheLatestDeploymentIsNotFound() { + IStatus status = mock(IStatus.class); + when(status.getCode()).thenReturn(IHttpConstants.STATUS_NOT_FOUND); + OpenShiftException e = new OpenShiftException(new RuntimeException(), status, ""); + when(client.get(anyString(),anyString(),anyString())).thenThrow(e); + } }