brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [1/3] brooklyn-server git commit: Adds TestSshCommand.SHELL_ENVIRONMENT
Date Tue, 07 Jun 2016 14:38:53 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master 19e63db79 -> a371d9739


Adds TestSshCommand.SHELL_ENVIRONMENT


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/be20f71f
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/be20f71f
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/be20f71f

Branch: refs/heads/master
Commit: be20f71f7be641bd32f07278ba086adb7dc8b24b
Parents: cef5d65
Author: Aled Sage <aled.sage@gmail.com>
Authored: Tue Jun 7 12:06:18 2016 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Tue Jun 7 15:03:31 2016 +0100

----------------------------------------------------------------------
 .../brooklyn/test/framework/TestSshCommand.java |  4 ++
 .../test/framework/TestSshCommandImpl.java      | 67 ++++++++++++--------
 .../test/framework/TestSshCommandTest.java      | 20 ++++++
 3 files changed, 63 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/be20f71f/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestSshCommand.java
----------------------------------------------------------------------
diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestSshCommand.java
b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestSshCommand.java
index 36b6ece..6b5cf44 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestSshCommand.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestSshCommand.java
@@ -28,6 +28,7 @@ import com.google.common.collect.ImmutableMap;
 import org.apache.brooklyn.api.entity.ImplementedBy;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
 import org.apache.brooklyn.core.sensor.AttributeSensorAndConfigKey;
 import org.apache.brooklyn.entity.software.base.SoftwareProcess;
 import org.apache.brooklyn.util.core.flags.SetFromFlag;
@@ -56,6 +57,9 @@ public interface TestSshCommand extends BaseTest {
     @SetFromFlag("downloadUrl")
     AttributeSensorAndConfigKey<String, String> DOWNLOAD_URL = SoftwareProcess.DOWNLOAD_URL;
 
+    @SetFromFlag("env")
+    ConfigKey<Map<String, Object>> SHELL_ENVIRONMENT = BrooklynConfigKeys.SHELL_ENVIRONMENT;
+
     /**
      * Where the script will be downloaded on the target machine.
      */

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/be20f71f/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestSshCommandImpl.java
----------------------------------------------------------------------
diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestSshCommandImpl.java
b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestSshCommandImpl.java
index 02b977f..5d1b886 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestSshCommandImpl.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestSshCommandImpl.java
@@ -30,21 +30,13 @@ import static org.apache.brooklyn.util.text.Strings.isNonBlank;
 
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.base.Joiner;
-import com.google.common.base.Splitter;
-import com.google.common.base.Suppliers;
-import com.google.common.collect.ImmutableMap;
-
 import org.apache.brooklyn.api.location.Location;
 import org.apache.brooklyn.api.mgmt.TaskFactory;
 import org.apache.brooklyn.core.effector.ssh.SshEffectorTasks;
@@ -60,6 +52,15 @@ import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.text.Identifiers;
 import org.apache.brooklyn.util.text.Strings;
 import org.apache.brooklyn.util.time.Duration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Splitter;
+import com.google.common.base.Suppliers;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
 
 // TODO assertions below should use TestFrameworkAssertions but that class needs to be improved
to give better error messages
 public class TestSshCommandImpl extends TargetableTestComponentImpl implements TestSshCommand
{
@@ -149,6 +150,9 @@ public class TestSshCommandImpl extends TargetableTestComponentImpl implements
T
         String downloadName = DOWNLOAD_URL.getName();
         String commandName = COMMAND.getName();
 
+        Map<String, Object> env = getConfig(SHELL_ENVIRONMENT);
+        if (env == null) env = ImmutableMap.of();
+        
         if (!(isNonBlank(downloadUrl) ^ isNonBlank(command))) {
             throw illegal("Must specify exactly one of", downloadName, "and", commandName);
         }
@@ -156,49 +160,55 @@ public class TestSshCommandImpl extends TargetableTestComponentImpl
implements T
         if (isNonBlank(downloadUrl)) {
             String scriptDir = getConfig(SCRIPT_DIR);
             String scriptPath = calculateDestPath(downloadUrl, scriptDir);
-            result = executeDownloadedScript(machineLocation, downloadUrl, scriptPath);
+            result = executeDownloadedScript(machineLocation, downloadUrl, scriptPath, env);
         }
 
         if (isNonBlank(command)) {
-            result = executeShellCommand(machineLocation, command);
+            result = executeShellCommand(machineLocation, command, env);
         }
 
         handle(result);
     }
 
-    private Result executeDownloadedScript(SshMachineLocation machineLocation, String url,
String scriptPath) {
+    private Result executeDownloadedScript(SshMachineLocation machineLocation, String url,
String scriptPath, Map<String, Object> env) {
 
         TaskFactory<?> install = SshTasks.installFromUrl(ImmutableMap.<String, Object>of(),
machineLocation, url, scriptPath);
         DynamicTasks.queue(install);
         DynamicTasks.waitForLast();
 
-        List<String> commands = new ArrayList<>();
-        commands.add("chmod u+x " + scriptPath);
-        maybeCdToRunDir(commands);
-        commands.add(scriptPath);
+        List<String> commands = ImmutableList.<String>builder()
+                .add("chmod u+x " + scriptPath)
+                .addAll(maybeCdToRunDirCmd())
+                .add(scriptPath)
+                .build();
 
-        return runCommands(machineLocation, commands);
+        return runCommands(machineLocation, commands, env);
     }
 
-    private Result executeShellCommand(SshMachineLocation machineLocation, String command)
{
+    private Result executeShellCommand(SshMachineLocation machineLocation, String command,
Map<String, Object> env) {
 
-        List<String> commands = new ArrayList<>();
-        maybeCdToRunDir(commands);
-        commands.add(command);
+        List<String> commands = ImmutableList.<String>builder()
+                .addAll(maybeCdToRunDirCmd())
+                .add(command)
+                .build();
 
-        return runCommands(machineLocation, commands);
+        return runCommands(machineLocation, commands, env);
     }
 
-    private void maybeCdToRunDir(List<String> commands) {
+    private List<String> maybeCdToRunDirCmd() {
         String runDir = getConfig(RUN_DIR);
         if (!isBlank(runDir)) {
-            commands.add(CD + " " + runDir);
+            return ImmutableList.of(CD + " " + runDir);
+        } else {
+            return ImmutableList.of();
         }
     }
 
-    private Result runCommands(SshMachineLocation machine, List<String> commands) {
+    private Result runCommands(SshMachineLocation machine, List<String> commands, Map<String,
Object> env) {
+        @SuppressWarnings({ "unchecked", "rawtypes" })
         SshEffectorTasks.SshEffectorTaskFactory<Integer> etf = SshEffectorTasks.ssh(commands.toArray(new
String[]{}))
-            .machine(machine);
+                .environmentVariables((Map<String, String>)(Map)env)
+                .machine(machine);
 
         ProcessTaskWrapper<Integer> job = DynamicTasks.queue(etf);
         job.asTask().blockUntilEnded();
@@ -207,8 +217,9 @@ public class TestSshCommandImpl extends TargetableTestComponentImpl implements
T
 
 
 
-    private IllegalArgumentException illegal(String message, String ...messages) {
-        return new IllegalArgumentException(Joiner.on(' ').join(this.toString() + ":", message,
messages));
+    private IllegalArgumentException illegal(String message, String... messages) {
+        Iterable<String> allmsgs = Iterables.concat(MutableList.of(this.toString()
+ ":", message), Arrays.asList(messages));
+        return new IllegalArgumentException(Joiner.on(' ').join(allmsgs));
     }
 
     private String calculateDestPath(String url, String directory) {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/be20f71f/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestSshCommandTest.java
----------------------------------------------------------------------
diff --git a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestSshCommandTest.java
b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestSshCommandTest.java
index 4104e97..7263dbe 100644
--- a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestSshCommandTest.java
+++ b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestSshCommandTest.java
@@ -27,6 +27,7 @@ import static org.apache.brooklyn.test.framework.TestSshCommand.ASSERT_OUT;
 import static org.apache.brooklyn.test.framework.TestSshCommand.ASSERT_STATUS;
 import static org.apache.brooklyn.test.framework.TestSshCommand.COMMAND;
 import static org.apache.brooklyn.test.framework.TestSshCommand.DOWNLOAD_URL;
+import static org.apache.brooklyn.test.framework.TestSshCommand.SHELL_ENVIRONMENT;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.IOException;
@@ -46,6 +47,7 @@ import org.apache.brooklyn.location.ssh.SshMachineLocation;
 import org.apache.brooklyn.test.Asserts;
 import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool;
+import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecCmd;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.text.Identifiers;
 import org.testng.annotations.AfterMethod;
@@ -275,6 +277,24 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
         assertServiceFailed(test);
     }
     
+    @Test
+    public void shouldIncludeEnv() throws Exception {
+        Map<String, Object> env = ImmutableMap.<String, Object>of("ENV1", "val1",
"ENV2", "val2");
+        
+        TestSshCommand test = app.createAndManageChild(EntitySpec.create(TestSshCommand.class)
+            .configure(TARGET_ENTITY, testEntity)
+            .configure(COMMAND, "mycmd")
+            .configure(SHELL_ENVIRONMENT, env));
+
+        app.start(ImmutableList.<Location>of());
+
+        assertServiceHealthy(test);
+        
+        ExecCmd cmdExecuted = RecordingSshTool.getLastExecCmd();
+        assertThat(cmdExecuted.commands).isEqualTo(ImmutableList.of("mycmd"));
+        assertThat(cmdExecuted.env).isEqualTo(env);
+    }
+
     private Path createTempScript(String filename, String contents) {
         try {
             Path tempFile = Files.createTempFile("TestSshCommandTest-" + filename, ".sh");


Mime
View raw message