Add RUN_DIR. https://github.com/apache/incubator-brooklyn/pull/1030#discussion_r45050093
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/cff1961f Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/cff1961f Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/cff1961f Branch: refs/heads/master Commit: cff1961fa442f6695ebc3b891f87a3235c3988c5 Parents: 9303918 Author: Geoff Macartney <[email protected]> Authored: Tue Nov 17 12:18:52 2015 +0000 Committer: Geoff Macartney <[email protected]> Committed: Tue Nov 17 12:18:52 2015 +0000 ---------------------------------------------------------------------- .../brooklyn/test/framework/SimpleCommand.java | 11 ++++- .../test/framework/SimpleCommandImpl.java | 17 +++++--- .../SimpleCommandScriptIntegrationTest.java | 45 ++++++++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/cff1961f/usage/test-framework/src/main/java/org/apache/brooklyn/test/framework/SimpleCommand.java ---------------------------------------------------------------------- diff --git a/usage/test-framework/src/main/java/org/apache/brooklyn/test/framework/SimpleCommand.java b/usage/test-framework/src/main/java/org/apache/brooklyn/test/framework/SimpleCommand.java index c5ff777..0a58fe2 100644 --- a/usage/test-framework/src/main/java/org/apache/brooklyn/test/framework/SimpleCommand.java +++ b/usage/test-framework/src/main/java/org/apache/brooklyn/test/framework/SimpleCommand.java @@ -56,6 +56,15 @@ public interface SimpleCommand extends Entity, Startable { @SetFromFlag("downloadUrl") AttributeSensorAndConfigKey<String, String> DOWNLOAD_URL = SoftwareProcess.DOWNLOAD_URL; + /** + * Where the script will be downloaded on the target machine. + */ @SetFromFlag("scriptDir") - ConfigKey<String> SCRIPT_DIR = newConfigKey("scriptDir", "directory where downloaded scripts should be put", TMP_DEFAULT); + ConfigKey<String> SCRIPT_DIR = newConfigKey("script.dir", "directory where downloaded scripts should be put", TMP_DEFAULT); + + /** + * The working directory that the script will be run from on the target machine. + */ + @SetFromFlag("runDir") + ConfigKey<String> RUN_DIR = newConfigKey("run.dir", "directory where downloaded scripts should be put", TMP_DEFAULT); } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/cff1961f/usage/test-framework/src/main/java/org/apache/brooklyn/test/framework/SimpleCommandImpl.java ---------------------------------------------------------------------- diff --git a/usage/test-framework/src/main/java/org/apache/brooklyn/test/framework/SimpleCommandImpl.java b/usage/test-framework/src/main/java/org/apache/brooklyn/test/framework/SimpleCommandImpl.java index 6b5f87c..b339e4d 100644 --- a/usage/test-framework/src/main/java/org/apache/brooklyn/test/framework/SimpleCommandImpl.java +++ b/usage/test-framework/src/main/java/org/apache/brooklyn/test/framework/SimpleCommandImpl.java @@ -63,6 +63,8 @@ public class SimpleCommandImpl extends AbstractEntity implements SimpleCommand { private static final Logger LOG = LoggerFactory.getLogger(SimpleCommandImpl.class); private static final int A_LINE = 80; public static final String DEFAULT_NAME = "download.sh"; + private static final String CD = "cd"; + private static final String SHELL_AND = "&&"; private ResourceUtils resourceUtils; @@ -150,8 +152,8 @@ public class SimpleCommandImpl extends AbstractEntity implements SimpleCommand { if (Strings.isNonBlank(downloadUrl)) { String scriptDir = getConfig(SCRIPT_DIR); - String destPath = calculateDestPath(downloadUrl, scriptDir); - result = executeDownloadedScript(machineLocation, downloadUrl, destPath); + String scriptPath = calculateDestPath(downloadUrl, scriptDir); + result = executeDownloadedScript(machineLocation, downloadUrl, scriptPath); } if (Strings.isNonBlank(command)) { @@ -165,17 +167,20 @@ public class SimpleCommandImpl extends AbstractEntity implements SimpleCommand { return new IllegalArgumentException(Joiner.on(' ').join(this.toString() + ":", messages)); } - private SimpleCommand.Result executeDownloadedScript(MachineLocation machineLocation, String downloadUrl, String destPath) { + private SimpleCommand.Result executeDownloadedScript(MachineLocation machineLocation, String url, String scriptPath) { SshMachineLocation machine = getSshMachine(ImmutableList.<Location>of(machineLocation)); - TaskFactory<?> install = SshTasks.installFromUrl(ImmutableMap.<String, Object>of(), machine, downloadUrl, destPath); + TaskFactory<?> install = SshTasks.installFromUrl(ImmutableMap.<String, Object>of(), machine, url, scriptPath); DynamicTasks.queue(install); DynamicTasks.waitForLast(); - machine.execCommands("make the script executable", ImmutableList.<String>of("chmod u+x " + destPath)); + machine.execCommands("make the script executable", ImmutableList.<String>of("chmod u+x " + scriptPath)); - return executeShellCommand(machineLocation, destPath); + String runDir = getConfig(RUN_DIR); + String cdAndRun = Joiner.on(' ').join(CD, runDir, SHELL_AND, scriptPath); + + return executeShellCommand(machineLocation, cdAndRun); } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/cff1961f/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandScriptIntegrationTest.java ---------------------------------------------------------------------- diff --git a/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandScriptIntegrationTest.java b/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandScriptIntegrationTest.java index b65a3e3..cb6c9ed 100644 --- a/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandScriptIntegrationTest.java +++ b/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandScriptIntegrationTest.java @@ -37,6 +37,7 @@ import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocati import org.apache.brooklyn.test.http.TestHttpRequestHandler; import org.apache.brooklyn.test.http.TestHttpServer; import org.apache.brooklyn.util.core.task.Tasks; +import org.apache.brooklyn.util.core.task.system.internal.SystemProcessTaskFactory; import org.apache.brooklyn.util.exceptions.Exceptions; import org.apache.brooklyn.util.exceptions.FatalRuntimeException; import org.apache.brooklyn.util.http.HttpAsserts; @@ -46,7 +47,12 @@ import org.testng.annotations.*; import java.net.MalformedURLException; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.attribute.FileAttribute; import java.util.Iterator; +import java.util.Random; import java.util.UUID; import static org.apache.brooklyn.test.framework.BaseTest.TARGET_ENTITY; @@ -130,4 +136,43 @@ public class SimpleCommandScriptIntegrationTest { .withFailMessage("Service should be marked running"); } + @Test + public void shouldExecuteInTheRightPlace() throws Exception { + TestEntity testEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); + + String testUrl = server.getUrl() + "/" + SCRIPT_NAME; + HttpAsserts.assertContentContainsText(testUrl, TEXT); + + String remoteTmp = randomName(); + SimpleCommandTest uptime = app.createAndManageChild(EntitySpec.create(SimpleCommandTest.class) + .configure(TARGET_ENTITY, testEntity) + .configure(DEFAULT_COMMAND, "mkdir " + remoteTmp) + .configure(ASSERT_STATUS, ImmutableMap.of(EQUALS, 0))); + + Path localTmpPath = Paths.get("/tmp/").resolve(randomName()); + Path pwdSh = localTmpPath.resolve("pwd.sh"); + Files.createDirectory(localTmpPath); + Files.createFile(pwdSh); + Files.write(pwdSh, "pwd".getBytes()); + String pwdUrl = "file:" + pwdSh.toString(); + + SimpleCommandTest pwd = app.createAndManageChild(EntitySpec.create(SimpleCommandTest.class) + .configure(TARGET_ENTITY, testEntity) + .configure(DOWNLOAD_URL, pwdUrl) + .configure(RUN_DIR, remoteTmp) + .configure(ASSERT_STATUS, ImmutableMap.of(EQUALS, 0)) + .configure(ASSERT_OUT, ImmutableMap.of(CONTAINS, remoteTmp))); + + app.start(ImmutableList.of(localhost)); + + assertThat(uptime.sensors().get(SERVICE_UP)).isTrue() + .withFailMessage("Service should be up"); + assertThat(ServiceStateLogic.getExpectedState(uptime)).isEqualTo(Lifecycle.RUNNING) + .withFailMessage("Service should be marked running"); + } + + private String randomName() { + return Integer.valueOf(new Random(System.currentTimeMillis()).nextInt(100000)).toString(); + } + }
