http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/AbstractYamlTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/AbstractYamlTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/AbstractYamlTest.java deleted file mode 100644 index 67a2009..0000000 --- a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/AbstractYamlTest.java +++ /dev/null @@ -1,182 +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 io.brooklyn.camp.brooklyn; - -import io.brooklyn.camp.spi.Assembly; -import io.brooklyn.camp.spi.AssemblyTemplate; - -import java.io.Reader; -import java.io.StringReader; -import java.util.Set; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; - -import brooklyn.catalog.internal.CatalogUtils; -import brooklyn.entity.Entity; -import brooklyn.entity.basic.BrooklynTaskTags; -import brooklyn.entity.basic.Entities; -import brooklyn.management.ManagementContext; -import brooklyn.management.Task; -import brooklyn.management.internal.LocalManagementContext; -import brooklyn.test.entity.LocalManagementContextForTests; -import brooklyn.util.ResourceUtils; -import brooklyn.util.config.ConfigBag; - -import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; - -public abstract class AbstractYamlTest { - - private static final Logger LOG = LoggerFactory.getLogger(AbstractYamlTest.class); - protected static final String TEST_VERSION = "0.1.2"; - - private ManagementContext brooklynMgmt; - protected BrooklynCampPlatform platform; - protected BrooklynCampPlatformLauncherNoServer launcher; - private boolean forceUpdate; - - public AbstractYamlTest() { - super(); - } - - protected ManagementContext mgmt() { return brooklynMgmt; } - - @BeforeMethod(alwaysRun = true) - public void setUp() { - forceUpdate = false; - launcher = new BrooklynCampPlatformLauncherNoServer() { - @Override - protected LocalManagementContext newMgmtContext() { - return newTestManagementContext(); - } - }; - launcher.launch(); - brooklynMgmt = launcher.getBrooklynMgmt(); - platform = launcher.getCampPlatform(); - } - - protected LocalManagementContext newTestManagementContext() { - // TODO they don't all need osgi, just a few do, so could speed it up by specifying when they do - return LocalManagementContextForTests.newInstanceWithOsgi(); - } - - @AfterMethod(alwaysRun = true) - public void tearDown() { - if (brooklynMgmt != null) Entities.destroyAll(brooklynMgmt); - if (launcher != null) launcher.stopServers(); - } - - protected void waitForApplicationTasks(Entity app) { - Set<Task<?>> tasks = BrooklynTaskTags.getTasksInEntityContext(brooklynMgmt.getExecutionManager(), app); - getLogger().info("Waiting on " + tasks.size() + " task(s)"); - for (Task<?> t : tasks) { - t.blockUntilEnded(); - } - } - - protected Reader loadYaml(String yamlFileName, String ...extraLines) throws Exception { - String input = new ResourceUtils(this).getResourceAsString(yamlFileName).trim(); - StringBuilder builder = new StringBuilder(input); - for (String l: extraLines) - builder.append("\n").append(l); - return new StringReader(builder.toString()); - } - - protected Entity createAndStartApplication(String... multiLineYaml) throws Exception { - return createAndStartApplication(joinLines(multiLineYaml)); - } - - protected Entity createAndStartApplication(String input) throws Exception { - return createAndStartApplication(new StringReader(input)); - } - - protected Entity createAndStartApplication(Reader input) throws Exception { - AssemblyTemplate at = platform.pdp().registerDeploymentPlan(input); - Assembly assembly; - try { - assembly = at.getInstantiator().newInstance().instantiate(at, platform); - } catch (Exception e) { - getLogger().warn("Unable to instantiate " + at + " (rethrowing): " + e); - throw e; - } - getLogger().info("Test - created " + assembly); - final Entity app = brooklynMgmt.getEntityManager().getEntity(assembly.getId()); - getLogger().info("App - " + app); - - // wait for app to have started - Set<Task<?>> tasks = brooklynMgmt.getExecutionManager().getTasksWithAllTags(ImmutableList.of( - BrooklynTaskTags.EFFECTOR_TAG, - BrooklynTaskTags.tagForContextEntity(app), - BrooklynTaskTags.tagForEffectorCall(app, "start", ConfigBag.newInstance(ImmutableMap.of("locations", ImmutableMap.of()))))); - Iterables.getOnlyElement(tasks).get(); - - return app; - } - - protected Entity createStartWaitAndLogApplication(Reader input) throws Exception { - Entity app = createAndStartApplication(input); - waitForApplicationTasks(app); - - getLogger().info("App started:"); - Entities.dumpInfo(app); - - return app; - } - - protected void addCatalogItems(Iterable<String> catalogYaml) { - addCatalogItems(joinLines(catalogYaml)); - } - - protected void addCatalogItems(String... catalogYaml) { - addCatalogItems(joinLines(catalogYaml)); - } - - protected void addCatalogItems(String catalogYaml) { - mgmt().getCatalog().addItems(catalogYaml, forceUpdate); - } - - protected void deleteCatalogEntity(String catalogItem) { - mgmt().getCatalog().deleteCatalogItem(catalogItem, TEST_VERSION); - } - - protected Logger getLogger() { - return LOG; - } - - private String joinLines(Iterable<String> catalogYaml) { - return Joiner.on("\n").join(catalogYaml); - } - - private String joinLines(String[] catalogYaml) { - return Joiner.on("\n").join(catalogYaml); - } - - protected String ver(String id) { - return CatalogUtils.getVersionedId(id, TEST_VERSION); - } - - public void forceCatalogUpdate() { - forceUpdate = true; - } -}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/AppYamlTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/AppYamlTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/AppYamlTest.java deleted file mode 100644 index 26784ff..0000000 --- a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/AppYamlTest.java +++ /dev/null @@ -1,119 +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 io.brooklyn.camp.brooklyn; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; - -import java.io.StringReader; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.Test; - -import com.google.api.client.repackaged.com.google.common.base.Joiner; -import com.google.common.collect.Iterables; - -import brooklyn.entity.basic.BasicApplication; -import brooklyn.test.entity.TestApplication; -import brooklyn.test.entity.TestEntity; - -@Test -public class AppYamlTest extends AbstractYamlTest { - private static final Logger log = LoggerFactory.getLogger(AppYamlTest.class); - - @Test - public void testAutoWrapsEntityInApp() throws Exception { - String yaml = Joiner.on("\n").join( - "services:", - "- serviceType: brooklyn.test.entity.TestEntity"); - - BasicApplication app = (BasicApplication) createStartWaitAndLogApplication(new StringReader(yaml)); - @SuppressWarnings("unused") - TestEntity entity = (TestEntity) Iterables.getOnlyElement(app.getChildren()); - } - - @Test - public void testDoesNotAutoWrapApp() throws Exception { - String yaml = Joiner.on("\n").join( - "services:", - "- serviceType: brooklyn.test.entity.TestApplication"); - - TestApplication app = (TestApplication) createStartWaitAndLogApplication(new StringReader(yaml)); - assertTrue(app.getChildren().isEmpty()); - } - - @Test - public void testWrapsAppIfNameAtTopLevelAndOnApp() throws Exception { - String yaml = Joiner.on("\n").join( - "name: myTopLevelName", - "services:", - "- serviceType: brooklyn.test.entity.TestApplication", - " name: myEntityName"); - - BasicApplication app = (BasicApplication) createStartWaitAndLogApplication(new StringReader(yaml)); - TestApplication entity = (TestApplication) Iterables.getOnlyElement(app.getChildren()); - assertEquals(app.getDisplayName(), "myTopLevelName"); - assertEquals(entity.getDisplayName(), "myEntityName"); - } - - @Test - public void testDoesNotWrapAppIfNoConflictingNameOnApp() throws Exception { - String yaml = Joiner.on("\n").join( - "name: myTopLevelName", - "services:", - "- serviceType: brooklyn.test.entity.TestApplication"); - - TestApplication app = (TestApplication) createStartWaitAndLogApplication(new StringReader(yaml)); - assertTrue(app.getChildren().isEmpty()); - assertEquals(app.getDisplayName(), "myTopLevelName"); - } - - @Test - public void testDoesNotWrapAppWithDefaultDisplayName() throws Exception { - String yaml = Joiner.on("\n").join( - "name: myTopLevelName", - "services:", - "- serviceType: brooklyn.test.entity.TestApplication", - " brooklyn.config:", - " defaultDisplayName: myDefaultEntityName"); - - TestApplication app = (TestApplication) createStartWaitAndLogApplication(new StringReader(yaml)); - assertTrue(app.getChildren().isEmpty()); - assertEquals(app.getDisplayName(), "myTopLevelName"); - } - - @Test - public void testUsesDefaultDisplayNameIfNoOther() throws Exception { - String yaml = Joiner.on("\n").join( - "services:", - "- serviceType: brooklyn.test.entity.TestApplication", - " brooklyn.config:", - " defaultDisplayName: myDefaultEntityName"); - - TestApplication app = (TestApplication) createStartWaitAndLogApplication(new StringReader(yaml)); - assertTrue(app.getChildren().isEmpty()); - assertEquals(app.getDisplayName(), "myDefaultEntityName"); - } - - @Override - protected Logger getLogger() { - return log; - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/BrooklynYamlTypeInstantiatorTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/BrooklynYamlTypeInstantiatorTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/BrooklynYamlTypeInstantiatorTest.java deleted file mode 100644 index 61fe084..0000000 --- a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/BrooklynYamlTypeInstantiatorTest.java +++ /dev/null @@ -1,76 +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 io.brooklyn.camp.brooklyn; - -import io.brooklyn.camp.brooklyn.spi.creation.BrooklynYamlTypeInstantiator; -import io.brooklyn.camp.brooklyn.spi.creation.BrooklynYamlTypeInstantiator.Factory; -import io.brooklyn.camp.brooklyn.spi.creation.BrooklynYamlTypeInstantiator.InstantiatorFromKey; - -import org.testng.Assert; -import org.testng.annotations.Test; - -import brooklyn.management.classloading.BrooklynClassLoadingContext; -import brooklyn.management.classloading.JavaBrooklynClassLoadingContext; -import brooklyn.policy.Policy; -import brooklyn.policy.ha.ServiceRestarter; -import brooklyn.util.collections.MutableMap; -import brooklyn.util.javalang.JavaClassNames; -import brooklyn.util.time.Duration; - -public class BrooklynYamlTypeInstantiatorTest extends AbstractYamlTest { - - protected BrooklynClassLoadingContext loader() { - return JavaBrooklynClassLoadingContext.create(mgmt()); - } - - @Test - public void testLoadPolicySpecProgrammatically() { - Factory loader = new BrooklynYamlTypeInstantiator.Factory(loader(), "test:"+JavaClassNames.niceClassAndMethod()); - InstantiatorFromKey decoL = loader.from(MutableMap.of("some_type", ServiceRestarter.class.getName())).prefix("some"); - - Assert.assertTrue(decoL.getConfigMap().isEmpty()); - Assert.assertEquals(decoL.getTypeName().get(), ServiceRestarter.class.getName()); - Assert.assertEquals(decoL.getType(), ServiceRestarter.class); - - Object sl1 = decoL.newInstance(); - Assert.assertTrue(sl1 instanceof ServiceRestarter); - - Policy sl2 = decoL.newInstance(Policy.class); - Assert.assertTrue(sl2 instanceof ServiceRestarter); - } - - @Test - public void testLoadPolicySpecWithBrooklynConfig() { - Factory loader = new BrooklynYamlTypeInstantiator.Factory(loader(), "test:"+JavaClassNames.niceClassAndMethod()); - InstantiatorFromKey decoL = loader.from(MutableMap.of("some_type", ServiceRestarter.class.getName(), - "brooklyn.config", MutableMap.of("failOnRecurringFailuresInThisDuration", Duration.seconds(42)))).prefix("some"); - Policy sl2 = decoL.newInstance(Policy.class); - Assert.assertEquals(sl2.getConfig(ServiceRestarter.FAIL_ON_RECURRING_FAILURES_IN_THIS_DURATION).toSeconds(), 42); - } - - @Test(groups = "WIP") - public void testLoadPolicySpecWithFlag() { - Factory loader = new BrooklynYamlTypeInstantiator.Factory(loader(), "test:"+JavaClassNames.niceClassAndMethod()); - InstantiatorFromKey decoL = loader.from(MutableMap.of("some_type", ServiceRestarter.class.getName(), - "failOnRecurringFailuresInThisDuration", Duration.seconds(42))).prefix("some"); - Policy sl2 = decoL.newInstance(Policy.class); - Assert.assertEquals(sl2.getConfig(ServiceRestarter.FAIL_ON_RECURRING_FAILURES_IN_THIS_DURATION).toSeconds(), 42); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/ByonLocationsYamlTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/ByonLocationsYamlTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/ByonLocationsYamlTest.java deleted file mode 100644 index eaf3884..0000000 --- a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/ByonLocationsYamlTest.java +++ /dev/null @@ -1,281 +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 io.brooklyn.camp.brooklyn; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; - -import java.io.StringReader; -import java.util.Map; -import java.util.Set; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.Test; - -import com.google.api.client.repackaged.com.google.common.base.Joiner; -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; -import com.google.common.net.HostAndPort; - -import brooklyn.entity.Entity; -import brooklyn.entity.basic.ConfigKeys; -import brooklyn.entity.basic.DoNothingSoftwareProcess; -import brooklyn.entity.basic.Entities; -import brooklyn.location.MachineLocation; -import brooklyn.location.access.PortForwardManager; -import brooklyn.location.basic.FixedListMachineProvisioningLocation; -import brooklyn.location.basic.LocationPredicates; -import brooklyn.location.basic.Machines; -import brooklyn.location.basic.SshMachineLocation; -import brooklyn.location.basic.WinRmMachineLocation; -import brooklyn.location.cloud.CloudLocationConfig; -import brooklyn.test.Asserts; -import brooklyn.util.net.UserAndHostAndPort; - -public class ByonLocationsYamlTest extends AbstractYamlTest { - private static final Logger log = LoggerFactory.getLogger(ByonLocationsYamlTest.class); - - @Test - @SuppressWarnings("unchecked") - public void testByonSpec() throws Exception { - String yaml = Joiner.on("\n").join( - "location: byon(user=myuser,mykey=myval,hosts=\"1.1.1.1\")", - "services:", - "- serviceType: brooklyn.entity.basic.BasicApplication"); - - Entity app = createStartWaitAndLogApplication(new StringReader(yaml)); - FixedListMachineProvisioningLocation<SshMachineLocation> loc = (FixedListMachineProvisioningLocation<SshMachineLocation>) Iterables.get(app.getLocations(), 0); - - Set<SshMachineLocation> machines = loc.getAvailable(); - SshMachineLocation machine = Iterables.getOnlyElement(machines); - assertMachine(machine, UserAndHostAndPort.fromParts("myuser", "1.1.1.1", 22), ImmutableMap.of("mykey", "myval")); - } - - @Test - @SuppressWarnings("unchecked") - public void testByonMachine() throws Exception { - String yaml = Joiner.on("\n").join( - "location:", - " byon:", - " hosts:", - " - ssh: 1.1.1.1:8022", - " privateAddresses: [10.0.0.1]", - " password: mypassword", - " user: myuser", - " mykey: myval", - "services:", - "- serviceType: brooklyn.entity.basic.BasicApplication"); - - Entity app = createStartWaitAndLogApplication(new StringReader(yaml)); - FixedListMachineProvisioningLocation<SshMachineLocation> loc = (FixedListMachineProvisioningLocation<SshMachineLocation>) Iterables.get(app.getLocations(), 0); - - Set<SshMachineLocation> machines = loc.getAvailable(); - SshMachineLocation machine = Iterables.getOnlyElement(machines); - assertMachine(machine, UserAndHostAndPort.fromParts("myuser", "1.1.1.1", 8022), ImmutableMap.of( - SshMachineLocation.PASSWORD.getName(), "mypassword", - "mykey", "myval")); - assertEquals(machine.getPrivateAddresses(), ImmutableSet.of("10.0.0.1")); - } - - @Test - @SuppressWarnings("unchecked") - public void testByonWindowsMachine() throws Exception { - String yaml = Joiner.on("\n").join( - "location:", - " byon:", - " hosts:", - " - winrm: 1.1.1.1:8985", - " privateAddresses: [10.0.0.1]", - " password: mypassword", - " user: myuser", - " mykey: myval", - " osfamily: windows", - "services:", - "- serviceType: brooklyn.entity.basic.BasicApplication"); - - Entity app = createStartWaitAndLogApplication(new StringReader(yaml)); - FixedListMachineProvisioningLocation<WinRmMachineLocation> loc = (FixedListMachineProvisioningLocation<WinRmMachineLocation>) Iterables.get(app.getLocations(), 0); - - Set<WinRmMachineLocation> machines = loc.getAvailable(); - WinRmMachineLocation machine = Iterables.getOnlyElement(machines); - assertMachine(machine, UserAndHostAndPort.fromParts("myuser", "1.1.1.1", 8985), ImmutableMap.of( - SshMachineLocation.PASSWORD.getName(), "mypassword", - "mykey", "myval")); - assertEquals(machine.getPrivateAddresses(), ImmutableSet.of("10.0.0.1")); - } - - @Test - @SuppressWarnings("unchecked") - public void testByonMultiMachine() throws Exception { - String yaml = Joiner.on("\n").join( - "location:", - " byon:", - " hosts:", - " - ssh: 1.1.1.1:8022", - " privateAddresses: [10.0.0.1]", - " password: mypassword", - " user: myuser", - " mykey: myval1", - " - ssh: 1.1.1.2:8022", - " privateAddresses: [10.0.0.2]", - " password: mypassword", - " user: myuser", - " mykey: myval2", - " - winrm: 1.1.1.3:8985", - " privateAddresses: [10.0.0.3]", - " password: mypassword", - " user: myuser", - " mykey: myval3", - " osfamily: windows", - "services:", - "- serviceType: brooklyn.entity.basic.BasicApplication"); - - Entity app = createStartWaitAndLogApplication(new StringReader(yaml)); - FixedListMachineProvisioningLocation<MachineLocation> loc = (FixedListMachineProvisioningLocation<MachineLocation>) Iterables.get(app.getLocations(), 0); - - Set<MachineLocation> machines = loc.getAvailable(); - assertEquals(machines.size(), 3, "machines="+machines); - SshMachineLocation machine1 = (SshMachineLocation) Iterables.find(machines, LocationPredicates.configEqualTo(ConfigKeys.newStringConfigKey("mykey"), "myval1")); - SshMachineLocation machine2 = (SshMachineLocation) Iterables.find(machines, LocationPredicates.configEqualTo(ConfigKeys.newStringConfigKey("mykey"), "myval2")); - WinRmMachineLocation machine3 = (WinRmMachineLocation) Iterables.find(machines, Predicates.instanceOf(WinRmMachineLocation.class)); - - assertMachine(machine1, UserAndHostAndPort.fromParts("myuser", "1.1.1.1", 8022), ImmutableMap.of( - SshMachineLocation.PASSWORD.getName(), "mypassword", - "mykey", "myval1")); - assertEquals(machine1.getPrivateAddresses(), ImmutableSet.of("10.0.0.1")); - - assertMachine(machine2, UserAndHostAndPort.fromParts("myuser", "1.1.1.2", 8022), ImmutableMap.of( - SshMachineLocation.PASSWORD.getName(), "mypassword", - "mykey", "myval2")); - assertEquals(machine2.getPrivateAddresses(), ImmutableSet.of("10.0.0.2")); - - assertMachine(machine3, UserAndHostAndPort.fromParts("myuser", "1.1.1.3", 8985), ImmutableMap.of( - SshMachineLocation.PASSWORD.getName(), "mypassword", - "mykey", "myval3")); - assertEquals(machine3.getPrivateAddresses(), ImmutableSet.of("10.0.0.3")); - } - - @Test - @SuppressWarnings("unchecked") - public void testByonPortMapping() throws Exception { - String yaml = Joiner.on("\n").join( - "location:", - " byon:", - " hosts:", - " - ssh: 1.1.1.1:22", - " privateAddresses: [10.0.0.1]", - " tcpPortMappings: {22: \"83.222.229.1:12001\", 8080: \"83.222.229.1:12002\"}", - " password: mypassword", - " user: myuser", - " mykey: myval1", - " - winrm: 1.1.1.2:8985", - " privateAddresses: [10.0.0.2]", - " tcpPortMappings: {8985: \"83.222.229.2:12003\", 8080: \"83.222.229.2:12004\"}", - " password: mypassword", - " user: myuser", - " mykey: myval2", - " osfamily: windows", - "services:", - "- serviceType: brooklyn.entity.basic.BasicApplication"); - - Entity app = createStartWaitAndLogApplication(new StringReader(yaml)); - FixedListMachineProvisioningLocation<MachineLocation> loc = (FixedListMachineProvisioningLocation<MachineLocation>) Iterables.get(app.getLocations(), 0); - PortForwardManager pfm = (PortForwardManager) mgmt().getLocationRegistry().resolve("portForwardManager(scope=global)"); - - Set<MachineLocation> machines = loc.getAvailable(); - assertEquals(machines.size(), 2, "machines="+machines); - SshMachineLocation machine1 = (SshMachineLocation) Iterables.find(machines, LocationPredicates.configEqualTo(ConfigKeys.newStringConfigKey("mykey"), "myval1")); - WinRmMachineLocation machine2 = (WinRmMachineLocation) Iterables.find(machines, Predicates.instanceOf(WinRmMachineLocation.class)); - - assertMachine(machine1, UserAndHostAndPort.fromParts("myuser", "83.222.229.1", 12001), ImmutableMap.of( - SshMachineLocation.PASSWORD.getName(), "mypassword", - "mykey", "myval1")); - assertEquals(machine1.getPrivateAddresses(), ImmutableSet.of("10.0.0.1")); - assertEquals(pfm.lookup(machine1, 22), HostAndPort.fromParts("83.222.229.1", 12001)); - assertEquals(pfm.lookup(machine1, 8080), HostAndPort.fromParts("83.222.229.1", 12002)); - assertNull(pfm.lookup(machine1, 12345)); - - assertMachine(machine2, UserAndHostAndPort.fromParts("myuser", "83.222.229.2", 12003), ImmutableMap.of( - SshMachineLocation.PASSWORD.getName(), "mypassword", - "mykey", "myval2")); - assertEquals(machine2.getPrivateAddresses(), ImmutableSet.of("10.0.0.2")); - assertEquals(pfm.lookup(machine2, 8985), HostAndPort.fromParts("83.222.229.2", 12003)); - assertEquals(pfm.lookup(machine2, 8080), HostAndPort.fromParts("83.222.229.2", 12004)); - assertNull(pfm.lookup(machine2, 12345)); - } - - @Test - @SuppressWarnings("unchecked") - public void testPassesInboundPortsToMachineAndRemovesOnceMachineReleased() throws Exception { - String yaml = Joiner.on("\n").join( - "location:", - " byon:", - " hosts:", - " - ssh: 1.1.1.1:22", - " password: mypassword", - " user: myuser", - "services:", - "- type: brooklyn.entity.basic.DoNothingSoftwareProcess", - " brooklyn.config:", - " requiredOpenLoginPorts: [22, 1024]"); - - Entity app = createStartWaitAndLogApplication(new StringReader(yaml)); - DoNothingSoftwareProcess entity = (DoNothingSoftwareProcess) Iterables.find(Entities.descendants(app), Predicates.instanceOf(DoNothingSoftwareProcess.class)); - FixedListMachineProvisioningLocation<MachineLocation> loc = (FixedListMachineProvisioningLocation<MachineLocation>) Iterables.get(app.getLocations(), 0); - - // Machine should have been given the inbound-ports - SshMachineLocation machine = Machines.findUniqueSshMachineLocation(entity.getLocations()).get(); - Asserts.assertEqualsIgnoringOrder((Iterable<?>)machine.config().get(CloudLocationConfig.INBOUND_PORTS), ImmutableList.of(22, 1024)); - - // Stop the entity; should release the machine - entity.stop(); - MachineLocation availableMachine = Iterables.getOnlyElement(loc.getAvailable()); - assertEquals(availableMachine, machine); - assertNull(machine.config().get(CloudLocationConfig.INBOUND_PORTS)); - } - - private void assertMachine(SshMachineLocation machine, UserAndHostAndPort conn, Map<String, ?> config) { - assertEquals(machine.getAddress().getHostAddress(), conn.getHostAndPort().getHostText()); - assertEquals(machine.getPort(), conn.getHostAndPort().getPort()); - assertEquals(machine.getUser(), conn.getUser()); - for (Map.Entry<String, ?> entry : config.entrySet()) { - Object actualVal = machine.getConfig(ConfigKeys.newConfigKey(Object.class, entry.getKey())); - assertEquals(actualVal, entry.getValue()); - } - } - - private void assertMachine(WinRmMachineLocation machine, UserAndHostAndPort conn, Map<String, ?> config) { - assertEquals(machine.getAddress().getHostAddress(), conn.getHostAndPort().getHostText()); - assertEquals(machine.getConfig(WinRmMachineLocation.WINRM_PORT), (Integer) conn.getHostAndPort().getPort()); - assertEquals(machine.getUser(), conn.getUser()); - for (Map.Entry<String, ?> entry : config.entrySet()) { - Object actualVal = machine.getConfig(ConfigKeys.newConfigKey(Object.class, entry.getKey())); - assertEquals(actualVal, entry.getValue()); - } - } - - @Override - protected Logger getLogger() { - return log; - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java deleted file mode 100644 index c90104f..0000000 --- a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java +++ /dev/null @@ -1,245 +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 io.brooklyn.camp.brooklyn; - -import java.io.File; -import java.util.Set; -import java.util.concurrent.Callable; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.Test; - -import brooklyn.config.ConfigKey; -import brooklyn.entity.Application; -import brooklyn.entity.Entity; -import brooklyn.entity.basic.Attributes; -import brooklyn.entity.basic.ConfigKeys; -import brooklyn.entity.basic.Entities; -import brooklyn.entity.basic.EntityInternal; -import brooklyn.entity.rebind.RebindTestUtils; -import brooklyn.event.Sensor; -import brooklyn.event.basic.Sensors; -import brooklyn.management.ManagementContext; -import brooklyn.management.internal.LocalManagementContext; -import brooklyn.test.entity.TestEntity; -import brooklyn.util.collections.MutableSet; -import brooklyn.util.task.Tasks; - -import com.google.common.collect.Iterables; -import com.google.common.io.Files; - -@Test -public class DslAndRebindYamlTest extends AbstractYamlTest { - - private static final Logger log = LoggerFactory.getLogger(DslAndRebindYamlTest.class); - - protected ClassLoader classLoader = getClass().getClassLoader(); - protected File mementoDir; - protected Set<ManagementContext> mgmtContexts = MutableSet.of(); - - @Override - protected LocalManagementContext newTestManagementContext() { - if (mementoDir!=null) throw new IllegalStateException("already created mgmt context"); - mementoDir = Files.createTempDir(); - LocalManagementContext mgmt = RebindTestUtils.newPersistingManagementContext(mementoDir, classLoader, 1); - mgmtContexts.add(mgmt); - return mgmt; - } - - @AfterMethod(alwaysRun = true) - @Override - public void tearDown() { - for (ManagementContext mgmt: mgmtContexts) Entities.destroyAll(mgmt); - super.tearDown(); - mementoDir = null; - mgmtContexts.clear(); - } - - @Override - protected Logger getLogger() { - return log; - } - - public Application rebind(Application app) throws Exception { - RebindTestUtils.waitForPersisted(app); - // not strictly needed, but for good measure: - RebindTestUtils.checkCurrentMementoSerializable(app); - Application result = RebindTestUtils.rebind(mementoDir, getClass().getClassLoader()); - mgmtContexts.add(result.getManagementContext()); - return result; - } - - - protected Entity setupAndCheckTestEntityInBasicYamlWith(String ...extras) throws Exception { - Entity app = createAndStartApplication(loadYaml("test-entity-basic-template.yaml", extras)); - waitForApplicationTasks(app); - - Assert.assertEquals(app.getDisplayName(), "test-entity-basic-template"); - - log.info("App started:"); - Entities.dumpInfo(app); - - Assert.assertTrue(app.getChildren().iterator().hasNext(), "Expected app to have child entity"); - Entity entity = app.getChildren().iterator().next(); - Assert.assertTrue(entity instanceof TestEntity, "Expected TestEntity, found " + entity.getClass()); - - return (TestEntity)entity; - } - - public static <T> T getConfigInTask(final Entity entity, final ConfigKey<T> key) { - return Entities.submit(entity, Tasks.<T>builder().body(new Callable<T>() { - @Override - public T call() throws Exception { - return entity.getConfig(key); - } - }).build()).getUnchecked(); - } - - @Test - public void testDslAttributeWhenReady() throws Exception { - Entity testEntity = entityWithAttributeWhenReady(); - ((EntityInternal)testEntity).setAttribute(Sensors.newStringSensor("foo"), "bar"); - Assert.assertEquals(getConfigInTask(testEntity, TestEntity.CONF_NAME), "bar"); - } - - @Test - public void testDslAttributeWhenReadyRebind() throws Exception { - Entity testEntity = entityWithAttributeWhenReady(); - ((EntityInternal)testEntity).setAttribute(Sensors.newStringSensor("foo"), "bar"); - Application app2 = rebind(testEntity.getApplication()); - Entity e2 = Iterables.getOnlyElement( app2.getChildren() ); - - Assert.assertEquals(getConfigInTask(e2, TestEntity.CONF_NAME), "bar"); - } - - private Entity entityWithAttributeWhenReady() throws Exception { - return setupAndCheckTestEntityInBasicYamlWith( - " id: x", - " brooklyn.config:", - " test.confName: $brooklyn:component(\"x\").attributeWhenReady(\"foo\")"); - } - - private void doTestOnEntityWithSensor(Entity testEntity, Sensor<?> expectedSensor) throws Exception { - doTestOnEntityWithSensor(testEntity, expectedSensor, true); - } - private void doTestOnEntityWithSensor(Entity testEntity, Sensor<?> expectedSensor, boolean inTask) throws Exception { - @SuppressWarnings("rawtypes") - ConfigKey<Sensor> configKey = ConfigKeys.newConfigKey(Sensor.class, "test.sensor"); - Sensor<?> s; - s = inTask ? getConfigInTask(testEntity, configKey) : testEntity.getConfig(configKey); - Assert.assertEquals(s, expectedSensor); - Application app2 = rebind(testEntity.getApplication()); - Entity te2 = Iterables.getOnlyElement( app2.getChildren() ); - s = inTask ? getConfigInTask(te2, configKey) : te2.getConfig(configKey); - Assert.assertEquals(s, expectedSensor); - } - - @Test - public void testDslSensorFromClass() throws Exception { - doTestOnEntityWithSensor(entityWithSensorFromClass(), Attributes.SERVICE_UP); - // without context it can still find it - doTestOnEntityWithSensor(entityWithSensorFromClass(), Attributes.SERVICE_UP, false); - } - @Test - public void testDslSensorLocal() throws Exception { - doTestOnEntityWithSensor(entityWithSensorLocal(), TestEntity.SEQUENCE); - // here without context it makes one up, so type info (and description etc) not present; - // but context is needed to submit the DslDeferredSupplier object, so this would fail -// doTestOnEntityWithSensor(entityWithSensorAdHoc(), Sensors.newSensor(Object.class, TestEntity.SEQUENCE.getName()), false); - } - @Test - public void testDslSensorAdHoc() throws Exception { - doTestOnEntityWithSensor(entityWithSensorAdHoc(), Sensors.newSensor(Object.class, "sensor.foo")); - // here context has no impact, but it is needed to submit the DslDeferredSupplier object so this would fail -// doTestOnEntityWithSensor(entityWithSensorAdHoc(), Sensors.newSensor(Object.class, "sensor.foo"), false); - } - - private Entity entityWithSensorFromClass() throws Exception { - return setupAndCheckTestEntityInBasicYamlWith( - " id: x", - " brooklyn.config:", - " test.sensor: $brooklyn:sensor(\""+Attributes.class.getName()+"\", \""+Attributes.SERVICE_UP.getName()+"\")"); - } - - private Entity entityWithSensorLocal() throws Exception { - return setupAndCheckTestEntityInBasicYamlWith( - " id: x", - " brooklyn.config:", - " test.sensor: $brooklyn:sensor(\""+TestEntity.SEQUENCE.getName()+"\")"); - } - - private Entity entityWithSensorAdHoc() throws Exception { - return setupAndCheckTestEntityInBasicYamlWith( - " id: x", - " brooklyn.config:", - " test.sensor: $brooklyn:sensor(\"sensor.foo\")"); - } - - - @Test - public void testDslConfigFromRoot() throws Exception { - Entity testEntity = entityWithConfigFromRoot(); - Assert.assertEquals(getConfigInTask(testEntity, TestEntity.CONF_NAME), "bar"); - } - - @Test - public void testDslConfigFromRootRebind() throws Exception { - Entity testEntity = entityWithConfigFromRoot(); - Application app2 = rebind(testEntity.getApplication()); - Entity e2 = Iterables.getOnlyElement( app2.getChildren() ); - - Assert.assertEquals(getConfigInTask(e2, TestEntity.CONF_NAME), "bar"); - } - - private Entity entityWithConfigFromRoot() throws Exception { - return setupAndCheckTestEntityInBasicYamlWith( - " id: x", - " brooklyn.config:", - " test.confName: $brooklyn:component(\"x\").config(\"foo\")", - "brooklyn.config:", - " foo: bar"); - } - - - @Test - public void testDslFormatString() throws Exception { - Entity testEntity = entityWithFormatString(); - Assert.assertEquals(getConfigInTask(testEntity, TestEntity.CONF_NAME), "hello world"); - } - - @Test - public void testDslFormatStringRebind() throws Exception { - Entity testEntity = entityWithFormatString(); - Application app2 = rebind(testEntity.getApplication()); - Entity e2 = Iterables.getOnlyElement( app2.getChildren() ); - - Assert.assertEquals(getConfigInTask(e2, TestEntity.CONF_NAME), "hello world"); - } - - private Entity entityWithFormatString() throws Exception { - return setupAndCheckTestEntityInBasicYamlWith( - " id: x", - " brooklyn.config:", - " test.confName: $brooklyn:formatString(\"hello %s\", \"world\")"); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EmptySoftwareProcessYamlTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EmptySoftwareProcessYamlTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EmptySoftwareProcessYamlTest.java deleted file mode 100644 index 9b7bc7b..0000000 --- a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EmptySoftwareProcessYamlTest.java +++ /dev/null @@ -1,103 +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 io.brooklyn.camp.brooklyn; - -import java.util.Iterator; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.Test; - -import brooklyn.entity.Entity; -import brooklyn.entity.basic.EmptySoftwareProcess; -import brooklyn.entity.basic.Entities; -import brooklyn.location.Location; -import brooklyn.location.basic.SshMachineLocation; -import brooklyn.util.collections.Jsonya; - -@Test -public class EmptySoftwareProcessYamlTest extends AbstractYamlTest { - private static final Logger log = LoggerFactory.getLogger(EnrichersYamlTest.class); - - @Test(groups="Integration") - public void testProvisioningProperties() throws Exception { - Entity app = createAndStartApplication( - "location: localhost", - "services:", - "- type: "+EmptySoftwareProcess.class.getName(), - " provisioning.properties:", - " minRam: 16384"); - waitForApplicationTasks(app); - - log.info("App started:"); - Entities.dumpInfo(app); - - EmptySoftwareProcess entity = (EmptySoftwareProcess) app.getChildren().iterator().next(); - Map<String, Object> pp = entity.getConfig(EmptySoftwareProcess.PROVISIONING_PROPERTIES); - Assert.assertEquals(pp.get("minRam"), 16384); - } - - @Test(groups="Integration") - public void testProvisioningPropertiesViaJsonya() throws Exception { - Entity app = createAndStartApplication( - Jsonya.newInstance() - .put("location", "localhost") - .at("services").list() - .put("type", EmptySoftwareProcess.class.getName()) - .at("provisioning.properties").put("minRam", 16384) - .root().toString()); - waitForApplicationTasks(app); - - log.info("App started:"); - Entities.dumpInfo(app); - - EmptySoftwareProcess entity = (EmptySoftwareProcess) app.getChildren().iterator().next(); - Map<String, Object> pp = entity.getConfig(EmptySoftwareProcess.PROVISIONING_PROPERTIES); - Assert.assertEquals(pp.get("minRam"), 16384); - } - - // for https://github.com/brooklyncentral/brooklyn/issues/1377 - @Test(groups="Integration") - public void testWithAppAndEntityLocations() throws Exception { - Entity app = createAndStartApplication( - "services:", - "- type: "+EmptySoftwareProcess.class.getName(), - " location: localhost:(name=localhost on entity)", - "location: byon:(hosts=\"127.0.0.1\", name=loopback on app)"); - waitForApplicationTasks(app); - Entities.dumpInfo(app); - - Assert.assertEquals(app.getLocations().size(), 1); - Assert.assertEquals(app.getChildren().size(), 1); - Entity entity = app.getChildren().iterator().next(); - - Location appLocation = app.getLocations().iterator().next(); - Assert.assertEquals(appLocation.getDisplayName(), "loopback on app"); - - Assert.assertEquals(entity.getLocations().size(), 2); - Iterator<Location> entityLocationIterator = entity.getLocations().iterator(); - Assert.assertEquals(entityLocationIterator.next().getDisplayName(), "localhost on entity"); - Location actualMachine = entityLocationIterator.next(); - Assert.assertTrue(actualMachine instanceof SshMachineLocation, "wrong location: "+actualMachine); - // TODO this, below, probably should be 'localhost on entity', see #1377 - Assert.assertEquals(actualMachine.getParent().getDisplayName(), "loopback on app"); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EnrichersSlightlySimplerYamlTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EnrichersSlightlySimplerYamlTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EnrichersSlightlySimplerYamlTest.java deleted file mode 100644 index 9001824..0000000 --- a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EnrichersSlightlySimplerYamlTest.java +++ /dev/null @@ -1,134 +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 io.brooklyn.camp.brooklyn; - -import java.net.URI; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.Test; - -import brooklyn.entity.Entity; -import brooklyn.entity.basic.Attributes; -import brooklyn.entity.basic.Entities; -import brooklyn.entity.basic.EntityInternal; -import brooklyn.entity.group.DynamicCluster; -import brooklyn.entity.webapp.JavaWebAppSoftwareProcess; -import brooklyn.event.basic.Sensors; -import brooklyn.test.EntityTestUtils; -import brooklyn.util.collections.CollectionFunctionals; -import brooklyn.util.collections.MutableList; -import brooklyn.util.math.MathPredicates; -import brooklyn.util.text.StringPredicates; - -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; -import com.google.common.collect.Iterables; - -/** Tests some improvements to enricher classes to make them a bit more yaml friendly. - * Called "SlightlySimpler" as it would be nice to make enrichers a lot more yaml friendly! */ -@Test -public class EnrichersSlightlySimplerYamlTest extends AbstractYamlTest { - private static final Logger log = LoggerFactory.getLogger(EnrichersSlightlySimplerYamlTest.class); - - @SuppressWarnings({ "unchecked", "rawtypes" }) - @Test - public void testWithAppEnricher() throws Exception { - Entity app = createAndStartApplication(loadYaml("test-app-with-enrichers-slightly-simpler.yaml")); - waitForApplicationTasks(app); - log.info("Started "+app+":"); - Entities.dumpInfo(app); - - Entity cluster = Iterables.getOnlyElement( app.getChildren() ); - Collection<Entity> leafs = ((DynamicCluster)cluster).getMembers(); - Iterator<Entity> li = leafs.iterator(); - - Entity e1 = li.next(); - ((EntityInternal)e1).setAttribute(Sensors.newStringSensor("ip"), "127.0.0.1"); - EntityTestUtils.assertAttributeEqualsEventually(e1, Sensors.newStringSensor("url"), "http://127.0.0.1/"); - EntityTestUtils.assertAttributeEqualsEventually(e1, Attributes.MAIN_URI, URI.create("http://127.0.0.1/")); - - int i=2; - while (li.hasNext()) { - Entity ei = li.next(); - ((EntityInternal)ei).setAttribute(Sensors.newStringSensor("ip"), "127.0.0."+i); - i++; - } - - EntityTestUtils.assertAttributeEventually(cluster, Sensors.newSensor(Iterable.class, "urls.list"), - (Predicate)CollectionFunctionals.sizeEquals(3)); - - EntityTestUtils.assertAttributeEventually(cluster, Sensors.newSensor(String.class, "urls.list.comma_separated.max_2"), - StringPredicates.matchesRegex("\"http:\\/\\/127[^\"]*\\/\",\"http:\\/\\/127[^\"]*\\/\"")); - - EntityTestUtils.assertAttributeEventually(cluster, Attributes.MAIN_URI, Predicates.notNull()); - URI main = cluster.getAttribute(Attributes.MAIN_URI); - Assert.assertTrue(main.toString().matches("http:\\/\\/127.0.0..\\/"), "Wrong URI: "+main); - - EntityTestUtils.assertAttributeEventually(app, Attributes.MAIN_URI, Predicates.notNull()); - main = app.getAttribute(Attributes.MAIN_URI); - Assert.assertTrue(main.toString().matches("http:\\/\\/127.0.0..\\/"), "Wrong URI: "+main); - - // TODO would we want to allow "all-but-usual" as the default if nothing specified - } - - @Test(groups="Integration") - public void testWebappWithAveragingEnricher() throws Exception { - Entity app = createAndStartApplication(loadYaml("test-webapp-with-averaging-enricher.yaml")); - waitForApplicationTasks(app); - log.info("Started "+app+":"); - Entities.dumpInfo(app); - - List<JavaWebAppSoftwareProcess> appservers = MutableList.copyOf(Entities.descendants(app, JavaWebAppSoftwareProcess.class)); - Assert.assertEquals(appservers.size(), 3); - - EntityInternal srv0 = (EntityInternal) appservers.get(0); - EntityInternal dwac = (EntityInternal) srv0.getParent(); - EntityInternal cdwac = (EntityInternal) dwac.getParent(); - - srv0.setAttribute(Sensors.newDoubleSensor("my.load"), 20.0); - - EntityTestUtils.assertAttributeEventually(dwac, Sensors.newSensor(Double.class, "my.load.averaged"), - MathPredicates.equalsApproximately(20)); - EntityTestUtils.assertAttributeEventually(cdwac, Sensors.newSensor(Double.class, "my.load.averaged"), - MathPredicates.equalsApproximately(20)); - - srv0.setAttribute(Sensors.newDoubleSensor("my.load"), null); - EntityTestUtils.assertAttributeEventually(cdwac, Sensors.newSensor(Double.class, "my.load.averaged"), - Predicates.isNull()); - - ((EntityInternal) appservers.get(1)).setAttribute(Sensors.newDoubleSensor("my.load"), 10.0); - ((EntityInternal) appservers.get(2)).setAttribute(Sensors.newDoubleSensor("my.load"), 20.0); - EntityTestUtils.assertAttributeEventually(cdwac, Sensors.newSensor(Double.class, "my.load.averaged"), - MathPredicates.equalsApproximately(15)); - srv0.setAttribute(Sensors.newDoubleSensor("my.load"), 0.0); - EntityTestUtils.assertAttributeEventually(cdwac, Sensors.newSensor(Double.class, "my.load.averaged"), - MathPredicates.equalsApproximately(10)); - } - - @Override - protected Logger getLogger() { - return log; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EnrichersYamlTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EnrichersYamlTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EnrichersYamlTest.java deleted file mode 100644 index 4391ba9..0000000 --- a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EnrichersYamlTest.java +++ /dev/null @@ -1,257 +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 io.brooklyn.camp.brooklyn; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.Test; - -import brooklyn.config.ConfigKey; -import brooklyn.enricher.basic.Propagator; -import brooklyn.entity.Entity; -import brooklyn.entity.basic.Entities; -import brooklyn.entity.basic.EntityAdjuncts; -import brooklyn.entity.basic.EntityInternal; -import brooklyn.policy.Enricher; -import brooklyn.test.Asserts; -import brooklyn.test.entity.TestEntity; -import brooklyn.test.policy.TestEnricher; -import brooklyn.util.collections.MutableMap; - -import com.google.common.base.Predicates; -import com.google.common.base.Supplier; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; - -@Test -public class EnrichersYamlTest extends AbstractYamlTest { - private static final Logger log = LoggerFactory.getLogger(EnrichersYamlTest.class); - - @Test - public void testWithAppEnricher() throws Exception { - Entity app = createAndStartApplication(loadYaml("test-app-with-enricher.yaml")); - waitForApplicationTasks(app); - Assert.assertEquals(app.getDisplayName(), "test-app-with-enricher"); - - log.info("App started:"); - Entities.dumpInfo(app); - - Assert.assertEquals(EntityAdjuncts.getNonSystemEnrichers(app).size(), 1); - final Enricher enricher = EntityAdjuncts.getNonSystemEnrichers(app).iterator().next(); - Assert.assertTrue(enricher instanceof TestEnricher, "enricher="+enricher); - Assert.assertEquals(enricher.getConfig(TestEnricher.CONF_NAME), "Name from YAML"); - Assert.assertEquals(enricher.getConfig(TestEnricher.CONF_FROM_FUNCTION), "$brooklyn: is a fun place"); - - Entity target = ((EntityInternal)app).getExecutionContext().submit(MutableMap.of(), new Callable<Entity>() { - public Entity call() { - return enricher.getConfig(TestEnricher.TARGET_ENTITY); - }}).get(); - Assert.assertNotNull(target); - Assert.assertEquals(target.getDisplayName(), "testentity"); - Assert.assertEquals(target, app.getChildren().iterator().next()); - Entity targetFromFlag = ((EntityInternal)app).getExecutionContext().submit(MutableMap.of(), new Callable<Entity>() { - public Entity call() { - return enricher.getConfig(TestEnricher.TARGET_ENTITY_FROM_FLAG); - }}).get(); - Assert.assertEquals(targetFromFlag, target); - Map<?, ?> leftoverProperties = ((TestEnricher) enricher).getLeftoverProperties(); - Assert.assertEquals(leftoverProperties.get("enricherLiteralValue1"), "Hello"); - Assert.assertEquals(leftoverProperties.get("enricherLiteralValue2"), "World"); - Assert.assertEquals(leftoverProperties.size(), 2); - } - - @Test - public void testWithEntityEnricher() throws Exception { - final Entity app = createAndStartApplication(loadYaml("test-entity-with-enricher.yaml")); - waitForApplicationTasks(app); - Assert.assertEquals(app.getDisplayName(), "test-entity-with-enricher"); - - log.info("App started:"); - Entities.dumpInfo(app); - - Assert.assertEquals(EntityAdjuncts.getNonSystemEnrichers(app).size(), 0); - Assert.assertEquals(app.getChildren().size(), 1); - final Entity child = app.getChildren().iterator().next(); - Asserts.eventually(new Supplier<Integer>() { - @Override - public Integer get() { - return EntityAdjuncts.getNonSystemEnrichers(child).size(); - } - }, Predicates.<Integer> equalTo(1)); - final Enricher enricher = EntityAdjuncts.getNonSystemEnrichers(child).iterator().next(); - Assert.assertNotNull(enricher); - Assert.assertTrue(enricher instanceof TestEnricher, "enricher=" + enricher + "; type=" + enricher.getClass()); - Assert.assertEquals(enricher.getConfig(TestEnricher.CONF_NAME), "Name from YAML"); - Assert.assertEquals(enricher.getConfig(TestEnricher.CONF_FROM_FUNCTION), "$brooklyn: is a fun place"); - - Assert.assertEquals(((TestEnricher) enricher).getLeftoverProperties(), - ImmutableMap.of("enricherLiteralValue1", "Hello", "enricherLiteralValue2", "World")); - } - - @Test - public void testPropagatingEnricher() throws Exception { - Entity app = createAndStartApplication(loadYaml("test-propagating-enricher.yaml")); - waitForApplicationTasks(app); - Assert.assertEquals(app.getDisplayName(), "test-propagating-enricher"); - - log.info("App started:"); - Entities.dumpInfo(app); - TestEntity entity = (TestEntity)app.getChildren().iterator().next(); - entity.setAttribute(TestEntity.NAME, "New Name"); - Asserts.eventually(Entities.attributeSupplier(app, TestEntity.NAME), Predicates.<String>equalTo("New Name")); - } - - @Test - public void testPropogateChildSensor() throws Exception { - Entity app = createAndStartApplication(loadYaml("test-entity-basic-template.yaml", - " brooklyn.config:", - " test.confName: parent entity", - " id: parentId", - " brooklyn.enrichers:", - " - enricherType: brooklyn.enricher.basic.Propagator", - " brooklyn.config:", - " enricher.producer: $brooklyn:component(\"childId\")", - " enricher.propagating.propagatingAll: true", - " brooklyn.children:", - " - serviceType: brooklyn.test.entity.TestEntity", - " id: childId", - " brooklyn.config:", - " test.confName: Child Name")); - waitForApplicationTasks(app); - - log.info("App started:"); - Entities.dumpInfo(app); - Assert.assertEquals(app.getChildren().size(), 1); - final Entity parentEntity = app.getChildren().iterator().next(); - Assert.assertTrue(parentEntity instanceof TestEntity, "Expected parent entity to be TestEntity, found:" + parentEntity); - Assert.assertEquals(parentEntity.getChildren().size(), 1); - Entity childEntity = parentEntity.getChildren().iterator().next(); - Assert.assertTrue(childEntity instanceof TestEntity, "Expected child entity to be TestEntity, found:" + childEntity); - Asserts.eventually(new Supplier<Integer>() { - @Override - public Integer get() { - return EntityAdjuncts.getNonSystemEnrichers(parentEntity).size(); - } - }, Predicates.<Integer>equalTo(1)); - Enricher enricher = EntityAdjuncts.getNonSystemEnrichers(parentEntity).iterator().next(); - Asserts.assertTrue(enricher instanceof Propagator, "Expected enricher to be Propagator, found:" + enricher); - final Propagator propagator = (Propagator)enricher; - Entity producer = ((EntityInternal)parentEntity).getExecutionContext().submit(MutableMap.of(), new Callable<Entity>() { - public Entity call() { - return propagator.getConfig(Propagator.PRODUCER); - }}).get(); - Assert.assertEquals(producer, childEntity); - Asserts.assertTrue(Boolean.valueOf(propagator.getConfig(Propagator.PROPAGATING_ALL)), "Expected Propagator.PROPAGATING_ALL to be true"); - ((TestEntity)childEntity).setAttribute(TestEntity.NAME, "New Name"); - Asserts.eventually(Entities.attributeSupplier(parentEntity, TestEntity.NAME), Predicates.<String>equalTo("New Name")); - } - - @Test - public void testMultipleEnricherReferences() throws Exception { - final Entity app = createAndStartApplication(loadYaml("test-referencing-enrichers.yaml")); - waitForApplicationTasks(app); - - Entity entity1 = null, entity2 = null, child1 = null, child2 = null, grandchild1 = null, grandchild2 = null; - - Assert.assertEquals(app.getChildren().size(), 2); - for (Entity child : app.getChildren()) { - if (child.getDisplayName().equals("entity 1")) - entity1 = child; - if (child.getDisplayName().equals("entity 2")) - entity2 = child; - } - Assert.assertNotNull(entity1); - Assert.assertNotNull(entity2); - - Assert.assertEquals(entity1.getChildren().size(), 2); - for (Entity child : entity1.getChildren()) { - if (child.getDisplayName().equals("child 1")) - child1 = child; - if (child.getDisplayName().equals("child 2")) - child2 = child; - } - Assert.assertNotNull(child1); - Assert.assertNotNull(child2); - - Assert.assertEquals(child1.getChildren().size(), 2); - for (Entity child : child1.getChildren()) { - if (child.getDisplayName().equals("grandchild 1")) - grandchild1 = child; - if (child.getDisplayName().equals("grandchild 2")) - grandchild2 = child; - } - Assert.assertNotNull(grandchild1); - Assert.assertNotNull(grandchild2); - - ImmutableSet<Enricher> enrichers = new ImmutableSet.Builder<Enricher>() - .add(getEnricher(app)) - .add(getEnricher(entity1)) - .add(getEnricher(entity2)) - .add(getEnricher(child1)) - .add(getEnricher(child2)) - .add(getEnricher(grandchild1)) - .add(getEnricher(grandchild2)) - .build(); - - Map<ConfigKey<Entity>, Entity> keyToEntity = new ImmutableMap.Builder<ConfigKey<Entity>, Entity>() - .put(TestReferencingEnricher.TEST_APPLICATION, app) - .put(TestReferencingEnricher.TEST_ENTITY_1, entity1) - .put(TestReferencingEnricher.TEST_ENTITY_2, entity2) - .put(TestReferencingEnricher.TEST_CHILD_1, child1) - .put(TestReferencingEnricher.TEST_CHILD_2, child2) - .put(TestReferencingEnricher.TEST_GRANDCHILD_1, grandchild1) - .put(TestReferencingEnricher.TEST_GRANDCHILD_2, grandchild2) - .build(); - - for (Enricher enricher : enrichers) - checkReferences(enricher, keyToEntity); - } - - private void checkReferences(final Enricher enricher, Map<ConfigKey<Entity>, Entity> keyToEntity) throws Exception { - for (final ConfigKey<Entity> key : keyToEntity.keySet()) { - final Entity entity = keyToEntity.get(key); // Grab an entity whose execution context we can use - Entity fromConfig = ((EntityInternal)entity).getExecutionContext().submit(MutableMap.of(), new Callable<Entity>() { - @Override - public Entity call() throws Exception { - return (Entity) enricher.getConfig(key); - } - }).get(); - Assert.assertEquals(fromConfig, keyToEntity.get(key)); - } - } - - private Enricher getEnricher(Entity entity) { - List<Enricher> enrichers = EntityAdjuncts.getNonSystemEnrichers(entity); - Assert.assertEquals(enrichers.size(), 1, "Wrong number of enrichers: "+enrichers); - Enricher enricher = enrichers.iterator().next(); - Assert.assertTrue(enricher instanceof TestReferencingEnricher, "Wrong enricher: "+enricher); - return enricher; - } - - @Override - protected Logger getLogger() { - return log; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EntitiesYamlIntegrationTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EntitiesYamlIntegrationTest.java b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EntitiesYamlIntegrationTest.java deleted file mode 100644 index f06d862..0000000 --- a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/EntitiesYamlIntegrationTest.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package io.brooklyn.camp.brooklyn; - -import static org.testng.Assert.*; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.Test; - -import com.google.common.collect.FluentIterable; -import com.google.common.collect.Iterables; - -import brooklyn.entity.Entity; -import brooklyn.entity.group.DynamicCluster; -import brooklyn.entity.proxy.nginx.NginxController; -import brooklyn.entity.proxying.EntitySpec; -import brooklyn.entity.webapp.ControlledDynamicWebAppCluster; -import brooklyn.entity.webapp.tomcat.TomcatServer; - -public class EntitiesYamlIntegrationTest extends AbstractYamlTest { - - private static final Logger LOG = LoggerFactory.getLogger(EntitiesYamlIntegrationTest.class); - - @Test(groups = "Integration") - public void testStartTomcatCluster() throws Exception { - Entity app = createAndStartApplication(loadYaml("test-tomcat-cluster.yaml")); - waitForApplicationTasks(app); - - assertNotNull(app); - assertEquals(app.getChildren().size(), 1); - final Entity entity = Iterables.getOnlyElement(app.getChildren()); - assertTrue(entity instanceof ControlledDynamicWebAppCluster, "entity="+entity); - ControlledDynamicWebAppCluster cluster = (ControlledDynamicWebAppCluster) entity; - - assertTrue(cluster.getController() instanceof NginxController, "controller="+cluster.getController()); - Iterable<TomcatServer> tomcats = FluentIterable.from(cluster.getCluster().getMembers()).filter(TomcatServer.class); - assertEquals(Iterables.size(tomcats), 2); - for (TomcatServer tomcat : tomcats) { - assertTrue(tomcat.getAttribute(TomcatServer.SERVICE_UP), "serviceup"); - } - - EntitySpec<?> spec = entity.getConfig(DynamicCluster.MEMBER_SPEC); - assertNotNull(spec); - assertEquals(spec.getType(), TomcatServer.class); - assertEquals(spec.getConfig().get(DynamicCluster.QUARANTINE_FAILED_ENTITIES), Boolean.FALSE); - assertEquals(spec.getConfig().get(DynamicCluster.INITIAL_QUORUM_SIZE), 2); - } - - - @Override - protected Logger getLogger() { - return LOG; - } -}
