You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by ge...@apache.org on 2018/04/23 08:53:33 UTC
[1/2] brooklyn-server git commit:
SshCommandSensor.testDslWithSshSensor: convert to non-integration
Repository: brooklyn-server
Updated Branches:
refs/heads/master 70c9e6ac9 -> cd40893d1
SshCommandSensor.testDslWithSshSensor: convert to non-integration
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/628f3830
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/628f3830
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/628f3830
Branch: refs/heads/master
Commit: 628f383030538d2810802e6df190a9cd898fc473
Parents: 9a13fff
Author: Aled Sage <al...@gmail.com>
Authored: Fri Apr 20 10:50:22 2018 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Fri Apr 20 10:50:22 2018 +0100
----------------------------------------------------------------------
.../camp/brooklyn/SshCommandSensorYamlTest.java | 59 +++++++++++++++++---
1 file changed, 52 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/628f3830/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/SshCommandSensorYamlTest.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/SshCommandSensorYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/SshCommandSensorYamlTest.java
index 1efc04c..e71713e 100644
--- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/SshCommandSensorYamlTest.java
+++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/SshCommandSensorYamlTest.java
@@ -18,6 +18,9 @@
*/
package org.apache.brooklyn.camp.brooklyn;
+import java.util.List;
+import java.util.Map;
+
import org.apache.brooklyn.api.entity.Application;
import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.entity.EntitySpec;
@@ -29,11 +32,15 @@ import org.apache.brooklyn.core.test.entity.TestApplication;
import org.apache.brooklyn.entity.software.base.VanillaSoftwareProcess;
import org.apache.brooklyn.test.Asserts;
import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool;
+import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.CustomResponse;
+import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecParams;
+import org.apache.brooklyn.util.text.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import com.google.common.base.Optional;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
@@ -111,18 +118,54 @@ public class SshCommandSensorYamlTest extends AbstractYamlTest {
}});
}
- // "Integration" because takes a couple of seconds
- @Test(groups="Integration")
+ @Test
public void testDslWithSshSensor() throws Exception {
+ // Set up a custom response for the echo command.
+ // Simulate the execution of the echo, substituting the environment variables as well.
+ RecordingSshTool.setCustomResponse(".*echo \"mytest .*", new RecordingSshTool.CustomResponseGenerator() {
+ @Override public CustomResponse generate(ExecParams execParams) throws Exception {
+ Optional<String> echoBody = extractEchoBody(execParams.commands);
+ if (echoBody.isPresent()) {
+ String stdout = evaluateEchoBody(execParams, echoBody.get());
+ String stderr = "";
+ return new CustomResponse(0, stdout, stderr);
+ } else {
+ return new CustomResponse(0, "", "");
+ }
+ }
+ private Optional<String> extractEchoBody(List<String> cmds) {
+ for (String cmd : cmds) {
+ if (cmd.contains("echo \"mytest ")) {
+ String echoBody = cmd.substring(cmd.indexOf("echo \"mytest ") + 6);
+ echoBody = Strings.removeFromEnd(echoBody.trim(), "\"");
+ return Optional.of(echoBody);
+ }
+ }
+ return Optional.absent();
+ }
+ private String evaluateEchoBody(ExecParams execParams, String echoBody) {
+ String result = echoBody;
+ for (Map.Entry<String, ?> entry : execParams.env.entrySet()) {
+ String envName = entry.getKey();
+ String envVal = entry.getValue() != null ? entry.getValue().toString() : "";
+ result = result.replaceAll("\\$"+envName, envVal);
+ result = result.replaceAll("\\$\\{"+envName+"\\}", envVal);
+ }
+ return result;
+ }
+ });
+
AttributeSensor<String> mySensor = Sensors.newStringSensor("mySensor");
AttributeSensor<String> sourceSensor = Sensors.newStringSensor("sourceSensor");
Entity app = createAndStartApplication(
"location:",
- " localhost",
+ " localhost:",
+ " sshToolClass: "+RecordingSshTool.class.getName(),
"services:",
"- type: " + VanillaSoftwareProcess.class.getName(),
" brooklyn.config:",
+ " myconf: myconfval",
" onbox.base.dir.skipResolution: true",
" checkRunning.command: true",
" brooklyn.initializers:",
@@ -134,9 +177,11 @@ public class SshCommandSensorYamlTest extends AbstractYamlTest {
" - type: org.apache.brooklyn.core.sensor.ssh.SshCommandSensor",
" brooklyn.config:",
" name: " + mySensor.getName(),
- " command: ",
+ " shell.env:",
+ " MY_ENV: $brooklyn:config(\"myconf\")",
+ " command:",
" $brooklyn:formatString:",
- " - echo %s",
+ " - echo \"mytest %s ${MY_ENV}\"",
" - $brooklyn:attributeWhenReady(\"sourceSensor\")",
" suppressDuplicates: true",
" period: 10ms",
@@ -144,10 +189,10 @@ public class SshCommandSensorYamlTest extends AbstractYamlTest {
waitForApplicationTasks(app);
VanillaSoftwareProcess entity = (VanillaSoftwareProcess) Iterables.getOnlyElement(app.getChildren());
- EntityAsserts.assertAttributeEqualsEventually(entity, mySensor, "someValue");
+ EntityAsserts.assertAttributeEqualsEventually(entity, mySensor, "mytest someValue myconfval");
entity.sensors().set(sourceSensor, "newValue");
- EntityAsserts.assertAttributeEqualsEventually(entity, mySensor, "someValue");
+ EntityAsserts.assertAttributeEqualsEventually(entity, mySensor, "mytest newValue myconfval");
}
@Override
[2/2] brooklyn-server git commit: Closes #957
Posted by ge...@apache.org.
Closes #957
SshCommandSensor.testDslWithSshSensor: convert to non-integration
See my comments in PR #953
This now uses the `RecordingSshTool` to execute the ssh command sensor, so the test is quicker. If you run it 100 times, the first time still takes 2 seconds (presumably due to classloading etc), and then it takes about 200ms per test after that.
Also fixes the final assertion, that it is the `newValue` in the sensor.
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/cd40893d
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/cd40893d
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/cd40893d
Branch: refs/heads/master
Commit: cd40893d11c0a069fc88094492aa6f5aa559ebc4
Parents: 70c9e6a 628f383
Author: Geoff Macartney <ge...@cloudsoftcorp.com>
Authored: Mon Apr 23 09:53:22 2018 +0100
Committer: Geoff Macartney <ge...@cloudsoftcorp.com>
Committed: Mon Apr 23 09:53:22 2018 +0100
----------------------------------------------------------------------
.../camp/brooklyn/SshCommandSensorYamlTest.java | 59 +++++++++++++++++---
1 file changed, 52 insertions(+), 7 deletions(-)
----------------------------------------------------------------------