ServerPool: creates/registers single ServerPoolLocation
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/9f1a1208 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/9f1a1208 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/9f1a1208 Branch: refs/heads/master Commit: 9f1a120812895a604f42df29f8e16ec4d4a1ed30 Parents: 453183f Author: Aled Sage <[email protected]> Authored: Thu Mar 17 12:59:24 2016 +0000 Committer: Aled Sage <[email protected]> Committed: Sat Mar 19 22:03:55 2016 +0000 ---------------------------------------------------------------------- .../entity/machine/pool/ServerPool.java | 5 +- .../entity/machine/pool/ServerPoolImpl.java | 53 +++++++++------- .../entity/machine/pool/ServerPoolLocation.java | 53 ++++++++++++++-- .../pool/ServerPoolLocationResolver.java | 64 +++++++++---------- .../machine/pool/AbstractServerPoolTest.java | 23 ++++--- .../pool/ServerPoolLocationResolverTest.java | 65 ++++++-------------- .../entity/machine/pool/ServerPoolTest.java | 8 +-- 7 files changed, 148 insertions(+), 123 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9f1a1208/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPool.java ---------------------------------------------------------------------- diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPool.java b/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPool.java index 668a89e..4e42778 100644 --- a/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPool.java +++ b/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPool.java @@ -21,8 +21,6 @@ package org.apache.brooklyn.entity.machine.pool; import java.util.Collection; import java.util.Map; -import com.google.common.annotations.Beta; - import org.apache.brooklyn.api.catalog.Catalog; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.entity.EntitySpec; @@ -41,6 +39,8 @@ import org.apache.brooklyn.core.sensor.Sensors; import org.apache.brooklyn.entity.group.DynamicCluster; import org.apache.brooklyn.entity.machine.MachineEntity; +import com.google.common.annotations.Beta; + /** * A preallocated server pool is an entity that other applications can deploy to. * Behaving as a cluster, the machines it creates for its members are reused. @@ -70,6 +70,7 @@ import org.apache.brooklyn.entity.machine.MachineEntity; @ImplementedBy(ServerPoolImpl.class) @Beta public interface ServerPool extends DynamicCluster, LocationOwner<ServerPoolLocation, ServerPool> { + ConfigKey<String> LOCATION_NAME_PREFIX = ConfigKeys.newConfigKeyWithDefault(LocationOwner.LOCATION_NAME_PREFIX, "server-pool"); ConfigKey<Integer> INITIAL_SIZE = ConfigKeys.newConfigKeyWithDefault(DynamicCluster.INITIAL_SIZE, 2); http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9f1a1208/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolImpl.java ---------------------------------------------------------------------- diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolImpl.java b/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolImpl.java index 513ee29..e7ad6db 100644 --- a/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolImpl.java +++ b/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolImpl.java @@ -25,6 +25,8 @@ import java.util.Map; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.location.Location; import org.apache.brooklyn.api.location.LocationDefinition; +import org.apache.brooklyn.api.location.LocationRegistry; +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.api.mgmt.LocationManager; @@ -38,19 +40,19 @@ import org.apache.brooklyn.core.entity.Attributes; import org.apache.brooklyn.core.entity.EntityInternal; import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; import org.apache.brooklyn.core.entity.trait.Startable; -import org.apache.brooklyn.core.location.BasicLocationDefinition; import org.apache.brooklyn.core.location.Machines; import org.apache.brooklyn.core.location.dynamic.DynamicLocation; import org.apache.brooklyn.core.location.internal.LocationInternal; import org.apache.brooklyn.core.mgmt.internal.LocalLocationManager; +import org.apache.brooklyn.core.location.dynamic.LocationOwner; import org.apache.brooklyn.core.sensor.Sensors; import org.apache.brooklyn.entity.group.AbstractMembershipTrackingPolicy; import org.apache.brooklyn.entity.group.DynamicClusterImpl; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.apache.brooklyn.util.collections.MutableMap; import org.apache.brooklyn.util.core.task.DynamicTasks; import org.apache.brooklyn.util.guava.Maybe; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.google.common.base.Function; import com.google.common.base.Joiner; @@ -64,6 +66,19 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.reflect.TypeToken; +/** + * The normal usage pattern is to instantiate this entity to create your cluster, which will + * automatically create a {@link ServerPoolLocation}. The location can be looked up from the + * {@link LocationRegistry} (e.g. {@code mgmt.getLocationRegistry().resolve("my-pool-name")}), + * which uses the {@link ServerPoolLocationResolver}. + * + * Each location delegates to the associated {@link ServerPool} to claim machines (which are + * provisioned as required), and to release machines back to the pool. + * + * This differs from Clocker's use of {@link DynamicLocation} in that the ServerPool has <em>multiple</em> + * locations. This makes the use of {@link LocationOwner} a little confusing + * (e.g. {@link LocationOwner#DYNAMIC_LOCATION} is not set). + */ public class ServerPoolImpl extends DynamicClusterImpl implements ServerPool { private static final Logger LOG = LoggerFactory.getLogger(ServerPoolImpl.class); @@ -96,9 +111,6 @@ public class ServerPoolImpl extends DynamicClusterImpl implements ServerPool { public static final AttributeSensor<Map<MachineLocation, Entity>> MACHINE_ENTITY = Sensors.newSensor(new TypeToken<Map<MachineLocation, Entity>>() {}, "pool.machineEntityMap", "A mapping of machine locations and their entities"); - public static final AttributeSensor<LocationDefinition> DYNAMIC_LOCATION_DEFINITION = Sensors.newSensor(LocationDefinition.class, - "pool.locationDefinition", "The location definition used to create the pool's dynamic location"); - public static final ConfigKey<Boolean> REMOVABLE = ConfigKeys.newBooleanConfigKey( "pool.member.removable", "Whether a pool member is removable from the cluster. Used to denote additional " + "existing machines that were manually added to the pool", true); @@ -171,16 +183,18 @@ public class ServerPoolImpl extends DynamicClusterImpl implements ServerPool { locationName = Joiner.on("-").skipNulls().join(prefix, getId(), suffix); } - String locationSpec = String.format(ServerPoolLocationResolver.POOL_SPEC, getId()) + String.format(":(name=\"%s\")", locationName); - LocationDefinition definition = new BasicLocationDefinition(locationName, locationSpec, flags); - getManagementContext().getLocationRegistry().updateDefinedLocation(definition); - Location location = getManagementContext().getLocationManager().createLocation( getManagementContext().getLocationRegistry().getLocationSpec(definition).get() ); - LOG.info("Resolved and registered dynamic location {}: {}", locationName, location); + ServerPoolLocation location = getManagementContext().getLocationManager().createLocation(LocationSpec.create(ServerPoolLocation.class) + .displayName("Server Pool(" + getId() + ")") + .configure(flags) + .configure("owner", getProxy()) + .configure("locationName", locationName)); + + LocationDefinition definition = location.register(); + LOG.info("Resolved and registered dynamic location {} for server pool {}: {}", new Object[] {locationName, this, location}); - sensors().set(LOCATION_SPEC, locationSpec); + sensors().set(LOCATION_SPEC, definition.getSpec()); + sensors().set(LOCATION_NAME, locationName); sensors().set(DYNAMIC_LOCATION, location); - sensors().set(LOCATION_NAME, location.getId()); - sensors().set(DYNAMIC_LOCATION_DEFINITION, definition); return (ServerPoolLocation) location; } @@ -189,20 +203,15 @@ public class ServerPoolImpl extends DynamicClusterImpl implements ServerPool { public void deleteLocation() { LocationManager mgr = getManagementContext().getLocationManager(); ServerPoolLocation location = getDynamicLocation(); - if (mgr.isManaged(location)) { + if (location != null && mgr.isManaged(location)) { LOG.debug("{} deleting and unmanaging location {}", this, location); + location.deregister(); mgr.unmanage(location); } - // definition will only be null if deleteLocation has already been called, e.g. by two calls to stop(). - LocationDefinition definition = getAttribute(DYNAMIC_LOCATION_DEFINITION); - if (definition != null) { - LOG.debug("{} unregistering dynamic location {}", this, definition); - getManagementContext().getLocationRegistry().removeDefinedLocation(definition.getId()); - } + sensors().set(LOCATION_SPEC, null); sensors().set(DYNAMIC_LOCATION, null); sensors().set(LOCATION_NAME, null); - sensors().set(DYNAMIC_LOCATION_DEFINITION, null); } @Override http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9f1a1208/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocation.java ---------------------------------------------------------------------- diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocation.java b/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocation.java index bc1e2dd..3bb985a 100644 --- a/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocation.java +++ b/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocation.java @@ -23,19 +23,21 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.util.Collection; import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.Maps; - +import org.apache.brooklyn.api.location.LocationDefinition; 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.config.ConfigKey; import org.apache.brooklyn.core.config.ConfigKeys; import org.apache.brooklyn.core.location.AbstractLocation; +import org.apache.brooklyn.core.location.BasicLocationDefinition; import org.apache.brooklyn.core.location.dynamic.DynamicLocation; import org.apache.brooklyn.util.core.flags.SetFromFlag; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; public class ServerPoolLocation extends AbstractLocation implements MachineProvisioningLocation<MachineLocation>, DynamicLocation<ServerPool, ServerPoolLocation> { @@ -46,6 +48,12 @@ public class ServerPoolLocation extends AbstractLocation implements MachineProvi public static final ConfigKey<ServerPool> OWNER = ConfigKeys.newConfigKey( ServerPool.class, "pool.location.owner"); + @SetFromFlag("locationName") + public static final ConfigKey<String> LOCATION_NAME = ConfigKeys.newStringConfigKey("pool.location.name"); + + @SetFromFlag("locationRegistrationId") + private String locationRegistrationId; + @Override public void init() { LOG.debug("Initialising. Owner is: {}", checkNotNull(getConfig(OWNER), OWNER.getName())); @@ -53,6 +61,41 @@ public class ServerPoolLocation extends AbstractLocation implements MachineProvi } @Override + public void rebind() { + super.rebind(); + + if (config().get(LOCATION_NAME) != null) { + register(); + } + } + + @Override + public LocationDefinition register() { + String locationName = checkNotNull(config().get(LOCATION_NAME), "config %s", LOCATION_NAME.getName()); + LocationDefinition check = getManagementContext().getLocationRegistry().getDefinedLocationByName(locationName); + if (check != null) { + throw new IllegalStateException("Location " + locationName + " is already defined: " + check); + } + + String locationSpec = String.format(ServerPoolLocationResolver.POOL_SPEC, getId()) + String.format(":(name=\"%s\")", locationName); + + LocationDefinition definition = new BasicLocationDefinition(locationName, locationSpec, ImmutableMap.<String, Object>of()); + getManagementContext().getLocationRegistry().updateDefinedLocation(definition); + + locationRegistrationId = definition.getId(); + requestPersist(); + + return definition; + } + + @Override + public void deregister() { + if (locationRegistrationId != null) { + getManagementContext().getLocationRegistry().removeDefinedLocation(locationRegistrationId); + } + } + + @Override public ServerPool getOwner() { return getConfig(OWNER); } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9f1a1208/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocationResolver.java ---------------------------------------------------------------------- diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocationResolver.java b/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocationResolver.java index 54f232b..6cc9102 100644 --- a/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocationResolver.java +++ b/software/base/src/main/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocationResolver.java @@ -26,22 +26,24 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.api.internal.AbstractBrooklynObjectSpec; +import org.apache.brooklyn.api.location.Location; import org.apache.brooklyn.api.location.LocationRegistry; import org.apache.brooklyn.api.location.LocationResolver; import org.apache.brooklyn.api.location.LocationSpec; import org.apache.brooklyn.api.mgmt.ManagementContext; +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.config.ConfigKeys; import org.apache.brooklyn.core.location.BasicLocationRegistry; import org.apache.brooklyn.core.location.LocationConfigUtils; -import org.apache.brooklyn.core.location.LocationPropertiesFromBrooklynProperties; -import org.apache.brooklyn.core.location.dynamic.DynamicLocation; -import org.apache.brooklyn.core.location.internal.LocationInternal; -import org.apache.brooklyn.util.collections.MutableMap; +import org.apache.brooklyn.core.objs.proxy.SpecialBrooklynObjectConstructor; +import org.apache.brooklyn.core.objs.proxy.SpecialBrooklynObjectConstructor.Config; import org.apache.brooklyn.util.text.KeyValueParser; import org.apache.brooklyn.util.text.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.annotations.Beta; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; @@ -55,7 +57,7 @@ public class ServerPoolLocationResolver implements LocationResolver { ":([a-zA-Z0-9]+)" + // pool Id "(:\\((.*)\\))?$"); // arguments, e.g. displayName - private static final Set<String> ACCEPTABLE_ARGS = ImmutableSet.of("name", "displayName"); + private static final Set<String> ACCEPTABLE_ARGS = ImmutableSet.of("name"); private ManagementContext managementContext; @@ -85,7 +87,6 @@ public class ServerPoolLocationResolver implements LocationResolver { if (LOG.isDebugEnabled()) { LOG.debug("Resolving location '" + spec + "' with flags " + Joiner.on(",").withKeyValueSeparator("=").join(locationFlags)); } - String namedLocation = (String) locationFlags.get(LocationInternal.NAMED_SPEC_NAME.getName()); Matcher matcher = PATTERN.matcher(spec); if (!matcher.matches()) { @@ -94,43 +95,42 @@ public class ServerPoolLocationResolver implements LocationResolver { throw new IllegalArgumentException(m); } + // TODO Could validate that the namePart matches the existing poolLoc String argsPart = matcher.group(4); Map<String, String> argsMap = (argsPart != null) ? KeyValueParser.parseMap(argsPart) : Collections.<String,String>emptyMap(); - String displayNamePart = argsMap.get("displayName"); + @SuppressWarnings("unused") String namePart = argsMap.get("name"); if (!ACCEPTABLE_ARGS.containsAll(argsMap.keySet())) { Set<String> illegalArgs = Sets.difference(argsMap.keySet(), ACCEPTABLE_ARGS); throw new IllegalArgumentException("Invalid location '"+spec+"'; illegal args "+illegalArgs+"; acceptable args are "+ACCEPTABLE_ARGS); } - if (argsMap.containsKey("displayName") && Strings.isEmpty(displayNamePart)) { - throw new IllegalArgumentException("Invalid location '"+spec+"'; if displayName supplied then value must be non-empty"); - } - if (argsMap.containsKey("name") && Strings.isEmpty(namePart)) { - throw new IllegalArgumentException("Invalid location '"+spec+"'; if name supplied then value must be non-empty"); - } - Map<String, Object> filteredProperties = new LocationPropertiesFromBrooklynProperties() - .getLocationProperties(PREFIX, namedLocation, registry.getProperties()); - MutableMap<String, Object> flags = MutableMap.<String, Object>builder() - .putAll(filteredProperties) - .putAll(locationFlags) - .build(); - - String poolId = matcher.group(2); - if (Strings.isBlank(poolId)) { - throw new IllegalArgumentException("Invalid location '"+spec+"'; pool's entity id must be non-empty"); + String poolLocId = matcher.group(2); + if (Strings.isBlank(poolLocId)) { + throw new IllegalArgumentException("Invalid location '"+spec+"'; pool's location id must be non-empty"); } - final String displayName = displayNamePart != null ? displayNamePart : "Server Pool " + poolId; - final String locationName = namePart != null ? namePart : "serverpool-" + poolId; + Location poolLoc = managementContext.getLocationManager().getLocation(poolLocId); + if (poolLoc == null) { + throw new IllegalArgumentException("Unknown ServerPool location id "+poolLocId+", spec "+spec); + } else if (!(poolLoc instanceof ServerPoolLocation)) { + throw new IllegalArgumentException("Invalid location id for ServerPool, spec "+spec+"; instead matches "+poolLoc); + } - Entity pool = managementContext.getEntityManager().getEntity(poolId); return LocationSpec.create(ServerPoolLocation.class) - .configure(flags) - .configure(DynamicLocation.OWNER, pool) - .configure(LocationInternal.NAMED_SPEC_NAME, locationName) - .displayName(displayName); + .configure(LocationConstructor.LOCATION, poolLoc) + .configure(Config.SPECIAL_CONSTRUCTOR, LocationConstructor.class); + } + + @Beta + public static class LocationConstructor implements SpecialBrooklynObjectConstructor { + public static ConfigKey<Location> LOCATION = ConfigKeys.newConfigKey(Location.class, "serverpoolresolver.location"); + + @SuppressWarnings("unchecked") + @Override + public <T> T create(ManagementContext mgmt, Class<T> type, AbstractBrooklynObjectSpec<?, ?> spec) { + return (T) checkNotNull(spec.getConfig().get(LOCATION), LOCATION.getName()); + } } - } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9f1a1208/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/AbstractServerPoolTest.java ---------------------------------------------------------------------- diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/AbstractServerPoolTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/AbstractServerPoolTest.java index 4a13d0d..0da5a2e 100644 --- a/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/AbstractServerPoolTest.java +++ b/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/AbstractServerPoolTest.java @@ -30,12 +30,11 @@ import org.apache.brooklyn.api.mgmt.ManagementContext; import org.apache.brooklyn.core.entity.Attributes; import org.apache.brooklyn.core.entity.BrooklynConfigKeys; import org.apache.brooklyn.core.entity.Entities; -import org.apache.brooklyn.core.entity.factory.ApplicationBuilder; +import org.apache.brooklyn.core.entity.EntityAsserts; import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; import org.apache.brooklyn.core.test.entity.TestApplication; import org.apache.brooklyn.entity.machine.pool.ServerPool; import org.apache.brooklyn.entity.software.base.EmptySoftwareProcess; -import org.apache.brooklyn.test.EntityTestUtils; import org.apache.brooklyn.util.exceptions.Exceptions; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; @@ -62,15 +61,14 @@ public abstract class AbstractServerPoolTest { createdApps.clear(); mgmt = createManagementContext(); location = createLocation(); - EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class) - .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, shouldSkipOnBoxBaseDirResolution()); - poolApp = ApplicationBuilder.newManagedApp(appSpec, mgmt); + poolApp = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class) + .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, shouldSkipOnBoxBaseDirResolution())); pool = poolApp.createAndManageChild(EntitySpec.create(ServerPool.class) .configure(ServerPool.INITIAL_SIZE, getInitialPoolSize()) .configure(ServerPool.MEMBER_SPEC, EntitySpec.create(EmptySoftwareProcess.class))); poolApp.start(ImmutableList.of(location)); - EntityTestUtils.assertAttributeEqualsEventually(pool, Attributes.SERVICE_UP, true); + EntityAsserts.assertAttributeEqualsEventually(pool, Attributes.SERVICE_UP, true); assertAvailableCountEventuallyEquals(getInitialPoolSize()); } @@ -120,7 +118,7 @@ public abstract class AbstractServerPoolTest { } protected void assertAvailableCountEventuallyEquals(ServerPool pool, int count) { - EntityTestUtils.assertAttributeEqualsEventually(pool, ServerPool.AVAILABLE_COUNT, count); + EntityAsserts.assertAttributeEqualsEventually(pool, ServerPool.AVAILABLE_COUNT, count); } protected void assertClaimedCountEventuallyEquals(int count) { @@ -128,16 +126,17 @@ public abstract class AbstractServerPoolTest { } protected void assertClaimedCountEventuallyEquals(ServerPool pool, Integer count) { - EntityTestUtils.assertAttributeEqualsEventually(pool, ServerPool.CLAIMED_COUNT, count); + EntityAsserts.assertAttributeEqualsEventually(pool, ServerPool.CLAIMED_COUNT, count); } protected TestApplication createAppWithChildren(int numChildren) { if (numChildren < 0) fail("Invalid number of children for app: " + numChildren); - EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class) - .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, shouldSkipOnBoxBaseDirResolution()); - TestApplication app = ApplicationBuilder.newManagedApp(appSpec, mgmt); + + TestApplication app = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class) + .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, shouldSkipOnBoxBaseDirResolution())); + while (numChildren-- > 0) { - app.createAndManageChild(EntitySpec.create(EmptySoftwareProcess.class)); + app.addChild(EntitySpec.create(EmptySoftwareProcess.class)); } createdApps.add(app); return app; http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9f1a1208/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocationResolverTest.java ---------------------------------------------------------------------- diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocationResolverTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocationResolverTest.java index cd71497..482ba42 100644 --- a/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocationResolverTest.java +++ b/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/ServerPoolLocationResolverTest.java @@ -20,71 +20,44 @@ package org.apache.brooklyn.entity.machine.pool; import static org.testng.Assert.assertEquals; -import java.util.Map; - -import org.apache.brooklyn.api.entity.Entity; 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.core.entity.Entities; -import org.apache.brooklyn.core.entity.factory.ApplicationBuilder; -import org.apache.brooklyn.core.internal.BrooklynProperties; -import org.apache.brooklyn.core.location.dynamic.DynamicLocation; -import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; -import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; -import org.apache.brooklyn.core.test.entity.TestApplication; -import org.apache.brooklyn.entity.machine.pool.ServerPool; -import org.apache.brooklyn.entity.machine.pool.ServerPoolLocation; +import org.apache.brooklyn.core.entity.BrooklynConfigKeys; +import org.apache.brooklyn.core.location.dynamic.LocationOwner; +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; import org.apache.brooklyn.entity.software.base.EmptySoftwareProcess; -import org.apache.brooklyn.util.collections.MutableMap; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; +import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; -import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation; - -public class ServerPoolLocationResolverTest { +public class ServerPoolLocationResolverTest extends BrooklynAppUnitTestSupport { - private LocalManagementContext managementContext; - private Entity locationOwner; + private ServerPool serverPool; @BeforeMethod(alwaysRun=true) + @Override public void setUp() throws Exception { - managementContext = new LocalManagementContextForTests(BrooklynProperties.Factory.newEmpty()); - TestApplication t = ApplicationBuilder.newManagedApp(TestApplication.class, managementContext); - locationOwner = t.createAndManageChild(EntitySpec.create(ServerPool.class) + super.setUp(); + serverPool = app.createAndManageChild(EntitySpec.create(ServerPool.class) + .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, shouldSkipOnBoxBaseDirResolution()) .configure(ServerPool.INITIAL_SIZE, 0) .configure(ServerPool.MEMBER_SPEC, EntitySpec.create(EmptySoftwareProcess.class))); - Location poolLocation = managementContext.getLocationManager() + Location localhostLoc = mgmt.getLocationManager() .createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class)); - t.start(ImmutableList.of(poolLocation)); - } - - @AfterMethod(alwaysRun=true) - public void tearDown() throws Exception { - if (managementContext != null) Entities.destroyAll(managementContext); + app.start(ImmutableList.of(localhostLoc)); } @Test public void testResolve() { - ServerPoolLocation location = resolve("pool:" + locationOwner.getId()); - assertEquals(location.getOwner().getId(), locationOwner.getId()); + String spec = "pool:" + serverPool.getDynamicLocation().getId(); + ServerPoolLocation loc = (ServerPoolLocation) mgmt.getLocationRegistry().getLocationManaged(spec); + assertEquals(loc, serverPool.getDynamicLocation()); + + String spec2 = serverPool.sensors().get(LocationOwner.LOCATION_SPEC); + ServerPoolLocation loc2 = (ServerPoolLocation) mgmt.getLocationRegistry().getLocationManaged(spec2); + assertEquals(loc2, serverPool.getDynamicLocation()); } - - @Test - public void testSetsDisplayName() { - ServerPoolLocation location = resolve("pool:" + locationOwner.getId() + ":(displayName=xyz)"); - assertEquals(location.getDisplayName(), "xyz"); - } - - private ServerPoolLocation resolve(String val) { - Map<String, Object> flags = MutableMap.<String, Object>of(DynamicLocation.OWNER.getName(), locationOwner); - Location l = managementContext.getLocationRegistry().getLocationManaged(val, flags); - Assert.assertNotNull(l); - return (ServerPoolLocation) l; - } - } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9f1a1208/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/ServerPoolTest.java ---------------------------------------------------------------------- diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/ServerPoolTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/ServerPoolTest.java index 4a24d62..3dacf7f 100644 --- a/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/ServerPoolTest.java +++ b/software/base/src/test/java/org/apache/brooklyn/entity/machine/pool/ServerPoolTest.java @@ -28,10 +28,10 @@ import java.util.Iterator; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.location.LocationSpec; import org.apache.brooklyn.core.entity.Attributes; +import org.apache.brooklyn.core.entity.EntityAsserts; import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; import org.apache.brooklyn.core.test.entity.TestApplication; import org.apache.brooklyn.entity.machine.pool.ServerPoolImpl; -import org.apache.brooklyn.test.EntityTestUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.testng.annotations.Test; @@ -59,7 +59,7 @@ public class ServerPoolTest extends AbstractServerPoolTest { public void testFailureWhenNotEnoughServersAvailable() { TestApplication app = createAppWithChildren(getInitialPoolSize() + 1); assertNoMachinesAvailableForApp(app); - EntityTestUtils.assertAttributeEqualsEventually(app, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE); + EntityAsserts.assertAttributeEqualsEventually(app, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE); } @Test @@ -68,7 +68,7 @@ public class ServerPoolTest extends AbstractServerPoolTest { TestApplication app2 = createAppWithChildren(1); app.start(ImmutableList.of(pool.getDynamicLocation())); - EntityTestUtils.assertAttributeEqualsEventually(app, Attributes.SERVICE_UP, true); + EntityAsserts.assertAttributeEqualsEventually(app, Attributes.SERVICE_UP, true); assertAvailableCountEventuallyEquals(0); assertNoMachinesAvailableForApp(app2); @@ -77,7 +77,7 @@ public class ServerPoolTest extends AbstractServerPoolTest { assertAvailableCountEventuallyEquals(getInitialPoolSize()); app2.start(ImmutableList.of(pool.getDynamicLocation())); - EntityTestUtils.assertAttributeEqualsEventually(app2, Attributes.SERVICE_UP, true); + EntityAsserts.assertAttributeEqualsEventually(app2, Attributes.SERVICE_UP, true); assertAvailableCountEventuallyEquals(getInitialPoolSize() - 1); assertClaimedCountEventuallyEquals(1);
