brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From drigod...@apache.org
Subject [3/4] brooklyn-server git commit: Adds TestSshCommand.maxAttempts
Date Mon, 10 Apr 2017 11:43:51 GMT
Adds TestSshCommand.maxAttempts

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

Branch: refs/heads/master
Commit: 2c1547ce3e3e0689d95f3f84558b13897fc186d8
Parents: 2b3c241
Author: Aled Sage <aled.sage@gmail.com>
Authored: Wed Apr 5 21:24:44 2017 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Wed Apr 5 21:24:44 2017 +0100

----------------------------------------------------------------------
 .../brooklyn/test/framework/TestSshCommand.java |  4 +
 .../test/framework/TestSshCommandImpl.java      |  2 +
 .../test/framework/TestSshCommandTest.java      | 78 +++++++++++++++++++-
 3 files changed, 81 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2c1547ce/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 85d402c..df6df72 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
@@ -106,4 +106,8 @@ public interface TestSshCommand extends BaseTest {
     ConfigKey<Object> ASSERT_ERR = ConfigKeys.newConfigKey(Object.class, "assert.err",
"Assertions on command standard error",
         ImmutableList.<Map<String, Object>>of());
 
+    /**
+     * The maximum number of times to execute the ssh command, before throwing an exception.
+     */
+    ConfigKey<Integer> MAX_ATTEMPTS = ConfigKeys.newIntegerConfigKey("maxAttempts",
"Maximum number of attempts");
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2c1547ce/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 3f0f116..b8b0e1d 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
@@ -125,6 +125,7 @@ public class TestSshCommandImpl extends TargetableTestComponentImpl implements
T
             final SshMachineLocation machineLocation =
                     Machines.findUniqueMachineLocation(resolveTarget().getLocations(), SshMachineLocation.class).get();
             final Duration timeout = getRequiredConfig(TIMEOUT);
+            final Integer maxAttempts = getConfig(MAX_ATTEMPTS);
             final Duration backoffToPeriod = getRequiredConfig(BACKOFF_TO_PERIOD);
 
             // TODO use TestFrameworkAssertions (or use Repeater in the same way as that
does)?
@@ -132,6 +133,7 @@ public class TestSshCommandImpl extends TargetableTestComponentImpl implements
T
             // for limitTimeTo, backoffTo, etc?
             ReferenceWithError<Boolean> result = Repeater.create("Running ssh-command
tests")
                     .limitTimeTo(timeout)
+                    .limitIterationsTo((maxAttempts != null) ? maxAttempts : Integer.MAX_VALUE)
                     .backoffTo((backoffToPeriod != null) ? backoffToPeriod : Duration.millis(500))
                     .until(new Callable<Boolean>() {
                         @Override

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2c1547ce/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 344c53d..094433f 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
@@ -18,6 +18,8 @@
  */
 package org.apache.brooklyn.test.framework;
 
+import static org.apache.brooklyn.core.entity.EntityAsserts.assertEntityFailed;
+import static org.apache.brooklyn.core.entity.EntityAsserts.assertEntityHealthy;
 import static org.apache.brooklyn.test.framework.BaseTest.TIMEOUT;
 import static org.apache.brooklyn.test.framework.TargetableTestComponent.TARGET_ENTITY;
 import static org.apache.brooklyn.test.framework.TestFrameworkAssertions.CONTAINS;
@@ -25,18 +27,21 @@ import static org.apache.brooklyn.test.framework.TestFrameworkAssertions.EQUALS;
 import static org.apache.brooklyn.test.framework.TestSshCommand.ASSERT_ERR;
 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.BACKOFF_TO_PERIOD;
 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.MAX_ATTEMPTS;
 import static org.apache.brooklyn.test.framework.TestSshCommand.SHELL_ENVIRONMENT;
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.apache.brooklyn.core.entity.EntityAsserts.assertEntityFailed;
-import static org.apache.brooklyn.core.entity.EntityAsserts.assertEntityHealthy;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
 
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.brooklyn.api.entity.EntitySpec;
 import org.apache.brooklyn.api.location.Location;
@@ -46,7 +51,10 @@ import org.apache.brooklyn.core.test.entity.TestEntity;
 import org.apache.brooklyn.location.ssh.SshMachineLocation;
 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.ExecCmd;
+import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecCmdPredicates;
+import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecParams;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.text.Identifiers;
 import org.apache.brooklyn.util.time.Duration;
@@ -58,6 +66,7 @@ import org.testng.annotations.Test;
 import com.google.common.base.Stopwatch;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
 
 public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
 
@@ -140,6 +149,7 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
         RecordingSshTool.setCustomResponse(cmd, new RecordingSshTool.CustomResponse(1, null,
null));
         
         TestSshCommand test = app.createAndManageChild(EntitySpec.create(TestSshCommand.class)
+            .configure(MAX_ATTEMPTS, 1)
             .configure(TARGET_ENTITY, testEntity)
             .configure(COMMAND, cmd));
 
@@ -176,6 +186,7 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
         RecordingSshTool.setCustomResponse(cmd, new RecordingSshTool.CustomResponse(0, "wrongstdout",
null));
         
         TestSshCommand test = app.createAndManageChild(EntitySpec.create(TestSshCommand.class)
+            .configure(MAX_ATTEMPTS, 1)
             .configure(TARGET_ENTITY, testEntity)
             .configure(COMMAND, cmd)
             .configure(ASSERT_OUT, makeAssertions(ImmutableMap.of(CONTAINS, "mystdout"))));
@@ -196,6 +207,7 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
         RecordingSshTool.setCustomResponse(cmd, new RecordingSshTool.CustomResponse(0, null,
"wrongstderr"));
         
         TestSshCommand test = app.createAndManageChild(EntitySpec.create(TestSshCommand.class)
+            .configure(MAX_ATTEMPTS, 1)
             .configure(TARGET_ENTITY, testEntity)
             .configure(COMMAND, cmd)
             .configure(ASSERT_ERR, makeAssertions(ImmutableMap.of(CONTAINS, "mystderr"))));
@@ -211,12 +223,13 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
     }
 
     @Test
-    public void shouldNotBeUpIfAssertionsFail() {
+    public void shouldFailOnUnmatchedExitCode() {
         Map<String, ?> equalsOne = ImmutableMap.of(EQUALS, 1);
 
         Map<String, ?> equals255 = ImmutableMap.of(EQUALS, 255);
 
         TestSshCommand test = app.createAndManageChild(EntitySpec.create(TestSshCommand.class)
+            .configure(MAX_ATTEMPTS, 1)
             .configure(TARGET_ENTITY, testEntity)
             .configure(COMMAND, "uptime")
             .configure(ASSERT_STATUS, makeAssertions(equalsOne, equals255)));
@@ -315,6 +328,65 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
         assertThat(cmdExecuted.env).isEqualTo(env);
     }
 
+    @Test
+    public void testRetries() {
+        String cmd = "commandExpectedToFail-" + Identifiers.randomLong();
+        RecordingSshTool.setCustomResponse(cmd, new RecordingSshTool.CustomResponseGenerator()
{
+            final AtomicInteger counter = new AtomicInteger();
+            @Override public CustomResponse generate(ExecParams execParams) throws Exception
{
+                // First call fails; subsequent calls succeed
+                int code = (counter.incrementAndGet() > 1) ? 0 : 1;
+                return new RecordingSshTool.CustomResponse(code, null, null);
+            }
+        });
+        
+        TestSshCommand test = app.createAndManageChild(EntitySpec.create(TestSshCommand.class)
+                .configure(BACKOFF_TO_PERIOD, Duration.millis(1))
+                .configure(TIMEOUT, Duration.minutes(1))
+                .configure(TARGET_ENTITY, testEntity)
+                .configure(COMMAND, cmd));
+        
+        Stopwatch stopwatch = Stopwatch.createStarted();
+        
+        app.start(ImmutableList.<Location>of());
+        assertEntityHealthy(test);
+
+        Iterable<ExecCmd> calls = Iterables.filter(RecordingSshTool.getExecCmds(),
ExecCmdPredicates.containsCmd(cmd));
+        assertEquals(Iterables.size(calls), 2, "matchingCalls="+calls);
+        
+        Duration duration = Duration.of(stopwatch);
+        assertTrue(duration.isShorterThan(Asserts.DEFAULT_LONG_TIMEOUT), "duration="+duration);
+    }
+
+    @Test
+    public void testMaxAttempts() {
+        String cmd = "commandExpectedToFail-" + Identifiers.randomLong();
+        RecordingSshTool.setCustomResponse(cmd, new RecordingSshTool.CustomResponse(1, null,
null));
+        
+        TestSshCommand test = app.createAndManageChild(EntitySpec.create(TestSshCommand.class)
+                .configure(MAX_ATTEMPTS, 2)
+                .configure(BACKOFF_TO_PERIOD, Duration.millis(1))
+                .configure(TIMEOUT, Duration.minutes(1))
+                .configure(TARGET_ENTITY, testEntity)
+                .configure(COMMAND, cmd));
+        
+        Stopwatch stopwatch = Stopwatch.createStarted();
+        try {
+            app.start(ImmutableList.<Location>of());
+            Asserts.shouldHaveFailedPreviously();
+        } catch (Throwable t) {
+            Asserts.expectedFailureContains(t, "exit code expected equals 0 but found 1");
+        }
+
+        assertEntityFailed(test);
+
+        Iterable<ExecCmd> calls = Iterables.filter(RecordingSshTool.getExecCmds(),
ExecCmdPredicates.containsCmd(cmd));
+        assertEquals(Iterables.size(calls), 2, "matchingCalls="+calls);
+        
+        Duration duration = Duration.of(stopwatch);
+        assertTrue(duration.isShorterThan(Asserts.DEFAULT_LONG_TIMEOUT), "duration="+duration);
+    }
+
     private Path createTempScript(String filename, String contents) {
         try {
             Path tempFile = Files.createTempFile("TestSshCommandTest-" + filename, ".sh");


Mime
View raw message