Repository: incubator-brooklyn Updated Branches: refs/heads/master 3e40b2bfd -> f271d5ccf
Config supplied to SshToolâs constructor Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/63afa0fa Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/63afa0fa Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/63afa0fa Branch: refs/heads/master Commit: 63afa0fabc11653863e51106a014603513527fdc Parents: 443ed5e Author: Aled Sage <[email protected]> Authored: Wed Nov 11 16:41:40 2015 +0000 Committer: Aled Sage <[email protected]> Committed: Thu Nov 12 14:38:20 2015 +0000 ---------------------------------------------------------------------- .../location/ssh/SshMachineLocation.java | 41 ++++++++++++++--- .../ssh/SshMachineLocationSshToolTest.java | 47 +++++++++++++++++--- .../location/ssh/SshMachineLocationTest.java | 4 +- .../core/internal/ssh/RecordingSshTool.java | 7 +++ .../brooklyn/entity/java/JavaOptsTest.java | 4 +- .../entity/software/base/EntitySshToolTest.java | 8 +++- 6 files changed, 92 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/63afa0fa/core/src/main/java/org/apache/brooklyn/location/ssh/SshMachineLocation.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/location/ssh/SshMachineLocation.java b/core/src/main/java/org/apache/brooklyn/location/ssh/SshMachineLocation.java index 7daa523..b71e47d 100644 --- a/core/src/main/java/org/apache/brooklyn/location/ssh/SshMachineLocation.java +++ b/core/src/main/java/org/apache/brooklyn/location/ssh/SshMachineLocation.java @@ -18,8 +18,6 @@ */ package org.apache.brooklyn.location.ssh; -import static org.apache.brooklyn.core.config.ConfigKeys.newConfigKeyWithPrefix; -import static org.apache.brooklyn.core.config.ConfigKeys.newStringConfigKey; import static org.apache.brooklyn.util.groovy.GroovyJavaMethods.truth; import java.io.Closeable; @@ -148,6 +146,25 @@ public class SshMachineLocation extends AbstractLocation implements MachineLocat BrooklynConfigKeys.BROOKLYN_SSH_CONFIG_KEY_PREFIX, Preconditions.checkNotNull(BrooklynConfigKeys.SSH_TOOL_CLASS, "static final initializer classload ordering problem")); + /** + * Prefix for config key:values to be passed to the ssh tool on construction. For example, + * one could define the location below. When executing ssh commands, it would instantiate + * an instance of {@code com.acme.brooklyn.MyCustomSshTool}, calling its constructor with a + * {@code Map<String, Object>} that contained the configuration. In this case, the map would + * include: {@code address=1.2.3.4}; {@code user=myname}; and {@code myparam=myvalue}. + * + * <pre> + * {@code + * brooklyn.location.named.myLocation = byon:(hosts=1.2.3.4,user=myname) + * brooklyn.location.named.myLocation.sshToolClass = com.acme.brooklyn.MyCustomSshTool + * brooklyn.location.named.myLocation.sshToolClass.myparam = myvalue + * } + * } + * </pre> + * <p> + */ + public static final String SSH_TOOL_CLASS_PROPERTIES_PREFIX = SSH_TOOL_CLASS.getName()+"."; + public static final ConfigKey<Duration> SSH_CACHE_EXPIRY_DURATION = ConfigKeys.newConfigKey(Duration.class, "sshCacheExpiryDuration", "Expiry time for unused cached ssh connections", Duration.FIVE_MINUTES); @@ -580,10 +597,19 @@ public class SshMachineLocation extends AbstractLocation implements MachineLocat .configure(SshTool.PROP_HOST, address.getHostName()); for (Map.Entry<String,Object> entry: config().getBag().getAllConfig().entrySet()) { + boolean include = false; String key = entry.getKey(); if (key.startsWith(SshTool.BROOKLYN_CONFIG_KEY_PREFIX)) { key = Strings.removeFromStart(key, SshTool.BROOKLYN_CONFIG_KEY_PREFIX); - } else if (ALL_SSH_CONFIG_KEY_NAMES.contains(entry.getKey())) { + include = true; + } + + if (key.startsWith(SSH_TOOL_CLASS_PROPERTIES_PREFIX)) { + key = Strings.removeFromStart(key, SSH_TOOL_CLASS_PROPERTIES_PREFIX); + include = true; + } + + if (ALL_SSH_CONFIG_KEY_NAMES.contains(entry.getKey())) { // key should be included, and does not need to be changed // TODO make this config-setting mechanism more universal @@ -591,11 +617,12 @@ public class SshMachineLocation extends AbstractLocation implements MachineLocat // thinking either we know about the tool here, // or we don't allow unadorned keys to be set // (require use of BROOKLYN_CONFIG_KEY_PREFIX) - } else { - // this key is not applicable here; ignore it - continue; + include = true; + } + + if (include) { + args.putStringKey(key, entry.getValue()); } - args.putStringKey(key, entry.getValue()); } // Explicit props trump all. http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/63afa0fa/core/src/test/java/org/apache/brooklyn/location/ssh/SshMachineLocationSshToolTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/ssh/SshMachineLocationSshToolTest.java b/core/src/test/java/org/apache/brooklyn/location/ssh/SshMachineLocationSshToolTest.java index daa9233..cec7ab0 100644 --- a/core/src/test/java/org/apache/brooklyn/location/ssh/SshMachineLocationSshToolTest.java +++ b/core/src/test/java/org/apache/brooklyn/location/ssh/SshMachineLocationSshToolTest.java @@ -18,9 +18,13 @@ */ package org.apache.brooklyn.location.ssh; +import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; +import java.util.Map; + import org.apache.brooklyn.api.location.LocationSpec; +import org.apache.brooklyn.api.location.MachineProvisioningLocation; import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool; import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecCmd; @@ -30,9 +34,12 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; /** - * Test that the right SshTool is picked up, based on the {@link SshMachineLocation}'s configuration. + * Test that the right SshTool is picked up (and configured), based on the + * {@link SshMachineLocation}'s configuration. */ public class SshMachineLocationSshToolTest extends BrooklynAppUnitTestSupport { @@ -41,18 +48,18 @@ public class SshMachineLocationSshToolTest extends BrooklynAppUnitTestSupport { // configuration options. If you *just* instantiate the location directly, then it doesn't get the // mgmt.config options. // - // See EntitySshToolTest for an equivalent that configures the SshTool on the management context - // and on the entity. + // See EntitySshToolTest in software-base for an equivalent that configures the SshTool on the + // management context and on the entity. @BeforeMethod(alwaysRun=true) public void setUp() throws Exception { super.setUp(); - RecordingSshTool.execScriptCmds.clear(); + RecordingSshTool.clear(); } @AfterMethod(alwaysRun=true) public void tearDown() throws Exception { - RecordingSshTool.execScriptCmds.clear(); + RecordingSshTool.clear(); super.tearDown(); } @@ -82,7 +89,35 @@ public class SshMachineLocationSshToolTest extends BrooklynAppUnitTestSupport { .configure(SshTool.PROP_TOOL_CLASS.getName(), "class.does.not.exist")); runCustomSshToolClass(machine); } - + + @Test + @SuppressWarnings("unchecked") + public void testCustomSshToolDefinedOnNamedLocation() throws Exception { + mgmt.getBrooklynProperties().putAll(ImmutableMap.of( + "brooklyn.location.named.localhostWithCustomTool", "localhost", + "brooklyn.location.named.localhostWithCustomTool."+SshMachineLocation.SSH_TOOL_CLASS.getName(), RecordingSshTool.class.getName())); + MachineProvisioningLocation<SshMachineLocation> loc = (MachineProvisioningLocation<SshMachineLocation>) mgmt.getLocationRegistry().resolve("localhostWithCustomTool"); + SshMachineLocation machine = loc.obtain(ImmutableMap.of()); + runCustomSshToolClass(machine); + } + + @Test + @SuppressWarnings("unchecked") + public void testCustomSshToolWithCustomConfigDefinedOnNamedLocation() throws Exception { + mgmt.getBrooklynProperties().putAll(ImmutableMap.of( + "brooklyn.location.named.localhostWithCustomTool", "byon:(hosts=127.0.0.1, user=myname)", + "brooklyn.location.named.localhostWithCustomTool."+SshMachineLocation.SSH_TOOL_CLASS.getName(), RecordingSshTool.class.getName(), + "brooklyn.location.named.localhostWithCustomTool."+SshMachineLocation.SSH_TOOL_CLASS.getName()+".myparam", "myvalue")); + MachineProvisioningLocation<SshMachineLocation> loc = (MachineProvisioningLocation<SshMachineLocation>) mgmt.getLocationRegistry().resolve("localhostWithCustomTool"); + SshMachineLocation machine = loc.obtain(ImmutableMap.of()); + runCustomSshToolClass(machine); + + Map<?, ?> props = Iterables.getLast(RecordingSshTool.constructorProps); + assertEquals(props.get("myparam"), "myvalue", "props="+props); + assertEquals(props.get("host"), "127.0.0.1", "props="+props); + assertEquals(props.get("user"), "myname", "props="+props); + } + protected void runCustomSshToolClass(SshMachineLocation host2) throws Exception { host2.execCommands("mySummary", ImmutableList.of("myCommand")); http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/63afa0fa/core/src/test/java/org/apache/brooklyn/location/ssh/SshMachineLocationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/ssh/SshMachineLocationTest.java b/core/src/test/java/org/apache/brooklyn/location/ssh/SshMachineLocationTest.java index e7f7c57..c9d8ef7 100644 --- a/core/src/test/java/org/apache/brooklyn/location/ssh/SshMachineLocationTest.java +++ b/core/src/test/java/org/apache/brooklyn/location/ssh/SshMachineLocationTest.java @@ -91,7 +91,7 @@ public class SshMachineLocationTest extends BrooklynAppUnitTestSupport { super.setUp(); host = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) .configure("address", Networking.getLocalHost())); - RecordingSshTool.execScriptCmds.clear(); + RecordingSshTool.clear(); } @AfterMethod(alwaysRun=true) @@ -99,7 +99,7 @@ public class SshMachineLocationTest extends BrooklynAppUnitTestSupport { try { if (host != null) Streams.closeQuietly(host); } finally { - RecordingSshTool.execScriptCmds.clear(); + RecordingSshTool.clear(); super.tearDown(); } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/63afa0fa/core/src/test/java/org/apache/brooklyn/util/core/internal/ssh/RecordingSshTool.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/util/core/internal/ssh/RecordingSshTool.java b/core/src/test/java/org/apache/brooklyn/util/core/internal/ssh/RecordingSshTool.java index c1e8006..b31299d 100644 --- a/core/src/test/java/org/apache/brooklyn/util/core/internal/ssh/RecordingSshTool.java +++ b/core/src/test/java/org/apache/brooklyn/util/core/internal/ssh/RecordingSshTool.java @@ -51,10 +51,17 @@ public class RecordingSshTool implements SshTool { } public static List<ExecCmd> execScriptCmds = Lists.newCopyOnWriteArrayList(); + public static List<Map<?,?>> constructorProps = Lists.newCopyOnWriteArrayList(); private boolean connected; + public static void clear() { + execScriptCmds.clear(); + constructorProps.clear(); + } + public RecordingSshTool(Map<?,?> props) { + constructorProps.add(props); } @Override public void connect() { connected = true; http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/63afa0fa/software/base/src/test/java/org/apache/brooklyn/entity/java/JavaOptsTest.java ---------------------------------------------------------------------- diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/java/JavaOptsTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/java/JavaOptsTest.java index 2928f2c..15243c5 100644 --- a/software/base/src/test/java/org/apache/brooklyn/entity/java/JavaOptsTest.java +++ b/software/base/src/test/java/org/apache/brooklyn/entity/java/JavaOptsTest.java @@ -64,7 +64,7 @@ public class JavaOptsTest extends BrooklynAppUnitTestSupport { @BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception { - RecordingSshTool.execScriptCmds.clear(); + RecordingSshTool.clear(); super.setUp(); loc = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) .configure("address", "localhost") @@ -75,7 +75,7 @@ public class JavaOptsTest extends BrooklynAppUnitTestSupport { @Override public void tearDown() throws Exception { super.tearDown(); - RecordingSshTool.execScriptCmds.clear(); + RecordingSshTool.clear(); } @Test http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/63afa0fa/software/base/src/test/java/org/apache/brooklyn/entity/software/base/EntitySshToolTest.java ---------------------------------------------------------------------- diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/EntitySshToolTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/EntitySshToolTest.java index 12464ef..ecb9be9 100644 --- a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/EntitySshToolTest.java +++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/EntitySshToolTest.java @@ -26,6 +26,7 @@ import org.apache.brooklyn.core.entity.BrooklynConfigKeys; import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; import org.apache.brooklyn.entity.machine.MachineEntity; import org.apache.brooklyn.location.ssh.SshMachineLocation; +import org.apache.brooklyn.location.ssh.SshMachineLocationSshToolTest; import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool; import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecCmd; import org.testng.annotations.AfterMethod; @@ -36,6 +37,9 @@ import com.google.common.collect.ImmutableList; /** * Test that the right SshTool is picked up, based on the entity's configuration. + * See {@link SshMachineLocationSshToolTest} for more tests. These ones are just + * for those involving the entity (and the config on the top-level brooklyn properties, + * as we need a {@link MachineEntity} to do that. */ public class EntitySshToolTest extends BrooklynAppUnitTestSupport { @@ -44,7 +48,7 @@ public class EntitySshToolTest extends BrooklynAppUnitTestSupport { @BeforeMethod(alwaysRun=true) public void setUp() throws Exception { super.setUp(); - RecordingSshTool.execScriptCmds.clear(); + RecordingSshTool.clear(); machine = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) .configure("address", "localhost")); @@ -52,7 +56,7 @@ public class EntitySshToolTest extends BrooklynAppUnitTestSupport { @AfterMethod(alwaysRun=true) public void tearDown() throws Exception { - RecordingSshTool.execScriptCmds.clear(); + RecordingSshTool.clear(); super.tearDown(); }
