You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by dr...@apache.org on 2017/09/28 13:24:30 UTC

[3/4] brooklyn-server git commit: Fix machine customizers, for use with DSL

Fix machine customizers, for use with DSL

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

Branch: refs/heads/master
Commit: 9d35e776cc04a0e6876012fd25c0b94b5848acc7
Parents: 3ac961d
Author: Aled Sage <al...@gmail.com>
Authored: Fri Sep 22 16:55:35 2017 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Fri Sep 22 16:57:55 2017 +0100

----------------------------------------------------------------------
 ...loudsCustomizerInstantiationYamlDslTest.java | 156 ++++++++++++++-----
 .../FixedListMachineProvisioningLocation.java   |   3 +-
 .../jclouds/LocationCustomizerDelegate.java     |   8 +-
 3 files changed, 123 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9d35e776/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/JcloudsCustomizerInstantiationYamlDslTest.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/JcloudsCustomizerInstantiationYamlDslTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/JcloudsCustomizerInstantiationYamlDslTest.java
index d3c9851..3e3d68e 100644
--- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/JcloudsCustomizerInstantiationYamlDslTest.java
+++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/JcloudsCustomizerInstantiationYamlDslTest.java
@@ -20,26 +20,25 @@ package org.apache.brooklyn.camp.brooklyn;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertSame;
-import static org.testng.Assert.assertTrue;
 
-import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.NoSuchElementException;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Predicates;
-import com.google.common.collect.ImmutableList;
 import org.apache.brooklyn.api.entity.Application;
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.api.location.BasicMachineLocationCustomizer;
+import org.apache.brooklyn.api.location.MachineLocation;
 import org.apache.brooklyn.camp.brooklyn.spi.creation.CampTypePlanTransformer;
 import org.apache.brooklyn.core.entity.trait.Startable;
-import org.apache.brooklyn.core.test.entity.TestApplication;
+import org.apache.brooklyn.core.location.Machines;
 import org.apache.brooklyn.core.typereg.RegisteredTypeLoadingContexts;
 import org.apache.brooklyn.entity.machine.MachineEntity;
 import org.apache.brooklyn.location.jclouds.BasicJcloudsLocationCustomizer;
 import org.apache.brooklyn.location.jclouds.JcloudsLocation;
 import org.apache.brooklyn.location.jclouds.JcloudsMachineLocation;
+import org.apache.brooklyn.location.ssh.SshMachineLocation;
 import org.apache.brooklyn.util.collections.MutableList;
 import org.jclouds.compute.ComputeService;
 import org.jclouds.compute.domain.Template;
@@ -50,6 +49,9 @@ import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 import com.google.common.base.Joiner;
+import com.google.common.base.Optional;
+import com.google.common.base.Predicates;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
@@ -81,6 +83,7 @@ public class JcloudsCustomizerInstantiationYamlDslTest extends AbstractJcloudsSt
     @Override
     public void setUp() throws Exception {
         RecordingLocationCustomizer.clear();
+        RecordingMachineCustomizer.clear();
         super.setUp();
     }
     
@@ -91,6 +94,7 @@ public class JcloudsCustomizerInstantiationYamlDslTest extends AbstractJcloudsSt
             super.tearDown();
         } finally {
             RecordingLocationCustomizer.clear();
+            RecordingMachineCustomizer.clear();
         }
     }
     
@@ -106,45 +110,64 @@ public class JcloudsCustomizerInstantiationYamlDslTest extends AbstractJcloudsSt
                 "    metrics.usage.retrieve: false",
                 "    enabled: true",
                 "    provisioning.properties:",
-                "      customizer:",
-                "        $brooklyn:object:",
-                "          type: " + RecordingLocationCustomizer.class.getName(),
-                "          object.fields:",
-                "            enabled: $brooklyn:config(\"enabled\")");
+                "      customizers:",
+                "        - $brooklyn:object:",
+                "            type: " + RecordingLocationCustomizer.class.getName(),
+                "            object.fields:",
+                "              enabled: $brooklyn:config(\"enabled\")");
 
         EntitySpec<?> spec = managementContext.getTypeRegistry().createSpecFromPlan(CampTypePlanTransformer.FORMAT, yaml, RegisteredTypeLoadingContexts.spec(Application.class), EntitySpec.class);
         Entity app = managementContext.getEntityManager().createEntity(spec);
 
+        // On start(), assert customize calls are made
         app.invoke(Startable.START, ImmutableMap.<String, Object>of()).get();
+        RecordingLocationCustomizer.assertCallsEqual("customize1", "customize2", "customize3", "customize4");
 
-        assertEquals(RecordingLocationCustomizer.calls.size(), 4,
-                "Assert all customize functions called: size=" + RecordingLocationCustomizer.calls.size() + "; calls=" + RecordingLocationCustomizer.calls);
-
-        // Assert same instance used for all calls
-        RecordingLocationCustomizer firstInstance = RecordingLocationCustomizer.calls.get(0).instance;
-        for (RecordingLocationCustomizer.CallParams call : RecordingLocationCustomizer.calls) {
+        // Assert same instance used for all calls during provisioning (may be different instance on stop)
+        RecordingLocationCustomizer firstInstance = (RecordingLocationCustomizer) RecordingLocationCustomizer.calls.get(0).instance;
+        for (CallParams call : RecordingLocationCustomizer.calls) {
             assertSame(call.instance, firstInstance);
         }
 
-        assertCallsMade("customize1", "customize2", "customize3", "customize4");
-
+        // On stop, assert pre- and post- release are made
         app.invoke(Startable.STOP, ImmutableMap.<String, Object>of()).get();
-        // assert that pre and post release have now been called
-        assertEquals(RecordingLocationCustomizer.calls.size(), 6,
-            "assert that pre and post release have now been called: "
-            + "size=" + RecordingLocationCustomizer.calls.size() + "; calls=" + RecordingLocationCustomizer.calls);
-        assertCallsMade("customize1", "customize2", "customize3", "customize4", "preRelease", "postRelease");
+        RecordingLocationCustomizer.assertCallsEqual("customize1", "customize2", "customize3", "customize4", "preRelease", "postRelease");
     }
 
-    private void assertCallsMade(String ...values) {
-        List<String> expected = MutableList.of();
-        expected.addAll(Arrays.asList(values));
-        for (RecordingLocationCustomizer.CallParams parm : RecordingLocationCustomizer.calls) {
-            assertTrue(expected.remove(parm.method));
-        }
-        assertEquals(expected.size(), 0);
-    }
+    @Test
+    public void testMachineCustomizers() throws Exception {
+        String yaml = Joiner.on("\n").join(
+                "location: " + LOCATION_CATALOG_ID,
+                "services:\n" +
+                "- type: " + MachineEntity.class.getName(),
+                "  brooklyn.config:",
+                "    onbox.base.dir.skipResolution: true",
+                "    sshMonitoring.enabled: false",
+                "    metrics.usage.retrieve: false",
+                "    enabled: true",
+                "    provisioning.properties:",
+                "      machineCustomizers:",
+                "        - $brooklyn:object:",
+                "            type: " + RecordingMachineCustomizer.class.getName(),
+                "            object.fields:",
+                "              enabled: $brooklyn:config(\"enabled\")");
 
+        EntitySpec<?> spec = managementContext.getTypeRegistry().createSpecFromPlan(CampTypePlanTransformer.FORMAT, yaml, RegisteredTypeLoadingContexts.spec(Application.class), EntitySpec.class);
+        Entity app = managementContext.getEntityManager().createEntity(spec);
+        Entity entity = Iterables.getOnlyElement(app.getChildren());
+
+        // On start, assert customize is called
+        app.invoke(Startable.START, ImmutableMap.<String, Object>of()).get();
+        SshMachineLocation machine = Machines.findUniqueMachineLocation(entity.getLocations(), SshMachineLocation.class).get();
+
+        RecordingMachineCustomizer.assertCallsEqual("customize");
+        RecordingMachineCustomizer.calls.get(0).assertCallEquals("customize", ImmutableList.of(machine));
+
+        // On stop, assert preRelease is called
+        app.invoke(Startable.STOP, ImmutableMap.<String, Object>of()).get();
+        RecordingMachineCustomizer.assertCallsEqual("customize", "preRelease");
+        RecordingMachineCustomizer.calls.get(1).assertCallEquals("preRelease", ImmutableList.of(machine));
+    }
 
     public static class RecordingLocationCustomizer extends BasicJcloudsLocationCustomizer {
 
@@ -154,6 +177,15 @@ public class JcloudsCustomizerInstantiationYamlDslTest extends AbstractJcloudsSt
             calls.clear();
         }
 
+        static void assertCallsEqual(String... values) {
+            List<String> expected = ImmutableList.copyOf(values);
+            List<String> actual = new ArrayList<>();
+            for (CallParams parm : calls) {
+                actual.add(parm.method);
+            }
+            assertEquals(actual, expected, "actual="+actual+"; expected="+expected);
+        }
+
         private Boolean enabled;
 
         public void setEnabled(Boolean val) {
@@ -211,17 +243,61 @@ public class JcloudsCustomizerInstantiationYamlDslTest extends AbstractJcloudsSt
                 calls.add(new CallParams(this, "postRelease", MutableList.of(machine)));
             }
         }
+    }
+    
+    public static class RecordingMachineCustomizer extends BasicMachineLocationCustomizer {
 
-        public static class CallParams {
-            RecordingLocationCustomizer instance;
-            String method;
-            List<?> args;
+        public static final List<CallParams> calls = Lists.newCopyOnWriteArrayList();
 
-            public CallParams(RecordingLocationCustomizer instance, String method, List<?> args) {
-                this.instance = instance;
-                this.method = method;
-                this.args = args;
+        public static void clear() {
+            calls.clear();
+        }
+
+        static void assertCallsEqual(String... values) {
+            List<String> expected = ImmutableList.copyOf(values);
+            List<String> actual = new ArrayList<>();
+            for (CallParams parm : calls) {
+                actual.add(parm.method);
             }
+            assertEquals(actual, expected, "actual="+actual+"; expected="+expected);
+        }
+
+        private Boolean enabled;
+
+        public void setEnabled(Boolean val) {
+            this.enabled = val;
+        }
+        
+        @Override
+        public void customize(MachineLocation machine) {
+            if (Boolean.TRUE.equals(enabled)) {
+                calls.add(new CallParams(this, "customize", MutableList.of(machine)));
+            }
+        }
+        
+        @Override
+        public void preRelease(MachineLocation machine) {
+            if (Boolean.TRUE.equals(enabled)) {
+                calls.add(new CallParams(this, "preRelease", MutableList.of(machine)));
+            }
+        }
+    }
+    
+    public static class CallParams {
+        Object instance;
+        String method;
+        List<?> args;
+
+        public CallParams(Object instance, String method, List<?> args) {
+            this.instance = instance;
+            this.method = method;
+            this.args = args;
+        }
+        
+        void assertCallEquals(String expectedMethod, List<?> expectedArgs) {
+            assertEquals(method, expectedMethod);
+            assertEquals(args, expectedArgs);
         }
+        
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9d35e776/core/src/main/java/org/apache/brooklyn/location/byon/FixedListMachineProvisioningLocation.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/location/byon/FixedListMachineProvisioningLocation.java b/core/src/main/java/org/apache/brooklyn/location/byon/FixedListMachineProvisioningLocation.java
index 87b83f8..168bd30 100644
--- a/core/src/main/java/org/apache/brooklyn/location/byon/FixedListMachineProvisioningLocation.java
+++ b/core/src/main/java/org/apache/brooklyn/location/byon/FixedListMachineProvisioningLocation.java
@@ -37,6 +37,7 @@ import org.apache.brooklyn.api.location.NoMachinesAvailableException;
 import org.apache.brooklyn.api.mgmt.LocationManager;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.entity.EntityInitializers;
 import org.apache.brooklyn.core.location.AbstractLocation;
 import org.apache.brooklyn.core.location.cloud.CloudLocationConfig;
 import org.apache.brooklyn.location.ssh.SshMachineLocation;
@@ -415,7 +416,7 @@ implements MachineProvisioningLocation<T>, Closeable {
     }
 
     protected Collection<MachineLocationCustomizer> getMachineCustomizers(ConfigBag setup) {
-        Collection<MachineLocationCustomizer> customizers = setup.get(MACHINE_LOCATION_CUSTOMIZERS);
+        Collection<MachineLocationCustomizer> customizers = EntityInitializers.resolve(setup, MACHINE_LOCATION_CUSTOMIZERS);
         return (customizers == null ? ImmutableList.<MachineLocationCustomizer>of() : customizers);
     }
 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9d35e776/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/LocationCustomizerDelegate.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/LocationCustomizerDelegate.java b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/LocationCustomizerDelegate.java
index b268e53..bf902b1 100644
--- a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/LocationCustomizerDelegate.java
+++ b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/LocationCustomizerDelegate.java
@@ -24,6 +24,7 @@ import java.util.List;
 
 import org.apache.brooklyn.api.location.MachineLocationCustomizer;
 import org.apache.brooklyn.api.mgmt.ManagementContext;
+import org.apache.brooklyn.core.entity.EntityInitializers;
 import org.apache.brooklyn.util.core.config.ConfigBag;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.guava.Maybe;
@@ -193,8 +194,8 @@ public class LocationCustomizerDelegate implements JcloudsLocationCustomizer {
 
     @SuppressWarnings("deprecation")
     public static Collection<JcloudsLocationCustomizer> getCustomizers(ManagementContext mgmt, ConfigBag setup) {
-        JcloudsLocationCustomizer customizer = setup.get(JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZER);
-        Collection<JcloudsLocationCustomizer> customizers = setup.get(JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZERS);
+        JcloudsLocationCustomizer customizer = EntityInitializers.resolve(setup, JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZER);
+        Collection<JcloudsLocationCustomizer> customizers = EntityInitializers.resolve(setup, JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZERS);
         String customizerType = setup.get(JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZER_TYPE);
         String customizersSupplierType = setup.get(JcloudsLocationConfig.JCLOUDS_LOCATION_CUSTOMIZERS_SUPPLIER_TYPE);
 
@@ -232,7 +233,8 @@ public class LocationCustomizerDelegate implements JcloudsLocationCustomizer {
     }
 
     protected static Collection<MachineLocationCustomizer> getMachineCustomizers(ManagementContext mgmt, ConfigBag setup) {
-        Collection<MachineLocationCustomizer> customizers = setup.get(JcloudsLocationConfig.MACHINE_LOCATION_CUSTOMIZERS);
+        Collection<MachineLocationCustomizer> customizers = EntityInitializers.resolve(setup, JcloudsLocationConfig.MACHINE_LOCATION_CUSTOMIZERS);
+//        Collection<MachineLocationCustomizer> customizers = setup.get(JcloudsLocationConfig.MACHINE_LOCATION_CUSTOMIZERS);
         return (customizers == null ? ImmutableList.<MachineLocationCustomizer>of() : customizers);
     }