brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjcorb...@apache.org
Subject [2/3] incubator-brooklyn git commit: Extracts AbstractBlueprintTest and marks MongoDbBlueprintTest as WIP
Date Fri, 19 Dec 2014 11:51:57 GMT
Extracts AbstractBlueprintTest and marks MongoDbBlueprintTest as WIP


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

Branch: refs/heads/master
Commit: 51ffe808258a964710776b820ddce29ec50c34e6
Parents: 02bb7c7
Author: Aled Sage <aled.sage@gmail.com>
Authored: Thu Dec 18 10:30:25 2014 +0000
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Thu Dec 18 10:30:54 2014 +0000

----------------------------------------------------------------------
 .../blueprints/AbstractBlueprintTest.java       | 193 +++++++++++++++++++
 .../blueprints/MongoDbBlueprintTest.java        |  77 ++------
 2 files changed, 208 insertions(+), 62 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/51ffe808/usage/launcher/src/test/java/brooklyn/launcher/blueprints/AbstractBlueprintTest.java
----------------------------------------------------------------------
diff --git a/usage/launcher/src/test/java/brooklyn/launcher/blueprints/AbstractBlueprintTest.java
b/usage/launcher/src/test/java/brooklyn/launcher/blueprints/AbstractBlueprintTest.java
new file mode 100644
index 0000000..e9df79c
--- /dev/null
+++ b/usage/launcher/src/test/java/brooklyn/launcher/blueprints/AbstractBlueprintTest.java
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package brooklyn.launcher.blueprints;
+
+import static org.testng.Assert.assertNotEquals;
+import io.brooklyn.camp.brooklyn.BrooklynCampPlatformLauncherAbstract;
+
+import java.io.File;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.Collection;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+
+import brooklyn.entity.Application;
+import brooklyn.entity.Entity;
+import brooklyn.entity.basic.Attributes;
+import brooklyn.entity.basic.Entities;
+import brooklyn.entity.basic.Lifecycle;
+import brooklyn.entity.basic.SoftwareProcess;
+import brooklyn.entity.rebind.RebindOptions;
+import brooklyn.entity.rebind.RebindTestUtils;
+import brooklyn.entity.rebind.persister.FileBasedObjectStore;
+import brooklyn.launcher.BrooklynLauncher;
+import brooklyn.launcher.SimpleYamlLauncherForTests;
+import brooklyn.launcher.camp.BrooklynCampPlatformLauncher;
+import brooklyn.management.ManagementContext;
+import brooklyn.management.internal.LocalManagementContext;
+import brooklyn.test.Asserts;
+import brooklyn.test.EntityTestUtils;
+import brooklyn.util.ResourceUtils;
+import brooklyn.util.os.Os;
+
+public abstract class AbstractBlueprintTest {
+
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractBlueprintTest.class);
+    
+    private File mementoDir;
+    private ClassLoader classLoader = AbstractBlueprintTest.class.getClassLoader();
+    
+    private ManagementContext mgmt;
+    private SimpleYamlLauncherForTests launcher;
+    private BrooklynLauncher viewer;
+
+    @BeforeMethod(alwaysRun=true)
+    public void setUp() throws Exception {
+        mementoDir = Os.newTempDir(getClass());
+        mgmt = createOrigManagementContext();
+        LOG.info("Test "+getClass()+" persisting to "+mementoDir);
+
+        launcher = new SimpleYamlLauncherForTests() {
+            @Override
+            protected BrooklynCampPlatformLauncherAbstract newPlatformLauncher() {
+                return new BrooklynCampPlatformLauncher() {
+                    protected ManagementContext newManagementContext() {
+                        return AbstractBlueprintTest.this.mgmt;
+                    }
+                };
+            }
+        };
+        viewer = BrooklynLauncher.newInstance()
+                .managementContext(mgmt)
+                .start();
+    }
+
+    @AfterMethod(alwaysRun=true)
+    public void tearDown() throws Exception {
+        try {
+            if (mgmt != null) {
+                for (Application app: mgmt.getApplications()) {
+                    LOG.debug("destroying app "+app+" (managed? "+Entities.isManaged(app)+";
mgmt is "+mgmt+")");
+                    try {
+                        Entities.destroy(app);
+                        LOG.debug("destroyed app "+app+"; mgmt now "+mgmt);
+                    } catch (Exception e) {
+                        LOG.error("problems destroying app "+app, e);
+                    }
+                }
+            }
+            if (launcher != null) launcher.destroyAll();
+            if (viewer != null) viewer.terminate();
+            if (mgmt != null) Entities.destroyAll(mgmt);
+            if (mementoDir != null) FileBasedObjectStore.deleteCompletely(mementoDir);
+        } catch (Throwable t) {
+            LOG.error("Caught exception in tearDown method", t);
+        } finally {
+            mgmt = null;
+            launcher = null;
+        }
+    }
+
+    protected void runTest(String yamlFile) throws Exception {
+        final Application app = launcher.launchAppYaml(yamlFile);
+        
+        assertNoFires(app);
+        
+        Application newApp = rebind();
+        assertNoFires(newApp);
+    }
+    
+    protected void assertNoFires(final Entity app) {
+        EntityTestUtils.assertAttributeEqualsEventually(app, Attributes.SERVICE_UP, true);
+        EntityTestUtils.assertAttributeEqualsEventually(app, Attributes.SERVICE_STATE_ACTUAL,
Lifecycle.RUNNING);
+        
+        Asserts.succeedsEventually(new Runnable() {
+            public void run() {
+                for (Entity entity : Entities.descendants(app)) {
+                    assertNotEquals(entity.getAttribute(Attributes.SERVICE_STATE_ACTUAL),
Lifecycle.ON_FIRE);
+                    assertNotEquals(entity.getAttribute(Attributes.SERVICE_UP), false);
+                    
+                    if (entity instanceof SoftwareProcess) {
+                        EntityTestUtils.assertAttributeEquals(entity, Attributes.SERVICE_STATE_ACTUAL,
Lifecycle.RUNNING);
+                        EntityTestUtils.assertAttributeEquals(entity, Attributes.SERVICE_UP,
Boolean.TRUE);
+                    }
+                }
+            }});
+    }
+
+    protected Reader loadYaml(String url, String location) {
+        String yaml = 
+                "location: "+location+"\n"+
+                new ResourceUtils(this).getResourceAsString(url);
+        return new StringReader(yaml);
+    }
+    
+    
+    //////////////////////////////////////////////////////////////////
+    // FOR REBIND                                                   //
+    // See brooklyn.entity.rebind.RebindTestFixture in core's tests //
+    //////////////////////////////////////////////////////////////////
+
+    /** rebinds, and sets newApp */
+    protected Application rebind() throws Exception {
+        return rebind(RebindOptions.create());
+    }
+
+    protected Application rebind(RebindOptions options) throws Exception {
+        ManagementContext origMgmt = mgmt;
+        ManagementContext newMgmt = createNewManagementContext();
+        Collection<Application> origApps = origMgmt.getApplications();
+        
+        options = RebindOptions.create(options);
+        if (options.classLoader == null) options.classLoader(classLoader);
+        if (options.mementoDir == null) options.mementoDir(mementoDir);
+        if (options.origManagementContext == null) options.origManagementContext(origMgmt);
+        if (options.newManagementContext == null) options.newManagementContext(newMgmt);
+        
+        for (Application origApp : origApps) {
+            RebindTestUtils.waitForPersisted(origApp);
+        }
+        
+        mgmt = options.newManagementContext;
+        Application newApp = RebindTestUtils.rebind(options);
+        return newApp;
+    }
+    
+    /** @return A started management context */
+    protected LocalManagementContext createOrigManagementContext() {
+        return RebindTestUtils.managementContextBuilder(mementoDir, classLoader)
+                .persistPeriodMillis(1)
+                .forLive(true)
+                .emptyCatalog(true)
+                .buildStarted();
+    }
+
+    /** @return An unstarted management context */
+    protected LocalManagementContext createNewManagementContext() {
+        return RebindTestUtils.managementContextBuilder(mementoDir, classLoader)
+                .persistPeriodMillis(1)
+                .forLive(true)
+                .emptyCatalog(true)
+                .buildUnstarted();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/51ffe808/usage/launcher/src/test/java/brooklyn/launcher/blueprints/MongoDbBlueprintTest.java
----------------------------------------------------------------------
diff --git a/usage/launcher/src/test/java/brooklyn/launcher/blueprints/MongoDbBlueprintTest.java
b/usage/launcher/src/test/java/brooklyn/launcher/blueprints/MongoDbBlueprintTest.java
index c3e6d18..53dd416 100644
--- a/usage/launcher/src/test/java/brooklyn/launcher/blueprints/MongoDbBlueprintTest.java
+++ b/usage/launcher/src/test/java/brooklyn/launcher/blueprints/MongoDbBlueprintTest.java
@@ -18,81 +18,34 @@
  */
 package brooklyn.launcher.blueprints;
 
-import static org.testng.Assert.assertNotEquals;
-
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
-import brooklyn.entity.Application;
-import brooklyn.entity.Entity;
-import brooklyn.entity.basic.Attributes;
-import brooklyn.entity.basic.Entities;
-import brooklyn.entity.basic.Lifecycle;
-import brooklyn.launcher.BrooklynLauncher;
-import brooklyn.launcher.SimpleYamlLauncherForTests;
-import brooklyn.test.Asserts;
-import brooklyn.test.EntityTestUtils;
-
-public class MongoDbBlueprintTest {
+public class MongoDbBlueprintTest extends AbstractBlueprintTest {
 
     // TODO Some tests are failing! Needs investigated.
     
-    private SimpleYamlLauncherForTests launcher;
-    
-    // The "viewer" is just for having a Brooklyn web-console available for visual inspection;
-    // could be removed without affecting behaviour
-    private BrooklynLauncher viewer;
-    
-    @BeforeMethod(alwaysRun=true)
-    public void setUp() throws Exception {
-        launcher = new SimpleYamlLauncherForTests();
-        viewer = BrooklynLauncher.newInstance().managementContext(launcher.getManagementContext()).start();
-    }
-    
-    @AfterMethod(alwaysRun=true)
-    public void tearDown() throws Exception {
-        if (launcher != null) launcher.destroyAll();
-        if (viewer != null) viewer.terminate();
-    }
-    
-    @Test(groups="Integration")
-    public void testMongoSharded() {
-        runAppAndAssertNoFires("mongo-sharded.yaml");
+    @Test(groups={"Integration", "WIP"})
+    public void testMongoSharded() throws Exception {
+        runTest("mongo-sharded.yaml");
     }
 
-    @Test(groups="Integration")
-    public void testMongoReplicaSet() {
-        runAppAndAssertNoFires("mongo-blueprint.yaml");
+    @Test(groups={"Integration", "WIP"})
+    public void testMongoReplicaSet() throws Exception {
+        runTest("mongo-blueprint.yaml");
     }
 
-    @Test(groups="Integration")
-    public void testMongoClientAndSingleServer() {
-        runAppAndAssertNoFires("mongo-client-single-server.yaml");
+    @Test(groups={"Integration", "WIP"})
+    public void testMongoClientAndSingleServer() throws Exception {
+        runTest("mongo-client-single-server.yaml");
     }
 
-    @Test(groups="Integration")
-    public void testMongoScripts() {
-        runAppAndAssertNoFires("mongo-scripts.yaml");
+    @Test(groups={"Integration", "WIP"})
+    public void testMongoScripts() throws Exception {
+        runTest("mongo-scripts.yaml");
     }
 
     @Test(groups="Integration")
-    public void testMongoSingleServer() {
-        runAppAndAssertNoFires("mongo-single-server-blueprint.yaml");
-    }
-
-    protected void runAppAndAssertNoFires(String yamlFile) {
-        final Application app = launcher.launchAppYaml(yamlFile);
-
-        EntityTestUtils.assertAttributeEqualsEventually(app, Attributes.SERVICE_UP, true);
-        EntityTestUtils.assertAttributeEqualsEventually(app, Attributes.SERVICE_STATE_ACTUAL,
Lifecycle.RUNNING);
-        
-        Asserts.succeedsEventually(new Runnable() {
-            public void run() {
-                for (Entity entity : Entities.descendants(app)) {
-                    assertNotEquals(entity.getAttribute(Attributes.SERVICE_STATE_ACTUAL),
Lifecycle.ON_FIRE);
-                    assertNotEquals(entity.getAttribute(Attributes.SERVICE_UP), false);
-                }
-            }});
+    public void testMongoSingleServer() throws Exception {
+        runTest("mongo-single-server-blueprint.yaml");
     }
 }


Mime
View raw message