You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by sj...@apache.org on 2015/06/12 13:04:33 UTC

[1/3] incubator-brooklyn git commit: Simplify JcloudsLocationTest

Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 5ceb8aadb -> 681b8fe90


Simplify JcloudsLocationTest


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

Branch: refs/heads/master
Commit: 9645e8c34f23d99c545b6ed56b73943062968937
Parents: 284b763
Author: Sam Corbett <sa...@cloudsoftcorp.com>
Authored: Fri Jun 5 14:46:29 2015 +0100
Committer: Sam Corbett <sa...@cloudsoftcorp.com>
Committed: Thu Jun 11 15:03:12 2015 +0100

----------------------------------------------------------------------
 .../jclouds/BailOutJcloudsLocation.java         | 187 ++++++++++++
 .../location/jclouds/JcloudsLocationTest.java   | 299 +++++--------------
 2 files changed, 266 insertions(+), 220 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9645e8c3/locations/jclouds/src/test/java/brooklyn/location/jclouds/BailOutJcloudsLocation.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/test/java/brooklyn/location/jclouds/BailOutJcloudsLocation.java b/locations/jclouds/src/test/java/brooklyn/location/jclouds/BailOutJcloudsLocation.java
new file mode 100644
index 0000000..ba5dc2b
--- /dev/null
+++ b/locations/jclouds/src/test/java/brooklyn/location/jclouds/BailOutJcloudsLocation.java
@@ -0,0 +1,187 @@
+/*
+ * 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.location.jclouds;
+
+import java.util.Collections;
+import java.util.Map;
+import javax.annotation.Nullable;
+
+import org.jclouds.compute.ComputeService;
+import org.jclouds.compute.domain.Image;
+import org.jclouds.compute.domain.Template;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+import com.google.common.reflect.TypeToken;
+
+import brooklyn.config.BrooklynProperties;
+import brooklyn.config.ConfigKey;
+import brooklyn.entity.basic.ConfigKeys;
+import brooklyn.location.LocationSpec;
+import brooklyn.management.ManagementContext;
+import brooklyn.management.internal.LocalManagementContext;
+import brooklyn.util.collections.MutableMap;
+import brooklyn.util.config.ConfigBag;
+import brooklyn.util.exceptions.CompoundRuntimeException;
+import brooklyn.util.exceptions.Exceptions;
+
+public class BailOutJcloudsLocation extends JcloudsLocation {
+
+    // Don't care which image; not actually provisioning
+    private static final String US_EAST_IMAGE_ID = "us-east-1/ami-7d7bfc14";
+    public static final RuntimeException BAIL_OUT_FOR_TESTING = new RuntimeException("early termination for test");
+
+    public static final ConfigKey<Function<ConfigBag, Void>> BUILD_TEMPLATE_INTERCEPTOR = ConfigKeys.newConfigKey(
+            new TypeToken<Function<ConfigBag, Void>>() {},
+            "buildtemplateinterceptor");
+
+    public static final ConfigKey<Boolean> BUILD_TEMPLATE = ConfigKeys.newBooleanConfigKey(
+            "buildtemplate");
+
+    private static final long serialVersionUID = -3373789512935057842L;
+
+    ConfigBag lastConfigBag;
+    Template template;
+
+    public BailOutJcloudsLocation() {
+        super();
+    }
+
+    public BailOutJcloudsLocation(Map<?, ?> conf) {
+        super(conf);
+    }
+
+    @Override
+    public Template buildTemplate(ComputeService computeService, ConfigBag config) {
+        lastConfigBag = config;
+        if (getConfig(BUILD_TEMPLATE_INTERCEPTOR) != null) {
+            getConfig(BUILD_TEMPLATE_INTERCEPTOR).apply(config);
+        }
+        if (Boolean.TRUE.equals(getConfig(BUILD_TEMPLATE))) {
+            template = super.buildTemplate(computeService, config);
+        }
+        throw BAIL_OUT_FOR_TESTING;
+    }
+
+    public Template getTemplate() {
+        return template;
+    }
+
+    public void tryObtain() {
+        tryObtain(Collections.emptyMap());
+    }
+
+    public void tryObtain(Map<?, ?> flags) {
+        tryObtainAndCheck(flags, Predicates.alwaysTrue());
+    }
+
+    public void tryObtainAndCheck(Map<?, ?> flags, Predicate<? super ConfigBag> test) {
+        try {
+            obtain(flags);
+        } catch (Exception e) {
+            if (e == BAIL_OUT_FOR_TESTING || e.getCause() == BAIL_OUT_FOR_TESTING
+                    || (e instanceof CompoundRuntimeException && ((CompoundRuntimeException) e).getAllCauses().contains(BAIL_OUT_FOR_TESTING))) {
+                test.apply(lastConfigBag);
+            } else {
+                throw Exceptions.propagate(e);
+            }
+        }
+    }
+
+    @Override
+    @VisibleForTesting
+    public UserCreation createUserStatements(@Nullable Image image, ConfigBag config) {
+        return super.createUserStatements(image, config);
+    }
+
+
+    public static BailOutJcloudsLocation newBailOutJcloudsLocation(ManagementContext mgmt) {
+        return newBailOutJcloudsLocation(mgmt, Collections.<ConfigKey<?>, Object>emptyMap());
+    }
+
+    public static BailOutJcloudsLocation newBailOutJcloudsLocation(ManagementContext mgmt, Map<ConfigKey<?>, ?> config) {
+        Map<ConfigKey<?>, ?> allConfig = MutableMap.<ConfigKey<?>, Object>builder()
+                .put(IMAGE_ID, "bogus")
+                .put(CLOUD_PROVIDER, "aws-ec2")
+                .put(ACCESS_IDENTITY, "bogus")
+                .put(CLOUD_REGION_ID, "bogus")
+                .put(ACCESS_CREDENTIAL, "bogus")
+                .put(USER, "fred")
+                .put(MIN_RAM, 16)
+                .put(JcloudsLocation.MACHINE_CREATE_ATTEMPTS, 1)
+                .putAll(config)
+                .build();
+        return mgmt.getLocationManager().createLocation(
+                LocationSpec.create(BailOutJcloudsLocation.class).configure(allConfig));
+    }
+
+
+    // todo better name
+
+    /** As {@link BailOutJcloudsLocation}, counting the number of {@link #buildTemplate} calls. */
+    public static class CountingBailOutJcloudsLocation extends BailOutJcloudsLocation {
+        private static final long serialVersionUID = 2433684033045735773L;
+        int buildTemplateCount = 0;
+        @Override
+        public Template buildTemplate(ComputeService computeService, ConfigBag config) {
+            buildTemplateCount++;
+            return super.buildTemplate(computeService, config);
+        }
+    }
+
+    public static CountingBailOutJcloudsLocation newCountingBailOutJcloudsLocation(ManagementContext mgmt, Map flags) {
+        LocationSpec<CountingBailOutJcloudsLocation> spec = LocationSpec.create(CountingBailOutJcloudsLocation.class)
+                .configure(flags);
+        return mgmt.getLocationManager().createLocation(spec);
+    }
+
+    /** @see #newBailOutJcloudsLocationForLiveTest(LocalManagementContext, Map)} */
+    public static BailOutJcloudsLocation newBailOutJcloudsLocationForLiveTest(LocalManagementContext mgmt) {
+        return newBailOutJcloudsLocationForLiveTest(mgmt, Collections.<ConfigKey<?>, Object>emptyMap());
+    }
+
+    /**
+     * Takes identity and access credential from management context's Brooklyn properties and sets
+     * inbound ports to [22, 80, 9999].
+     */
+    public static BailOutJcloudsLocation newBailOutJcloudsLocationForLiveTest(LocalManagementContext mgmt, Map<ConfigKey<?>, ?> config) {
+        BrooklynProperties brooklynProperties = mgmt.getBrooklynProperties();
+        String identity = (String) brooklynProperties.get("brooklyn.location.jclouds.aws-ec2.identity");
+        if (identity == null) identity = (String) brooklynProperties.get("brooklyn.jclouds.aws-ec2.identity");
+        String credential = (String) brooklynProperties.get("brooklyn.location.jclouds.aws-ec2.credential");
+        if (credential == null) credential = (String) brooklynProperties.get("brooklyn.jclouds.aws-ec2.credential");
+
+        Map<ConfigKey<?>, ?> allConfig = MutableMap.<ConfigKey<?>, Object>builder()
+                .put(CLOUD_PROVIDER, AbstractJcloudsLiveTest.AWS_EC2_PROVIDER)
+                .put(CLOUD_REGION_ID, AbstractJcloudsLiveTest.AWS_EC2_USEAST_REGION_NAME)
+                .put(IMAGE_ID, US_EAST_IMAGE_ID) // so Brooklyn does not attempt to load all EC2 images
+                .put(ACCESS_IDENTITY, identity)
+                .put(ACCESS_CREDENTIAL, credential)
+                .put(INBOUND_PORTS, "[22, 80, 9999]")
+                .put(BUILD_TEMPLATE, true)
+                .putAll(config)
+                .build();
+
+        return newBailOutJcloudsLocation(mgmt, allConfig);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9645e8c3/locations/jclouds/src/test/java/brooklyn/location/jclouds/JcloudsLocationTest.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/test/java/brooklyn/location/jclouds/JcloudsLocationTest.java b/locations/jclouds/src/test/java/brooklyn/location/jclouds/JcloudsLocationTest.java
index 62817c2..4b74d84 100644
--- a/locations/jclouds/src/test/java/brooklyn/location/jclouds/JcloudsLocationTest.java
+++ b/locations/jclouds/src/test/java/brooklyn/location/jclouds/JcloudsLocationTest.java
@@ -24,12 +24,8 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
-
 import javax.annotation.Nullable;
 
-import org.jclouds.compute.ComputeService;
-import org.jclouds.compute.domain.Image;
-import org.jclouds.compute.domain.Template;
 import org.jclouds.scriptbuilder.domain.OsFamily;
 import org.jclouds.scriptbuilder.domain.StatementList;
 import org.mockito.Mockito;
@@ -40,6 +36,12 @@ import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.reflect.TypeToken;
+
 import brooklyn.config.BrooklynProperties;
 import brooklyn.config.ConfigKey;
 import brooklyn.entity.basic.ConfigKeys;
@@ -56,18 +58,8 @@ import brooklyn.test.Asserts;
 import brooklyn.test.entity.LocalManagementContextForTests;
 import brooklyn.util.collections.MutableMap;
 import brooklyn.util.config.ConfigBag;
-import brooklyn.util.exceptions.CompoundRuntimeException;
 import brooklyn.util.exceptions.Exceptions;
 
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Function;
-import com.google.common.base.Predicate;
-import com.google.common.base.Predicates;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
-import com.google.common.reflect.TypeToken;
-
 /**
  * @author Shane Witbeck
  */
@@ -75,145 +67,6 @@ public class JcloudsLocationTest implements JcloudsLocationConfig {
 
     private static final Logger log = LoggerFactory.getLogger(JcloudsLocationTest.class);
     
-    // Don't care which image; not actually provisioning
-    private static final String US_EAST_IMAGE_ID = "us-east-1/ami-7d7bfc14";
-    
-    public static final RuntimeException BAIL_OUT_FOR_TESTING = 
-            new RuntimeException("early termination for test");
-    
-    @SuppressWarnings("serial")
-    public static class BailOutJcloudsLocation extends JcloudsLocation {
-        public static final ConfigKey<Function<ConfigBag,Void>> BUILD_TEMPLATE_INTERCEPTOR = ConfigKeys.newConfigKey(new TypeToken<Function<ConfigBag,Void>>() {}, "buildtemplateinterceptor");
-        
-        ConfigBag lastConfigBag;
-
-        public BailOutJcloudsLocation() {
-            super();
-        }
-
-        public BailOutJcloudsLocation(Map<?, ?> conf) {
-            super(conf);
-        }
-
-        @Override
-        public Template buildTemplate(ComputeService computeService, ConfigBag config) {
-            lastConfigBag = config;
-            if (getConfig(BUILD_TEMPLATE_INTERCEPTOR) != null) getConfig(BUILD_TEMPLATE_INTERCEPTOR).apply(config);
-            throw BAIL_OUT_FOR_TESTING;
-        }
-        protected void tryObtainAndCheck(Map<?,?> flags, Predicate<? super ConfigBag> test) {
-            try {
-                obtain(flags);
-            } catch (Exception e) {
-                if (e==BAIL_OUT_FOR_TESTING || e.getCause()==BAIL_OUT_FOR_TESTING 
-                        || (e instanceof CompoundRuntimeException && ((CompoundRuntimeException)e).getAllCauses().contains(BAIL_OUT_FOR_TESTING))) {
-                    test.apply(lastConfigBag);
-                } else {
-                    throw Exceptions.propagate(e);
-                }
-            }
-        }
-        @Override @VisibleForTesting
-        public UserCreation createUserStatements(@Nullable Image image, ConfigBag config) {
-            return super.createUserStatements(image, config);
-        }
-    }
-
-    @SuppressWarnings("serial")
-    public static class CountingBailOutJcloudsLocation extends BailOutJcloudsLocation {
-        int buildTemplateCount = 0;
-        @Override
-        public Template buildTemplate(ComputeService computeService, ConfigBag config) {
-            buildTemplateCount++;
-            return super.buildTemplate(computeService, config);
-        }
-    }
-    
-    @SuppressWarnings("serial")
-    public static class BailOutWithTemplateJcloudsLocation extends JcloudsLocation {
-        ConfigBag lastConfigBag;
-        Template template;
-
-        public BailOutWithTemplateJcloudsLocation() {
-            super();
-        }
-
-        public BailOutWithTemplateJcloudsLocation(Map<?, ?> conf) {
-            super(conf);
-        }
-
-        @Override
-        public Template buildTemplate(ComputeService computeService, ConfigBag config) {
-            template = super.buildTemplate(computeService, config);
-
-            lastConfigBag = config;
-            throw BAIL_OUT_FOR_TESTING;
-        }
-
-        protected synchronized void tryObtainAndCheck(Map<?,?> flags, Predicate<ConfigBag> test) {
-            try {
-                obtain(flags);
-            } catch (Throwable e) {
-                if (e == BAIL_OUT_FOR_TESTING) {
-                    test.apply(lastConfigBag);
-                } else {
-                    throw Exceptions.propagate(e);
-                }
-            }
-        }
-         
-        public Template getTemplate() {
-            return template;
-        }
-    }
-    
-    protected BailOutJcloudsLocation newSampleBailOutJcloudsLocationForTesting() {
-        return newSampleBailOutJcloudsLocationForTesting(ImmutableMap.<ConfigKey<?>,Object>of());
-    }
-    
-    @SuppressWarnings({ "unchecked", "rawtypes" })
-    protected BailOutJcloudsLocation newSampleBailOutJcloudsLocationForTesting(Map<?,?> config) {
-        Map<ConfigKey<?>,?> allConfig = MutableMap.<ConfigKey<?>,Object>builder()
-                .put(IMAGE_ID, "bogus")
-                .put(CLOUD_PROVIDER, "aws-ec2")
-                .put(ACCESS_IDENTITY, "bogus")
-                .put(CLOUD_REGION_ID, "bogus")
-                .put(ACCESS_CREDENTIAL, "bogus")
-                .put(USER, "fred")
-                .put(MIN_RAM, 16)
-                .put(JcloudsLocation.MACHINE_CREATE_ATTEMPTS, 1)
-                .putAll((Map)config)
-                .build();
-        return managementContext.getLocationManager().createLocation(LocationSpec.create(BailOutJcloudsLocation.class)
-                .configure(allConfig));
-    }
-    
-    protected BailOutWithTemplateJcloudsLocation newSampleBailOutWithTemplateJcloudsLocation() {
-        return newSampleBailOutWithTemplateJcloudsLocation(ImmutableMap.<ConfigKey<?>,Object>of());
-    }
-
-    @SuppressWarnings({ "unchecked", "rawtypes" })
-    protected BailOutWithTemplateJcloudsLocation newSampleBailOutWithTemplateJcloudsLocation(Map<?,?> config) {
-        String identity = (String) brooklynProperties.get("brooklyn.location.jclouds.aws-ec2.identity");
-        if (identity == null) identity = (String) brooklynProperties.get("brooklyn.jclouds.aws-ec2.identity");
-        String credential = (String) brooklynProperties.get("brooklyn.location.jclouds.aws-ec2.credential");
-        if (credential == null) credential = (String) brooklynProperties.get("brooklyn.jclouds.aws-ec2.credential");
-        
-        Map<ConfigKey<?>,?> allConfig = MutableMap.<ConfigKey<?>,Object>builder()
-                .put(CLOUD_PROVIDER, AbstractJcloudsLiveTest.AWS_EC2_PROVIDER)
-                .put(CLOUD_REGION_ID, AbstractJcloudsLiveTest.AWS_EC2_USEAST_REGION_NAME)
-                .put(IMAGE_ID, US_EAST_IMAGE_ID) // so it runs faster, without loading all EC2 images
-                .put(ACCESS_IDENTITY, identity)
-                .put(ACCESS_CREDENTIAL, credential)
-                .put(USER, "fred")
-                .put(INBOUND_PORTS, "[22, 80, 9999]")
-                .putAll((Map)config)
-                .build();
-        
-        return managementContext.getLocationManager().createLocation(LocationSpec.create(BailOutWithTemplateJcloudsLocation.class)
-                .configure(allConfig));
-    }
-
     public static Predicate<ConfigBag> checkerFor(final String user, final Integer minRam, final Integer minCores) {
         return new Predicate<ConfigBag>() {
             @Override
@@ -236,13 +89,11 @@ public class JcloudsLocationTest implements JcloudsLocationConfig {
         };
     }
     
-    private BrooklynProperties brooklynProperties;
     private LocalManagementContext managementContext;
     
     @BeforeMethod(alwaysRun=true)
     public void setUp() throws Exception {
         managementContext = LocalManagementContextForTests.newInstance(BrooklynProperties.Factory.builderEmpty().build());
-        brooklynProperties = managementContext.getBrooklynProperties();
     }
     
     @AfterMethod(alwaysRun=true)
@@ -252,19 +103,19 @@ public class JcloudsLocationTest implements JcloudsLocationConfig {
     
     @Test
     public void testCreateWithFlagsDirectly() throws Exception {
-        BailOutJcloudsLocation jcl = newSampleBailOutJcloudsLocationForTesting();
+        BailOutJcloudsLocation jcl = BailOutJcloudsLocation.newBailOutJcloudsLocation(managementContext);
         jcl.tryObtainAndCheck(MutableMap.of(MIN_CORES, 2), checkerFor("fred", 16, 2));
     }
 
     @Test
     public void testCreateWithFlagsDirectlyAndOverride() throws Exception {
-        BailOutJcloudsLocation jcl = newSampleBailOutJcloudsLocationForTesting();
+        BailOutJcloudsLocation jcl = BailOutJcloudsLocation.newBailOutJcloudsLocation(managementContext);
         jcl.tryObtainAndCheck(MutableMap.of(MIN_CORES, 2, MIN_RAM, 8), checkerFor("fred", 8, 2));
     }
 
     @Test
     public void testCreateWithFlagsSubLocation() throws Exception {
-        BailOutJcloudsLocation jcl = newSampleBailOutJcloudsLocationForTesting();
+        BailOutJcloudsLocation jcl = BailOutJcloudsLocation.newBailOutJcloudsLocation(managementContext);
         jcl = (BailOutJcloudsLocation) jcl.newSubLocation(MutableMap.of(USER, "jon", MIN_CORES, 2));
         jcl.tryObtainAndCheck(MutableMap.of(MIN_CORES, 3), checkerFor("jon", 16, 3));
     }
@@ -310,37 +161,36 @@ public class JcloudsLocationTest implements JcloudsLocationConfig {
 
     @Test
     public void testVMCreationIsRetriedOnFailure() {
-        Map<ConfigKey<?>, Object> flags = Maps.newHashMap();
-        flags.put(IMAGE_ID, "bogus");
-        flags.put(CLOUD_PROVIDER, "aws-ec2");
-        flags.put(ACCESS_IDENTITY, "bogus");
-        flags.put(CLOUD_REGION_ID, "bogus");
-        flags.put(ACCESS_CREDENTIAL, "bogus");
-        flags.put(USER, "fred");
-        flags.put(MIN_RAM, 16);
-        flags.put(MACHINE_CREATE_ATTEMPTS, 3);
-        CountingBailOutJcloudsLocation jcl = managementContext.getLocationManager().createLocation(
-                LocationSpec.create(CountingBailOutJcloudsLocation.class).configure(flags));
-        jcl.tryObtainAndCheck(ImmutableMap.of(), Predicates.<ConfigBag>alwaysTrue());
-        Assert.assertEquals(jcl.buildTemplateCount, 3);
+        final AtomicInteger count = new AtomicInteger();
+        Function<ConfigBag, Void> countingInterceptor = new Function<ConfigBag, Void>() {
+            @Override public Void apply(ConfigBag input) {
+                count.incrementAndGet();
+                return null;
+            }
+        };
+        BailOutJcloudsLocation loc = BailOutJcloudsLocation.newBailOutJcloudsLocation(managementContext, ImmutableMap.<ConfigKey<?>, Object>of(
+                MACHINE_CREATE_ATTEMPTS, 3,
+                BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, countingInterceptor));
+        loc.tryObtain();
+        Assert.assertEquals(count.get(), 3);
     }
 
     @Test(groups={"Live", "Live-sanity"})
     public void testCreateWithInboundPorts() {
-        BailOutWithTemplateJcloudsLocation jcloudsLocation = newSampleBailOutWithTemplateJcloudsLocation();
-        jcloudsLocation = (BailOutWithTemplateJcloudsLocation) jcloudsLocation.newSubLocation(MutableMap.of());
+        BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocationForLiveTest(managementContext);
+        jcloudsLocation = (BailOutJcloudsLocation) jcloudsLocation.newSubLocation(MutableMap.of());
         jcloudsLocation.tryObtainAndCheck(MutableMap.of(), templateCheckerFor("[22, 80, 9999]"));
         int[] ports = new int[] {22, 80, 9999};
-        Assert.assertEquals(jcloudsLocation.template.getOptions().getInboundPorts(), ports);
+        Assert.assertEquals(jcloudsLocation.getTemplate().getOptions().getInboundPorts(), ports);
     }
     
     @Test(groups={"Live", "Live-sanity"})
     public void testCreateWithInboundPortsOverride() {
-        BailOutWithTemplateJcloudsLocation jcloudsLocation = newSampleBailOutWithTemplateJcloudsLocation();
-        jcloudsLocation = (BailOutWithTemplateJcloudsLocation) jcloudsLocation.newSubLocation(MutableMap.of());
+        BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocationForLiveTest(managementContext);
+        jcloudsLocation = (BailOutJcloudsLocation) jcloudsLocation.newSubLocation(MutableMap.of());
         jcloudsLocation.tryObtainAndCheck(MutableMap.of(INBOUND_PORTS, "[23, 81, 9998]"), templateCheckerFor("[23, 81, 9998]"));
         int[] ports = new int[] {23, 81, 9998};
-        Assert.assertEquals(jcloudsLocation.template.getOptions().getInboundPorts(), ports);
+        Assert.assertEquals(jcloudsLocation.getTemplate().getOptions().getInboundPorts(), ports);
     }
 
     @Test
@@ -350,21 +200,21 @@ public class JcloudsLocationTest implements JcloudsLocationConfig {
         ExecutorService executor = Executors.newCachedThreadPool();
 
         try {
-            final BailOutJcloudsLocation jcloudsLocation = newSampleBailOutJcloudsLocationForTesting(ImmutableMap.of(BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, interceptor));
-            
+            final BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocation(
+                    managementContext, ImmutableMap.<ConfigKey<?>, Object>of(
+                            BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, interceptor));
             for (int i = 0; i < numCalls; i++) {
                 executor.execute(new Runnable() {
-                    @Override public void run() {
-                        jcloudsLocation.tryObtainAndCheck(MutableMap.of(), Predicates.alwaysTrue());
-                    }});
+                    @Override
+                    public void run() {
+                        jcloudsLocation.tryObtain();
+                    }
+                });
             }
-            
             interceptor.assertCallCountEventually(numCalls);
-            
             interceptor.unblock();
             executor.shutdown();
             executor.awaitTermination(10, TimeUnit.SECONDS);
-            
         } finally {
             executor.shutdownNow();
         }
@@ -378,25 +228,28 @@ public class JcloudsLocationTest implements JcloudsLocationConfig {
         ExecutorService executor = Executors.newCachedThreadPool();
         
         try {
-            final BailOutJcloudsLocation jcloudsLocation = newSampleBailOutJcloudsLocationForTesting(ImmutableMap.of(
-                    BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, interceptor,
-                    JcloudsLocation.MAX_CONCURRENT_MACHINE_CREATIONS, maxConcurrentCreations));
-            
+            final BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocation(
+                    managementContext, ImmutableMap.of(
+                            BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, interceptor,
+                            JcloudsLocation.MAX_CONCURRENT_MACHINE_CREATIONS, maxConcurrentCreations));
+
             for (int i = 0; i < numCalls; i++) {
                 executor.execute(new Runnable() {
-                    @Override public void run() {
-                        jcloudsLocation.tryObtainAndCheck(MutableMap.of(), Predicates.alwaysTrue());
-                    }});
+                    @Override
+                    public void run() {
+                        jcloudsLocation.tryObtain();
+                    }
+                });
             }
-            
+
             interceptor.assertCallCountEventually(maxConcurrentCreations);
             interceptor.assertCallCountContinually(maxConcurrentCreations);
-            
+
             interceptor.unblock();
             interceptor.assertCallCountEventually(numCalls);
             executor.shutdown();
             executor.awaitTermination(10, TimeUnit.SECONDS);
-            
+
         } finally {
             executor.shutdownNow();
         }
@@ -410,22 +263,24 @@ public class JcloudsLocationTest implements JcloudsLocationConfig {
         ExecutorService executor = Executors.newCachedThreadPool();
 
         try {
-            final BailOutJcloudsLocation jcloudsLocation = newSampleBailOutJcloudsLocationForTesting(ImmutableMap.of(
-                    BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, interceptor,
-                    JcloudsLocation.MAX_CONCURRENT_MACHINE_CREATIONS, maxConcurrentCreations));
-            
-    
+            final BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocation(
+                    managementContext, ImmutableMap.of(
+                            BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, interceptor,
+                            JcloudsLocation.MAX_CONCURRENT_MACHINE_CREATIONS, maxConcurrentCreations));
+
             for (int i = 0; i < numCalls; i++) {
                 final BailOutJcloudsLocation subLocation = (BailOutJcloudsLocation) jcloudsLocation.newSubLocation(MutableMap.of());
                 executor.execute(new Runnable() {
-                    @Override public void run() {
-                        subLocation.tryObtainAndCheck(MutableMap.of(), Predicates.alwaysTrue());
-                    }});
+                    @Override
+                    public void run() {
+                        subLocation.tryObtain();
+                    }
+                });
             }
-            
+
             interceptor.assertCallCountEventually(maxConcurrentCreations);
             interceptor.assertCallCountContinually(maxConcurrentCreations);
-            
+
             interceptor.unblock();
             interceptor.assertCallCountEventually(numCalls);
             executor.shutdown();
@@ -439,8 +294,9 @@ public class JcloudsLocationTest implements JcloudsLocationConfig {
     @Test
     public void testCreateWithCustomMachineNamer() {
         final String machineNamerClass = CustomMachineNamer.class.getName();
-        BailOutJcloudsLocation jcloudsLocation = newSampleBailOutJcloudsLocationForTesting(ImmutableMap.of(
-                LocationConfigKeys.CLOUD_MACHINE_NAMER_CLASS, machineNamerClass));
+        BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocation(
+                managementContext, ImmutableMap.<ConfigKey<?>, Object>of(
+                        LocationConfigKeys.CLOUD_MACHINE_NAMER_CLASS, machineNamerClass));
         jcloudsLocation.tryObtainAndCheck(ImmutableMap.of(CustomMachineNamer.MACHINE_NAME_TEMPLATE, "ignored"), new Predicate<ConfigBag>() {
             public boolean apply(ConfigBag input) {
                 Assert.assertEquals(input.get(LocationConfigKeys.CLOUD_MACHINE_NAMER_CLASS), machineNamerClass);
@@ -452,10 +308,11 @@ public class JcloudsLocationTest implements JcloudsLocationConfig {
     @Test
     public void testCreateWithCustomMachineNamerOnObtain() {
         final String machineNamerClass = CustomMachineNamer.class.getName();
-        BailOutJcloudsLocation jcloudsLocation = newSampleBailOutJcloudsLocationForTesting();
-        jcloudsLocation.tryObtainAndCheck(ImmutableMap.of(
+        BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocation(managementContext);
+        ImmutableMap<ConfigKey<String>, String> flags = ImmutableMap.of(
                 CustomMachineNamer.MACHINE_NAME_TEMPLATE, "ignored",
-                LocationConfigKeys.CLOUD_MACHINE_NAMER_CLASS, machineNamerClass), new Predicate<ConfigBag>() {
+                LocationConfigKeys.CLOUD_MACHINE_NAMER_CLASS, machineNamerClass);
+        jcloudsLocation.tryObtainAndCheck(flags, new Predicate<ConfigBag>() {
             public boolean apply(ConfigBag input) {
                 Assert.assertEquals(input.get(LocationConfigKeys.CLOUD_MACHINE_NAMER_CLASS), machineNamerClass);
                 return true;
@@ -603,33 +460,35 @@ public class JcloudsLocationTest implements JcloudsLocationConfig {
 
     // now test creating users
     
-    protected String getCreateUserStatementsFor(Map<?,?> config) {
-        BailOutJcloudsLocation jl = newSampleBailOutJcloudsLocationForTesting(MutableMap.<Object,Object>builder()
-            .put(JcloudsLocationConfig.LOGIN_USER, "root").put(JcloudsLocationConfig.LOGIN_USER_PASSWORD, "m0ck")
-            .put(JcloudsLocationConfig.USER, "bob").put(JcloudsLocationConfig.LOGIN_USER_PASSWORD, "b0b")
-            .putAll(config).build());
-        
+    protected String getCreateUserStatementsFor(Map<ConfigKey<?>,?> config) {
+        BailOutJcloudsLocation jl = BailOutJcloudsLocation.newBailOutJcloudsLocation(
+                managementContext, MutableMap.<ConfigKey<?>, Object>builder()
+                        .put(JcloudsLocationConfig.LOGIN_USER, "root").put(JcloudsLocationConfig.LOGIN_USER_PASSWORD, "m0ck")
+                        .put(JcloudsLocationConfig.USER, "bob").put(JcloudsLocationConfig.LOGIN_USER_PASSWORD, "b0b")
+                        .putAll(config).build());
+
         UserCreation creation = jl.createUserStatements(null, jl.config().getBag());
-        return new StatementList(creation.statements).render(OsFamily.UNIX);        
+        return new StatementList(creation.statements).render(OsFamily.UNIX);
     }
     
     @Test
     public void testDisablesRoot() {
-        String statements = getCreateUserStatementsFor(ImmutableMap.of());
+        String statements = getCreateUserStatementsFor(ImmutableMap.<ConfigKey<?>, Object>of());
         Assert.assertTrue(statements.contains("PermitRootLogin"), "Error:\n"+statements);
         Assert.assertTrue(statements.matches("(?s).*sudoers.*useradd.*bob.*wheel.*"), "Error:\n"+statements);
     }
 
     @Test
     public void testDisableRootFalse() {
-        String statements = getCreateUserStatementsFor(ImmutableMap.of(JcloudsLocationConfig.DISABLE_ROOT_AND_PASSWORD_SSH, false));
+        String statements = getCreateUserStatementsFor(ImmutableMap.<ConfigKey<?>, Object>of(
+                JcloudsLocationConfig.DISABLE_ROOT_AND_PASSWORD_SSH, false));
         Assert.assertFalse(statements.contains("PermitRootLogin"), "Error:\n"+statements);
         Assert.assertTrue(statements.matches("(?s).*sudoers.*useradd.*bob.*wheel.*"), "Error:\n"+statements);
     }
     
     @Test
     public void testDisableRootAndSudoFalse() {
-        String statements = getCreateUserStatementsFor(ImmutableMap.of(
+        String statements = getCreateUserStatementsFor(ImmutableMap.<ConfigKey<?>, Object>of(
             JcloudsLocationConfig.DISABLE_ROOT_AND_PASSWORD_SSH, false,
             JcloudsLocationConfig.GRANT_USER_SUDO, false));
         Assert.assertFalse(statements.contains("PermitRootLogin"), "Error:\n"+statements);


[2/3] incubator-brooklyn git commit: Adds PROVISION_LATCH config

Posted by sj...@apache.org.
Adds PROVISION_LATCH config

Behaviour as other latches. Used in MachineLifecycleEffectorTasks.


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

Branch: refs/heads/master
Commit: 620e4c187666137c69396397805b6413716c2075
Parents: 9645e8c
Author: Sam Corbett <sa...@cloudsoftcorp.com>
Authored: Thu Jun 11 15:07:31 2015 +0100
Committer: Sam Corbett <sa...@cloudsoftcorp.com>
Committed: Thu Jun 11 15:07:31 2015 +0100

----------------------------------------------------------------------
 .../entity/basic/BrooklynConfigKeys.java        |  1 +
 software/base/pom.xml                           |  7 ++
 .../software/MachineLifecycleEffectorTasks.java |  3 +-
 .../MachineLifecycleEffectorTasksTest.java      | 82 ++++++++++++++++++++
 4 files changed, 92 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/620e4c18/core/src/main/java/brooklyn/entity/basic/BrooklynConfigKeys.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/basic/BrooklynConfigKeys.java b/core/src/main/java/brooklyn/entity/basic/BrooklynConfigKeys.java
index 25972c6..3c641cb 100644
--- a/core/src/main/java/brooklyn/entity/basic/BrooklynConfigKeys.java
+++ b/core/src/main/java/brooklyn/entity/basic/BrooklynConfigKeys.java
@@ -143,6 +143,7 @@ public class BrooklynConfigKeys {
      * component is up, but this entity does not care about the dependent component's actual config values.
      */
 
+    public static final ConfigKey<Boolean> PROVISION_LATCH = newBooleanConfigKey("provision.latch", "Latch for blocking location provision until ready");
     public static final ConfigKey<Boolean> START_LATCH = newBooleanConfigKey("start.latch", "Latch for blocking start until ready");
     public static final ConfigKey<Boolean> SETUP_LATCH = newBooleanConfigKey("setup.latch", "Latch for blocking setup until ready");
     public static final ConfigKey<Boolean> PRE_INSTALL_RESOURCES_LATCH = newBooleanConfigKey("resources.preInstall.latch", "Latch for blocking pre-install resources until ready");

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/620e4c18/software/base/pom.xml
----------------------------------------------------------------------
diff --git a/software/base/pom.xml b/software/base/pom.xml
index 443b8cd..ba077ce 100644
--- a/software/base/pom.xml
+++ b/software/base/pom.xml
@@ -141,6 +141,13 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.apache.brooklyn</groupId>
+            <artifactId>brooklyn-locations-jclouds</artifactId>
+            <version>${project.version}</version>
+            <classifier>tests</classifier>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>mx4j</groupId>
             <artifactId>mx4j-tools</artifactId>
             <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/620e4c18/software/base/src/main/java/brooklyn/entity/software/MachineLifecycleEffectorTasks.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/brooklyn/entity/software/MachineLifecycleEffectorTasks.java b/software/base/src/main/java/brooklyn/entity/software/MachineLifecycleEffectorTasks.java
index 9c929bb..2ba42f7 100644
--- a/software/base/src/main/java/brooklyn/entity/software/MachineLifecycleEffectorTasks.java
+++ b/software/base/src/main/java/brooklyn/entity/software/MachineLifecycleEffectorTasks.java
@@ -25,7 +25,6 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Callable;
-
 import javax.annotation.Nullable;
 
 import org.slf4j.Logger;
@@ -275,6 +274,8 @@ public abstract class MachineLifecycleEffectorTasks {
         return DynamicTasks.queue(Tasks.<MachineLocation>builder().name("provisioning ("+location.getDisplayName()+")").body(
                 new Callable<MachineLocation>() {
                     public MachineLocation call() throws Exception {
+                        // Blocks if a latch was configured.
+                        entity().getConfig(BrooklynConfigKeys.PROVISION_LATCH);
                         final Map<String,Object> flags = obtainProvisioningFlags(location);
                         if (!(location instanceof LocalhostMachineProvisioningLocation))
                             log.info("Starting {}, obtaining a new location instance in {} with ports {}", new Object[] {entity(), location, flags.get("inboundPorts")});

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/620e4c18/software/base/src/test/java/brooklyn/entity/software/MachineLifecycleEffectorTasksTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/brooklyn/entity/software/MachineLifecycleEffectorTasksTest.java b/software/base/src/test/java/brooklyn/entity/software/MachineLifecycleEffectorTasksTest.java
index 2e73ca7..24fa657 100644
--- a/software/base/src/test/java/brooklyn/entity/software/MachineLifecycleEffectorTasksTest.java
+++ b/software/base/src/test/java/brooklyn/entity/software/MachineLifecycleEffectorTasksTest.java
@@ -19,14 +19,41 @@
 package brooklyn.entity.software;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
+import java.util.List;
 
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+
+import brooklyn.entity.Entity;
+import brooklyn.entity.basic.BasicEntity;
 import brooklyn.entity.basic.BasicEntityImpl;
+import brooklyn.entity.basic.BrooklynConfigKeys;
+import brooklyn.entity.basic.BrooklynTaskTags;
+import brooklyn.entity.basic.EmptySoftwareProcess;
+import brooklyn.entity.basic.Entities;
+import brooklyn.entity.basic.EntityLocal;
 import brooklyn.entity.basic.Lifecycle;
 import brooklyn.entity.basic.SoftwareProcess;
 import brooklyn.entity.basic.SoftwareProcess.StopSoftwareParameters.StopMode;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.entity.trait.Startable;
+import brooklyn.event.AttributeSensor;
+import brooklyn.event.basic.DependentConfiguration;
+import brooklyn.event.basic.Sensors;
+import brooklyn.location.jclouds.BailOutJcloudsLocation;
+import brooklyn.management.Task;
+import brooklyn.test.Asserts;
+import brooklyn.test.entity.TestApplication;
+import brooklyn.util.task.TaskInternal;
+import brooklyn.util.time.Duration;
 
 public class MachineLifecycleEffectorTasksTest {
     public static boolean canStop(StopMode stopMode, boolean isEntityStopped) {
@@ -54,4 +81,59 @@ public class MachineLifecycleEffectorTasksTest {
         assertEquals(canStop, expected);
     }
 
+    @Test
+    public void testProvisionLatchObeyed() throws Exception {
+
+        AttributeSensor<Boolean> ready = Sensors.newBooleanSensor("readiness");
+
+        TestApplication app = TestApplication.Factory.newManagedInstanceForTests();
+        BasicEntity triggerEntity = app.createAndManageChild(EntitySpec.create(BasicEntity.class));
+
+        EmptySoftwareProcess entity = app.createAndManageChild(EntitySpec.create(EmptySoftwareProcess.class)
+                .configure(BrooklynConfigKeys.PROVISION_LATCH, DependentConfiguration.attributeWhenReady(triggerEntity, ready)));
+
+        final Task<Void> task = Entities.invokeEffector(app, app, Startable.START, ImmutableMap.of(
+                "locations", ImmutableList.of(BailOutJcloudsLocation.newBailOutJcloudsLocation(app.getManagementContext()))));
+
+        assertEffectorBlockingDetailsEventually(entity, "Waiting for config " + BrooklynConfigKeys.PROVISION_LATCH.getName());
+
+        Asserts.succeedsContinually(new Runnable() {
+            @Override
+            public void run() {
+                assertFalse(task.isDone());
+            }
+        });
+        try {
+            ((EntityLocal) triggerEntity).setAttribute(ready, true);
+            task.get(Duration.THIRTY_SECONDS);
+        } catch (Throwable t) {
+            // BailOut location throws but we don't care.
+        } finally {
+            Entities.destroyAll(app.getManagementContext());
+        }
+    }
+
+    private void assertEffectorBlockingDetailsEventually(final Entity entity, final String blockingDetailsSnippet) {
+        Asserts.succeedsEventually(new Runnable() {
+            @Override public void run() {
+                Task<?> entityTask = Iterables.getOnlyElement(entity.getApplication().getManagementContext().getExecutionManager().getTasksWithAllTags(
+                        ImmutableList.of(BrooklynTaskTags.EFFECTOR_TAG, BrooklynTaskTags.tagForContextEntity(entity))));
+                String blockingDetails = getBlockingDetails(entityTask);
+                assertTrue(blockingDetails.contains(blockingDetailsSnippet));
+            }});
+    }
+
+    private String getBlockingDetails(Task<?> task) {
+        List<TaskInternal<?>> taskChain = Lists.newArrayList();
+        TaskInternal<?> taskI = (TaskInternal<?>) task;
+        while (taskI != null) {
+            taskChain.add(taskI);
+            if (taskI.getBlockingDetails() != null) {
+                return taskI.getBlockingDetails();
+            }
+            taskI = (TaskInternal<?>) taskI.getBlockingTask();
+        }
+        throw new IllegalStateException("No blocking details for "+task+" (walked task chain "+taskChain+")");
+    }
+
 }


[3/3] incubator-brooklyn git commit: This closes #681

Posted by sj...@apache.org.
This closes #681


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

Branch: refs/heads/master
Commit: 681b8fe90e5dde04ade1788e81dcfd7dc04e17f0
Parents: 5ceb8aa 620e4c1
Author: Sam Corbett <sa...@cloudsoftcorp.com>
Authored: Fri Jun 12 11:55:01 2015 +0100
Committer: Sam Corbett <sa...@cloudsoftcorp.com>
Committed: Fri Jun 12 11:55:01 2015 +0100

----------------------------------------------------------------------
 .../entity/basic/BrooklynConfigKeys.java        |   1 +
 .../jclouds/BailOutJcloudsLocation.java         | 187 ++++++++++++
 .../location/jclouds/JcloudsLocationTest.java   | 299 +++++--------------
 software/base/pom.xml                           |   7 +
 .../software/MachineLifecycleEffectorTasks.java |   3 +-
 .../MachineLifecycleEffectorTasksTest.java      |  82 +++++
 6 files changed, 358 insertions(+), 221 deletions(-)
----------------------------------------------------------------------