Skip to content

Commit

Permalink
Undeploy command added
Browse files Browse the repository at this point in the history
  • Loading branch information
Radim Hatlapatka committed Feb 13, 2016
1 parent 169cca7 commit cfc233d
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.wildfly.extras.creaper.commands.deployments;


import org.wildfly.extras.creaper.core.online.OnlineCommand;
import org.wildfly.extras.creaper.core.online.OnlineCommandContext;

/**
* Command which takes care about undeploying specified application (the content is by default also removed)
* <p>
* In case of domain the deployment is by default undeployed from all relevant server groups
* (server groups having the deployment enabled)
* </p>
*/
public final class Undeploy implements OnlineCommand {
private final String deploymentName;
private final boolean keepContent;

private Undeploy(Builder builder) {
this.deploymentName = builder.deploymentName;
this.keepContent = builder.keepContent;
}

@Override
public void apply(OnlineCommandContext ctx) throws Exception {
StringBuilder cmd = new StringBuilder("undeploy ").append(deploymentName);
if (keepContent) {
cmd.append(" --keep-content");
}
if (ctx.options.isDomain) {
cmd.append(" --all-relevant-server-groups");
}
ctx.client.executeCli(cmd.toString());
}

public static final class Builder {
private String deploymentName;
private boolean keepContent = false;

public Builder(String deploymentName) {
this.deploymentName = deploymentName;
}

/**
* Defines that the content should be left in the deployment repository => only undeployed without removing it.
*/
public Builder keepContent() {
this.keepContent = true;
return this;
}


public Undeploy build() {
return new Undeploy(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.wildfly.extras.creaper.commands.deployments;

import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
Expand All @@ -13,8 +15,8 @@
import org.junit.experimental.categories.Category;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.wildfly.extras.creaper.core.CommandFailedException;
import org.wildfly.extras.creaper.core.ManagementClient;
import org.wildfly.extras.creaper.core.online.CliException;
import org.wildfly.extras.creaper.core.online.OnlineManagementClient;
import org.wildfly.extras.creaper.core.online.OnlineOptions;
import org.wildfly.extras.creaper.core.online.operations.Address;
Expand All @@ -26,12 +28,14 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeoutException;

/**
* This class tests deploy command in standalone mode.
* This class tests deploy and undeploy commands in standalone mode.
*/
@Category(WildFlyTests.class)
@RunWith(Arquillian.class)
@RunAsClient
public class DeployUndeployCmdTest {

private static final String DEPLOYMENT_NAME = "test-deployment.war";
Expand All @@ -47,41 +51,68 @@ public class DeployUndeployCmdTest {
private Administration admin;

@Before
public void connect() throws IOException {
public void prepare() throws IOException, OperationException, TimeoutException, InterruptedException {
client = ManagementClient.online(OnlineOptions.standalone().localDefault().build());
ops = new Operations(client);
admin = new Administration(client);
ops.removeIfExists(TEST_DEPLOYMENT_ADDRESS);
admin.reloadIfRequired();
}

@After
public void close() throws IOException, CliException, OperationException {
client.close();
}

@Test
public void deployAsInputStream_commandSucceeds() throws Exception {
Assert.assertFalse(ops.exists(TEST_DEPLOYMENT_ADDRESS));
public void cleanAndClose() throws Exception {
try {
InputStream inputStream = TEST_DEPLOYMENT.as(ZipExporter.class).exportAsInputStream();
client.apply(new Deploy.DeployCmdBuilder(inputStream, DEPLOYMENT_NAME, true).build());
Assert.assertTrue(ops.exists(TEST_DEPLOYMENT_ADDRESS));
} finally {
ops.removeIfExists(TEST_DEPLOYMENT_ADDRESS);
admin.reloadIfRequired();
} finally {
client.close();
}
}

@Test
public void deployAsInputStream_commandSucceeds() throws Exception {
InputStream inputStream = TEST_DEPLOYMENT.as(ZipExporter.class).exportAsInputStream();
client.apply(new Deploy.DeployCmdBuilder(inputStream, DEPLOYMENT_NAME, true).build());
Assert.assertTrue(ops.exists(TEST_DEPLOYMENT_ADDRESS));
undeploy(DEPLOYMENT_NAME);
}

@Test
public void deployAsFile_commandSucceeds() throws Exception {
Assert.assertFalse(ops.exists(TEST_DEPLOYMENT_ADDRESS));
File testDeploymentFile = new File(tmp.getRoot(), TEST_DEPLOYMENT.getName());
try {
TEST_DEPLOYMENT.as(ZipExporter.class).exportTo(testDeploymentFile, true);
client.apply(new Deploy.DeployCmdBuilder(testDeploymentFile).build());
Assert.assertTrue(ops.exists(TEST_DEPLOYMENT_ADDRESS));
} finally {
ops.removeIfExists(TEST_DEPLOYMENT_ADDRESS);
admin.reloadIfRequired();
}
deployAsFile(TEST_DEPLOYMENT);
undeploy(TEST_DEPLOYMENT.getName());
}

@Test
public void deployAfterUndeployTest() throws Exception {
deployAsFile(TEST_DEPLOYMENT);
undeploy(TEST_DEPLOYMENT.getName());
deployAsFile(TEST_DEPLOYMENT);
undeploy(TEST_DEPLOYMENT.getName());
}

@Test
public void undeployKeepContentTest() throws OperationException, IOException, CommandFailedException {
deployAsFile(TEST_DEPLOYMENT);
client.apply(new Undeploy.Builder(TEST_DEPLOYMENT.getName()).keepContent().build());
assertDeploymentExists(TEST_DEPLOYMENT.getName(), true);
ops.removeIfExists(TEST_DEPLOYMENT_ADDRESS);
}

private void assertDeploymentExists(String deploymentName, boolean shouldExist)
throws IOException, OperationException {
Assert.assertEquals("Deployment exists", shouldExist, ops.exists(Address.of("deployment", deploymentName)));
}

private void deployAsFile(Archive archive) throws CommandFailedException, IOException, OperationException {
File testDeploymentFile = new File(tmp.getRoot(), archive.getName());
TEST_DEPLOYMENT.as(ZipExporter.class).exportTo(testDeploymentFile, true);
client.apply(new Deploy.DeployCmdBuilder(testDeploymentFile).build());
assertDeploymentExists(archive.getName(), true);
}

private void undeploy(String deploymentName) throws CommandFailedException, IOException, OperationException {
client.apply(new Undeploy.Builder(deploymentName).build());
assertDeploymentExists(deploymentName, false);
}
}

0 comments on commit cfc233d

Please sign in to comment.