brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [1/2] brooklyn-server git commit: Expose assertServiceFailed(Entity) and assertServiceHealthy(Entity)
Date Thu, 07 Jul 2016 23:08:00 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master f0c550e52 -> 10613d16d


Expose assertServiceFailed(Entity) and assertServiceHealthy(Entity)


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

Branch: refs/heads/master
Commit: e239558be286bc3dc5524a9bd9e33400395e4cfc
Parents: c7731a4
Author: Valentin Aitken <bostko@gmail.com>
Authored: Thu Jun 30 16:32:47 2016 +0300
Committer: Valentin Aitken <bostko@gmail.com>
Committed: Tue Jul 5 11:26:19 2016 +0300

----------------------------------------------------------------------
 .../brooklyn/core/entity/EntityAsserts.java     | 34 ++++++++++++++++-
 .../test/framework/TestSshCommandTest.java      | 39 ++++++--------------
 2 files changed, 44 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/e239558b/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java b/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java
index e78a1af..a793c1f 100644
--- a/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java
+++ b/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java
@@ -32,17 +32,19 @@ import org.apache.brooklyn.api.sensor.AttributeSensor;
 import org.apache.brooklyn.api.sensor.SensorEvent;
 import org.apache.brooklyn.api.sensor.SensorEventListener;
 import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;
+import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
 import org.apache.brooklyn.test.Asserts;
 import org.apache.brooklyn.util.time.Duration;
 
 import java.util.Collection;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.Callable;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 
-import static org.apache.brooklyn.test.Asserts.assertEquals;
+import static org.apache.brooklyn.core.entity.trait.Startable.SERVICE_UP;
+import static org.apache.brooklyn.test.Asserts.*;
 
 /**
  * Convenience class containing assertions that may be made about entities.
@@ -223,4 +225,32 @@ public class EntityAsserts {
         });
     }
 
+    /**
+     * Asserts sensors {@code service.isUp} is true, and that {@code service.state} is "running".
+     * Setting these sensors is common behaviour for entities, but depends on the particular
entity
+     * implementation.
+     */
+    @Beta
+    public static void assertEntityHealthy(Entity entity) {
+        assertAttributeEquals(entity, SERVICE_UP, true);
+        assertAttributeEquals(entity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING);
+        assertAttribute(entity, Attributes.SERVICE_STATE_EXPECTED, new Predicate<Lifecycle.Transition>()
{
+            @Override public boolean apply(Lifecycle.Transition transition) {
+                assertNotNull(transition);
+                return Lifecycle.RUNNING.equals(transition.getState());
+            }
+        });
+    }
+
+    /**
+     * Asserts sensors {@code service.isUp} is false, and that {@code service.state} is "on
fire".
+     * Setting these sensors is common behaviour for entities, but depends on the particular
entity
+     * implementation.
+     */
+    @Beta
+    public static void assertEntityFailed(Entity entity) {
+        assertAttributeEquals(entity, SERVICE_UP, false);
+        assertAttributeEquals(entity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE);
+    }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/e239558b/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 37e79c9..e3ea450 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,7 +18,6 @@
  */
 package org.apache.brooklyn.test.framework;
 
-import static org.apache.brooklyn.core.entity.trait.Startable.SERVICE_UP;
 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;
@@ -30,6 +29,8 @@ 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 static org.apache.brooklyn.core.entity.EntityAsserts.assertEntityFailed;
+import static org.apache.brooklyn.core.entity.EntityAsserts.assertEntityHealthy;
 
 import java.io.IOException;
 import java.nio.file.Files;
@@ -40,8 +41,6 @@ import java.util.Map;
 import org.apache.brooklyn.api.entity.EntitySpec;
 import org.apache.brooklyn.api.location.Location;
 import org.apache.brooklyn.api.location.LocationSpec;
-import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;
-import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
 import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
 import org.apache.brooklyn.core.test.entity.TestEntity;
 import org.apache.brooklyn.location.ssh.SshMachineLocation;
@@ -131,7 +130,7 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
 
         app.start(ImmutableList.<Location>of());
 
-        assertServiceHealthy(test);
+        assertEntityHealthy(test);
         assertThat(RecordingSshTool.getLastExecCmd().commands).isEqualTo(ImmutableList.of("uptime"));
     }
 
@@ -151,7 +150,7 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
             Asserts.expectedFailureContains(t, "exit code equals 0");
         }
 
-        assertServiceFailed(test);
+        assertEntityFailed(test);
         assertThat(RecordingSshTool.getLastExecCmd().commands).isEqualTo(ImmutableList.of(cmd));
     }
 
@@ -168,7 +167,7 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
 
         app.start(ImmutableList.<Location>of());
 
-        assertServiceHealthy(test);
+        assertEntityHealthy(test);
     }
 
     @Test
@@ -188,7 +187,7 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
             Asserts.expectedFailureContains(t, "stdout contains mystdout");
         }
 
-        assertServiceFailed(test);
+        assertEntityFailed(test);
     }
 
     @Test
@@ -208,7 +207,7 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
             Asserts.expectedFailureContains(t, "stderr contains mystderr");
         }
 
-        assertServiceFailed(test);
+        assertEntityFailed(test);
     }
 
     @Test
@@ -229,7 +228,7 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
             Asserts.expectedFailureContains(e, "exit code equals 1", "exit code equals 255");
         }
 
-        assertServiceFailed(test);
+        assertEntityFailed(test);
     }
 
     @Test
@@ -247,7 +246,7 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
 
             app.start(ImmutableList.<Location>of());
 
-            assertServiceHealthy(test);
+            assertEntityHealthy(test);
             assertThat(RecordingSshTool.getLastExecCmd().commands.toString()).contains("TestSshCommandTest-script");
 
         } finally {
@@ -270,7 +269,7 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
             Asserts.expectedFailureContains(e, "No instances of class "+SshMachineLocation.class.getName()+"
available");
         }
 
-        assertServiceFailed(test);
+        assertEntityFailed(test);
     }
     
     @Test
@@ -295,7 +294,7 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
             Asserts.assertTrue(elapsed.isShorterThan(longTimeout.subtract(Duration.millis(20))),
"elapsed="+elapsed);
         }
 
-        assertServiceFailed(test);
+        assertEntityFailed(test);
     }
     
     @Test
@@ -309,7 +308,7 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
 
         app.start(ImmutableList.<Location>of());
 
-        assertServiceHealthy(test);
+        assertEntityHealthy(test);
         
         ExecCmd cmdExecuted = RecordingSshTool.getLastExecCmd();
         assertThat(cmdExecuted.commands).isEqualTo(ImmutableList.of("mycmd"));
@@ -326,20 +325,6 @@ public class TestSshCommandTest extends BrooklynAppUnitTestSupport {
         }
     }
     
-    private void assertServiceFailed(TestSshCommand test) {
-        assertThat(test.sensors().get(SERVICE_UP)).isFalse()
-            .withFailMessage("Service should be down");
-        assertThat(ServiceStateLogic.getExpectedState(test)).isEqualTo(Lifecycle.ON_FIRE)
-            .withFailMessage("Service should be marked on fire");
-    }
-
-    private void assertServiceHealthy(TestSshCommand test) {
-        assertThat(test.sensors().get(SERVICE_UP)).isTrue()
-            .withFailMessage("Service should be up");
-        assertThat(ServiceStateLogic.getExpectedState(test)).isEqualTo(Lifecycle.RUNNING)
-            .withFailMessage("Service should be marked running");
-    }
-
     private List<Map<String, ?>> makeAssertions(Map<String, ?> map) {
         return ImmutableList.<Map<String, ?>>of(map);
     }


Mime
View raw message