You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2015/08/19 13:10:09 UTC
[51/72] [abbrv] incubator-brooklyn git commit: BROOKLYN-162 - jclouds
last few package prefixes needed,
and tidy in core and elsewhere related (or observed in the process)
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/entity/group/zoneaware/ProportionalZoneFailureDetectorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/entity/group/zoneaware/ProportionalZoneFailureDetectorTest.java b/core/src/test/java/org/apache/brooklyn/entity/group/zoneaware/ProportionalZoneFailureDetectorTest.java
index 3bc5b9d..451ec1b 100644
--- a/core/src/test/java/org/apache/brooklyn/entity/group/zoneaware/ProportionalZoneFailureDetectorTest.java
+++ b/core/src/test/java/org/apache/brooklyn/entity/group/zoneaware/ProportionalZoneFailureDetectorTest.java
@@ -32,7 +32,7 @@ import org.apache.brooklyn.entity.group.zoneaware.ProportionalZoneFailureDetecto
import org.apache.brooklyn.util.time.Duration;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
-import org.apache.brooklyn.location.basic.SimulatedLocation;
+import org.apache.brooklyn.location.core.SimulatedLocation;
import com.google.common.base.Ticker;
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/entity/hello/LocalEntitiesTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/entity/hello/LocalEntitiesTest.java b/core/src/test/java/org/apache/brooklyn/entity/hello/LocalEntitiesTest.java
index 01b4992..3dd71e7 100644
--- a/core/src/test/java/org/apache/brooklyn/entity/hello/LocalEntitiesTest.java
+++ b/core/src/test/java/org/apache/brooklyn/entity/hello/LocalEntitiesTest.java
@@ -47,7 +47,7 @@ import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.collections.Lists;
-import org.apache.brooklyn.location.basic.SimulatedLocation;
+import org.apache.brooklyn.location.core.SimulatedLocation;
import org.apache.brooklyn.test.Asserts;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.time.Time;
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/entity/stock/BasicStartableTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/entity/stock/BasicStartableTest.java b/core/src/test/java/org/apache/brooklyn/entity/stock/BasicStartableTest.java
index 19ef05e..0f64426 100644
--- a/core/src/test/java/org/apache/brooklyn/entity/stock/BasicStartableTest.java
+++ b/core/src/test/java/org/apache/brooklyn/entity/stock/BasicStartableTest.java
@@ -50,8 +50,8 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
-import org.apache.brooklyn.location.basic.Locations.LocationsFilter;
-import org.apache.brooklyn.location.basic.SimulatedLocation;
+import org.apache.brooklyn.location.core.SimulatedLocation;
+import org.apache.brooklyn.location.core.Locations.LocationsFilter;
public class BasicStartableTest {
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/entity/stock/DataEntityTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/entity/stock/DataEntityTest.java b/core/src/test/java/org/apache/brooklyn/entity/stock/DataEntityTest.java
index 49ae17e..a177a1b 100644
--- a/core/src/test/java/org/apache/brooklyn/entity/stock/DataEntityTest.java
+++ b/core/src/test/java/org/apache/brooklyn/entity/stock/DataEntityTest.java
@@ -39,7 +39,7 @@ import org.apache.brooklyn.util.collections.MutableMap;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
-import org.apache.brooklyn.location.basic.SimulatedLocation;
+import org.apache.brooklyn.location.core.SimulatedLocation;
import com.google.common.base.Predicates;
import com.google.common.base.Supplier;
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/entity/trait/StartableMethodsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/entity/trait/StartableMethodsTest.java b/core/src/test/java/org/apache/brooklyn/entity/trait/StartableMethodsTest.java
index 49c4456..8a099c6 100644
--- a/core/src/test/java/org/apache/brooklyn/entity/trait/StartableMethodsTest.java
+++ b/core/src/test/java/org/apache/brooklyn/entity/trait/StartableMethodsTest.java
@@ -31,7 +31,7 @@ import org.apache.brooklyn.entity.trait.FailingEntity.RecordingEventListener;
import org.apache.brooklyn.util.core.task.Tasks;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
-import org.apache.brooklyn.location.basic.SimulatedLocation;
+import org.apache.brooklyn.location.core.SimulatedLocation;
import com.google.common.collect.ImmutableList;
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/location/access/BrooklynAccessUtilsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/location/access/BrooklynAccessUtilsTest.java b/core/src/test/java/org/apache/brooklyn/location/access/BrooklynAccessUtilsTest.java
index c43ca5a..91e06f8 100644
--- a/core/src/test/java/org/apache/brooklyn/location/access/BrooklynAccessUtilsTest.java
+++ b/core/src/test/java/org/apache/brooklyn/location/access/BrooklynAccessUtilsTest.java
@@ -30,8 +30,8 @@ import org.apache.brooklyn.api.location.LocationSpec;
import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
import org.apache.brooklyn.core.test.entity.TestEntity;
import org.apache.brooklyn.entity.core.Attributes;
-import org.apache.brooklyn.location.basic.SshMachineLocation;
-import org.apache.brooklyn.location.basic.SupportsPortForwarding;
+import org.apache.brooklyn.location.core.SupportsPortForwarding;
+import org.apache.brooklyn.location.ssh.SshMachineLocation;
import org.apache.brooklyn.util.net.Cidr;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/location/access/PortForwardManagerRebindTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/location/access/PortForwardManagerRebindTest.java b/core/src/test/java/org/apache/brooklyn/location/access/PortForwardManagerRebindTest.java
index 45b6991..58308f4 100644
--- a/core/src/test/java/org/apache/brooklyn/location/access/PortForwardManagerRebindTest.java
+++ b/core/src/test/java/org/apache/brooklyn/location/access/PortForwardManagerRebindTest.java
@@ -30,12 +30,12 @@ import org.apache.brooklyn.api.mgmt.ha.MementoCopyMode;
import org.apache.brooklyn.api.sensor.AttributeSensor;
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.mgmt.persist.BrooklynPersistenceUtils;
+import org.apache.brooklyn.core.mgmt.persist.FileBasedObjectStore;
+import org.apache.brooklyn.core.mgmt.persist.PersistenceObjectStore;
import org.apache.brooklyn.core.mgmt.rebind.RebindOptions;
import org.apache.brooklyn.core.mgmt.rebind.RebindTestFixtureWithApp;
import org.apache.brooklyn.core.mgmt.rebind.RebindTestUtils;
-import org.apache.brooklyn.core.mgmt.rebind.persister.BrooklynPersistenceUtils;
-import org.apache.brooklyn.core.mgmt.rebind.persister.FileBasedObjectStore;
-import org.apache.brooklyn.core.mgmt.rebind.persister.PersistenceObjectStore;
import org.apache.brooklyn.core.test.entity.TestEntity;
import org.apache.brooklyn.core.test.entity.TestEntityImpl;
import org.apache.brooklyn.sensor.core.Sensors;
@@ -47,7 +47,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
-import org.apache.brooklyn.location.basic.SshMachineLocation;
+import org.apache.brooklyn.location.ssh.SshMachineLocation;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/location/access/PortForwardManagerTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/location/access/PortForwardManagerTest.java b/core/src/test/java/org/apache/brooklyn/location/access/PortForwardManagerTest.java
index 9246694..1017828 100644
--- a/core/src/test/java/org/apache/brooklyn/location/access/PortForwardManagerTest.java
+++ b/core/src/test/java/org/apache/brooklyn/location/access/PortForwardManagerTest.java
@@ -35,7 +35,7 @@ import org.apache.brooklyn.core.internal.BrooklynProperties;
import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
import org.apache.brooklyn.entity.core.Entities;
-import org.apache.brooklyn.location.basic.SshMachineLocation;
+import org.apache.brooklyn.location.ssh.SshMachineLocation;
import com.google.common.base.Predicate;
import com.google.common.net.HostAndPort;
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/location/basic/AbstractLocationTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/location/basic/AbstractLocationTest.java b/core/src/test/java/org/apache/brooklyn/location/basic/AbstractLocationTest.java
deleted file mode 100644
index af9191d..0000000
--- a/core/src/test/java/org/apache/brooklyn/location/basic/AbstractLocationTest.java
+++ /dev/null
@@ -1,183 +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.basic;
-
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertNotEquals;
-import static org.testng.Assert.assertTrue;
-
-import java.util.Collections;
-import java.util.Map;
-
-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.test.entity.LocalManagementContextForTests;
-import org.apache.brooklyn.entity.core.Entities;
-import org.apache.brooklyn.util.collections.MutableMap;
-import org.apache.brooklyn.util.collections.MutableSet;
-import org.apache.brooklyn.util.core.flags.SetFromFlag;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
-
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-
-public class AbstractLocationTest {
-
- public static class ConcreteLocation extends AbstractLocation {
- private static final long serialVersionUID = 3954199300889119970L;
- @SetFromFlag(defaultVal="mydefault")
- String myfield;
-
- public ConcreteLocation() {
- super();
- }
-
- public ConcreteLocation(Map<?,?> properties) {
- super(properties);
- }
- }
-
- private ManagementContext mgmt;
-
- @BeforeMethod(alwaysRun=true)
- public void setUp() throws Exception {
- mgmt = LocalManagementContextForTests.newInstance();
- }
-
- @AfterMethod(alwaysRun = true)
- public void tearDown(){
- if (mgmt!=null) Entities.destroyAll(mgmt);
- }
-
- private ConcreteLocation createConcrete() {
- return createConcrete(MutableMap.<String,Object>of());
- }
- private ConcreteLocation createConcrete(Map<String,?> flags) {
- return createConcrete(null, flags);
- }
- @SuppressWarnings("deprecation")
- private ConcreteLocation createConcrete(String id, Map<String,?> flags) {
- return mgmt.getLocationManager().createLocation( LocationSpec.create(ConcreteLocation.class).id(id).configure(flags) );
- }
-
- @Test
- public void testEqualsUsesId() {
- Location l1 = createConcrete("1", MutableMap.of("name", "bob"));
- Location l1b = new ConcreteLocation(ImmutableMap.of("id", 1));
- Location l2 = createConcrete("2", MutableMap.of("name", "frank"));
- assertEquals(l1, l1b);
- assertNotEquals(l1, l2);
- }
-
- @Test
- public void nullNameAndParentLocationIsAcceptable() {
- Location location = createConcrete(MutableMap.of("name", null, "parentLocation", null));
- assertEquals(location.getDisplayName(), null);
- assertEquals(location.getParent(), null);
- }
-
- @Test
- public void testSettingParentLocation() {
- Location location = createConcrete();
- Location locationSub = createConcrete();
- locationSub.setParent(location);
-
- assertEquals(ImmutableList.copyOf(location.getChildren()), ImmutableList.of(locationSub));
- assertEquals(locationSub.getParent(), location);
- }
-
- @Test
- public void testClearingParentLocation() {
- Location location = createConcrete();
- Location locationSub = createConcrete();
- locationSub.setParent(location);
-
- locationSub.setParent(null);
- assertEquals(ImmutableList.copyOf(location.getChildren()), Collections.emptyList());
- assertEquals(locationSub.getParent(), null);
- }
-
- @Test
- public void testContainsLocation() {
- Location location = createConcrete();
- Location locationSub = createConcrete();
- locationSub.setParent(location);
-
- assertTrue(location.containsLocation(location));
- assertTrue(location.containsLocation(locationSub));
- assertFalse(locationSub.containsLocation(location));
- }
-
-
- @Test
- public void queryingNameReturnsNameGivenInConstructor() {
- String name = "Outer Mongolia";
- Location location = createConcrete(MutableMap.of("name", "Outer Mongolia"));
- assertEquals(location.getDisplayName(), name);;
- }
-
- @Test
- public void constructorParentLocationReturnsExpectedLocation() {
- Location parent = createConcrete(MutableMap.of("name", "Middle Earth"));
- Location child = createConcrete(MutableMap.of("name", "The Shire", "parentLocation", parent));
- assertEquals(child.getParent(), parent);
- assertEquals(ImmutableList.copyOf(parent.getChildren()), ImmutableList.of(child));
- }
-
- @Test
- public void setParentLocationReturnsExpectedLocation() {
- Location parent = createConcrete(MutableMap.of("name", "Middle Earth"));
- Location child = createConcrete(MutableMap.of("name", "The Shire"));
- child.setParent(parent);
- assertEquals(child.getParent(), parent);
- assertEquals(ImmutableList.copyOf(parent.getChildren()), ImmutableList.of(child));
- }
-
- @Test
- public void testAddChildToParentLocationReturnsExpectedLocation() {
- ConcreteLocation parent = createConcrete();
- Location child = createConcrete();
- parent.addChild(child);
- assertEquals(child.getParent(), parent);
- assertEquals(ImmutableList.copyOf(parent.getChildren()), ImmutableList.of(child));
- }
-
- @Test
- public void testFieldSetFromFlag() {
- ConcreteLocation loc = createConcrete(MutableMap.of("myfield", "myval"));
- assertEquals(loc.myfield, "myval");
- }
-
- @Test
- public void testFieldSetFromFlagUsesDefault() {
- ConcreteLocation loc = createConcrete();
- assertEquals(loc.myfield, "mydefault");
- }
-
- @Test
- public void testLocationTags() throws Exception {
- LocationInternal loc = mgmt.getLocationManager().createLocation(LocationSpec.create(ConcreteLocation.class).tag("x"));
- assertEquals(loc.tags().getTags(), MutableSet.of("x"));
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/location/basic/AggregatingMachineProvisioningLocationTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/location/basic/AggregatingMachineProvisioningLocationTest.java b/core/src/test/java/org/apache/brooklyn/location/basic/AggregatingMachineProvisioningLocationTest.java
deleted file mode 100644
index 75996a1..0000000
--- a/core/src/test/java/org/apache/brooklyn/location/basic/AggregatingMachineProvisioningLocationTest.java
+++ /dev/null
@@ -1,115 +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.basic;
-
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.fail;
-
-import java.util.Map;
-
-import org.apache.brooklyn.api.location.Location;
-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.core.mgmt.internal.LocalManagementContext;
-import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
-import org.apache.brooklyn.entity.core.Entities;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
-import org.apache.brooklyn.location.basic.LocalhostMachineProvisioningLocation.LocalhostMachine;
-
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-
-public class AggregatingMachineProvisioningLocationTest {
-
-
- private LocalManagementContext managementContext;
- private AggregatingMachineProvisioningLocation<LocalhostMachine> aggregator;
- private LocalhostMachine machine1a;
- private LocalhostMachine machine1b;
- private LocalhostMachine machine2a;
- private LocalhostMachine machine2b;
- private MachineProvisioningLocation<LocalhostMachine> provisioner1;
- private MachineProvisioningLocation<LocalhostMachine> provisioner2;
-
- @BeforeMethod(alwaysRun=true)
- @SuppressWarnings("unchecked")
- public void setUp() {
- managementContext = new LocalManagementContextForTests();
- machine1a = newLocation(LocalhostMachine.class, "1a");
- machine1b = newLocation(LocalhostMachine.class, "1b");
- machine2a = newLocation(LocalhostMachine.class, "2a");
- machine2b = newLocation(LocalhostMachine.class, "2b");
- provisioner1 = newLocation(FixedListMachineProvisioningLocation.class, ImmutableMap.of("machines", ImmutableList.of(machine1a, machine1b)));
- provisioner2 = newLocation(FixedListMachineProvisioningLocation.class, ImmutableMap.of("machines", ImmutableList.of(machine2a, machine2b)));
- }
-
- @AfterMethod(alwaysRun=true)
- public void tearDown() {
- if (managementContext != null) Entities.destroyAll(managementContext);
- }
-
- @Test
- @SuppressWarnings("unchecked")
- public void testObtainAndRelease() throws Exception {
- aggregator = newLocation(AggregatingMachineProvisioningLocation.class, ImmutableMap.of("provisioners", ImmutableList.of(provisioner1, provisioner2)));
- assertEquals(aggregator.obtain(), machine1a);
- assertEquals(aggregator.obtain(), machine2a);
- assertEquals(aggregator.obtain(), machine1b);
- assertEquals(aggregator.obtain(), machine2b);
-
- try {
- aggregator.obtain();
- fail();
- } catch (NoMachinesAvailableException e) {
- // success
- }
-
- aggregator.release(machine2b);
- assertEquals(aggregator.obtain(), machine2b);
- }
-
- @Test
- @SuppressWarnings("unchecked")
- public void testReleaseWhenNotHeldThrows() throws Exception {
- aggregator = newLocation(AggregatingMachineProvisioningLocation.class, ImmutableMap.of("provisioners", ImmutableList.of(provisioner1, provisioner2)));
- try {
- aggregator.release(machine1a);
- fail();
- } catch (IllegalStateException e) {
- if (!e.toString().contains("machine is not currently allocated")) throw e;
- }
- }
-
- private <T extends Location> T newLocation(Class<T> clazz, String displayName) {
- return newLocation(clazz, displayName, ImmutableMap.of());
- }
-
- private <T extends Location> T newLocation(Class<T> clazz, Map<?,?> config) {
- return newLocation(clazz, "mydisplayname", config);
- }
-
- private <T extends Location> T newLocation(Class<T> clazz, String displayName, Map<?,?> config) {
- return managementContext.getLocationManager().createLocation(LocationSpec.create(clazz)
- .displayName(displayName)
- .configure(config));
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/location/basic/ByonLocationResolverTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/location/basic/ByonLocationResolverTest.java b/core/src/test/java/org/apache/brooklyn/location/basic/ByonLocationResolverTest.java
deleted file mode 100644
index bd799b0..0000000
--- a/core/src/test/java/org/apache/brooklyn/location/basic/ByonLocationResolverTest.java
+++ /dev/null
@@ -1,422 +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.basic;
-
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.fail;
-
-import java.net.InetAddress;
-import java.util.List;
-import java.util.Map;
-import java.util.NoSuchElementException;
-import java.util.Set;
-
-import javax.annotation.Nullable;
-
-import org.apache.brooklyn.api.location.MachineLocation;
-import org.apache.brooklyn.api.location.MachineProvisioningLocation;
-import org.apache.brooklyn.api.location.NoMachinesAvailableException;
-import org.apache.brooklyn.core.config.ConfigKeys;
-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.entity.core.Entities;
-import org.apache.brooklyn.test.Asserts;
-import org.apache.brooklyn.util.collections.MutableMap;
-import org.apache.brooklyn.util.net.Networking;
-import org.apache.brooklyn.util.net.UserAndHostAndPort;
-import org.apache.brooklyn.util.os.Os;
-import org.apache.brooklyn.util.text.StringPredicates;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.testng.Assert;
-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.Joiner;
-import com.google.common.base.Predicate;
-import com.google.common.base.Predicates;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
-
-public class ByonLocationResolverTest {
-
- private static final Logger log = LoggerFactory.getLogger(ByonLocationResolverTest.class);
-
- private BrooklynProperties brooklynProperties;
- private LocalManagementContext managementContext;
- private Predicate<CharSequence> defaultNamePredicate;
-
- @BeforeMethod(alwaysRun=true)
- public void setUp() throws Exception {
- managementContext = LocalManagementContextForTests.newInstance();
- brooklynProperties = managementContext.getBrooklynProperties();
- defaultNamePredicate = StringPredicates.startsWith(FixedListMachineProvisioningLocation.class.getSimpleName());
- }
-
- @AfterMethod(alwaysRun=true)
- public void tearDown() throws Exception {
- if (managementContext != null) Entities.destroyAll(managementContext);
- }
-
- @Test
- public void testTakesByonScopedProperties() {
- brooklynProperties.put("brooklyn.location.byon.privateKeyFile", "myprivatekeyfile");
- brooklynProperties.put("brooklyn.location.byon.publicKeyFile", "mypublickeyfile");
- brooklynProperties.put("brooklyn.location.byon.privateKeyData", "myprivateKeyData");
- brooklynProperties.put("brooklyn.location.byon.publicKeyData", "myPublicKeyData");
- brooklynProperties.put("brooklyn.location.byon.privateKeyPassphrase", "myprivateKeyPassphrase");
-
- Map<String, Object> conf = resolve("byon(hosts=\"1.1.1.1\")").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 testNamedByonLocation() throws Exception {
- brooklynProperties.put("brooklyn.location.named.mynamed", "byon(hosts=\"1.1.1.1\")");
-
- FixedListMachineProvisioningLocation<MachineLocation> loc = resolve("named:mynamed");
- assertEquals(loc.obtain().getAddress(), InetAddress.getByName("1.1.1.1"));
- }
-
- @Test
- public void testPropertiesInSpec() throws Exception {
- FixedListMachineProvisioningLocation<MachineLocation> loc = resolve("byon(privateKeyFile=myprivatekeyfile,hosts=\"1.1.1.1\")");
- SshMachineLocation machine = (SshMachineLocation)loc.obtain();
-
- assertEquals(machine.config().getBag().getStringKey("privateKeyFile"), "myprivatekeyfile");
- assertEquals(machine.getAddress(), Networking.getInetAddressWithFixedName("1.1.1.1"));
- }
-
- @Test
- public void testPropertyScopePrecedence() throws Exception {
- brooklynProperties.put("brooklyn.location.named.mynamed", "byon(hosts=\"1.1.1.1\")");
-
- // prefer those in "named" over everything else
- brooklynProperties.put("brooklyn.location.named.mynamed.privateKeyFile", "privateKeyFile-inNamed");
- brooklynProperties.put("brooklyn.location.byon.privateKeyFile", "privateKeyFile-inProviderSpecific");
- brooklynProperties.put("brooklyn.localhost.privateKeyFile", "privateKeyFile-inGeneric");
-
- // prefer those in provider-specific over generic
- brooklynProperties.put("brooklyn.location.byon.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 testThrowsOnInvalid() throws Exception {
- assertThrowsNoSuchElement("wrongprefix:(hosts=\"1.1.1.1\")");
- assertThrowsIllegalArgument("byon"); // no hosts
- assertThrowsIllegalArgument("byon()"); // no hosts
- assertThrowsIllegalArgument("byon(hosts=\"\")"); // empty hosts
- assertThrowsIllegalArgument("byon(hosts=\"1.1.1.1\""); // no closing bracket
- assertThrowsIllegalArgument("byon(hosts=\"1.1.1.1\", name)"); // no value for name
- assertThrowsIllegalArgument("byon(hosts=\"1.1.1.1\", name=)"); // no value for name
- }
-
- @Test(expectedExceptions={IllegalArgumentException.class})
- public void testRegistryCommaResolutionInListNotAllowed() throws NoMachinesAvailableException {
- // disallowed since 0.7.0
- // fails because it interprets the entire string as a single byon spec, which does not parse
- managementContext.getLocationRegistry().resolve(ImmutableList.of("byon(hosts=\"192.168.0.1\",user=bob),byon(hosts=\"192.168.0.2\",user=bob2)"));
- }
-
- @Test
- public void testResolvesHosts() throws Exception {
- assertByonClusterEquals(resolve("byon(hosts=\"1.1.1.1\")"), ImmutableSet.of("1.1.1.1"));
- assertByonClusterEquals(resolve("byon(hosts=\"1.1.1.1\")"), ImmutableSet.of("1.1.1.1"));
- assertByonClusterEquals(resolve("byon(hosts=\"1.1.1.1,1.1.1.2\")"), ImmutableSet.of("1.1.1.1","1.1.1.2"));
- assertByonClusterEquals(resolve("byon(hosts=\"1.1.1.1, 1.1.1.2\")"), ImmutableSet.of("1.1.1.1","1.1.1.2"));
- }
-
- @Test
- public void testWithOldStyleColon() throws Exception {
- assertByonClusterEquals(resolve("byon:(hosts=\"1.1.1.1\")"), ImmutableSet.of("1.1.1.1"));
- assertByonClusterEquals(resolve("byon:(hosts=\"1.1.1.1\", name=myname)"), ImmutableSet.of("1.1.1.1"), "myname");
- }
-
- @Test
- public void testUsesDisplayName() throws Exception {
- assertByonClusterEquals(resolve("byon(hosts=\"1.1.1.1\", name=myname)"), ImmutableSet.of("1.1.1.1"), "myname");
- assertByonClusterEquals(resolve("byon(hosts=\"1.1.1.1\", name=\"myname\")"), ImmutableSet.of("1.1.1.1"), "myname");
- }
-
- @Test
- public void testResolvesHostsGlobExpansion() throws Exception {
- assertByonClusterEquals(resolve("byon(hosts=\"1.1.1.{1,2}\")"), ImmutableSet.of("1.1.1.1","1.1.1.2"));
- assertByonClusterEquals(resolve("byon(hosts=\"1.1.{1.1,2.{1,2}}\")"),
- ImmutableSet.of("1.1.1.1","1.1.2.1","1.1.2.2"));
- assertByonClusterEquals(resolve("byon(hosts=\"1.1.{1,2}.{1,2}\")"),
- ImmutableSet.of("1.1.1.1","1.1.1.2","1.1.2.1","1.1.2.2"));
- }
-
- @Test(groups="Integration")
- public void testNiceError() throws Exception {
- Asserts.assertFailsWith(new Runnable() {
- @Override public void run() {
- FixedListMachineProvisioningLocation<MachineLocation> x =
- resolve("byon(hosts=\"1.1.1.{1,2}}\")");
- log.error("got "+x+" but should have failed (your DNS is giving an IP for hostname '1.1.1.1}' (with the extra '}')");
- }
- }, new Predicate<Throwable>() {
- @Override
- public boolean apply(@Nullable Throwable input) {
- String s = input.toString();
- // words
- if (!s.contains("Invalid host")) return false;
- // problematic entry
- if (!s.contains("1.1.1.1}")) return false;
- // original spec
- if (!s.contains("1.1.1.{1,2}}")) return false;
- return true;
- }
- });
- }
-
- @Test
- public void testResolvesUsernameAtHost() throws Exception {
- assertByonClusterWithUsersEquals(resolve("byon(hosts=\"myuser@1.1.1.1\")"),
- ImmutableSet.of(UserAndHostAndPort.fromParts("myuser", "1.1.1.1", 22)));
- assertByonClusterWithUsersEquals(resolve("byon(hosts=\"myuser@1.1.1.1,myuser2@1.1.1.1\")"), ImmutableSet.of(
- UserAndHostAndPort.fromParts("myuser", "1.1.1.1", 22), UserAndHostAndPort.fromParts("myuser2", "1.1.1.1", 22)));
- assertByonClusterWithUsersEquals(resolve("byon(hosts=\"myuser@1.1.1.1,myuser2@1.1.1.2\")"), ImmutableSet.of(
- UserAndHostAndPort.fromParts("myuser", "1.1.1.1", 22), UserAndHostAndPort.fromParts("myuser2", "1.1.1.2", 22)));
- }
-
- @Test
- public void testResolvesUserArg() throws Exception {
- assertByonClusterWithUsersEquals(resolve("byon(hosts=\"1.1.1.1\",user=bob)"),
- ImmutableSet.of(UserAndHostAndPort.fromParts("bob", "1.1.1.1", 22)));
- assertByonClusterWithUsersEquals(resolve("byon(user=\"bob\",hosts=\"myuser@1.1.1.1,1.1.1.1\")"),
- ImmutableSet.of(UserAndHostAndPort.fromParts("myuser", "1.1.1.1", 22), UserAndHostAndPort.fromParts("bob", "1.1.1.1", 22)));
- }
-
- @Test
- public void testResolvesUserArg2() throws Exception {
- String spec = "byon(hosts=\"1.1.1.1\",user=bob)";
- FixedListMachineProvisioningLocation<MachineLocation> ll = resolve(spec);
- SshMachineLocation l = (SshMachineLocation)ll.obtain();
- Assert.assertEquals("bob", l.getUser());
- }
-
- @SuppressWarnings("unchecked")
- @Test
- public void testResolvesUserArg3() throws Exception {
- String spec = "byon(hosts=\"1.1.1.1\")";
- managementContext.getLocationRegistry().getProperties().putAll(MutableMap.of(
- "brooklyn.location.named.foo", spec,
- "brooklyn.location.named.foo.user", "bob"));
- ((BasicLocationRegistry)managementContext.getLocationRegistry()).updateDefinedLocations();
-
- MachineProvisioningLocation<SshMachineLocation> ll = (MachineProvisioningLocation<SshMachineLocation>)
- new NamedLocationResolver().newLocationFromString(MutableMap.of(), "named:foo", managementContext.getLocationRegistry());
- SshMachineLocation l = ll.obtain(MutableMap.of());
- Assert.assertEquals("bob", l.getUser());
- }
-
- @Test
- public void testResolvesPortArg() throws Exception {
- assertByonClusterWithUsersEquals(resolve("byon(user=bob,port=8022,hosts=\"1.1.1.1\")"),
- ImmutableSet.of(UserAndHostAndPort.fromParts("bob", "1.1.1.1", 8022)));
- assertByonClusterWithUsersEquals(resolve("byon(user=bob,port=8022,hosts=\"myuser@1.1.1.1,1.1.1.2:8901\")"),
- ImmutableSet.of(UserAndHostAndPort.fromParts("myuser", "1.1.1.1", 8022), UserAndHostAndPort.fromParts("bob", "1.1.1.2", 8901)));
- }
-
- @SuppressWarnings("unchecked")
- @Test
- /** private key should be inherited, so confirm that happens correctly */
- public void testResolvesPrivateKeyArgInheritance() throws Exception {
- String spec = "byon(hosts=\"1.1.1.1\")";
- managementContext.getLocationRegistry().getProperties().putAll(MutableMap.of(
- "brooklyn.location.named.foo", spec,
- "brooklyn.location.named.foo.user", "bob",
- "brooklyn.location.named.foo.privateKeyFile", "/tmp/x"));
- ((BasicLocationRegistry)managementContext.getLocationRegistry()).updateDefinedLocations();
-
- MachineProvisioningLocation<SshMachineLocation> ll = (MachineProvisioningLocation<SshMachineLocation>)
- new NamedLocationResolver().newLocationFromString(MutableMap.of(), "named:foo", managementContext.getLocationRegistry());
-
- Assert.assertEquals("/tmp/x", ll.getConfig(LocationConfigKeys.PRIVATE_KEY_FILE));
- Assert.assertTrue(((LocationInternal)ll).config().getLocalRaw(LocationConfigKeys.PRIVATE_KEY_FILE).isPresent());
- Assert.assertEquals("/tmp/x", ((LocationInternal)ll).config().getLocalBag().getStringKey(LocationConfigKeys.PRIVATE_KEY_FILE.getName()));
- Assert.assertEquals("/tmp/x", ((LocationInternal)ll).config().getBag().get(LocationConfigKeys.PRIVATE_KEY_FILE));
-
- SshMachineLocation l = ll.obtain(MutableMap.of());
-
- Assert.assertEquals("/tmp/x", l.getConfig(LocationConfigKeys.PRIVATE_KEY_FILE));
-
- Assert.assertTrue(l.config().getRaw(LocationConfigKeys.PRIVATE_KEY_FILE).isPresent());
- Assert.assertTrue(l.config().getLocalRaw(LocationConfigKeys.PRIVATE_KEY_FILE).isAbsent());
-
- Assert.assertEquals("/tmp/x", l.config().getBag().getStringKey(LocationConfigKeys.PRIVATE_KEY_FILE.getName()));
- Assert.assertEquals("/tmp/x", l.config().getBag().getStringKey(LocationConfigKeys.PRIVATE_KEY_FILE.getName()));
-
- Assert.assertEquals("/tmp/x", l.config().getBag().get(LocationConfigKeys.PRIVATE_KEY_FILE));
- }
-
- @Test
- public void testResolvesLocalTempDir() throws Exception {
- String localTempDir = Os.mergePaths(Os.tmp(), "testResolvesUsernameAtHost");
- brooklynProperties.put("brooklyn.location.byon.localTempDir", localTempDir);
-
- FixedListMachineProvisioningLocation<MachineLocation> byon = resolve("byon(hosts=\"1.1.1.1\",osFamily=\"windows\")");
- MachineLocation machine = byon.obtain();
- assertEquals(machine.getConfig(SshMachineLocation.LOCAL_TEMP_DIR), localTempDir);
- }
-
- @Test
- public void testMachinesObtainedInOrder() throws Exception {
- List<String> ips = ImmutableList.of("1.1.1.1", "1.1.1.6", "1.1.1.3", "1.1.1.4", "1.1.1.5");
- String spec = "byon(hosts=\""+Joiner.on(",").join(ips)+"\")";
-
- MachineProvisioningLocation<MachineLocation> ll = resolve(spec);
-
- for (String expected : ips) {
- MachineLocation obtained = ll.obtain(ImmutableMap.of());
- assertEquals(obtained.getAddress().getHostAddress(), expected);
- }
- }
-
- @Test
- public void testEmptySpec() throws Exception {
- String spec = "byon";
- Map<String, ?> flags = ImmutableMap.of(
- "hosts", ImmutableList.of("1.1.1.1", "2.2.2.22"),
- "name", "foo",
- "user", "myuser"
- );
- MachineProvisioningLocation<MachineLocation> provisioner = resolve(spec, flags);
- SshMachineLocation location1 = (SshMachineLocation)provisioner.obtain(ImmutableMap.of());
- Assert.assertEquals("myuser", location1.getUser());
- Assert.assertEquals("1.1.1.1", location1.getAddress().getHostAddress());
- }
-
- @Test
- public void testWindowsMachines() throws Exception {
- brooklynProperties.put("brooklyn.location.byon.user", "myuser");
- brooklynProperties.put("brooklyn.location.byon.password", "mypassword");
- String spec = "byon";
- Map<String, ?> flags = ImmutableMap.of(
- "hosts", ImmutableList.of("1.1.1.1", "2.2.2.2"),
- "osfamily", "windows"
- );
- MachineProvisioningLocation<MachineLocation> provisioner = resolve(spec, flags);
- WinRmMachineLocation location = (WinRmMachineLocation) provisioner.obtain(ImmutableMap.of());
-
- assertEquals(location.config().get(WinRmMachineLocation.USER), "myuser");
- assertEquals(location.config().get(WinRmMachineLocation.PASSWORD), "mypassword");
- assertEquals(location.config().get(WinRmMachineLocation.ADDRESS).getHostAddress(), "1.1.1.1");
- }
-
- @Test
- public void testNoneWindowsMachines() throws Exception {
- String spec = "byon";
- Map<String, ?> flags = ImmutableMap.of(
- "hosts", ImmutableList.of("1.1.1.1", "2.2.2.2"),
- "osfamily", "linux"
- );
- MachineProvisioningLocation<MachineLocation> provisioner = resolve(spec, flags);
- MachineLocation location = provisioner.obtain(ImmutableMap.of());
- assertTrue(location instanceof SshMachineLocation, "Expected location to be SshMachineLocation, found " + location);
- }
-
- @Test
- public void testAdditionalConfig() throws Exception {
- FixedListMachineProvisioningLocation<MachineLocation> loc = resolve("byon(mykey=myval,hosts=\"1.1.1.1\")");
- MachineLocation machine = loc.obtain(ImmutableMap.of());
- assertEquals(machine.getConfig(ConfigKeys.newConfigKey(String.class, "mykey")), "myval");
- }
-
- private void assertByonClusterEquals(FixedListMachineProvisioningLocation<? extends MachineLocation> cluster, Set<String> expectedHosts) {
- assertByonClusterEquals(cluster, expectedHosts, defaultNamePredicate);
- }
-
- private void assertByonClusterEquals(FixedListMachineProvisioningLocation<? extends MachineLocation> cluster, Set<String> expectedHosts, String expectedName) {
- assertByonClusterEquals(cluster, expectedHosts, Predicates.equalTo(expectedName));
- }
-
- private void assertByonClusterEquals(FixedListMachineProvisioningLocation<? extends MachineLocation> cluster, Set<String> expectedHosts, Predicate<? super String> expectedName) {
- Set<String> actualHosts = ImmutableSet.copyOf(Iterables.transform(cluster.getMachines(), new Function<MachineLocation, String>() {
- @Override public String apply(MachineLocation input) {
- return input.getAddress().getHostName();
- }}));
- assertEquals(actualHosts, expectedHosts);
- assertTrue(expectedName.apply(cluster.getDisplayName()), "name="+cluster.getDisplayName());
- }
-
- private void assertByonClusterWithUsersEquals(FixedListMachineProvisioningLocation<? extends MachineLocation> cluster, Set<UserAndHostAndPort> expectedHosts) {
- assertByonClusterWithUsersEquals(cluster, expectedHosts, defaultNamePredicate);
- }
-
- private void assertByonClusterWithUsersEquals(FixedListMachineProvisioningLocation<? extends MachineLocation> cluster, Set<UserAndHostAndPort> expectedHosts, Predicate<? super String> expectedName) {
- Set<UserAndHostAndPort> actualHosts = ImmutableSet.copyOf(Iterables.transform(cluster.getMachines(), new Function<MachineLocation, UserAndHostAndPort>() {
- @Override public UserAndHostAndPort apply(MachineLocation input) {
- SshMachineLocation machine = (SshMachineLocation) input;
- return UserAndHostAndPort.fromParts(machine.getUser(), machine.getAddress().getHostName(), machine.getPort());
- }}));
- assertEquals(actualHosts, expectedHosts);
- assertTrue(expectedName.apply(cluster.getDisplayName()), "name="+cluster.getDisplayName());
- }
-
- private void assertThrowsNoSuchElement(String val) {
- try {
- resolve(val);
- fail();
- } catch (NoSuchElementException e) {
- // success
- }
- }
-
- private void assertThrowsIllegalArgument(String val) {
- try {
- resolve(val);
- fail();
- } catch (IllegalArgumentException e) {
- // success
- }
- }
-
- @SuppressWarnings("unchecked")
- private FixedListMachineProvisioningLocation<MachineLocation> resolve(String val) {
- return (FixedListMachineProvisioningLocation<MachineLocation>) managementContext.getLocationRegistry().resolve(val);
- }
-
- @SuppressWarnings("unchecked")
- private FixedListMachineProvisioningLocation<MachineLocation> resolve(String val, Map<?, ?> locationFlags) {
- return (FixedListMachineProvisioningLocation<MachineLocation>) managementContext.getLocationRegistry().resolve(val, locationFlags);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/location/basic/FixedListMachineProvisioningLocationRebindTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/location/basic/FixedListMachineProvisioningLocationRebindTest.java b/core/src/test/java/org/apache/brooklyn/location/basic/FixedListMachineProvisioningLocationRebindTest.java
deleted file mode 100644
index 6d21014..0000000
--- a/core/src/test/java/org/apache/brooklyn/location/basic/FixedListMachineProvisioningLocationRebindTest.java
+++ /dev/null
@@ -1,128 +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.basic;
-
-import static org.testng.Assert.assertEquals;
-
-import java.io.File;
-import java.util.Set;
-
-import javax.annotation.Nullable;
-
-import org.apache.brooklyn.api.location.Location;
-import org.apache.brooklyn.api.mgmt.ManagementContext;
-import org.apache.brooklyn.core.mgmt.rebind.RebindTestUtils;
-import org.apache.brooklyn.core.test.entity.TestApplication;
-import org.apache.brooklyn.entity.core.Entities;
-import org.apache.brooklyn.entity.factory.ApplicationBuilder;
-import org.apache.brooklyn.util.collections.MutableSet;
-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.collect.ImmutableList;
-import com.google.common.collect.Iterables;
-
-public class FixedListMachineProvisioningLocationRebindTest {
-
- private FixedListMachineProvisioningLocation<SshMachineLocation> origLoc;
- private ClassLoader classLoader = getClass().getClassLoader();
- private ManagementContext origManagementContext;
- private TestApplication origApp;
- private TestApplication newApp;
- private File mementoDir;
-
- @BeforeMethod(alwaysRun=true)
- public void setUp() throws Exception {
- mementoDir = Os.newTempDir(getClass());
- origManagementContext = RebindTestUtils.newPersistingManagementContext(mementoDir, classLoader, 1);
-
- origLoc = new FixedListMachineProvisioningLocation.Builder(origManagementContext.getLocationManager())
- .addAddresses("localhost", "127.0.0.1")
- .user("myuser")
- .keyFile("/path/to/myPrivateKeyFile")
- .keyData("myKeyData")
- .keyPassphrase("myKeyPassphrase")
- .build();
- origApp = ApplicationBuilder.newManagedApp(TestApplication.class, origManagementContext);
- origApp.start(ImmutableList.of(origLoc));
- }
-
- @AfterMethod(alwaysRun = true)
- public void tearDown() throws Exception {
- if (origManagementContext != null) Entities.destroyAll(origManagementContext);
- if (newApp != null) Entities.destroyAll(newApp.getManagementContext());
- if (mementoDir != null) RebindTestUtils.deleteMementoDir(mementoDir);
- }
-
- @Test
- public void testRebindPreservesConfig() throws Exception {
- newApp = rebind();
- FixedListMachineProvisioningLocation<SshMachineLocation> newLoc = (FixedListMachineProvisioningLocation<SshMachineLocation>) Iterables.get(newApp.getLocations(), 0);
-
- assertEquals(newLoc.getId(), origLoc.getId());
- assertEquals(newLoc.getDisplayName(), origLoc.getDisplayName());
- assertEquals(newLoc.getHostGeoInfo(), origLoc.getHostGeoInfo());
- assertEquals(newLoc.getConfig(LocationConfigKeys.USER), origLoc.getConfig(LocationConfigKeys.USER));
- assertEquals(newLoc.getConfig(LocationConfigKeys.PRIVATE_KEY_PASSPHRASE), origLoc.getConfig(LocationConfigKeys.PRIVATE_KEY_PASSPHRASE));
- assertEquals(newLoc.getConfig(LocationConfigKeys.PRIVATE_KEY_FILE), origLoc.getConfig(LocationConfigKeys.PRIVATE_KEY_FILE));
- assertEquals(newLoc.getConfig(LocationConfigKeys.PRIVATE_KEY_DATA), origLoc.getConfig(LocationConfigKeys.PRIVATE_KEY_DATA));
- }
-
- @Test
- public void testRebindParentRelationship() throws Exception {
- newApp = rebind();
- FixedListMachineProvisioningLocation<SshMachineLocation> newLoc = (FixedListMachineProvisioningLocation<SshMachineLocation>) Iterables.get(newApp.getLocations(), 0);
-
- assertLocationIdsEqual(newLoc.getChildren(), origLoc.getChildren());
- assertEquals(Iterables.get(newLoc.getChildren(), 0).getParent(), newLoc);
- assertEquals(Iterables.get(newLoc.getChildren(), 1).getParent(), newLoc);
- }
-
- @Test
- public void testRebindPreservesInUseMachines() throws Exception {
- SshMachineLocation inuseMachine = origLoc.obtain();
- origApp.setAttribute(TestApplication.SERVICE_UP, true); // to force persist, and thus avoid race
-
- newApp = rebind();
- FixedListMachineProvisioningLocation<SshMachineLocation> newLoc = (FixedListMachineProvisioningLocation<SshMachineLocation>) Iterables.get(newApp.getLocations(), 0);
-
- assertLocationIdsEqual(newLoc.getInUse(), origLoc.getInUse());
- assertLocationIdsEqual(newLoc.getAvailable(), origLoc.getAvailable());
- }
-
- private TestApplication rebind() throws Exception {
- RebindTestUtils.waitForPersisted(origApp);
- return (TestApplication) RebindTestUtils.rebind(mementoDir, getClass().getClassLoader());
- }
-
- private void assertLocationIdsEqual(Iterable<? extends Location> actual, Iterable<? extends Location> expected) {
- Function<Location, String> locationIdFunction = new Function<Location, String>() {
- @Override public String apply(@Nullable Location input) {
- return (input != null) ? input.getId() : null;
- }
- };
- Set<String> actualIds = MutableSet.copyOf(Iterables.transform(actual, locationIdFunction));
- Set<String> expectedIds = MutableSet.copyOf(Iterables.transform(expected, locationIdFunction));
-
- assertEquals(actualIds, expectedIds);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/location/basic/FixedListMachineProvisioningLocationTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/location/basic/FixedListMachineProvisioningLocationTest.java b/core/src/test/java/org/apache/brooklyn/location/basic/FixedListMachineProvisioningLocationTest.java
deleted file mode 100644
index dffc759..0000000
--- a/core/src/test/java/org/apache/brooklyn/location/basic/FixedListMachineProvisioningLocationTest.java
+++ /dev/null
@@ -1,574 +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.basic;
-
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.fail;
-
-import java.net.Inet4Address;
-import java.net.UnknownHostException;
-import java.util.List;
-import java.util.Random;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import org.apache.brooklyn.api.location.LocationSpec;
-import org.apache.brooklyn.api.location.MachineLocation;
-import org.apache.brooklyn.api.location.NoMachinesAvailableException;
-import org.apache.brooklyn.config.ConfigKey;
-import org.apache.brooklyn.core.config.ConfigKeys;
-import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext;
-import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
-import org.apache.brooklyn.entity.core.Entities;
-import org.apache.brooklyn.util.collections.MutableList;
-import org.apache.brooklyn.util.collections.MutableMap;
-import org.apache.brooklyn.util.net.Networking;
-import org.apache.brooklyn.util.stream.Streams;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.testng.Assert;
-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.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
-
-/**
- * Provisions {@link SshMachineLocation}s in a specific location from a list of known machines
- */
-public class FixedListMachineProvisioningLocationTest {
-
- private static final Logger LOG = LoggerFactory.getLogger(FixedListMachineProvisioningLocationTest.class);
-
- SshMachineLocation machine;
- LocalManagementContext mgmt;
- FixedListMachineProvisioningLocation<SshMachineLocation> provisioner;
- FixedListMachineProvisioningLocation<SshMachineLocation> provisioner2;
-
- @SuppressWarnings("unchecked")
- @BeforeMethod(alwaysRun=true)
- public void createProvisioner() throws UnknownHostException {
- mgmt = LocalManagementContextForTests.newInstance();
-
- machine = mgmt.getLocationManager().createLocation(MutableMap.of("address", Inet4Address.getByName("192.168.144.200")), SshMachineLocation.class);
- provisioner = mgmt.getLocationManager().createLocation(
- MutableMap.of("machines", MutableList.of(machine)),
- FixedListMachineProvisioningLocation.class);
- }
-
- @AfterMethod(alwaysRun=true)
- public void tearDown() throws Exception {
- if (provisioner != null) Streams.closeQuietly(provisioner);
- if (provisioner2 != null) Streams.closeQuietly(provisioner2);
- Entities.destroyAll(mgmt);
- }
-
- @Test
- public void testSetsChildLocations() throws NoMachinesAvailableException {
- // Available machines should be listed as children
- assertEquals(ImmutableList.copyOf(provisioner.getChildren()), ImmutableList.of(machine));
-
- // In-use machines should also be listed as children
- provisioner.obtain();
- assertEquals(ImmutableList.copyOf(provisioner.getChildren()), ImmutableList.of(machine));
- }
-
- @Test
- public void canObtainMachine() throws NoMachinesAvailableException {
- SshMachineLocation obtained = provisioner.obtain();
- assertEquals(obtained, machine);
- }
-
- @SuppressWarnings("unused")
- @Test(expectedExceptions = { NoMachinesAvailableException.class })
- public void throwsExceptionIfNoMachinesAvailable() throws NoMachinesAvailableException {
- SshMachineLocation machine1 = provisioner.obtain();
- SshMachineLocation machine2 = provisioner.obtain();
- fail("Did not throw NoMachinesAvailableException as expected");
- }
-
- @Test
- public void canGetAMachineReturnItAndObtainItAgain() throws NoMachinesAvailableException {
- SshMachineLocation obtained = provisioner.obtain();
- provisioner.release(obtained);
- SshMachineLocation obtained2 = provisioner.obtain();
- assertEquals(obtained2, machine);
- }
-
- @Test
- public void theBuilder() throws NoMachinesAvailableException {
- provisioner2 =
- new FixedListMachineProvisioningLocation.Builder(mgmt.getLocationManager()).
- user("u1").
- addAddress("192.168.0.1").
- addAddress("u2@192.168.0.2").
- addAddress("192.168.0.{3,4}").
- addAddresses("192.168.0.{6-8}").
- addAddressMultipleTimes("192.168.0.{8,7}", 2).
- addAddress("u3@192.168.0.{11-20}").
- build();
- assertUserAndHost(provisioner2.obtain(), "u1", "192.168.0.1");
- assertUserAndHost(provisioner2.obtain(), "u2", "192.168.0.2");
- for (int i=3; i<=4; i++) assertUserAndHost(provisioner2.obtain(), "u1", "192.168.0."+i);
- for (int i=6; i<=8; i++) assertUserAndHost(provisioner2.obtain(), "u1", "192.168.0."+i);
- for (int j=0; j<2; j++)
- for (int i=8; i>=7; i--) assertUserAndHost(provisioner2.obtain(), "u1", "192.168.0."+i);
- for (int i=11; i<=20; i++) assertUserAndHost(provisioner2.obtain(), "u3", "192.168.0."+i);
- try {
- provisioner2.obtain();
- fail("Should not have obtained"); //throws error so not caught below
- } catch (Exception e) {
- /** expected */
- }
- }
-
- @Test
- public void theBuilderLegacy() throws NoMachinesAvailableException {
- provisioner2 =
- new FixedListMachineProvisioningLocation.Builder(mgmt.getLocationManager()).
- user("u1").
- addAddress("192.168.0.1").
- addAddress("u2@192.168.0.2").
- addAddress("192.168.0.{3,4}").
- addAddresses("192.168.0.{6-8}").
- addAddressMultipleTimes("192.168.0.{8,7}", 2).
- addAddress("u3@192.168.0.{11-20}").
- build();
- assertUserAndHost(provisioner2.obtain(), "u1", "192.168.0.1");
- assertUserAndHost(provisioner2.obtain(), "u2", "192.168.0.2");
- for (int i=3; i<=4; i++) assertUserAndHost(provisioner2.obtain(), "u1", "192.168.0."+i);
- for (int i=6; i<=8; i++) assertUserAndHost(provisioner2.obtain(), "u1", "192.168.0."+i);
- for (int j=0; j<2; j++)
- for (int i=8; i>=7; i--) assertUserAndHost(provisioner2.obtain(), "u1", "192.168.0."+i);
- for (int i=11; i<=20; i++) assertUserAndHost(provisioner2.obtain(), "u3", "192.168.0."+i);
- try {
- provisioner2.obtain();
- fail("Should not have obtained"); //throws error so not caught below
- } catch (Exception e) {
- /** expected */
- }
- }
-
- @Test(expectedExceptions = { IllegalStateException.class })
- public void throwsExceptionIfTryingToReleaseUnallocationMachine() throws NoMachinesAvailableException, UnknownHostException {
- @SuppressWarnings("unused")
- SshMachineLocation obtained = provisioner.obtain();
- provisioner.release(new SshMachineLocation(MutableMap.of("address", Inet4Address.getByName("192.168.144.201"))));
- fail("Did not throw IllegalStateException as expected");
- }
-
- @Test
- public void testCanAddMachineToPool() throws UnknownHostException, NoMachinesAvailableException {
- SshMachineLocation machine2 = new SshMachineLocation(
- MutableMap.of("address", Inet4Address.getByName("192.168.144.200")));
- provisioner2 = new FixedListMachineProvisioningLocation<SshMachineLocation>(
- MutableMap.of("machines", MutableList.of()));
- provisioner2.addMachine(machine2);
-
- assertEquals(ImmutableList.copyOf(provisioner2.getChildren()), ImmutableList.of(machine2));
- assertEquals(ImmutableSet.copyOf(provisioner2.getAvailable()), ImmutableSet.of(machine2));
-
- SshMachineLocation obtained = provisioner2.obtain();
- assertEquals(obtained, machine2);
-
- // Can only obtain the added machien once though (i.e. not added multiple times somehow)
- try {
- SshMachineLocation obtained2 = provisioner2.obtain();
- fail("obtained="+obtained2);
- } catch (NoMachinesAvailableException e) {
- // success
- }
- }
-
- @Test
- public void testCanRemoveAvailableMachineFromPool() {
- provisioner.removeMachine(machine);
-
- Assert.assertTrue(provisioner.getChildren().isEmpty());
- Assert.assertTrue(provisioner.getAvailable().isEmpty());
-
- try {
- SshMachineLocation obtained = provisioner.obtain();
- fail("obtained="+obtained);
- } catch (NoMachinesAvailableException e) {
- // success
- }
- }
-
- @Test
- public void testCanRemoveObtainedMachineFromPoolSoNotReallocated() throws NoMachinesAvailableException {
- SshMachineLocation obtained = provisioner.obtain();
- provisioner.removeMachine(obtained);
-
- // Continue to know about the machine until it is returned
- assertEquals(ImmutableList.copyOf(provisioner.getChildren()), ImmutableList.of(machine));
- Assert.assertTrue(provisioner.getAvailable().isEmpty());
-
- // When released, the machine is then removed entirely
- provisioner.release(obtained);
-
- Assert.assertTrue(provisioner.getChildren().isEmpty());
- Assert.assertTrue(provisioner.getAvailable().isEmpty());
-
- // So no machines left; cannot re-obtain
- try {
- SshMachineLocation obtained2 = provisioner.obtain();
- fail("obtained="+obtained2);
- } catch (NoMachinesAvailableException e) {
- // success
- }
- }
-
- @Test
- public void testObtainDesiredMachineThrowsIfNotKnown() throws Exception {
- SshMachineLocation machine2 = new SshMachineLocation(
- MutableMap.of("address", Inet4Address.getByName("192.168.144.201")));
- try {
- SshMachineLocation obtained = provisioner.obtain(MutableMap.of("desiredMachine", machine2));
- fail("obtained="+obtained);
- } catch (IllegalStateException e) {
- if (!e.toString().contains("machine unknown")) throw e;
- }
- }
-
- @Test
- public void testObtainDesiredMachineThrowsIfInUse() throws Exception {
- provisioner.addMachine(new SshMachineLocation(
- MutableMap.of("address", Inet4Address.getByName("192.168.144.201"))));
- SshMachineLocation obtained = provisioner.obtain();
- try {
- SshMachineLocation obtained2 = provisioner.obtain(MutableMap.of("desiredMachine", obtained));
- fail("obtained2="+obtained2);
- } catch (IllegalStateException e) {
- if (!e.toString().contains("machine in use")) throw e;
- }
- }
-
- @Test
- public void testObtainDesiredMachineReturnsDesired() throws Exception {
- int desiredMachineIndex = 10;
- SshMachineLocation desiredMachine = null;
- for (int i = 0; i < 20; i++) {
- SshMachineLocation newMachine = new SshMachineLocation(
- MutableMap.of("address", Inet4Address.getByName("192.168.144."+(201+i))));
- if (i == desiredMachineIndex) desiredMachine = newMachine;
- provisioner.addMachine(newMachine);
- }
- SshMachineLocation obtained = provisioner.obtain(MutableMap.of("desiredMachine", desiredMachine));
- assertEquals(obtained, desiredMachine);
- }
-
- @Test
- public void testAddAndRemoveChildUpdatesMachinesSet() throws Exception {
- SshMachineLocation anotherMachine = new SshMachineLocation(
- MutableMap.of("address", Inet4Address.getByName("192.168.144.201")));
- provisioner.addChild(anotherMachine);
- assertEquals(provisioner.getAllMachines(), ImmutableSet.of(machine, anotherMachine));
-
- provisioner.removeChild(anotherMachine);
- assertEquals(provisioner.getAllMachines(), ImmutableSet.of(machine));
- }
-
- @Test
- public void testCanAddAlreadyParentedMachine() throws UnknownHostException, NoMachinesAvailableException {
- provisioner.obtain(); // so no machines left
-
- FixedListMachineProvisioningLocation<SshMachineLocation> provisioner2 = new FixedListMachineProvisioningLocation.Builder(mgmt.getLocationManager())
- .addAddress("1.2.3.4")
- .build();
- SshMachineLocation machine = provisioner2.obtain();
-
- provisioner.addMachine(machine);
- assertEquals(provisioner.obtain(), machine);
- }
-
- @Test
- public void testCanCreateWithAlreadyParentedMachine() throws UnknownHostException, NoMachinesAvailableException {
- machine = provisioner.obtain();
-
- FixedListMachineProvisioningLocation<SshMachineLocation> provisioner2 = new FixedListMachineProvisioningLocation.Builder(mgmt.getLocationManager())
- .add(machine)
- .build();
- assertEquals(provisioner2.obtain(), machine);
- }
-
- @Test
- @SuppressWarnings("unchecked")
- public void testMachinesObtainedInOrder() throws Exception {
- List<SshMachineLocation> machines = ImmutableList.of(
- mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class).configure("address", Networking.getInetAddressWithFixedName("1.1.1.1"))),
- mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class).configure("address", Networking.getInetAddressWithFixedName("1.1.1.6"))),
- mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class).configure("address", Networking.getInetAddressWithFixedName("1.1.1.3"))),
- mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class).configure("address", Networking.getInetAddressWithFixedName("1.1.1.4"))),
- mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class).configure("address", Networking.getInetAddressWithFixedName("1.1.1.5"))));
-
- provisioner2 = mgmt.getLocationManager().createLocation(
- MutableMap.of("machines", machines),
- FixedListMachineProvisioningLocation.class);
-
- for (SshMachineLocation expected : machines) {
- assertEquals(provisioner2.obtain(), expected);
- }
- }
-
- @Test
- @SuppressWarnings("unchecked")
- public void testMachineChooser() throws Exception {
- List<SshMachineLocation> machines = Lists.newArrayList();
- for (int i = 0; i < 10; i++) {
- machines.add(mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class).configure("address", Networking.getInetAddressWithFixedName("1.1.1."+i))));
- }
- final List<SshMachineLocation> desiredOrder = randomized(machines);
-
- Function<Iterable<? extends MachineLocation>, MachineLocation> chooser = new Function<Iterable<? extends MachineLocation>, MachineLocation>() {
- @Override public MachineLocation apply(Iterable<? extends MachineLocation> input) {
- for (SshMachineLocation contender : desiredOrder) {
- if (Iterables.contains(input, contender)) {
- return contender;
- }
- }
- Assert.fail("No intersection of input="+input+" and desiredOrder="+desiredOrder);
- return null; // unreachable code
- }
- };
- provisioner2 = mgmt.getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class)
- .configure("machines", machines)
- .configure(FixedListMachineProvisioningLocation.MACHINE_CHOOSER, chooser));
-
- List<SshMachineLocation> result = Lists.newArrayList();
- for (int i = 0; i < machines.size(); i++) {
- result.add(provisioner2.obtain());
- }
- assertEquals(result, desiredOrder, "result="+result+"; desired="+desiredOrder);
- LOG.debug("chooser's desiredOrder="+desiredOrder);
- }
-
- @Test
- @SuppressWarnings("unchecked")
- public void testMachineChooserPassedToObtain() throws Exception {
- List<SshMachineLocation> machines = Lists.newArrayList();
- for (int i = 0; i < 10; i++) {
- machines.add(mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class).configure("address", Networking.getInetAddressWithFixedName("1.1.1."+i))));
- }
- final List<SshMachineLocation> desiredOrder = randomized(machines);
-
- Function<Iterable<? extends MachineLocation>, MachineLocation> chooser = new Function<Iterable<? extends MachineLocation>, MachineLocation>() {
- @Override public MachineLocation apply(Iterable<? extends MachineLocation> input) {
- for (SshMachineLocation contender : desiredOrder) {
- if (Iterables.contains(input, contender)) {
- return contender;
- }
- }
- Assert.fail("No intersection of input="+input+" and desiredOrder="+desiredOrder);
- return null; // unreachable code
- }
- };
- provisioner2 = mgmt.getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class)
- .configure("machines", machines));
-
- List<SshMachineLocation> result = Lists.newArrayList();
- for (int i = 0; i < machines.size(); i++) {
- result.add(provisioner2.obtain(ImmutableMap.of(FixedListMachineProvisioningLocation.MACHINE_CHOOSER, chooser)));
- }
- assertEquals(result, desiredOrder, "result="+result+"; desired="+desiredOrder);
- LOG.debug("chooser's desiredOrder="+desiredOrder);
- }
-
- @Test
- @SuppressWarnings("unchecked")
- public void testMachineChooserNotCalledWhenNoMachines() throws Exception {
- List<SshMachineLocation> machines = ImmutableList.of(
- mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class).configure("address", Networking.getInetAddressWithFixedName("1.1.1.1"))));
- final AtomicInteger callCount = new AtomicInteger();
-
- Function<Iterable<? extends MachineLocation>, MachineLocation> chooser = new Function<Iterable<? extends MachineLocation>, MachineLocation>() {
- @Override public MachineLocation apply(Iterable<? extends MachineLocation> input) {
- callCount.incrementAndGet();
- return Iterables.get(input, 0);
- }
- };
- provisioner2 = mgmt.getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class)
- .configure("machines", machines)
- .configure(FixedListMachineProvisioningLocation.MACHINE_CHOOSER, chooser));
- provisioner2.obtain();
-
- // When no machines available should fail gracefully, without asking the "chooser"
- try {
- provisioner2.obtain();
- fail("Expected "+NoMachinesAvailableException.class.getSimpleName());
- } catch (NoMachinesAvailableException e) {
- // Pass; sensible exception
- }
- assertEquals(callCount.get(), 1);
- }
-
- @Test
- @SuppressWarnings("unchecked")
- public void testFailsWhenMachineChooserReturnsAlreadyAllocatedMachine() throws Exception {
- final SshMachineLocation machine1 = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class).configure("address", Networking.getInetAddressWithFixedName("1.1.1.1")));
- final SshMachineLocation machine2 = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class).configure("address", Networking.getInetAddressWithFixedName("1.1.1.2")));
- List<SshMachineLocation> machines = ImmutableList.of(machine1, machine2);
-
- Function<Iterable<? extends MachineLocation>, MachineLocation> chooser = new Function<Iterable<? extends MachineLocation>, MachineLocation>() {
- @Override public MachineLocation apply(Iterable<? extends MachineLocation> input) {
- return machine1;
- }
- };
- provisioner2 = mgmt.getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class)
- .configure("machines", machines)
- .configure(FixedListMachineProvisioningLocation.MACHINE_CHOOSER, chooser));
- provisioner2.obtain();
-
- // Should fail when tries to return same machine for a second time
- try {
- provisioner2.obtain();
- fail("Expected "+IllegalStateException.class.getSimpleName());
- } catch (IllegalStateException e) {
- if (!e.toString().contains("Machine chooser attempted to choose ")) throw e;
- }
- }
-
- @Test
- @SuppressWarnings("unchecked")
- public void testFailsWhenMachineChooserReturnsInvalidMachine() throws Exception {
- final SshMachineLocation machine1 = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class).configure("address", Networking.getInetAddressWithFixedName("1.1.1.1")));
- final SshMachineLocation machineOther = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class).configure("address", Networking.getInetAddressWithFixedName("2.2.2.1")));
- List<SshMachineLocation> machines = ImmutableList.of(machine1);
-
- Function<Iterable<? extends MachineLocation>, MachineLocation> chooser = new Function<Iterable<? extends MachineLocation>, MachineLocation>() {
- @Override public MachineLocation apply(Iterable<? extends MachineLocation> input) {
- return machineOther;
- }
- };
- provisioner2 = mgmt.getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class)
- .configure("machines", machines)
- .configure(FixedListMachineProvisioningLocation.MACHINE_CHOOSER, chooser));
-
- // Call when no machines available should fail gracefully, without asking the "chooser"
- try {
- provisioner2.obtain();
- fail("Expected "+IllegalStateException.class.getSimpleName());
- } catch (IllegalStateException e) {
- if (!e.toString().contains("Machine chooser attempted to choose ")) throw e;
- }
- }
-
- @Test
- @SuppressWarnings("unchecked")
- public void testMachineCustomizerSetOnByon() throws Exception {
- machine = mgmt.getLocationManager().createLocation(MutableMap.of("address", Inet4Address.getByName("192.168.144.200")), SshMachineLocation.class);
- RecordingMachineLocationCustomizer customizer = new RecordingMachineLocationCustomizer();
-
- provisioner2 = mgmt.getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class)
- .configure("machines", ImmutableList.of(machine))
- .configure(FixedListMachineProvisioningLocation.MACHINE_LOCATION_CUSTOMIZERS.getName(), ImmutableList.of(customizer)));
-
- SshMachineLocation obtained = provisioner2.obtain();
- Assert.assertEquals(Iterables.getOnlyElement(customizer.calls), new RecordingMachineLocationCustomizer.Call("customize", ImmutableList.of(obtained)));
-
- provisioner2.release(obtained);
- assertEquals(customizer.calls.size(), 2);
- Assert.assertEquals(Iterables.get(customizer.calls, 1), new RecordingMachineLocationCustomizer.Call("preRelease", ImmutableList.of(obtained)));
- }
-
- @Test
- public void testMachineCustomizerSetOnObtainCall() throws Exception {
- RecordingMachineLocationCustomizer customizer = new RecordingMachineLocationCustomizer();
-
- SshMachineLocation obtained = provisioner.obtain(ImmutableMap.of(FixedListMachineProvisioningLocation.MACHINE_LOCATION_CUSTOMIZERS, ImmutableList.of(customizer)));
- Assert.assertEquals(Iterables.getOnlyElement(customizer.calls), new RecordingMachineLocationCustomizer.Call("customize", ImmutableList.of(obtained)));
-
- provisioner.release(obtained);
- assertEquals(customizer.calls.size(), 2);
- Assert.assertEquals(customizer.calls.get(1), new RecordingMachineLocationCustomizer.Call("preRelease", ImmutableList.of(obtained)));
- }
-
- @Test
- public void testMachineGivenCustomFlagForDurationOfUsage() throws Exception {
- boolean origContains = machine.config().getBag().getAllConfig().containsKey("mykey");
- SshMachineLocation obtained = provisioner.obtain(ImmutableMap.of("mykey", "myNewVal"));
- Object obtainedVal = obtained.config().getBag().getAllConfig().get("mykey");
- provisioner.release(obtained);
- boolean releasedContains = obtained.config().getBag().getAllConfig().containsKey("mykey");
-
- assertEquals(obtained, machine);
- assertFalse(origContains);
- assertEquals(obtainedVal, "myNewVal");
- assertFalse(releasedContains);
- }
-
- @Test
- public void testMachineConfigRestoredToDefaultsOnRelease() throws Exception {
- ConfigKey<String> mykey = ConfigKeys.newStringConfigKey("mykey");
-
- boolean origContains = machine.config().getBag().getAllConfig().containsKey("mykey");
- SshMachineLocation obtained = provisioner.obtain();
- obtained.config().set(mykey, "myNewVal");
- Object obtainedVal = obtained.config().getBag().getAllConfig().get("mykey");
-
- provisioner.release(obtained);
- boolean releasedContains = machine.config().getBag().getAllConfig().containsKey("mykey");
- releasedContains |= (machine.config().get(mykey) != null);
-
- assertEquals(obtained, machine);
- assertFalse(origContains);
- assertEquals(obtainedVal, "myNewVal");
- assertFalse(releasedContains);
- }
-
- @Test
- public void testMachineGivenOverriddenFlagForDurationOfUsage() throws Exception {
- SshMachineLocation machine2 = new SshMachineLocation(
- MutableMap.of("address", Inet4Address.getByName("192.168.144.200"), "mykey", "myval"));
- provisioner2 = new FixedListMachineProvisioningLocation<SshMachineLocation>(
- MutableMap.of("machines", MutableList.of(machine2)));
-
- Object origVal = machine2.config().getBag().getAllConfig().get("mykey");
- SshMachineLocation obtained = provisioner2.obtain(ImmutableMap.of("mykey", "myNewVal"));
- Object obtainedVal = obtained.config().getBag().getAllConfig().get("mykey");
- provisioner2.release(obtained);
- Object releasedVal = obtained.config().getBag().getAllConfig().get("mykey");
-
- assertEquals(obtained, machine2);
- assertEquals(origVal, "myval");
- assertEquals(obtainedVal, "myNewVal");
- assertEquals(releasedVal, "myval");
- }
-
- private static <T> List<T> randomized(Iterable<T> list) {
- // TODO inefficient implementation, but don't care for small tests
- Random random = new Random();
- List<T> result = Lists.newLinkedList();
- for (T element : list) {
- int index = (result.isEmpty() ? 0 : random.nextInt(result.size()));
- result.add(index, element);
- }
- return result;
- }
-
- private static void assertUserAndHost(SshMachineLocation l, String user, String host) {
- assertEquals(l.getUser(), user);
- assertEquals(l.getAddress().getHostAddress(), host);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a1ad34d7/core/src/test/java/org/apache/brooklyn/location/basic/HostLocationResolverTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/location/basic/HostLocationResolverTest.java b/core/src/test/java/org/apache/brooklyn/location/basic/HostLocationResolverTest.java
deleted file mode 100644
index 92d4d63..0000000
--- a/core/src/test/java/org/apache/brooklyn/location/basic/HostLocationResolverTest.java
+++ /dev/null
@@ -1,125 +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.basic;
-
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.fail;
-
-import java.net.InetAddress;
-import java.util.Map;
-import java.util.NoSuchElementException;
-
-import org.apache.brooklyn.api.location.MachineProvisioningLocation;
-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.entity.core.Entities;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
-
-import com.google.common.collect.ImmutableMap;
-
-public class HostLocationResolverTest {
-
- 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 testThrowsOnInvalid() throws Exception {
- assertThrowsNoSuchElement("wrongprefix:(hosts=\"1.1.1.1\")");
- assertThrowsIllegalArgument("host");
- }
-
- @Test
- public void testThrowsOnInvalidTarget() throws Exception {
- assertThrowsIllegalArgument("host:()");
- }
-
- @Test
- public void resolveHosts() {
- resolve("host:(\"1.1.1.1\")");
- resolve("host:(\"localhost\")");
- }
-
- @Test(groups="Integration")
- public void resolveRealHosts() {
- // must be online to resolve this
- resolve("host:(\"www.foo.com\")");
- }
-
- @Test
- public void testNamedByonLocation() throws Exception {
- brooklynProperties.put("brooklyn.location.named.mynamed", "host:(\"1.1.1.1\")");
-
- MachineProvisioningLocation<SshMachineLocation> loc = resolve("named:mynamed");
- assertEquals(loc.obtain(ImmutableMap.of()).getAddress(), InetAddress.getByName("1.1.1.1"));
- }
-
- @Test
- public void testPropertyScopePrescedence() throws Exception {
- brooklynProperties.put("brooklyn.location.named.mynamed", "host:(\"1.1.1.1\")");
-
- // prefer those in "named" over everything else
- brooklynProperties.put("brooklyn.location.named.mynamed.privateKeyFile", "privateKeyFile-inNamed");
- brooklynProperties.put("brooklyn.localhost.privateKeyFile", "privateKeyFile-inGeneric");
-
- // prefer location-generic if nothing else
- brooklynProperties.put("brooklyn.location.privateKeyData", "privateKeyData-inGeneric");
-
- Map<String, Object> conf = resolve("named:mynamed").obtain(ImmutableMap.of()).config().getBag().getAllConfig();
-
- assertEquals(conf.get("privateKeyFile"), "privateKeyFile-inNamed");
- assertEquals(conf.get("privateKeyData"), "privateKeyData-inGeneric");
- }
-
- private void assertThrowsNoSuchElement(String val) {
- try {
- resolve(val);
- fail();
- } catch (NoSuchElementException e) {
- // success
- }
- }
-
- private void assertThrowsIllegalArgument(String val) {
- try {
- resolve(val);
- fail();
- } catch (IllegalArgumentException e) {
- // success
- }
- }
-
- @SuppressWarnings("unchecked")
- private MachineProvisioningLocation<SshMachineLocation> resolve(String val) {
- return (MachineProvisioningLocation<SshMachineLocation>) managementContext.getLocationRegistry().resolve(val);
- }
-}