http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/538324e1/core/src/test/java/org/apache/brooklyn/location/core/RecordingMachineLocationCustomizer.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/core/RecordingMachineLocationCustomizer.java b/core/src/test/java/org/apache/brooklyn/location/core/RecordingMachineLocationCustomizer.java deleted file mode 100644 index efaa94f..0000000 --- a/core/src/test/java/org/apache/brooklyn/location/core/RecordingMachineLocationCustomizer.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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.location.core; - -import static com.google.common.base.Preconditions.checkNotNull; - -import java.util.List; - -import com.google.common.base.Objects; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; - -import org.apache.brooklyn.api.location.MachineLocation; -import org.apache.brooklyn.api.location.MachineLocationCustomizer; - -public class RecordingMachineLocationCustomizer implements MachineLocationCustomizer { - public static class Call { - public final String methodName; - public final List<?> args; - - public Call(String methodName, List<?> args) { - this.methodName = checkNotNull(methodName); - this.args = checkNotNull(args); - } - - @Override - public String toString() { - return methodName+args; - } - - @Override - public int hashCode() { - return Objects.hashCode(methodName, args); - } - - @Override - public boolean equals(Object other) { - return (other instanceof RecordingMachineLocationCustomizer.Call) && - methodName.equals(((RecordingMachineLocationCustomizer.Call)other).methodName) && - args.equals(((RecordingMachineLocationCustomizer.Call)other).args); - } - } - - public final List<RecordingMachineLocationCustomizer.Call> calls = Lists.newCopyOnWriteArrayList(); - - @Override - public void customize(MachineLocation machine) { - calls.add(new Call("customize", ImmutableList.of(machine))); - } - - @Override - public void preRelease(MachineLocation machine) { - calls.add(new Call("preRelease", ImmutableList.of(machine))); - } -} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/538324e1/core/src/test/java/org/apache/brooklyn/location/core/SimulatedLocation.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/core/SimulatedLocation.java b/core/src/test/java/org/apache/brooklyn/location/core/SimulatedLocation.java deleted file mode 100644 index e9f572c..0000000 --- a/core/src/test/java/org/apache/brooklyn/location/core/SimulatedLocation.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * 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.location.core; - -import java.net.InetAddress; -import java.util.Collection; -import java.util.Map; -import java.util.Set; - -import org.apache.brooklyn.api.location.HardwareDetails; -import org.apache.brooklyn.api.location.LocationSpec; -import org.apache.brooklyn.api.location.MachineDetails; -import org.apache.brooklyn.api.location.MachineLocation; -import org.apache.brooklyn.api.location.MachineProvisioningLocation; -import org.apache.brooklyn.api.location.OsDetails; -import org.apache.brooklyn.api.location.PortRange; -import org.apache.brooklyn.api.location.PortSupplier; -import org.apache.brooklyn.location.core.AbstractLocation; -import org.apache.brooklyn.location.core.BasicHardwareDetails; -import org.apache.brooklyn.location.core.BasicMachineDetails; -import org.apache.brooklyn.location.core.BasicOsDetails; -import org.apache.brooklyn.location.core.PortRanges; -import org.apache.brooklyn.util.collections.MutableMap; -import org.apache.brooklyn.util.net.Networking; - -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; -import com.google.common.collect.Sets; - - -/** Location for use in dev/test, defining custom start/stop support, and/or tweaking the ports which are permitted to be available - * (using setPermittedPorts(Iterable)) - */ -public class SimulatedLocation extends AbstractLocation implements MachineProvisioningLocation<MachineLocation>, MachineLocation, PortSupplier { - - private static final long serialVersionUID = 1L; - - private static final InetAddress address; - static { - address = Networking.getLocalHost(); - } - - Iterable<Integer> permittedPorts = PortRanges.fromString("1+"); - Set<Integer> usedPorts = Sets.newLinkedHashSet(); - - public SimulatedLocation() { - this(MutableMap.<String,Object>of()); - } - public SimulatedLocation(Map<String,? extends Object> flags) { - super(flags); - } - - @Override - public SimulatedLocation newSubLocation(Map<?,?> newFlags) { - // TODO shouldn't have to copy config bag as it should be inherited (but currently it is not used inherited everywhere; just most places) - return getManagementContext().getLocationManager().createLocation(LocationSpec.create(getClass()) - .parent(this) - .configure(config().getLocalBag().getAllConfig()) // FIXME Should this just be inherited? - .configure(newFlags)); - } - - public MachineLocation obtain(Map<?,?> flags) { - return this; - } - - public void release(MachineLocation machine) { - } - - public Map<String,Object> getProvisioningFlags(Collection<String> tags) { - return MutableMap.<String,Object>of(); - } - - public InetAddress getAddress() { - return address; - } - - @Override - public String getHostname() { - String hostname = address.getHostName(); - return (hostname == null || hostname.equals(address.getHostAddress())) ? null : hostname; - } - - @Override - public Set<String> getPublicAddresses() { - return ImmutableSet.of(address.getHostAddress()); - } - - @Override - public Set<String> getPrivateAddresses() { - return ImmutableSet.of(); - } - - public synchronized boolean obtainSpecificPort(int portNumber) { - if (!Iterables.contains(permittedPorts, portNumber)) return false; - if (usedPorts.contains(portNumber)) return false; - usedPorts.add(portNumber); - return true; - } - - public synchronized int obtainPort(PortRange range) { - for (int p: range) - if (obtainSpecificPort(p)) return p; - return -1; - } - - public synchronized void releasePort(int portNumber) { - usedPorts.remove(portNumber); - } - - public synchronized void setPermittedPorts(Iterable<Integer> ports) { - permittedPorts = ports; - } - - @Override - public OsDetails getOsDetails() { - return getMachineDetails().getOsDetails(); - } - - @Override - public MachineDetails getMachineDetails() { - HardwareDetails hardwareDetails = new BasicHardwareDetails(null, null); - OsDetails osDetails = BasicOsDetails.Factory.ANONYMOUS_LINUX; - return new BasicMachineDetails(hardwareDetails, osDetails); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/538324e1/core/src/test/java/org/apache/brooklyn/location/core/TestPortSupplierLocation.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/core/TestPortSupplierLocation.java b/core/src/test/java/org/apache/brooklyn/location/core/TestPortSupplierLocation.java deleted file mode 100644 index df37585..0000000 --- a/core/src/test/java/org/apache/brooklyn/location/core/TestPortSupplierLocation.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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.location.core; - -import static org.testng.Assert.assertEquals; - -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; -import org.apache.brooklyn.core.test.entity.TestEntity; -import org.apache.brooklyn.location.core.PortRanges; -import org.apache.brooklyn.sensor.core.PortAttributeSensorAndConfigKey; -import org.apache.brooklyn.sensor.feed.ConfigToAttributes; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableList; - -public class TestPortSupplierLocation extends BrooklynAppUnitTestSupport { - - SimulatedLocation loc; - PortAttributeSensorAndConfigKey ps; - TestEntity entity; - - @BeforeMethod(alwaysRun=true) - @Override - public void setUp() throws Exception { - super.setUp(); - loc = app.newSimulatedLocation(); - entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); - app.start(ImmutableList.of(loc)); - - ps = new PortAttributeSensorAndConfigKey("some.port", "for testing", "1234+"); - } - - @Test - public void testObtainsPort() throws Exception { - ConfigToAttributes.apply(entity, ps); - - int p = entity.getAttribute(ps); - assertEquals(p, 1234); - - //sensor access should keep the same value - p = entity.getAttribute(ps); - assertEquals(p, 1234); - } - - @Test - public void testRepeatedConvertAccessIncrements() throws Exception { - int p = ps.getAsSensorValue(entity); - assertEquals(p, 1234); - - //but direct access should see it as being reserved (not required behaviour, but it is the current behaviour) - int p2 = ps.getAsSensorValue(entity); - assertEquals(p2, 1235); - } - - @Test - public void testNullBeforeSetting() throws Exception { - // currently getting the attribute before explicitly setting return null; i.e. no "auto-set" -- - // but this behaviour may be changed - Integer p = entity.getAttribute(ps); - assertEquals(p, null); - } - - @Test - public void testSimulatedRestrictedPermitted() throws Exception { - loc.setPermittedPorts(PortRanges.fromString("1240+")); - - ConfigToAttributes.apply(entity, ps); - int p = entity.getAttribute(ps); - assertEquals((int)p, 1240); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/538324e1/core/src/test/java/org/apache/brooklyn/location/core/localhost/LocalhostLocationResolverTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/core/localhost/LocalhostLocationResolverTest.java b/core/src/test/java/org/apache/brooklyn/location/core/localhost/LocalhostLocationResolverTest.java deleted file mode 100644 index c27f140..0000000 --- a/core/src/test/java/org/apache/brooklyn/location/core/localhost/LocalhostLocationResolverTest.java +++ /dev/null @@ -1,269 +0,0 @@ -/* - * 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.location.core.localhost; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; - -import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; - -import org.apache.brooklyn.api.location.Location; -import org.apache.brooklyn.api.location.NoMachinesAvailableException; -import org.apache.brooklyn.core.entity.Entities; -import org.apache.brooklyn.core.internal.BrooklynProperties; -import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; -import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; -import org.apache.brooklyn.location.byon.FixedListMachineProvisioningLocation; -import org.apache.brooklyn.location.core.BasicLocationRegistry; -import org.apache.brooklyn.location.core.internal.LocationInternal; -import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation; -import org.apache.brooklyn.location.ssh.SshMachineLocation; -import org.apache.brooklyn.util.text.StringEscapes.JavaStringEscapes; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableList; - -public class LocalhostLocationResolverTest { - - private BrooklynProperties brooklynProperties; - private LocalManagementContext managementContext; - - @BeforeMethod(alwaysRun=true) - public void setUp() throws Exception { - managementContext = LocalManagementContextForTests.newInstance(); - brooklynProperties = managementContext.getBrooklynProperties(); - } - - @AfterMethod(alwaysRun=true) - public void tearDown() throws Exception { - if (managementContext != null) Entities.destroyAll(managementContext); - } - - @Test - public void testTakesLocalhostScopedProperties() { - brooklynProperties.put("brooklyn.location.localhost.privateKeyFile", "myprivatekeyfile"); - brooklynProperties.put("brooklyn.location.localhost.publicKeyFile", "mypublickeyfile"); - brooklynProperties.put("brooklyn.location.localhost.privateKeyData", "myprivateKeyData"); - brooklynProperties.put("brooklyn.location.localhost.publicKeyData", "myPublicKeyData"); - brooklynProperties.put("brooklyn.location.localhost.privateKeyPassphrase", "myprivateKeyPassphrase"); - - Map<String, Object> conf = resolve("localhost").config().getBag().getAllConfig(); - - assertEquals(conf.get("privateKeyFile"), "myprivatekeyfile"); - assertEquals(conf.get("publicKeyFile"), "mypublickeyfile"); - assertEquals(conf.get("privateKeyData"), "myprivateKeyData"); - assertEquals(conf.get("publicKeyData"), "myPublicKeyData"); - assertEquals(conf.get("privateKeyPassphrase"), "myprivateKeyPassphrase"); - } - - @Test - public void testTakesLocalhostDeprecatedScopedProperties() { - brooklynProperties.put("brooklyn.localhost.privateKeyFile", "myprivatekeyfile"); - brooklynProperties.put("brooklyn.localhost.publicKeyFile", "mypublickeyfile"); - brooklynProperties.put("brooklyn.localhost.privateKeyData", "myprivateKeyData"); - brooklynProperties.put("brooklyn.localhost.publicKeyData", "myPublicKeyData"); - brooklynProperties.put("brooklyn.localhost.privateKeyPassphrase", "myprivateKeyPassphrase"); - - Map<String, Object> conf = resolve("localhost").config().getBag().getAllConfig(); - - assertEquals(conf.get("privateKeyFile"), "myprivatekeyfile"); - assertEquals(conf.get("publicKeyFile"), "mypublickeyfile"); - assertEquals(conf.get("privateKeyData"), "myprivateKeyData"); - assertEquals(conf.get("publicKeyData"), "myPublicKeyData"); - assertEquals(conf.get("privateKeyPassphrase"), "myprivateKeyPassphrase"); - } - - @Test - public void testTakesDeprecatedProperties() { - brooklynProperties.put("brooklyn.localhost.private-key-file", "myprivatekeyfile"); - brooklynProperties.put("brooklyn.localhost.public-key-file", "mypublickeyfile"); - brooklynProperties.put("brooklyn.localhost.private-key-data", "myprivateKeyData"); - brooklynProperties.put("brooklyn.localhost.public-key-data", "myPublicKeyData"); - brooklynProperties.put("brooklyn.localhost.private-key-passphrase", "myprivateKeyPassphrase"); - Map<String, Object> conf = resolve("localhost").config().getBag().getAllConfig(); - - assertEquals(conf.get("privateKeyFile"), "myprivatekeyfile"); - assertEquals(conf.get("publicKeyFile"), "mypublickeyfile"); - assertEquals(conf.get("privateKeyData"), "myprivateKeyData"); - assertEquals(conf.get("publicKeyData"), "myPublicKeyData"); - assertEquals(conf.get("privateKeyPassphrase"), "myprivateKeyPassphrase"); - } - - @Test - public void testPropertyScopePrescedence() { - brooklynProperties.put("brooklyn.location.named.mynamed", "localhost"); - - // prefer those in "named" over everything else - brooklynProperties.put("brooklyn.location.named.mynamed.privateKeyFile", "privateKeyFile-inNamed"); - brooklynProperties.put("brooklyn.location.localhost.privateKeyFile", "privateKeyFile-inProviderSpecific"); - brooklynProperties.put("brooklyn.localhost.privateKeyFile", "privateKeyFile-inGeneric"); - - // prefer those in provider-specific over generic - brooklynProperties.put("brooklyn.location.localhost.publicKeyFile", "publicKeyFile-inProviderSpecific"); - brooklynProperties.put("brooklyn.location.publicKeyFile", "publicKeyFile-inGeneric"); - - // prefer location-generic if nothing else - brooklynProperties.put("brooklyn.location.privateKeyData", "privateKeyData-inGeneric"); - - Map<String, Object> conf = resolve("named:mynamed").config().getBag().getAllConfig(); - - assertEquals(conf.get("privateKeyFile"), "privateKeyFile-inNamed"); - assertEquals(conf.get("publicKeyFile"), "publicKeyFile-inProviderSpecific"); - assertEquals(conf.get("privateKeyData"), "privateKeyData-inGeneric"); - } - - @Test - public void testLocalhostLoads() { - Assert.assertTrue(resolve("localhost") instanceof LocalhostMachineProvisioningLocation); - } - - @Test - public void testThrowsOnInvalid() throws Exception { - assertThrowsNoSuchElement("wrongprefix"); - assertThrowsIllegalArgument("localhost(name=abc"); // no closing bracket - assertThrowsIllegalArgument("localhost(name)"); // no value for name - assertThrowsIllegalArgument("localhost(name=)"); // no value for name - } - - - @Test - public void testAcceptsList() { - List<Location> l = getLocationResolver().resolve(ImmutableList.of("localhost")); - assertEquals(l.size(), 1, "l="+l); - assertTrue(l.get(0) instanceof LocalhostMachineProvisioningLocation, "l="+l); - } - - @SuppressWarnings("unchecked") - @Test - public void testRegistryCommaResolution() throws NoMachinesAvailableException { - List<Location> l; - l = getLocationResolver().resolve(JavaStringEscapes.unwrapJsonishListIfPossible("localhost,localhost,localhost")); - assertEquals(l.size(), 3, "l="+l); - assertTrue(l.get(0) instanceof LocalhostMachineProvisioningLocation, "l="+l); - assertTrue(l.get(1) instanceof LocalhostMachineProvisioningLocation, "l="+l); - assertTrue(l.get(2) instanceof LocalhostMachineProvisioningLocation, "l="+l); - - // And check works if comma in brackets - l = getLocationResolver().resolve(JavaStringEscapes.unwrapJsonishListIfPossible( - "[ \"byon:(hosts=\\\"192.168.0.1\\\",user=bob)\", \"byon:(hosts=\\\"192.168.0.2\\\",user=bob2)\" ]")); - assertEquals(l.size(), 2, "l="+l); - assertTrue(l.get(0) instanceof FixedListMachineProvisioningLocation, "l="+l); - assertTrue(l.get(1) instanceof FixedListMachineProvisioningLocation, "l="+l); - assertEquals(((FixedListMachineProvisioningLocation<SshMachineLocation>)l.get(0)).obtain().getUser(), "bob"); - assertEquals(((FixedListMachineProvisioningLocation<SshMachineLocation>)l.get(1)).obtain().getUser(), "bob2"); - } - - @Test(expectedExceptions={NoSuchElementException.class}) - public void testRegistryCommaResolutionInListNotAllowed1() throws NoMachinesAvailableException { - // disallowed since 0.7.0 - getLocationResolver().resolve(ImmutableList.of("localhost,localhost,localhost")); - } - - @Test(expectedExceptions={IllegalArgumentException.class}) - public void testRegistryCommaResolutionInListNotAllowed2() throws NoMachinesAvailableException { - // disallowed since 0.7.0 - // fails because it interprets the entire string as a single spec, which does not parse - getLocationResolver().resolve(ImmutableList.of("localhost(),localhost()")); - } - - @Test(expectedExceptions={IllegalArgumentException.class}) - public void testRegistryCommaResolutionInListNotAllowed3() throws NoMachinesAvailableException { - // disallowed since 0.7.0 - // fails because it interprets the entire string as a single spec, which does not parse - getLocationResolver().resolve(ImmutableList.of("localhost(name=a),localhost(name=b)")); - } - - @Test(expectedExceptions={IllegalArgumentException.class}) - public void testDoesNotAcceptsListOLists() { - ((BasicLocationRegistry)managementContext.getLocationRegistry()).resolve(ImmutableList.of(ImmutableList.of("localhost"))); - } - - @Test - public void testResolvesExplicitName() throws Exception { - Location location = resolve("localhost(name=myname)"); - assertTrue(location instanceof LocalhostMachineProvisioningLocation); - assertEquals(location.getDisplayName(), "myname"); - } - - @Test - public void testWithOldStyleColon() throws Exception { - Location location = resolve("localhost:(name=myname)"); - assertTrue(location instanceof LocalhostMachineProvisioningLocation); - assertEquals(location.getDisplayName(), "myname"); - } - - @Test - public void testResolvesPropertiesInSpec() throws Exception { - LocationInternal location = resolve("localhost(privateKeyFile=myprivatekeyfile,name=myname)"); - assertTrue(location instanceof LocalhostMachineProvisioningLocation); - assertEquals(location.getDisplayName(), "myname"); - assertEquals(location.config().getBag().getStringKey("privateKeyFile"), "myprivatekeyfile"); - } - - @Test - public void testResolvesDefaultName() throws Exception { - Location location = resolve("localhost"); - assertTrue(location instanceof LocalhostMachineProvisioningLocation); - assertEquals(location.getDisplayName(), "localhost"); - - Location location2 = resolve("localhost()"); - assertTrue(location2 instanceof LocalhostMachineProvisioningLocation); - assertEquals(location2.getDisplayName(), "localhost"); - } - - private BasicLocationRegistry getLocationResolver() { - return (BasicLocationRegistry) managementContext.getLocationRegistry(); - } - - private LocationInternal resolve(String val) { - Location l = managementContext.getLocationRegistry().resolve(val); - Assert.assertNotNull(l); - return (LocationInternal) l; - } - - private void assertThrowsNoSuchElement(String val) { - try { - resolve(val); - fail(); - } catch (NoSuchElementException e) { - // success - } - - // and check the long form returns an Absent (not throwing) - Assert.assertTrue(managementContext.getLocationRegistry().resolve(val, false, null).isAbsent()); - } - - private void assertThrowsIllegalArgument(String val) { - try { - resolve(val); - fail(); - } catch (IllegalArgumentException e) { - // success - } - - // and check the long form returns an Absent (not throwing) - Assert.assertTrue(managementContext.getLocationRegistry().resolve(val, false, null).isAbsent()); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/538324e1/core/src/test/java/org/apache/brooklyn/location/core/localhost/LocalhostMachineProvisioningLocationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/core/localhost/LocalhostMachineProvisioningLocationTest.java b/core/src/test/java/org/apache/brooklyn/location/core/localhost/LocalhostMachineProvisioningLocationTest.java deleted file mode 100644 index 041a38f..0000000 --- a/core/src/test/java/org/apache/brooklyn/location/core/localhost/LocalhostMachineProvisioningLocationTest.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * 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.location.core.localhost; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.fail; - -import java.net.ServerSocket; - -import org.apache.brooklyn.util.collections.MutableMap; -import org.apache.brooklyn.util.net.Networking; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -import org.apache.brooklyn.api.location.LocationSpec; -import org.apache.brooklyn.api.location.MachineProvisioningLocation; -import org.apache.brooklyn.api.location.NoMachinesAvailableException; -import org.apache.brooklyn.api.location.PortRange; -import org.apache.brooklyn.core.entity.Entities; -import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; -import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; -import org.apache.brooklyn.location.core.PortRanges; -import org.apache.brooklyn.location.geo.HostGeoInfo; -import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation; -import org.apache.brooklyn.location.ssh.SshMachineLocation; - -public class LocalhostMachineProvisioningLocationTest { - - private static final Logger log = LoggerFactory.getLogger(LocalhostMachineProvisioningLocationTest.class); - - private LocalManagementContext mgmt; - - @BeforeMethod - @AfterClass - protected void clearStatics() { - LocalhostMachineProvisioningLocation.clearStaticData(); - } - - @BeforeClass - protected void setup() { - mgmt = LocalManagementContextForTests.newInstance(); - } - - @AfterClass - protected void teardown() { - Entities.destroyAll(mgmt); - } - - protected LocalhostMachineProvisioningLocation newLocalhostProvisioner() { - return mgmt.getLocationManager().createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class)); - } - - protected LocalhostMachineProvisioningLocation newLocalhostProvisionerWithAddress(String address) { - return mgmt.getLocationManager().createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class) - .configure("address", address)); - } - - @Test - public void defaultInvocationCanProvisionALocalhostInstance() throws Exception { - LocalhostMachineProvisioningLocation provisioner = mgmt.getLocationManager().createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class)); - SshMachineLocation machine = provisioner.obtain(); - assertNotNull(machine); - assertEquals(machine.getAddress(), Networking.getLocalHost()); - } - - @Test - public void testUsesLocationNameProvided() throws Exception { - LocalhostMachineProvisioningLocation provisioner = newLocalhostProvisionerWithAddress("localhost"); - assertEquals(((SshMachineLocation)provisioner.obtain()).getAddress().getHostName(), "localhost"); - - LocalhostMachineProvisioningLocation provisioner2 = newLocalhostProvisionerWithAddress("1.2.3.4"); - assertEquals(((SshMachineLocation)provisioner2.obtain()).getAddress().getHostName(), "1.2.3.4"); - - LocalhostMachineProvisioningLocation provisioner3 = newLocalhostProvisionerWithAddress("127.0.0.1"); - assertEquals(((SshMachineLocation)provisioner3.obtain()).getAddress().getHostName(), "127.0.0.1"); - } - - public void provisionWithASpecificNumberOfInstances() throws NoMachinesAvailableException { - LocalhostMachineProvisioningLocation provisioner = mgmt.getLocationManager().createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class) - .configure("count", 2)); - - // first machine - SshMachineLocation first = provisioner.obtain(); - assertNotNull(first); - assertEquals(first.getAddress(), Networking.getLocalHost()); - - // second machine - SshMachineLocation second = provisioner.obtain(); - assertNotNull(second); - assertEquals(second.getAddress(), Networking.getLocalHost()); - - // third machine - fails - try { - SshMachineLocation third = provisioner.obtain(); - fail("did not throw expected exception; got "+third); - } catch (NoMachinesAvailableException e) { - /* expected */ - } - } - - @Test - public void obtainTwoAddressesInRangeThenDontObtain() throws Exception { - LocalhostMachineProvisioningLocation p = newLocalhostProvisioner(); - SshMachineLocation m = p.obtain(); - - // Find two ports that are free, rather than risk false-negatives if a port was left open by something else. - int start = 48311; - while (true) { - if (Networking.isPortAvailable(m.getAddress(), start) && Networking.isPortAvailable(m.getAddress(), start+1)) { - break; - } else { - start++; - } - } - PortRange r = PortRanges.fromString(""+start+"-"+(start+1)); - - try { - int i1 = m.obtainPort(r); - Assert.assertEquals(i1, start); - int i2 = m.obtainPort(r); - Assert.assertEquals(i2, start+1); - - //should fail - int i3 = m.obtainPort(r); - Assert.assertEquals(i3, -1); - - //releasing and reapplying should succed - m.releasePort(i2); - int i4 = m.obtainPort(r); - Assert.assertEquals(i4, i2); - - } finally { - m.releasePort(start); - m.releasePort(start+1); - } - } - - @Test - public void obtainLowNumberedPortsAutomatically() throws Exception { - LocalhostMachineProvisioningLocation p = newLocalhostProvisioner(); - SshMachineLocation m = p.obtain(); - int start = 983; //random rarely used port, not that it matters - try { - int actual = m.obtainPort(PortRanges.fromInteger(start)); - Assert.assertEquals(actual, start); - } finally { - m.releasePort(start); - } - - } - - @Test - public void obtainPortFailsIfInUse() throws Exception { - LocalhostMachineProvisioningLocation p = newLocalhostProvisioner(); - SshMachineLocation m = p.obtain(); - - // Find two ports that are free, rather than risk false-negatives if a port was left open by something else. - int start = 48311; - while (true) { - if (Networking.isPortAvailable(m.getAddress(), start) && Networking.isPortAvailable(m.getAddress(), start+1)) { - break; - } else { - start++; - } - } - PortRange r = PortRanges.fromString(""+start+"-"+(start+1)); - - ServerSocket ss = null; - try { - ss = new ServerSocket(start, 0, m.getAddress()); - int i1 = m.obtainPort(r); - Assert.assertEquals(i1, start+1); - } finally { - if (ss!=null) ss.close(); - m.releasePort(start); - m.releasePort(start+1); - } - } - - @Test - public void obtainLocationWithGeography() throws Exception { - mgmt.getBrooklynProperties().put("brooklyn.location.named.lhx", "localhost"); - // bogus location so very little chance of it being what maxmind returns! - mgmt.getBrooklynProperties().put("brooklyn.location.named.lhx.latitude", 42d); - mgmt.getBrooklynProperties().put("brooklyn.location.named.lhx.longitude", -20d); - MachineProvisioningLocation<?> p = (MachineProvisioningLocation<?>) mgmt.getLocationRegistry().resolve("named:lhx"); - SshMachineLocation m = (SshMachineLocation) p.obtain(MutableMap.of()); - HostGeoInfo geo = HostGeoInfo.fromLocation(m); - log.info("Geo info for "+m+" is: "+geo); - Assert.assertEquals(geo.latitude, 42d, 0.00001); - Assert.assertEquals(geo.longitude, -20d, 0.00001); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/538324e1/core/src/test/java/org/apache/brooklyn/location/core/localhost/LocalhostProvisioningAndAccessTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/core/localhost/LocalhostProvisioningAndAccessTest.java b/core/src/test/java/org/apache/brooklyn/location/core/localhost/LocalhostProvisioningAndAccessTest.java deleted file mode 100644 index 92903df..0000000 --- a/core/src/test/java/org/apache/brooklyn/location/core/localhost/LocalhostProvisioningAndAccessTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.location.core.localhost; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; - -import java.util.Arrays; - -import org.apache.brooklyn.api.location.Location; -import org.apache.brooklyn.core.entity.Entities; -import org.apache.brooklyn.core.internal.BrooklynProperties; -import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; -import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation; -import org.apache.brooklyn.location.ssh.SshMachineLocation; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -public class LocalhostProvisioningAndAccessTest { - - private LocalManagementContext mgmt; - - @BeforeMethod(alwaysRun=true) - public void setUp() throws Exception { - mgmt = new LocalManagementContext(BrooklynProperties.Factory.newDefault()); - } - - @AfterMethod(alwaysRun = true) - public void tearDown(){ - if (mgmt != null) Entities.destroyAll(mgmt); - } - - @Test(groups="Integration") - public void testProvisionAndConnect() throws Exception { - Location location = mgmt.getLocationRegistry().resolve("localhost"); - assertTrue(location instanceof LocalhostMachineProvisioningLocation); - SshMachineLocation m = ((LocalhostMachineProvisioningLocation)location).obtain(); - int result = m.execCommands("test", Arrays.asList("echo hello world")); - assertEquals(result, 0); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/538324e1/core/src/test/java/org/apache/brooklyn/location/geo/HostGeoInfoTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/geo/HostGeoInfoTest.java b/core/src/test/java/org/apache/brooklyn/location/geo/HostGeoInfoTest.java deleted file mode 100644 index 56c1b41..0000000 --- a/core/src/test/java/org/apache/brooklyn/location/geo/HostGeoInfoTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.location.geo; - -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.AssertJUnit.assertNotNull; - -import org.apache.brooklyn.api.location.Location; -import org.apache.brooklyn.location.core.SimulatedLocation; -import org.apache.brooklyn.util.collections.MutableMap; -import org.testng.annotations.Test; - -public class HostGeoInfoTest { - private static final String IP = "192.168.0.1"; - - private static final Location CUSTOM_LOCATION = new SimulatedLocation(MutableMap.of("name", "custom", "latitude", 50d, "longitude", 0d)); - private static final Location CUSTOM_LOCATION_CHILD = new SimulatedLocation(MutableMap.of("name", "custom-child", "address", IP, "parentLocation", CUSTOM_LOCATION)); - - @Test - public void testCustomLocationCoordinates() { - HostGeoInfo hgi = HostGeoInfo.fromLocation(CUSTOM_LOCATION); - assertNotNull(hgi); - assertEquals(50.0d, hgi.latitude); - assertEquals(0.0d, hgi.longitude); - } - - @Test - public void testCustomLocationChildCoordinates() { - HostGeoInfo hgi = HostGeoInfo.fromLocation(CUSTOM_LOCATION_CHILD); - assertNotNull(hgi); - assertEquals(50.0d, hgi.latitude); - assertEquals(0.0d, hgi.longitude); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/538324e1/core/src/test/java/org/apache/brooklyn/location/geo/HostGeoLookupIntegrationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/geo/HostGeoLookupIntegrationTest.java b/core/src/test/java/org/apache/brooklyn/location/geo/HostGeoLookupIntegrationTest.java deleted file mode 100644 index 1990905..0000000 --- a/core/src/test/java/org/apache/brooklyn/location/geo/HostGeoLookupIntegrationTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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.location.geo; - -import java.net.InetAddress; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.Test; -import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation; -import org.apache.brooklyn.location.ssh.SshMachineLocation; -import org.apache.brooklyn.util.time.Duration; - -import com.google.common.base.Objects; - -public class HostGeoLookupIntegrationTest { - - public static final Logger log = LoggerFactory.getLogger(HostGeoLookupIntegrationTest.class); - - // Needs fast network connectivity to figure out the external IP. If response not returned in 2s fails. - @Test(groups = "Integration") - public void testLocalhostGetsLocation() throws Exception { - LocalhostMachineProvisioningLocation ll = new LocalhostMachineProvisioningLocation(); - SshMachineLocation l = ll.obtain(); - HostGeoInfo geo = HostGeoInfo.fromLocation(l); - Assert.assertNotNull(geo, "host lookup unavailable - is the maxmind database installed? or else network unavailable or too slow?"); - log.info("localhost is in "+geo); - Assert.assertNotNull(geo, "couldn't load data; must have a valid HostGeoLookup impl (e.g. MaxMind installed, or online and with Utrace credit)"); - Assert.assertTrue(-90 <= geo.latitude && geo.latitude <= 90); - ll.close(); - } - - @Deprecated // see GeoBytesHostGeoLookup - their API changed - @Test(groups = "Integration", enabled=false) - public void testGeobytesLookup() throws Exception { - HostGeoInfo geo = new GeoBytesHostGeoLookup().getHostGeoInfo(InetAddress.getByName("geobytes.com")); - Assert.assertNotNull(geo, "host lookup unavailable"); - Assert.assertEquals(geo.displayName, "Baltimore (US)"); - Assert.assertEquals(geo.latitude, 39.2894, 0.1); - Assert.assertEquals(geo.longitude, -76.6384, 0.1); - } - - @Test(groups = "Integration") - public void testUtraceLookup() throws Exception { - // The test times out in a VM - VirtualBox + Ubuntu Vivid, possibly due to proxy usage? - // Increase the timeout so we can actually test it's working correctly, regardless of test environment. - HostGeoInfo geo = new UtraceHostGeoLookup().getHostGeoInfo(InetAddress.getByName("utrace.de"), Duration.THIRTY_SECONDS); - Assert.assertNotNull(geo, "host lookup unavailable - maybe network not available "); - Assert.assertTrue(geo.displayName.contains("(DE)")); - Assert.assertEquals(geo.latitude, 51, 2); - Assert.assertEquals(geo.longitude, 9, 5); - } - - @Test(groups = "Integration") // only works if maxmind database is installed to ~/.brooklyn/ - public void testMaxmindLookup() throws Exception { - HostGeoInfo geo = new MaxMind2HostGeoLookup().getHostGeoInfo(InetAddress.getByName("maxmind.com")); - Assert.assertNotNull(geo, "host lookup unavailable - is the maxmind database installed?"); - log.info("maxmind.com at "+geo); - - // used to be Washington; now Dallas - in case this changes again, we will accept either! - // also have seen variation in lat/lon reported, so happy to within one degree now - Assert.assertTrue(Objects.equal(geo.displayName, "Washington, DC (US)") || Objects.equal(geo.displayName, "Dallas, TX (US)"), "name="+geo.displayName); - Assert.assertTrue(Math.abs(geo.latitude - 38.90) <= 1 || Math.abs(geo.latitude - 32.78) <= 1, "lat="+geo.latitude); - Assert.assertTrue(Math.abs(geo.longitude - -77.02) <= 1 || Math.abs(geo.longitude - -96.82) <= 1, "lon="+geo.longitude); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/538324e1/core/src/test/java/org/apache/brooklyn/location/geo/LocalhostExternalIpLoaderIntegrationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/geo/LocalhostExternalIpLoaderIntegrationTest.java b/core/src/test/java/org/apache/brooklyn/location/geo/LocalhostExternalIpLoaderIntegrationTest.java deleted file mode 100644 index e686985..0000000 --- a/core/src/test/java/org/apache/brooklyn/location/geo/LocalhostExternalIpLoaderIntegrationTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.location.geo; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; - -import java.util.Set; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.Test; - -import com.google.common.collect.Sets; - -public class LocalhostExternalIpLoaderIntegrationTest { - - private static final Logger LOG = LoggerFactory.getLogger(LocalhostExternalIpLoaderIntegrationTest.class); - - @Test(groups = "Integration") - public void testHostsAgreeOnExternalIp() { - Set<String> ips = Sets.newHashSet(); - for (String url : LocalhostExternalIpLoader.getIpAddressWebsites()) { - String ip = LocalhostExternalIpLoader.getIpAddressFrom(url); - LOG.debug("IP from {}: {}", url, ip); - ips.add(ip); - } - assertEquals(ips.size(), 1, "Expected all IP suppliers to agree on the external IP address of Brooklyn. " + - "Check logs for source responses. ips=" + ips); - } - - @Test(groups = "Integration") - public void testLoadExternalIp() { - assertNotNull(LocalhostExternalIpLoader.getLocalhostIpWaiting()); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/538324e1/core/src/test/java/org/apache/brooklyn/location/localhost/LocalhostLocationResolverTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/localhost/LocalhostLocationResolverTest.java b/core/src/test/java/org/apache/brooklyn/location/localhost/LocalhostLocationResolverTest.java new file mode 100644 index 0000000..cbc003f --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/location/localhost/LocalhostLocationResolverTest.java @@ -0,0 +1,269 @@ +/* + * 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.location.localhost; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; + +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; + +import org.apache.brooklyn.api.location.Location; +import org.apache.brooklyn.api.location.NoMachinesAvailableException; +import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.internal.BrooklynProperties; +import org.apache.brooklyn.core.location.BasicLocationRegistry; +import org.apache.brooklyn.core.location.internal.LocationInternal; +import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; +import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; +import org.apache.brooklyn.location.byon.FixedListMachineProvisioningLocation; +import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation; +import org.apache.brooklyn.location.ssh.SshMachineLocation; +import org.apache.brooklyn.util.text.StringEscapes.JavaStringEscapes; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; + +public class LocalhostLocationResolverTest { + + private BrooklynProperties brooklynProperties; + private LocalManagementContext managementContext; + + @BeforeMethod(alwaysRun=true) + public void setUp() throws Exception { + managementContext = LocalManagementContextForTests.newInstance(); + brooklynProperties = managementContext.getBrooklynProperties(); + } + + @AfterMethod(alwaysRun=true) + public void tearDown() throws Exception { + if (managementContext != null) Entities.destroyAll(managementContext); + } + + @Test + public void testTakesLocalhostScopedProperties() { + brooklynProperties.put("brooklyn.location.localhost.privateKeyFile", "myprivatekeyfile"); + brooklynProperties.put("brooklyn.location.localhost.publicKeyFile", "mypublickeyfile"); + brooklynProperties.put("brooklyn.location.localhost.privateKeyData", "myprivateKeyData"); + brooklynProperties.put("brooklyn.location.localhost.publicKeyData", "myPublicKeyData"); + brooklynProperties.put("brooklyn.location.localhost.privateKeyPassphrase", "myprivateKeyPassphrase"); + + Map<String, Object> conf = resolve("localhost").config().getBag().getAllConfig(); + + assertEquals(conf.get("privateKeyFile"), "myprivatekeyfile"); + assertEquals(conf.get("publicKeyFile"), "mypublickeyfile"); + assertEquals(conf.get("privateKeyData"), "myprivateKeyData"); + assertEquals(conf.get("publicKeyData"), "myPublicKeyData"); + assertEquals(conf.get("privateKeyPassphrase"), "myprivateKeyPassphrase"); + } + + @Test + public void testTakesLocalhostDeprecatedScopedProperties() { + brooklynProperties.put("brooklyn.localhost.privateKeyFile", "myprivatekeyfile"); + brooklynProperties.put("brooklyn.localhost.publicKeyFile", "mypublickeyfile"); + brooklynProperties.put("brooklyn.localhost.privateKeyData", "myprivateKeyData"); + brooklynProperties.put("brooklyn.localhost.publicKeyData", "myPublicKeyData"); + brooklynProperties.put("brooklyn.localhost.privateKeyPassphrase", "myprivateKeyPassphrase"); + + Map<String, Object> conf = resolve("localhost").config().getBag().getAllConfig(); + + assertEquals(conf.get("privateKeyFile"), "myprivatekeyfile"); + assertEquals(conf.get("publicKeyFile"), "mypublickeyfile"); + assertEquals(conf.get("privateKeyData"), "myprivateKeyData"); + assertEquals(conf.get("publicKeyData"), "myPublicKeyData"); + assertEquals(conf.get("privateKeyPassphrase"), "myprivateKeyPassphrase"); + } + + @Test + public void testTakesDeprecatedProperties() { + brooklynProperties.put("brooklyn.localhost.private-key-file", "myprivatekeyfile"); + brooklynProperties.put("brooklyn.localhost.public-key-file", "mypublickeyfile"); + brooklynProperties.put("brooklyn.localhost.private-key-data", "myprivateKeyData"); + brooklynProperties.put("brooklyn.localhost.public-key-data", "myPublicKeyData"); + brooklynProperties.put("brooklyn.localhost.private-key-passphrase", "myprivateKeyPassphrase"); + Map<String, Object> conf = resolve("localhost").config().getBag().getAllConfig(); + + assertEquals(conf.get("privateKeyFile"), "myprivatekeyfile"); + assertEquals(conf.get("publicKeyFile"), "mypublickeyfile"); + assertEquals(conf.get("privateKeyData"), "myprivateKeyData"); + assertEquals(conf.get("publicKeyData"), "myPublicKeyData"); + assertEquals(conf.get("privateKeyPassphrase"), "myprivateKeyPassphrase"); + } + + @Test + public void testPropertyScopePrescedence() { + brooklynProperties.put("brooklyn.location.named.mynamed", "localhost"); + + // prefer those in "named" over everything else + brooklynProperties.put("brooklyn.location.named.mynamed.privateKeyFile", "privateKeyFile-inNamed"); + brooklynProperties.put("brooklyn.location.localhost.privateKeyFile", "privateKeyFile-inProviderSpecific"); + brooklynProperties.put("brooklyn.localhost.privateKeyFile", "privateKeyFile-inGeneric"); + + // prefer those in provider-specific over generic + brooklynProperties.put("brooklyn.location.localhost.publicKeyFile", "publicKeyFile-inProviderSpecific"); + brooklynProperties.put("brooklyn.location.publicKeyFile", "publicKeyFile-inGeneric"); + + // prefer location-generic if nothing else + brooklynProperties.put("brooklyn.location.privateKeyData", "privateKeyData-inGeneric"); + + Map<String, Object> conf = resolve("named:mynamed").config().getBag().getAllConfig(); + + assertEquals(conf.get("privateKeyFile"), "privateKeyFile-inNamed"); + assertEquals(conf.get("publicKeyFile"), "publicKeyFile-inProviderSpecific"); + assertEquals(conf.get("privateKeyData"), "privateKeyData-inGeneric"); + } + + @Test + public void testLocalhostLoads() { + Assert.assertTrue(resolve("localhost") instanceof LocalhostMachineProvisioningLocation); + } + + @Test + public void testThrowsOnInvalid() throws Exception { + assertThrowsNoSuchElement("wrongprefix"); + assertThrowsIllegalArgument("localhost(name=abc"); // no closing bracket + assertThrowsIllegalArgument("localhost(name)"); // no value for name + assertThrowsIllegalArgument("localhost(name=)"); // no value for name + } + + + @Test + public void testAcceptsList() { + List<Location> l = getLocationResolver().resolve(ImmutableList.of("localhost")); + assertEquals(l.size(), 1, "l="+l); + assertTrue(l.get(0) instanceof LocalhostMachineProvisioningLocation, "l="+l); + } + + @SuppressWarnings("unchecked") + @Test + public void testRegistryCommaResolution() throws NoMachinesAvailableException { + List<Location> l; + l = getLocationResolver().resolve(JavaStringEscapes.unwrapJsonishListIfPossible("localhost,localhost,localhost")); + assertEquals(l.size(), 3, "l="+l); + assertTrue(l.get(0) instanceof LocalhostMachineProvisioningLocation, "l="+l); + assertTrue(l.get(1) instanceof LocalhostMachineProvisioningLocation, "l="+l); + assertTrue(l.get(2) instanceof LocalhostMachineProvisioningLocation, "l="+l); + + // And check works if comma in brackets + l = getLocationResolver().resolve(JavaStringEscapes.unwrapJsonishListIfPossible( + "[ \"byon:(hosts=\\\"192.168.0.1\\\",user=bob)\", \"byon:(hosts=\\\"192.168.0.2\\\",user=bob2)\" ]")); + assertEquals(l.size(), 2, "l="+l); + assertTrue(l.get(0) instanceof FixedListMachineProvisioningLocation, "l="+l); + assertTrue(l.get(1) instanceof FixedListMachineProvisioningLocation, "l="+l); + assertEquals(((FixedListMachineProvisioningLocation<SshMachineLocation>)l.get(0)).obtain().getUser(), "bob"); + assertEquals(((FixedListMachineProvisioningLocation<SshMachineLocation>)l.get(1)).obtain().getUser(), "bob2"); + } + + @Test(expectedExceptions={NoSuchElementException.class}) + public void testRegistryCommaResolutionInListNotAllowed1() throws NoMachinesAvailableException { + // disallowed since 0.7.0 + getLocationResolver().resolve(ImmutableList.of("localhost,localhost,localhost")); + } + + @Test(expectedExceptions={IllegalArgumentException.class}) + public void testRegistryCommaResolutionInListNotAllowed2() throws NoMachinesAvailableException { + // disallowed since 0.7.0 + // fails because it interprets the entire string as a single spec, which does not parse + getLocationResolver().resolve(ImmutableList.of("localhost(),localhost()")); + } + + @Test(expectedExceptions={IllegalArgumentException.class}) + public void testRegistryCommaResolutionInListNotAllowed3() throws NoMachinesAvailableException { + // disallowed since 0.7.0 + // fails because it interprets the entire string as a single spec, which does not parse + getLocationResolver().resolve(ImmutableList.of("localhost(name=a),localhost(name=b)")); + } + + @Test(expectedExceptions={IllegalArgumentException.class}) + public void testDoesNotAcceptsListOLists() { + ((BasicLocationRegistry)managementContext.getLocationRegistry()).resolve(ImmutableList.of(ImmutableList.of("localhost"))); + } + + @Test + public void testResolvesExplicitName() throws Exception { + Location location = resolve("localhost(name=myname)"); + assertTrue(location instanceof LocalhostMachineProvisioningLocation); + assertEquals(location.getDisplayName(), "myname"); + } + + @Test + public void testWithOldStyleColon() throws Exception { + Location location = resolve("localhost:(name=myname)"); + assertTrue(location instanceof LocalhostMachineProvisioningLocation); + assertEquals(location.getDisplayName(), "myname"); + } + + @Test + public void testResolvesPropertiesInSpec() throws Exception { + LocationInternal location = resolve("localhost(privateKeyFile=myprivatekeyfile,name=myname)"); + assertTrue(location instanceof LocalhostMachineProvisioningLocation); + assertEquals(location.getDisplayName(), "myname"); + assertEquals(location.config().getBag().getStringKey("privateKeyFile"), "myprivatekeyfile"); + } + + @Test + public void testResolvesDefaultName() throws Exception { + Location location = resolve("localhost"); + assertTrue(location instanceof LocalhostMachineProvisioningLocation); + assertEquals(location.getDisplayName(), "localhost"); + + Location location2 = resolve("localhost()"); + assertTrue(location2 instanceof LocalhostMachineProvisioningLocation); + assertEquals(location2.getDisplayName(), "localhost"); + } + + private BasicLocationRegistry getLocationResolver() { + return (BasicLocationRegistry) managementContext.getLocationRegistry(); + } + + private LocationInternal resolve(String val) { + Location l = managementContext.getLocationRegistry().resolve(val); + Assert.assertNotNull(l); + return (LocationInternal) l; + } + + private void assertThrowsNoSuchElement(String val) { + try { + resolve(val); + fail(); + } catch (NoSuchElementException e) { + // success + } + + // and check the long form returns an Absent (not throwing) + Assert.assertTrue(managementContext.getLocationRegistry().resolve(val, false, null).isAbsent()); + } + + private void assertThrowsIllegalArgument(String val) { + try { + resolve(val); + fail(); + } catch (IllegalArgumentException e) { + // success + } + + // and check the long form returns an Absent (not throwing) + Assert.assertTrue(managementContext.getLocationRegistry().resolve(val, false, null).isAbsent()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/538324e1/core/src/test/java/org/apache/brooklyn/location/localhost/LocalhostMachineProvisioningLocationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/localhost/LocalhostMachineProvisioningLocationTest.java b/core/src/test/java/org/apache/brooklyn/location/localhost/LocalhostMachineProvisioningLocationTest.java new file mode 100644 index 0000000..c924cd6 --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/location/localhost/LocalhostMachineProvisioningLocationTest.java @@ -0,0 +1,215 @@ +/* + * 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.location.localhost; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.fail; + +import java.net.ServerSocket; + +import org.apache.brooklyn.util.collections.MutableMap; +import org.apache.brooklyn.util.net.Networking; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; +import org.apache.brooklyn.api.location.LocationSpec; +import org.apache.brooklyn.api.location.MachineProvisioningLocation; +import org.apache.brooklyn.api.location.NoMachinesAvailableException; +import org.apache.brooklyn.api.location.PortRange; +import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.location.PortRanges; +import org.apache.brooklyn.core.location.geo.HostGeoInfo; +import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; +import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; +import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation; +import org.apache.brooklyn.location.ssh.SshMachineLocation; + +public class LocalhostMachineProvisioningLocationTest { + + private static final Logger log = LoggerFactory.getLogger(LocalhostMachineProvisioningLocationTest.class); + + private LocalManagementContext mgmt; + + @BeforeMethod + @AfterClass + protected void clearStatics() { + LocalhostMachineProvisioningLocation.clearStaticData(); + } + + @BeforeClass + protected void setup() { + mgmt = LocalManagementContextForTests.newInstance(); + } + + @AfterClass + protected void teardown() { + Entities.destroyAll(mgmt); + } + + protected LocalhostMachineProvisioningLocation newLocalhostProvisioner() { + return mgmt.getLocationManager().createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class)); + } + + protected LocalhostMachineProvisioningLocation newLocalhostProvisionerWithAddress(String address) { + return mgmt.getLocationManager().createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class) + .configure("address", address)); + } + + @Test + public void defaultInvocationCanProvisionALocalhostInstance() throws Exception { + LocalhostMachineProvisioningLocation provisioner = mgmt.getLocationManager().createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class)); + SshMachineLocation machine = provisioner.obtain(); + assertNotNull(machine); + assertEquals(machine.getAddress(), Networking.getLocalHost()); + } + + @Test + public void testUsesLocationNameProvided() throws Exception { + LocalhostMachineProvisioningLocation provisioner = newLocalhostProvisionerWithAddress("localhost"); + assertEquals(((SshMachineLocation)provisioner.obtain()).getAddress().getHostName(), "localhost"); + + LocalhostMachineProvisioningLocation provisioner2 = newLocalhostProvisionerWithAddress("1.2.3.4"); + assertEquals(((SshMachineLocation)provisioner2.obtain()).getAddress().getHostName(), "1.2.3.4"); + + LocalhostMachineProvisioningLocation provisioner3 = newLocalhostProvisionerWithAddress("127.0.0.1"); + assertEquals(((SshMachineLocation)provisioner3.obtain()).getAddress().getHostName(), "127.0.0.1"); + } + + public void provisionWithASpecificNumberOfInstances() throws NoMachinesAvailableException { + LocalhostMachineProvisioningLocation provisioner = mgmt.getLocationManager().createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class) + .configure("count", 2)); + + // first machine + SshMachineLocation first = provisioner.obtain(); + assertNotNull(first); + assertEquals(first.getAddress(), Networking.getLocalHost()); + + // second machine + SshMachineLocation second = provisioner.obtain(); + assertNotNull(second); + assertEquals(second.getAddress(), Networking.getLocalHost()); + + // third machine - fails + try { + SshMachineLocation third = provisioner.obtain(); + fail("did not throw expected exception; got "+third); + } catch (NoMachinesAvailableException e) { + /* expected */ + } + } + + @Test + public void obtainTwoAddressesInRangeThenDontObtain() throws Exception { + LocalhostMachineProvisioningLocation p = newLocalhostProvisioner(); + SshMachineLocation m = p.obtain(); + + // Find two ports that are free, rather than risk false-negatives if a port was left open by something else. + int start = 48311; + while (true) { + if (Networking.isPortAvailable(m.getAddress(), start) && Networking.isPortAvailable(m.getAddress(), start+1)) { + break; + } else { + start++; + } + } + PortRange r = PortRanges.fromString(""+start+"-"+(start+1)); + + try { + int i1 = m.obtainPort(r); + Assert.assertEquals(i1, start); + int i2 = m.obtainPort(r); + Assert.assertEquals(i2, start+1); + + //should fail + int i3 = m.obtainPort(r); + Assert.assertEquals(i3, -1); + + //releasing and reapplying should succed + m.releasePort(i2); + int i4 = m.obtainPort(r); + Assert.assertEquals(i4, i2); + + } finally { + m.releasePort(start); + m.releasePort(start+1); + } + } + + @Test + public void obtainLowNumberedPortsAutomatically() throws Exception { + LocalhostMachineProvisioningLocation p = newLocalhostProvisioner(); + SshMachineLocation m = p.obtain(); + int start = 983; //random rarely used port, not that it matters + try { + int actual = m.obtainPort(PortRanges.fromInteger(start)); + Assert.assertEquals(actual, start); + } finally { + m.releasePort(start); + } + + } + + @Test + public void obtainPortFailsIfInUse() throws Exception { + LocalhostMachineProvisioningLocation p = newLocalhostProvisioner(); + SshMachineLocation m = p.obtain(); + + // Find two ports that are free, rather than risk false-negatives if a port was left open by something else. + int start = 48311; + while (true) { + if (Networking.isPortAvailable(m.getAddress(), start) && Networking.isPortAvailable(m.getAddress(), start+1)) { + break; + } else { + start++; + } + } + PortRange r = PortRanges.fromString(""+start+"-"+(start+1)); + + ServerSocket ss = null; + try { + ss = new ServerSocket(start, 0, m.getAddress()); + int i1 = m.obtainPort(r); + Assert.assertEquals(i1, start+1); + } finally { + if (ss!=null) ss.close(); + m.releasePort(start); + m.releasePort(start+1); + } + } + + @Test + public void obtainLocationWithGeography() throws Exception { + mgmt.getBrooklynProperties().put("brooklyn.location.named.lhx", "localhost"); + // bogus location so very little chance of it being what maxmind returns! + mgmt.getBrooklynProperties().put("brooklyn.location.named.lhx.latitude", 42d); + mgmt.getBrooklynProperties().put("brooklyn.location.named.lhx.longitude", -20d); + MachineProvisioningLocation<?> p = (MachineProvisioningLocation<?>) mgmt.getLocationRegistry().resolve("named:lhx"); + SshMachineLocation m = (SshMachineLocation) p.obtain(MutableMap.of()); + HostGeoInfo geo = HostGeoInfo.fromLocation(m); + log.info("Geo info for "+m+" is: "+geo); + Assert.assertEquals(geo.latitude, 42d, 0.00001); + Assert.assertEquals(geo.longitude, -20d, 0.00001); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/538324e1/core/src/test/java/org/apache/brooklyn/location/localhost/LocalhostProvisioningAndAccessTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/localhost/LocalhostProvisioningAndAccessTest.java b/core/src/test/java/org/apache/brooklyn/location/localhost/LocalhostProvisioningAndAccessTest.java new file mode 100644 index 0000000..58d6709 --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/location/localhost/LocalhostProvisioningAndAccessTest.java @@ -0,0 +1,59 @@ +/* + * 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.location.localhost; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +import java.util.Arrays; + +import org.apache.brooklyn.api.location.Location; +import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.internal.BrooklynProperties; +import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; +import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation; +import org.apache.brooklyn.location.ssh.SshMachineLocation; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class LocalhostProvisioningAndAccessTest { + + private LocalManagementContext mgmt; + + @BeforeMethod(alwaysRun=true) + public void setUp() throws Exception { + mgmt = new LocalManagementContext(BrooklynProperties.Factory.newDefault()); + } + + @AfterMethod(alwaysRun = true) + public void tearDown(){ + if (mgmt != null) Entities.destroyAll(mgmt); + } + + @Test(groups="Integration") + public void testProvisionAndConnect() throws Exception { + Location location = mgmt.getLocationRegistry().resolve("localhost"); + assertTrue(location instanceof LocalhostMachineProvisioningLocation); + SshMachineLocation m = ((LocalhostMachineProvisioningLocation)location).obtain(); + int result = m.execCommands("test", Arrays.asList("echo hello world")); + assertEquals(result, 0); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/538324e1/core/src/test/java/org/apache/brooklyn/location/multi/MultiLocationRebindTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/multi/MultiLocationRebindTest.java b/core/src/test/java/org/apache/brooklyn/location/multi/MultiLocationRebindTest.java new file mode 100644 index 0000000..0170b5a --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/location/multi/MultiLocationRebindTest.java @@ -0,0 +1,122 @@ +/* + * 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.location.multi; + +import java.io.File; +import java.util.List; + +import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.api.location.Location; +import org.apache.brooklyn.api.location.LocationSpec; +import org.apache.brooklyn.api.mgmt.ManagementContext; +import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.entity.factory.ApplicationBuilder; +import org.apache.brooklyn.core.location.cloud.AvailabilityZoneExtension; +import org.apache.brooklyn.core.mgmt.rebind.RebindTestUtils; +import org.apache.brooklyn.core.test.entity.TestApplication; +import org.apache.brooklyn.location.byon.FixedListMachineProvisioningLocation; +import org.apache.brooklyn.location.multi.MultiLocation; +import org.apache.brooklyn.location.ssh.SshMachineLocation; +import org.apache.brooklyn.test.Asserts; +import org.apache.brooklyn.util.collections.MutableSet; +import org.apache.brooklyn.util.net.Networking; +import org.apache.brooklyn.util.os.Os; +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.Predicates; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; + +public class MultiLocationRebindTest { + + private ClassLoader classLoader = getClass().getClassLoader(); + private ManagementContext origManagementContext; + private ManagementContext newManagementContext; + private File mementoDir; + + private TestApplication origApp; + private TestApplication newApp; + private SshMachineLocation mac1a; + private SshMachineLocation mac2a; + private FixedListMachineProvisioningLocation<SshMachineLocation> loc1; + private FixedListMachineProvisioningLocation<SshMachineLocation> loc2; + private MultiLocation<SshMachineLocation> multiLoc; + + @BeforeMethod(alwaysRun=true) + public void setUp() throws Exception { + mementoDir = Os.newTempDir(getClass()); + origManagementContext = RebindTestUtils.newPersistingManagementContext(mementoDir, classLoader, 1); + origApp = ApplicationBuilder.newManagedApp(EntitySpec.create(TestApplication.class), origManagementContext); + } + + @AfterMethod(alwaysRun=true) + public void tearDown() throws Exception { + if (origManagementContext != null) Entities.destroyAll(origManagementContext); + if (newApp != null) Entities.destroyAll(newApp.getManagementContext()); + if (newManagementContext != null) Entities.destroyAll(newManagementContext); + if (mementoDir != null) RebindTestUtils.deleteMementoDir(mementoDir); + } + + @SuppressWarnings("unchecked") + @Test + public void testRebindsMultiLocation() throws Exception { + mac1a = origManagementContext.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) + .displayName("mac1a") + .configure("address", Networking.getInetAddressWithFixedName("1.1.1.1"))); + mac2a = origManagementContext.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) + .displayName("mac2a") + .configure("address", Networking.getInetAddressWithFixedName("1.1.1.3"))); + loc1 = origManagementContext.getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class) + .displayName("loc1") + .configure("machines", MutableSet.of(mac1a))); + loc2 = origManagementContext.getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class) + .displayName("loc2") + .configure("machines", MutableSet.of(mac2a))); + multiLoc = origManagementContext.getLocationManager().createLocation(LocationSpec.create(MultiLocation.class) + .displayName("multiLoc") + .configure("subLocations", ImmutableList.of(loc1, loc2))); + + newApp = rebind(); + newManagementContext = newApp.getManagementContext(); + + MultiLocation newMultiLoc = (MultiLocation) Iterables.find(newManagementContext.getLocationManager().getLocations(), Predicates.instanceOf(MultiLocation.class)); + AvailabilityZoneExtension azExtension = newMultiLoc.getExtension(AvailabilityZoneExtension.class); + List<Location> newSublLocs = azExtension.getAllSubLocations(); + Iterable<String> newSubLocNames = Iterables.transform(newSublLocs, new Function<Location, String>() { + @Override public String apply(Location input) { + return (input == null) ? null : input.getDisplayName(); + }}); + Asserts.assertEqualsIgnoringOrder(newSubLocNames, ImmutableList.of("loc1", "loc2")); + } + + private TestApplication rebind() throws Exception { + return rebind(true); + } + + private TestApplication rebind(boolean checkSerializable) throws Exception { + RebindTestUtils.waitForPersisted(origApp); + if (checkSerializable) { + RebindTestUtils.checkCurrentMementoSerializable(origApp); + } + return (TestApplication) RebindTestUtils.rebind(mementoDir, getClass().getClassLoader()); + } +}
