Repository: incubator-brooklyn Updated Branches: refs/heads/master 5ceb8aadb -> 681b8fe90
Simplify JcloudsLocationTest Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/9645e8c3 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/9645e8c3 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/9645e8c3 Branch: refs/heads/master Commit: 9645e8c34f23d99c545b6ed56b73943062968937 Parents: 284b763 Author: Sam Corbett <[email protected]> Authored: Fri Jun 5 14:46:29 2015 +0100 Committer: Sam Corbett <[email protected]> Committed: Thu Jun 11 15:03:12 2015 +0100 ---------------------------------------------------------------------- .../jclouds/BailOutJcloudsLocation.java | 187 ++++++++++++ .../location/jclouds/JcloudsLocationTest.java | 299 +++++-------------- 2 files changed, 266 insertions(+), 220 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9645e8c3/locations/jclouds/src/test/java/brooklyn/location/jclouds/BailOutJcloudsLocation.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/test/java/brooklyn/location/jclouds/BailOutJcloudsLocation.java b/locations/jclouds/src/test/java/brooklyn/location/jclouds/BailOutJcloudsLocation.java new file mode 100644 index 0000000..ba5dc2b --- /dev/null +++ b/locations/jclouds/src/test/java/brooklyn/location/jclouds/BailOutJcloudsLocation.java @@ -0,0 +1,187 @@ +/* + * 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 brooklyn.location.jclouds; + +import java.util.Collections; +import java.util.Map; +import javax.annotation.Nullable; + +import org.jclouds.compute.ComputeService; +import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.Template; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import com.google.common.reflect.TypeToken; + +import brooklyn.config.BrooklynProperties; +import brooklyn.config.ConfigKey; +import brooklyn.entity.basic.ConfigKeys; +import brooklyn.location.LocationSpec; +import brooklyn.management.ManagementContext; +import brooklyn.management.internal.LocalManagementContext; +import brooklyn.util.collections.MutableMap; +import brooklyn.util.config.ConfigBag; +import brooklyn.util.exceptions.CompoundRuntimeException; +import brooklyn.util.exceptions.Exceptions; + +public class BailOutJcloudsLocation extends JcloudsLocation { + + // Don't care which image; not actually provisioning + private static final String US_EAST_IMAGE_ID = "us-east-1/ami-7d7bfc14"; + public static final RuntimeException BAIL_OUT_FOR_TESTING = new RuntimeException("early termination for test"); + + public static final ConfigKey<Function<ConfigBag, Void>> BUILD_TEMPLATE_INTERCEPTOR = ConfigKeys.newConfigKey( + new TypeToken<Function<ConfigBag, Void>>() {}, + "buildtemplateinterceptor"); + + public static final ConfigKey<Boolean> BUILD_TEMPLATE = ConfigKeys.newBooleanConfigKey( + "buildtemplate"); + + private static final long serialVersionUID = -3373789512935057842L; + + ConfigBag lastConfigBag; + Template template; + + public BailOutJcloudsLocation() { + super(); + } + + public BailOutJcloudsLocation(Map<?, ?> conf) { + super(conf); + } + + @Override + public Template buildTemplate(ComputeService computeService, ConfigBag config) { + lastConfigBag = config; + if (getConfig(BUILD_TEMPLATE_INTERCEPTOR) != null) { + getConfig(BUILD_TEMPLATE_INTERCEPTOR).apply(config); + } + if (Boolean.TRUE.equals(getConfig(BUILD_TEMPLATE))) { + template = super.buildTemplate(computeService, config); + } + throw BAIL_OUT_FOR_TESTING; + } + + public Template getTemplate() { + return template; + } + + public void tryObtain() { + tryObtain(Collections.emptyMap()); + } + + public void tryObtain(Map<?, ?> flags) { + tryObtainAndCheck(flags, Predicates.alwaysTrue()); + } + + public void tryObtainAndCheck(Map<?, ?> flags, Predicate<? super ConfigBag> test) { + try { + obtain(flags); + } catch (Exception e) { + if (e == BAIL_OUT_FOR_TESTING || e.getCause() == BAIL_OUT_FOR_TESTING + || (e instanceof CompoundRuntimeException && ((CompoundRuntimeException) e).getAllCauses().contains(BAIL_OUT_FOR_TESTING))) { + test.apply(lastConfigBag); + } else { + throw Exceptions.propagate(e); + } + } + } + + @Override + @VisibleForTesting + public UserCreation createUserStatements(@Nullable Image image, ConfigBag config) { + return super.createUserStatements(image, config); + } + + + public static BailOutJcloudsLocation newBailOutJcloudsLocation(ManagementContext mgmt) { + return newBailOutJcloudsLocation(mgmt, Collections.<ConfigKey<?>, Object>emptyMap()); + } + + public static BailOutJcloudsLocation newBailOutJcloudsLocation(ManagementContext mgmt, Map<ConfigKey<?>, ?> config) { + Map<ConfigKey<?>, ?> allConfig = MutableMap.<ConfigKey<?>, Object>builder() + .put(IMAGE_ID, "bogus") + .put(CLOUD_PROVIDER, "aws-ec2") + .put(ACCESS_IDENTITY, "bogus") + .put(CLOUD_REGION_ID, "bogus") + .put(ACCESS_CREDENTIAL, "bogus") + .put(USER, "fred") + .put(MIN_RAM, 16) + .put(JcloudsLocation.MACHINE_CREATE_ATTEMPTS, 1) + .putAll(config) + .build(); + return mgmt.getLocationManager().createLocation( + LocationSpec.create(BailOutJcloudsLocation.class).configure(allConfig)); + } + + + // todo better name + + /** As {@link BailOutJcloudsLocation}, counting the number of {@link #buildTemplate} calls. */ + public static class CountingBailOutJcloudsLocation extends BailOutJcloudsLocation { + private static final long serialVersionUID = 2433684033045735773L; + int buildTemplateCount = 0; + @Override + public Template buildTemplate(ComputeService computeService, ConfigBag config) { + buildTemplateCount++; + return super.buildTemplate(computeService, config); + } + } + + public static CountingBailOutJcloudsLocation newCountingBailOutJcloudsLocation(ManagementContext mgmt, Map flags) { + LocationSpec<CountingBailOutJcloudsLocation> spec = LocationSpec.create(CountingBailOutJcloudsLocation.class) + .configure(flags); + return mgmt.getLocationManager().createLocation(spec); + } + + /** @see #newBailOutJcloudsLocationForLiveTest(LocalManagementContext, Map)} */ + public static BailOutJcloudsLocation newBailOutJcloudsLocationForLiveTest(LocalManagementContext mgmt) { + return newBailOutJcloudsLocationForLiveTest(mgmt, Collections.<ConfigKey<?>, Object>emptyMap()); + } + + /** + * Takes identity and access credential from management context's Brooklyn properties and sets + * inbound ports to [22, 80, 9999]. + */ + public static BailOutJcloudsLocation newBailOutJcloudsLocationForLiveTest(LocalManagementContext mgmt, Map<ConfigKey<?>, ?> config) { + BrooklynProperties brooklynProperties = mgmt.getBrooklynProperties(); + String identity = (String) brooklynProperties.get("brooklyn.location.jclouds.aws-ec2.identity"); + if (identity == null) identity = (String) brooklynProperties.get("brooklyn.jclouds.aws-ec2.identity"); + String credential = (String) brooklynProperties.get("brooklyn.location.jclouds.aws-ec2.credential"); + if (credential == null) credential = (String) brooklynProperties.get("brooklyn.jclouds.aws-ec2.credential"); + + Map<ConfigKey<?>, ?> allConfig = MutableMap.<ConfigKey<?>, Object>builder() + .put(CLOUD_PROVIDER, AbstractJcloudsLiveTest.AWS_EC2_PROVIDER) + .put(CLOUD_REGION_ID, AbstractJcloudsLiveTest.AWS_EC2_USEAST_REGION_NAME) + .put(IMAGE_ID, US_EAST_IMAGE_ID) // so Brooklyn does not attempt to load all EC2 images + .put(ACCESS_IDENTITY, identity) + .put(ACCESS_CREDENTIAL, credential) + .put(INBOUND_PORTS, "[22, 80, 9999]") + .put(BUILD_TEMPLATE, true) + .putAll(config) + .build(); + + return newBailOutJcloudsLocation(mgmt, allConfig); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9645e8c3/locations/jclouds/src/test/java/brooklyn/location/jclouds/JcloudsLocationTest.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/test/java/brooklyn/location/jclouds/JcloudsLocationTest.java b/locations/jclouds/src/test/java/brooklyn/location/jclouds/JcloudsLocationTest.java index 62817c2..4b74d84 100644 --- a/locations/jclouds/src/test/java/brooklyn/location/jclouds/JcloudsLocationTest.java +++ b/locations/jclouds/src/test/java/brooklyn/location/jclouds/JcloudsLocationTest.java @@ -24,12 +24,8 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; - import javax.annotation.Nullable; -import org.jclouds.compute.ComputeService; -import org.jclouds.compute.domain.Image; -import org.jclouds.compute.domain.Template; import org.jclouds.scriptbuilder.domain.OsFamily; import org.jclouds.scriptbuilder.domain.StatementList; import org.mockito.Mockito; @@ -40,6 +36,12 @@ 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.Predicate; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.reflect.TypeToken; + import brooklyn.config.BrooklynProperties; import brooklyn.config.ConfigKey; import brooklyn.entity.basic.ConfigKeys; @@ -56,18 +58,8 @@ import brooklyn.test.Asserts; import brooklyn.test.entity.LocalManagementContextForTests; import brooklyn.util.collections.MutableMap; import brooklyn.util.config.ConfigBag; -import brooklyn.util.exceptions.CompoundRuntimeException; import brooklyn.util.exceptions.Exceptions; -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Function; -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.Maps; -import com.google.common.reflect.TypeToken; - /** * @author Shane Witbeck */ @@ -75,145 +67,6 @@ public class JcloudsLocationTest implements JcloudsLocationConfig { private static final Logger log = LoggerFactory.getLogger(JcloudsLocationTest.class); - // Don't care which image; not actually provisioning - private static final String US_EAST_IMAGE_ID = "us-east-1/ami-7d7bfc14"; - - public static final RuntimeException BAIL_OUT_FOR_TESTING = - new RuntimeException("early termination for test"); - - @SuppressWarnings("serial") - public static class BailOutJcloudsLocation extends JcloudsLocation { - public static final ConfigKey<Function<ConfigBag,Void>> BUILD_TEMPLATE_INTERCEPTOR = ConfigKeys.newConfigKey(new TypeToken<Function<ConfigBag,Void>>() {}, "buildtemplateinterceptor"); - - ConfigBag lastConfigBag; - - public BailOutJcloudsLocation() { - super(); - } - - public BailOutJcloudsLocation(Map<?, ?> conf) { - super(conf); - } - - @Override - public Template buildTemplate(ComputeService computeService, ConfigBag config) { - lastConfigBag = config; - if (getConfig(BUILD_TEMPLATE_INTERCEPTOR) != null) getConfig(BUILD_TEMPLATE_INTERCEPTOR).apply(config); - throw BAIL_OUT_FOR_TESTING; - } - protected void tryObtainAndCheck(Map<?,?> flags, Predicate<? super ConfigBag> test) { - try { - obtain(flags); - } catch (Exception e) { - if (e==BAIL_OUT_FOR_TESTING || e.getCause()==BAIL_OUT_FOR_TESTING - || (e instanceof CompoundRuntimeException && ((CompoundRuntimeException)e).getAllCauses().contains(BAIL_OUT_FOR_TESTING))) { - test.apply(lastConfigBag); - } else { - throw Exceptions.propagate(e); - } - } - } - @Override @VisibleForTesting - public UserCreation createUserStatements(@Nullable Image image, ConfigBag config) { - return super.createUserStatements(image, config); - } - } - - @SuppressWarnings("serial") - public static class CountingBailOutJcloudsLocation extends BailOutJcloudsLocation { - int buildTemplateCount = 0; - @Override - public Template buildTemplate(ComputeService computeService, ConfigBag config) { - buildTemplateCount++; - return super.buildTemplate(computeService, config); - } - } - - @SuppressWarnings("serial") - public static class BailOutWithTemplateJcloudsLocation extends JcloudsLocation { - ConfigBag lastConfigBag; - Template template; - - public BailOutWithTemplateJcloudsLocation() { - super(); - } - - public BailOutWithTemplateJcloudsLocation(Map<?, ?> conf) { - super(conf); - } - - @Override - public Template buildTemplate(ComputeService computeService, ConfigBag config) { - template = super.buildTemplate(computeService, config); - - lastConfigBag = config; - throw BAIL_OUT_FOR_TESTING; - } - - protected synchronized void tryObtainAndCheck(Map<?,?> flags, Predicate<ConfigBag> test) { - try { - obtain(flags); - } catch (Throwable e) { - if (e == BAIL_OUT_FOR_TESTING) { - test.apply(lastConfigBag); - } else { - throw Exceptions.propagate(e); - } - } - } - - public Template getTemplate() { - return template; - } - } - - protected BailOutJcloudsLocation newSampleBailOutJcloudsLocationForTesting() { - return newSampleBailOutJcloudsLocationForTesting(ImmutableMap.<ConfigKey<?>,Object>of()); - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - protected BailOutJcloudsLocation newSampleBailOutJcloudsLocationForTesting(Map<?,?> config) { - Map<ConfigKey<?>,?> allConfig = MutableMap.<ConfigKey<?>,Object>builder() - .put(IMAGE_ID, "bogus") - .put(CLOUD_PROVIDER, "aws-ec2") - .put(ACCESS_IDENTITY, "bogus") - .put(CLOUD_REGION_ID, "bogus") - .put(ACCESS_CREDENTIAL, "bogus") - .put(USER, "fred") - .put(MIN_RAM, 16) - .put(JcloudsLocation.MACHINE_CREATE_ATTEMPTS, 1) - .putAll((Map)config) - .build(); - return managementContext.getLocationManager().createLocation(LocationSpec.create(BailOutJcloudsLocation.class) - .configure(allConfig)); - } - - protected BailOutWithTemplateJcloudsLocation newSampleBailOutWithTemplateJcloudsLocation() { - return newSampleBailOutWithTemplateJcloudsLocation(ImmutableMap.<ConfigKey<?>,Object>of()); - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - protected BailOutWithTemplateJcloudsLocation newSampleBailOutWithTemplateJcloudsLocation(Map<?,?> config) { - String identity = (String) brooklynProperties.get("brooklyn.location.jclouds.aws-ec2.identity"); - if (identity == null) identity = (String) brooklynProperties.get("brooklyn.jclouds.aws-ec2.identity"); - String credential = (String) brooklynProperties.get("brooklyn.location.jclouds.aws-ec2.credential"); - if (credential == null) credential = (String) brooklynProperties.get("brooklyn.jclouds.aws-ec2.credential"); - - Map<ConfigKey<?>,?> allConfig = MutableMap.<ConfigKey<?>,Object>builder() - .put(CLOUD_PROVIDER, AbstractJcloudsLiveTest.AWS_EC2_PROVIDER) - .put(CLOUD_REGION_ID, AbstractJcloudsLiveTest.AWS_EC2_USEAST_REGION_NAME) - .put(IMAGE_ID, US_EAST_IMAGE_ID) // so it runs faster, without loading all EC2 images - .put(ACCESS_IDENTITY, identity) - .put(ACCESS_CREDENTIAL, credential) - .put(USER, "fred") - .put(INBOUND_PORTS, "[22, 80, 9999]") - .putAll((Map)config) - .build(); - - return managementContext.getLocationManager().createLocation(LocationSpec.create(BailOutWithTemplateJcloudsLocation.class) - .configure(allConfig)); - } - public static Predicate<ConfigBag> checkerFor(final String user, final Integer minRam, final Integer minCores) { return new Predicate<ConfigBag>() { @Override @@ -236,13 +89,11 @@ public class JcloudsLocationTest implements JcloudsLocationConfig { }; } - private BrooklynProperties brooklynProperties; private LocalManagementContext managementContext; @BeforeMethod(alwaysRun=true) public void setUp() throws Exception { managementContext = LocalManagementContextForTests.newInstance(BrooklynProperties.Factory.builderEmpty().build()); - brooklynProperties = managementContext.getBrooklynProperties(); } @AfterMethod(alwaysRun=true) @@ -252,19 +103,19 @@ public class JcloudsLocationTest implements JcloudsLocationConfig { @Test public void testCreateWithFlagsDirectly() throws Exception { - BailOutJcloudsLocation jcl = newSampleBailOutJcloudsLocationForTesting(); + BailOutJcloudsLocation jcl = BailOutJcloudsLocation.newBailOutJcloudsLocation(managementContext); jcl.tryObtainAndCheck(MutableMap.of(MIN_CORES, 2), checkerFor("fred", 16, 2)); } @Test public void testCreateWithFlagsDirectlyAndOverride() throws Exception { - BailOutJcloudsLocation jcl = newSampleBailOutJcloudsLocationForTesting(); + BailOutJcloudsLocation jcl = BailOutJcloudsLocation.newBailOutJcloudsLocation(managementContext); jcl.tryObtainAndCheck(MutableMap.of(MIN_CORES, 2, MIN_RAM, 8), checkerFor("fred", 8, 2)); } @Test public void testCreateWithFlagsSubLocation() throws Exception { - BailOutJcloudsLocation jcl = newSampleBailOutJcloudsLocationForTesting(); + BailOutJcloudsLocation jcl = BailOutJcloudsLocation.newBailOutJcloudsLocation(managementContext); jcl = (BailOutJcloudsLocation) jcl.newSubLocation(MutableMap.of(USER, "jon", MIN_CORES, 2)); jcl.tryObtainAndCheck(MutableMap.of(MIN_CORES, 3), checkerFor("jon", 16, 3)); } @@ -310,37 +161,36 @@ public class JcloudsLocationTest implements JcloudsLocationConfig { @Test public void testVMCreationIsRetriedOnFailure() { - Map<ConfigKey<?>, Object> flags = Maps.newHashMap(); - flags.put(IMAGE_ID, "bogus"); - flags.put(CLOUD_PROVIDER, "aws-ec2"); - flags.put(ACCESS_IDENTITY, "bogus"); - flags.put(CLOUD_REGION_ID, "bogus"); - flags.put(ACCESS_CREDENTIAL, "bogus"); - flags.put(USER, "fred"); - flags.put(MIN_RAM, 16); - flags.put(MACHINE_CREATE_ATTEMPTS, 3); - CountingBailOutJcloudsLocation jcl = managementContext.getLocationManager().createLocation( - LocationSpec.create(CountingBailOutJcloudsLocation.class).configure(flags)); - jcl.tryObtainAndCheck(ImmutableMap.of(), Predicates.<ConfigBag>alwaysTrue()); - Assert.assertEquals(jcl.buildTemplateCount, 3); + final AtomicInteger count = new AtomicInteger(); + Function<ConfigBag, Void> countingInterceptor = new Function<ConfigBag, Void>() { + @Override public Void apply(ConfigBag input) { + count.incrementAndGet(); + return null; + } + }; + BailOutJcloudsLocation loc = BailOutJcloudsLocation.newBailOutJcloudsLocation(managementContext, ImmutableMap.<ConfigKey<?>, Object>of( + MACHINE_CREATE_ATTEMPTS, 3, + BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, countingInterceptor)); + loc.tryObtain(); + Assert.assertEquals(count.get(), 3); } @Test(groups={"Live", "Live-sanity"}) public void testCreateWithInboundPorts() { - BailOutWithTemplateJcloudsLocation jcloudsLocation = newSampleBailOutWithTemplateJcloudsLocation(); - jcloudsLocation = (BailOutWithTemplateJcloudsLocation) jcloudsLocation.newSubLocation(MutableMap.of()); + BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocationForLiveTest(managementContext); + jcloudsLocation = (BailOutJcloudsLocation) jcloudsLocation.newSubLocation(MutableMap.of()); jcloudsLocation.tryObtainAndCheck(MutableMap.of(), templateCheckerFor("[22, 80, 9999]")); int[] ports = new int[] {22, 80, 9999}; - Assert.assertEquals(jcloudsLocation.template.getOptions().getInboundPorts(), ports); + Assert.assertEquals(jcloudsLocation.getTemplate().getOptions().getInboundPorts(), ports); } @Test(groups={"Live", "Live-sanity"}) public void testCreateWithInboundPortsOverride() { - BailOutWithTemplateJcloudsLocation jcloudsLocation = newSampleBailOutWithTemplateJcloudsLocation(); - jcloudsLocation = (BailOutWithTemplateJcloudsLocation) jcloudsLocation.newSubLocation(MutableMap.of()); + BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocationForLiveTest(managementContext); + jcloudsLocation = (BailOutJcloudsLocation) jcloudsLocation.newSubLocation(MutableMap.of()); jcloudsLocation.tryObtainAndCheck(MutableMap.of(INBOUND_PORTS, "[23, 81, 9998]"), templateCheckerFor("[23, 81, 9998]")); int[] ports = new int[] {23, 81, 9998}; - Assert.assertEquals(jcloudsLocation.template.getOptions().getInboundPorts(), ports); + Assert.assertEquals(jcloudsLocation.getTemplate().getOptions().getInboundPorts(), ports); } @Test @@ -350,21 +200,21 @@ public class JcloudsLocationTest implements JcloudsLocationConfig { ExecutorService executor = Executors.newCachedThreadPool(); try { - final BailOutJcloudsLocation jcloudsLocation = newSampleBailOutJcloudsLocationForTesting(ImmutableMap.of(BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, interceptor)); - + final BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocation( + managementContext, ImmutableMap.<ConfigKey<?>, Object>of( + BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, interceptor)); for (int i = 0; i < numCalls; i++) { executor.execute(new Runnable() { - @Override public void run() { - jcloudsLocation.tryObtainAndCheck(MutableMap.of(), Predicates.alwaysTrue()); - }}); + @Override + public void run() { + jcloudsLocation.tryObtain(); + } + }); } - interceptor.assertCallCountEventually(numCalls); - interceptor.unblock(); executor.shutdown(); executor.awaitTermination(10, TimeUnit.SECONDS); - } finally { executor.shutdownNow(); } @@ -378,25 +228,28 @@ public class JcloudsLocationTest implements JcloudsLocationConfig { ExecutorService executor = Executors.newCachedThreadPool(); try { - final BailOutJcloudsLocation jcloudsLocation = newSampleBailOutJcloudsLocationForTesting(ImmutableMap.of( - BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, interceptor, - JcloudsLocation.MAX_CONCURRENT_MACHINE_CREATIONS, maxConcurrentCreations)); - + final BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocation( + managementContext, ImmutableMap.of( + BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, interceptor, + JcloudsLocation.MAX_CONCURRENT_MACHINE_CREATIONS, maxConcurrentCreations)); + for (int i = 0; i < numCalls; i++) { executor.execute(new Runnable() { - @Override public void run() { - jcloudsLocation.tryObtainAndCheck(MutableMap.of(), Predicates.alwaysTrue()); - }}); + @Override + public void run() { + jcloudsLocation.tryObtain(); + } + }); } - + interceptor.assertCallCountEventually(maxConcurrentCreations); interceptor.assertCallCountContinually(maxConcurrentCreations); - + interceptor.unblock(); interceptor.assertCallCountEventually(numCalls); executor.shutdown(); executor.awaitTermination(10, TimeUnit.SECONDS); - + } finally { executor.shutdownNow(); } @@ -410,22 +263,24 @@ public class JcloudsLocationTest implements JcloudsLocationConfig { ExecutorService executor = Executors.newCachedThreadPool(); try { - final BailOutJcloudsLocation jcloudsLocation = newSampleBailOutJcloudsLocationForTesting(ImmutableMap.of( - BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, interceptor, - JcloudsLocation.MAX_CONCURRENT_MACHINE_CREATIONS, maxConcurrentCreations)); - - + final BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocation( + managementContext, ImmutableMap.of( + BailOutJcloudsLocation.BUILD_TEMPLATE_INTERCEPTOR, interceptor, + JcloudsLocation.MAX_CONCURRENT_MACHINE_CREATIONS, maxConcurrentCreations)); + for (int i = 0; i < numCalls; i++) { final BailOutJcloudsLocation subLocation = (BailOutJcloudsLocation) jcloudsLocation.newSubLocation(MutableMap.of()); executor.execute(new Runnable() { - @Override public void run() { - subLocation.tryObtainAndCheck(MutableMap.of(), Predicates.alwaysTrue()); - }}); + @Override + public void run() { + subLocation.tryObtain(); + } + }); } - + interceptor.assertCallCountEventually(maxConcurrentCreations); interceptor.assertCallCountContinually(maxConcurrentCreations); - + interceptor.unblock(); interceptor.assertCallCountEventually(numCalls); executor.shutdown(); @@ -439,8 +294,9 @@ public class JcloudsLocationTest implements JcloudsLocationConfig { @Test public void testCreateWithCustomMachineNamer() { final String machineNamerClass = CustomMachineNamer.class.getName(); - BailOutJcloudsLocation jcloudsLocation = newSampleBailOutJcloudsLocationForTesting(ImmutableMap.of( - LocationConfigKeys.CLOUD_MACHINE_NAMER_CLASS, machineNamerClass)); + BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocation( + managementContext, ImmutableMap.<ConfigKey<?>, Object>of( + LocationConfigKeys.CLOUD_MACHINE_NAMER_CLASS, machineNamerClass)); jcloudsLocation.tryObtainAndCheck(ImmutableMap.of(CustomMachineNamer.MACHINE_NAME_TEMPLATE, "ignored"), new Predicate<ConfigBag>() { public boolean apply(ConfigBag input) { Assert.assertEquals(input.get(LocationConfigKeys.CLOUD_MACHINE_NAMER_CLASS), machineNamerClass); @@ -452,10 +308,11 @@ public class JcloudsLocationTest implements JcloudsLocationConfig { @Test public void testCreateWithCustomMachineNamerOnObtain() { final String machineNamerClass = CustomMachineNamer.class.getName(); - BailOutJcloudsLocation jcloudsLocation = newSampleBailOutJcloudsLocationForTesting(); - jcloudsLocation.tryObtainAndCheck(ImmutableMap.of( + BailOutJcloudsLocation jcloudsLocation = BailOutJcloudsLocation.newBailOutJcloudsLocation(managementContext); + ImmutableMap<ConfigKey<String>, String> flags = ImmutableMap.of( CustomMachineNamer.MACHINE_NAME_TEMPLATE, "ignored", - LocationConfigKeys.CLOUD_MACHINE_NAMER_CLASS, machineNamerClass), new Predicate<ConfigBag>() { + LocationConfigKeys.CLOUD_MACHINE_NAMER_CLASS, machineNamerClass); + jcloudsLocation.tryObtainAndCheck(flags, new Predicate<ConfigBag>() { public boolean apply(ConfigBag input) { Assert.assertEquals(input.get(LocationConfigKeys.CLOUD_MACHINE_NAMER_CLASS), machineNamerClass); return true; @@ -603,33 +460,35 @@ public class JcloudsLocationTest implements JcloudsLocationConfig { // now test creating users - protected String getCreateUserStatementsFor(Map<?,?> config) { - BailOutJcloudsLocation jl = newSampleBailOutJcloudsLocationForTesting(MutableMap.<Object,Object>builder() - .put(JcloudsLocationConfig.LOGIN_USER, "root").put(JcloudsLocationConfig.LOGIN_USER_PASSWORD, "m0ck") - .put(JcloudsLocationConfig.USER, "bob").put(JcloudsLocationConfig.LOGIN_USER_PASSWORD, "b0b") - .putAll(config).build()); - + protected String getCreateUserStatementsFor(Map<ConfigKey<?>,?> config) { + BailOutJcloudsLocation jl = BailOutJcloudsLocation.newBailOutJcloudsLocation( + managementContext, MutableMap.<ConfigKey<?>, Object>builder() + .put(JcloudsLocationConfig.LOGIN_USER, "root").put(JcloudsLocationConfig.LOGIN_USER_PASSWORD, "m0ck") + .put(JcloudsLocationConfig.USER, "bob").put(JcloudsLocationConfig.LOGIN_USER_PASSWORD, "b0b") + .putAll(config).build()); + UserCreation creation = jl.createUserStatements(null, jl.config().getBag()); - return new StatementList(creation.statements).render(OsFamily.UNIX); + return new StatementList(creation.statements).render(OsFamily.UNIX); } @Test public void testDisablesRoot() { - String statements = getCreateUserStatementsFor(ImmutableMap.of()); + String statements = getCreateUserStatementsFor(ImmutableMap.<ConfigKey<?>, Object>of()); Assert.assertTrue(statements.contains("PermitRootLogin"), "Error:\n"+statements); Assert.assertTrue(statements.matches("(?s).*sudoers.*useradd.*bob.*wheel.*"), "Error:\n"+statements); } @Test public void testDisableRootFalse() { - String statements = getCreateUserStatementsFor(ImmutableMap.of(JcloudsLocationConfig.DISABLE_ROOT_AND_PASSWORD_SSH, false)); + String statements = getCreateUserStatementsFor(ImmutableMap.<ConfigKey<?>, Object>of( + JcloudsLocationConfig.DISABLE_ROOT_AND_PASSWORD_SSH, false)); Assert.assertFalse(statements.contains("PermitRootLogin"), "Error:\n"+statements); Assert.assertTrue(statements.matches("(?s).*sudoers.*useradd.*bob.*wheel.*"), "Error:\n"+statements); } @Test public void testDisableRootAndSudoFalse() { - String statements = getCreateUserStatementsFor(ImmutableMap.of( + String statements = getCreateUserStatementsFor(ImmutableMap.<ConfigKey<?>, Object>of( JcloudsLocationConfig.DISABLE_ROOT_AND_PASSWORD_SSH, false, JcloudsLocationConfig.GRANT_USER_SUDO, false)); Assert.assertFalse(statements.contains("PermitRootLogin"), "Error:\n"+statements);
