brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From drigod...@apache.org
Subject [2/3] brooklyn-server git commit: BROOKLYN-490: propagate failure in LoopOverGroupMembersTestCase
Date Fri, 26 May 2017 07:54:36 GMT
BROOKLYN-490: propagate failure in LoopOverGroupMembersTestCase

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

Branch: refs/heads/master
Commit: 730241e015ce339637942626b84136268342503a
Parents: 0356345
Author: Aled Sage <aled.sage@gmail.com>
Authored: Thu May 25 17:13:19 2017 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Thu May 25 17:13:19 2017 +0100

----------------------------------------------------------------------
 .../framework/LoopOverGroupMembersTestCase.java |  1 +
 .../LoopOverGroupMembersTestCaseImpl.java       | 33 ++++++++---------
 .../LoopOverGroupMembersTestCaseTest.java       | 38 ++++++++++++++++++--
 3 files changed, 50 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/730241e0/test-framework/src/main/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCase.java
----------------------------------------------------------------------
diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCase.java
b/test-framework/src/main/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCase.java
index 4b2b0a3..46372f2 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCase.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCase.java
@@ -33,6 +33,7 @@ public interface LoopOverGroupMembersTestCase extends TargetableTestComponent
{
      * The test spec that will be run against each member of the group
      */
     @SetFromFlag("testSpec")
+    @SuppressWarnings("serial")
     ConfigKey<EntitySpec<? extends TargetableTestComponent>> TEST_SPEC = ConfigKeys.newConfigKey(
             new TypeToken<EntitySpec<? extends TargetableTestComponent>>(){},
             "test.spec",

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/730241e0/test-framework/src/main/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseImpl.java
----------------------------------------------------------------------
diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseImpl.java
b/test-framework/src/main/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseImpl.java
index 46edf2e..d64b270 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseImpl.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseImpl.java
@@ -18,7 +18,9 @@
  */
 package org.apache.brooklyn.test.framework;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
 
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.entity.EntitySpec;
@@ -47,26 +49,27 @@ public class LoopOverGroupMembersTestCaseImpl extends TargetableTestComponentImp
         Maybe<Entity> target = tryResolveTarget();
         if (!target.isPresent()) {
             logger.debug("Tasks NOT successfully run. LoopOverGroupMembersTestCaseImpl group
not set");
-            setServiceState(false, Lifecycle.ON_FIRE);
+            setUpAndRunState(false, Lifecycle.ON_FIRE);
             return;
         }
 
         if (!(target.get() instanceof Group)) {
             logger.debug("Tasks NOT successfully run. LoopOverGroupMembersTestCaseImpl target
is not a group");
-            setServiceState(false, Lifecycle.ON_FIRE);
+            setUpAndRunState(false, Lifecycle.ON_FIRE);
             return;
         }
 
         EntitySpec<? extends TargetableTestComponent> testSpec = config().get(TEST_SPEC);
         if (testSpec == null) {
             logger.debug("Tasks NOT successfully run. LoopOverGroupMembersTestCaseImpl test
spec not set");
-            setServiceState(false, Lifecycle.ON_FIRE);
+            setUpAndRunState(false, Lifecycle.ON_FIRE);
             return;
         }
 
         // Create the child-assertions (one per group-member)
         Group group = (Group) target.get();
         Collection<Entity> members = group.getMembers();
+        List<Throwable> exceptions = new ArrayList<>();
         boolean allSuccesful = true;
         for (Entity member : members) {
             EntitySpec<? extends TargetableTestComponent> testSpecCopy = EntitySpec.create(testSpec)
@@ -84,6 +87,7 @@ public class LoopOverGroupMembersTestCaseImpl extends TargetableTestComponentImp
             } catch (Throwable t) {
                 Exceptions.propagateIfFatal(t);
                 logger.warn("Problem in child test-case of "+this+", targetting "+member,
t);
+                exceptions.add(t);
                 allSuccesful = false;
             }
         }
@@ -91,11 +95,13 @@ public class LoopOverGroupMembersTestCaseImpl extends TargetableTestComponentImp
         if (allSuccesful) {
             // Let everyone know we've started up successfully (changes the icon in the GUI).
             logger.debug("Tasks successfully run. Update state of {} to RUNNING.", this);
-            setServiceState(true, Lifecycle.RUNNING);
+            setUpAndRunState(true, Lifecycle.RUNNING);
         } else {
-            // Let everyone know we've npt started up successfully (changes the icon in the
GUI).
+            // Let everyone know we've not started up successfully (changes the icon in the
GUI).
+            // Important to fail the start() method, so that parent TestCase entity also
reports failure.
             logger.debug("Tasks NOT successfully run. Update state of {} to ON_FIRE.", this);
-            setServiceState(false, Lifecycle.ON_FIRE);
+            setUpAndRunState(false, Lifecycle.ON_FIRE);
+            throw Exceptions.propagate("Test failed on group member(s)", exceptions);
         }
 
     }
@@ -112,10 +118,10 @@ public class LoopOverGroupMembersTestCaseImpl extends TargetableTestComponentImp
 
             // Let everyone know we've stopped successfully (changes the icon in the GUI).
             logger.debug("Tasks successfully run. Update state of {} to STOPPED.", this);
-            setServiceState(false, Lifecycle.STOPPED);
+            setUpAndRunState(false, Lifecycle.STOPPED);
         } catch (Throwable t) {
             logger.debug("Tasks NOT successfully run. Update state of {} to ON_FIRE.", this);
-            setServiceState(false, Lifecycle.ON_FIRE);
+            setUpAndRunState(false, Lifecycle.ON_FIRE);
             throw Exceptions.propagate(t);
         }
     }
@@ -126,15 +132,4 @@ public class LoopOverGroupMembersTestCaseImpl extends TargetableTestComponentImp
         stop();
         start(locations);
     }
-
-    /**
-     * Sets the state of the Entity. Useful so that the GUI shows the correct icon.
-     *
-     * @param serviceUpState     Whether or not the entity is up.
-     * @param serviceStateActual The actual state of the entity.
-     */
-    private void setServiceState(final boolean serviceUpState, final Lifecycle serviceStateActual)
{
-        sensors().set(SERVICE_UP, serviceUpState);
-        sensors().set(Attributes.SERVICE_STATE_ACTUAL, serviceStateActual);
-    }
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/730241e0/test-framework/src/test/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseTest.java
----------------------------------------------------------------------
diff --git a/test-framework/src/test/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseTest.java
b/test-framework/src/test/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseTest.java
index 198ad26..261ded1 100644
--- a/test-framework/src/test/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseTest.java
+++ b/test-framework/src/test/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseTest.java
@@ -30,12 +30,14 @@ import java.util.Set;
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.entity.EntitySpec;
 import org.apache.brooklyn.api.entity.Group;
+import org.apache.brooklyn.api.location.Location;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.entity.Entities;
 import org.apache.brooklyn.core.sensor.AttributeSensorAndConfigKey;
 import org.apache.brooklyn.core.test.entity.TestApplication;
 import org.apache.brooklyn.entity.group.DynamicGroup;
 import org.apache.brooklyn.entity.software.base.EmptySoftwareProcess;
+import org.apache.brooklyn.test.Asserts;
 import org.apache.brooklyn.util.collections.MutableSet;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
@@ -43,6 +45,7 @@ import org.testng.annotations.Test;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
 
 public class LoopOverGroupMembersTestCaseTest {
 
@@ -115,7 +118,7 @@ public class LoopOverGroupMembersTestCaseTest {
                 .configure(LoopOverGroupMembersTestCase.TEST_SPEC, testSpec)
                 .configure(LoopOverGroupMembersTestCase.TARGET_ENTITY, testGroup));
 
-        app.start(ImmutableList.of(app.newSimulatedLocation()));
+        startAppAssertingFailure(app, app.newSimulatedLocation());
 
         assertThat(loopOverGroupMembersTestCase.getChildren().size()).isEqualTo(4);
         assertThat(loopOverGroupMembersTestCase.sensors().get(SERVICE_UP)).isFalse();
@@ -140,7 +143,7 @@ public class LoopOverGroupMembersTestCaseTest {
                 .configure(LoopOverGroupMembersTestCase.TEST_SPEC, testSpec)
                 .configure(LoopOverGroupMembersTestCase.TARGET_ENTITY, testGroup));
 
-        app.start(ImmutableList.of(app.newSimulatedLocation()));
+        startAppAssertingFailure(app, app.newSimulatedLocation());
 
         assertThat(loopOverGroupMembersTestCase.getChildren().size()).isEqualTo(4);
         assertThat(loopOverGroupMembersTestCase.sensors().get(SERVICE_UP)).isFalse();
@@ -170,7 +173,7 @@ public class LoopOverGroupMembersTestCaseTest {
                 .configure(LoopOverGroupMembersTestCase.TEST_SPEC, testSpec)
                 .configure(LoopOverGroupMembersTestCase.TARGET_ENTITY, testGroup));
 
-        app.start(ImmutableList.of(app.newSimulatedLocation()));
+        startAppAssertingFailure(app, app.newSimulatedLocation());
 
         assertThat(loopOverGroupMembersTestCase.getChildren().size()).isEqualTo(1);
         assertThat(loopOverGroupMembersTestCase.sensors().get(SERVICE_UP)).isFalse();
@@ -181,6 +184,27 @@ public class LoopOverGroupMembersTestCaseTest {
         assertThat(loopChildEntity.config().get(LoopOverGroupMembersTestCase.TARGET_ENTITY)).isEqualTo(emptySoftwareProcess);
     }
 
+    // See https://issues.apache.org/jira/browse/BROOKLYN-490
+    @Test
+    public void testParentTestCaseReportsFailureIfLooperFails() {
+        addEmptySoftwareProcessToGroup();
+        EntitySpec<TestSensor> testSpec = createFailingTestSensorSpec();
+
+        TestCase parentTest = app.addChild(EntitySpec.create(TestCase.class)
+                .child(EntitySpec.create(LoopOverGroupMembersTestCase.class)
+                        .configure(LoopOverGroupMembersTestCase.TEST_SPEC, testSpec)
+                        .configure(LoopOverGroupMembersTestCase.TARGET_ENTITY, testGroup)));
+
+        LoopOverGroupMembersTestCase loopOverGroupMembersTestCase = (LoopOverGroupMembersTestCase)
Iterables.getOnlyElement(parentTest.getChildren());
+        
+        startAppAssertingFailure(app, app.newSimulatedLocation());
+        Entity loopChildEntity = Iterables.getOnlyElement(loopOverGroupMembersTestCase.getChildren());
+
+        assertThat(loopOverGroupMembersTestCase.sensors().get(SERVICE_UP)).isFalse();
+        assertThat(loopChildEntity.sensors().get(SERVICE_UP)).isFalse();
+        assertThat(parentTest.sensors().get(SERVICE_UP)).isFalse();
+    }
+
     //negative
     // without test spec
     // without target + taget id
@@ -265,4 +289,12 @@ public class LoopOverGroupMembersTestCaseTest {
         return emptySoftwareProcess;
     }
 
+    private void startAppAssertingFailure(TestApplication app, Location loc) {
+        try {
+            app.start(ImmutableList.of(loc));
+            Asserts.shouldHaveFailedPreviously();
+        } catch (Throwable t) {
+            Asserts.expectedFailureContains(t, "Test failed on group member");
+        }
+    }
 }


Mime
View raw message