brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [3/8] brooklyn-server git commit: PublicNetworkFaceEnricher: incorporate comments
Date Wed, 08 Jun 2016 20:23:33 GMT
PublicNetworkFaceEnricher: incorporate comments


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

Branch: refs/heads/master
Commit: 2066d4a3b21089efc566acec130b1c07503ffb5e
Parents: fb067f7
Author: Aled Sage <aled.sage@gmail.com>
Authored: Tue Jun 7 00:46:59 2016 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Tue Jun 7 12:20:58 2016 +0100

----------------------------------------------------------------------
 .../access/PublicNetworkFaceEnricher.java       | 21 ++++--
 .../PublicNetworkFaceEnricherRebindTest.java    | 69 ++++++++++++++++++++
 .../access/PublicNetworkFaceEnricherTest.java   | 36 +++++++++-
 3 files changed, 119 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2066d4a3/core/src/main/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricher.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricher.java
b/core/src/main/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricher.java
index 92de796..1247930 100644
--- a/core/src/main/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricher.java
+++ b/core/src/main/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricher.java
@@ -245,20 +245,28 @@ public class PublicNetworkFaceEnricher extends AbstractEnricher {
             return true;
         }
         try {
-            new URI(sensorVal.toString());
-            return true;
+            URI uri = new URI(sensorVal.toString());
+            return uri.getScheme() != null;
         } catch (URISyntaxException e) {
             return false;
         }
     }
 
     protected boolean isPort(Object sensorVal) {
-        return (sensorVal instanceof Integer || sensorVal instanceof Long || sensorVal instanceof
Short);
+        if (sensorVal instanceof Integer || sensorVal instanceof Long) {
+            return Networking.isPortValid(((Number)sensorVal).intValue());
+        } else if (sensorVal instanceof CharSequence) {
+            return sensorVal.toString().trim().matches("[0-9]+");
+        } else {
+            return false;
+        }
     }
 
     protected int toInteger(Object sensorVal) {
         if (sensorVal instanceof Number) {
             return ((Number)sensorVal).intValue();
+        } else if (sensorVal instanceof CharSequence) {
+            return Integer.parseInt(sensorVal.toString().trim());
         } else {
             throw new IllegalArgumentException("Expected number but got "+sensorVal+" of
type "+(sensorVal != null ? sensorVal.getClass() : null));
         }
@@ -287,12 +295,13 @@ public class PublicNetworkFaceEnricher extends AbstractEnricher {
         if (port != -1) {
             HostAndPort publicTarget = getPortForwardManager().lookup(machine, port);
             if (publicTarget == null) {
-                // TODO What if publicTarget is still null, but will be set soon? We're not
subscribed to changes in the PortForwardManager!
-                // TODO Should we return null or sensorVal? In this method we always return
sensorVal;
-                //      but in HostAndPortTransformingEnricher we always return null!
                 LOG.trace("network-facing enricher not transforming {} URI {}, because no
port-mapping for {}", new Object[] {source, sensorVal, machine});
                 return Maybe.absent();
             }
+            if (!publicTarget.hasPort()) {
+                LOG.debug("network-facing enricher not transforming {} URI {}, because no
port in public-target {} for {}", new Object[] {source, sensorVal, publicTarget, machine});
+                return Maybe.absent();
+            }
             URI result;
             try {
                 result = new URI(uri.getScheme(), uri.getUserInfo(), publicTarget.getHostText(),
publicTarget.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment());

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2066d4a3/core/src/test/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricherRebindTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricherRebindTest.java
b/core/src/test/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricherRebindTest.java
new file mode 100644
index 0000000..9606a29
--- /dev/null
+++ b/core/src/test/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricherRebindTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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 org.apache.brooklyn.core.location.access;
+
+import java.net.URI;
+
+import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.api.sensor.EnricherSpec;
+import org.apache.brooklyn.core.entity.Attributes;
+import org.apache.brooklyn.core.entity.EntityAsserts;
+import org.apache.brooklyn.core.mgmt.rebind.RebindTestFixtureWithApp;
+import org.apache.brooklyn.core.sensor.Sensors;
+import org.apache.brooklyn.core.test.entity.TestEntity;
+import org.apache.brooklyn.location.ssh.SshMachineLocation;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+import com.google.common.net.HostAndPort;
+
+public class PublicNetworkFaceEnricherRebindTest extends RebindTestFixtureWithApp {
+
+    private TestEntity origEntity;
+    private SshMachineLocation origMachine;
+
+    @BeforeMethod(alwaysRun=true)
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        origEntity = origApp.createAndManageChild(EntitySpec.create(TestEntity.class));
+        origMachine = origApp.newLocalhostProvisioningLocation().obtain();
+    }
+    
+    @Test
+    public <T> void testRebind() throws Exception {
+        origEntity.sensors().set(Attributes.SUBNET_ADDRESS, "127.0.0.1");
+        
+        origEntity.enrichers().add(EnricherSpec.create(PublicNetworkFaceEnricher.class)
+                .configure(PublicNetworkFaceEnricher.SENSOR, Attributes.MAIN_URI));
+
+        rebind();
+        TestEntity newEntity = (TestEntity) Iterables.getOnlyElement(newApp.getChildren());
+        PortForwardManager newPortForwardManager = (PortForwardManager) mgmt().getLocationRegistry().getLocationManaged(PortForwardManagerLocationResolver.PFM_GLOBAL_SPEC);
+        SshMachineLocation newMachine = (SshMachineLocation) mgmt().getLocationManager().getLocation(origMachine.getId());
+        
+        newEntity.sensors().set(Attributes.MAIN_URI, URI.create("http://127.0.0.1:1234/my/path"));
+        newPortForwardManager.associate("myPublicIp", HostAndPort.fromParts("mypublichost",
5678), newMachine, 1234);
+        newEntity.addLocations(ImmutableList.of(newMachine));
+        
+        EntityAsserts.assertAttributeEqualsEventually(newEntity, Sensors.newStringSensor(Attributes.MAIN_URI.getName()+".mapped.public"),
"http://mypublichost:5678/my/path");
+    }
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/2066d4a3/core/src/test/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricherTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricherTest.java
b/core/src/test/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricherTest.java
index 503d949..7e9cf81 100644
--- a/core/src/test/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricherTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/location/access/PublicNetworkFaceEnricherTest.java
@@ -22,6 +22,7 @@ import java.net.URI;
 import java.util.List;
 
 import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.api.location.LocationSpec;
 import org.apache.brooklyn.api.sensor.AttributeSensor;
 import org.apache.brooklyn.api.sensor.EnricherSpec;
 import org.apache.brooklyn.core.entity.Attributes;
@@ -53,7 +54,8 @@ public class PublicNetworkFaceEnricherTest extends BrooklynAppUnitTestSupport
{
     public void setUp() throws Exception {
         super.setUp();
         entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
-        machine = app.newLocalhostProvisioningLocation().obtain();
+        machine = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)
+                .configure("address", "127.0.0.1"));
         portForwardManager = (PortForwardManager) mgmt.getLocationRegistry().getLocationManaged(PortForwardManagerLocationResolver.PFM_GLOBAL_SPEC);
     }
     
@@ -67,6 +69,7 @@ public class PublicNetworkFaceEnricherTest extends BrooklynAppUnitTestSupport
{
                     result.add(new Object[] {setSensor, createAssociation, addLocation, Attributes.MAIN_URI,
URI.create("http://127.0.0.1:1234/my/path"), "http://mypublichost:5678/my/path"});
                     result.add(new Object[] {setSensor, createAssociation, addLocation, TestEntity.NAME,
"http://127.0.0.1:1234/my/path", "http://mypublichost:5678/my/path"});
                     result.add(new Object[] {setSensor, createAssociation, addLocation, Attributes.HTTP_PORT,
1234, "mypublichost:5678"});
+                    result.add(new Object[] {setSensor, createAssociation, addLocation, TestEntity.NAME,
"1234", "mypublichost:5678"});
                     result.add(new Object[] {setSensor, createAssociation, addLocation, TestEntity.NAME,
"127.0.0.1:1234", "mypublichost:5678"});
                     result.add(new Object[] {setSensor, createAssociation, addLocation, hostAndPortSensor,
HostAndPort.fromString("127.0.0.1:1234"), "mypublichost:5678"});
                 }
@@ -74,6 +77,23 @@ public class PublicNetworkFaceEnricherTest extends BrooklynAppUnitTestSupport
{
         }
         return result.toArray(new Object[result.size()][]);
     }
+    
+    @DataProvider(name = "invalidVariants")
+    public static Object[][] provideInvalidVariants() {
+        AttributeSensor<HostAndPort> hostAndPortSensor = Sensors.newSensor(HostAndPort.class,
"test.hostAndPort");
+        List<Object[]> result = Lists.newArrayList();
+        result.add(new Object[] {Attributes.MAIN_URI, (URI)null});
+        result.add(new Object[] {TestEntity.NAME, "127.0.0.1:1234/my/path"}); // must have
scheme
+        result.add(new Object[] {Attributes.HTTP_PORT, null});
+        result.add(new Object[] {Attributes.HTTP_PORT, 1234567});
+        result.add(new Object[] {TestEntity.NAME, null});
+        result.add(new Object[] {TestEntity.NAME, "1234567"});
+        result.add(new Object[] {TestEntity.NAME, "thisHasNoPort"});
+        result.add(new Object[] {TestEntity.NAME, "portIsTooBig:1234567"});
+        result.add(new Object[] {hostAndPortSensor, (HostAndPort)null});
+        return result.toArray(new Object[result.size()][]);
+    }
+
     enum Timing {
         BEFORE,
         AFTER;
@@ -112,6 +132,20 @@ public class PublicNetworkFaceEnricherTest extends BrooklynAppUnitTestSupport
{
         EntityAsserts.assertAttributeEquals(entity, sensor, sensorVal);
     }
     
+    
+    @Test(dataProvider = "invalidVariants")
+    public <T> void testIgnoresWhenInvalidAttribute(AttributeSensor<T> sensor,
T sensorVal) throws Exception {
+        entity.sensors().set(Attributes.SUBNET_ADDRESS, "127.0.0.1");
+        entity.sensors().set(sensor, sensorVal);
+        portForwardManager.associate("myPublicIp", HostAndPort.fromParts("mypublichost",
5678), machine, 1234);
+        entity.addLocations(ImmutableList.of(machine));
+        
+        entity.enrichers().add(EnricherSpec.create(PublicNetworkFaceEnricher.class)
+                .configure(PublicNetworkFaceEnricher.SENSOR, sensor));
+
+        EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", VERY_SHORT_WAIT),
entity, Sensors.newStringSensor(sensor.getName()+".mapped.public"), null);
+    }
+
     @Test
     public <T> void testTransformsDefaultHttp80() throws Exception {
         entity.sensors().set(Attributes.SUBNET_ADDRESS, "127.0.0.1");


Mime
View raw message