Updated following PR review, added integration tests
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/25ef6932 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/25ef6932 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/25ef6932 Branch: refs/heads/master Commit: 25ef69322f6a4e065d53e4064ef36c8fb75714d5 Parents: 6572c02 Author: Martin Harris <[email protected]> Authored: Fri Jun 20 13:54:53 2014 +0100 Committer: Martin Harris <[email protected]> Committed: Fri Jun 20 13:54:53 2014 +0100 ---------------------------------------------------------------------- .../elasticsearch/ElasticSearchClusterImpl.java | 3 +- .../ElasticSearchNodeSshDriver.java | 18 ++-- .../ElasticSearchClusterIntegrationTest.java | 102 +++++++++++++++++++ .../ElasticSearchNodeIntegrationTest.java | 94 +++++++++++++++++ 4 files changed, 206 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/25ef6932/software/nosql/src/main/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchClusterImpl.java ---------------------------------------------------------------------- diff --git a/software/nosql/src/main/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchClusterImpl.java b/software/nosql/src/main/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchClusterImpl.java index 32f8706..dd2a7b4 100644 --- a/software/nosql/src/main/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchClusterImpl.java +++ b/software/nosql/src/main/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchClusterImpl.java @@ -21,8 +21,7 @@ public class ElasticSearchClusterImpl extends DynamicClusterImpl implements Elas @Override protected EntitySpec<?> getMemberSpec() { - @SuppressWarnings("unchecked") - EntitySpec<ElasticSearchNode> spec = (EntitySpec<ElasticSearchNode>)getConfig(MEMBER_SPEC, EntitySpec.create(ElasticSearchNode.class)); + EntitySpec<?> spec = EntitySpec.create(getConfig(MEMBER_SPEC, EntitySpec.create(ElasticSearchNode.class))); spec.configure(ElasticSearchNode.CLUSTER_NAME, getConfig(ElasticSearchClusterImpl.CLUSTER_NAME)) .configure(ElasticSearchNode.NODE_NAME, "elasticsearch-" + nextMemberId.incrementAndGet()); http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/25ef6932/software/nosql/src/main/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchNodeSshDriver.java ---------------------------------------------------------------------- diff --git a/software/nosql/src/main/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchNodeSshDriver.java b/software/nosql/src/main/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchNodeSshDriver.java index 748975b..89e575e 100644 --- a/software/nosql/src/main/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchNodeSshDriver.java +++ b/software/nosql/src/main/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchNodeSshDriver.java @@ -67,12 +67,12 @@ public class ElasticSearchNodeSshDriver extends AbstractSoftwareProcessSshDriver if (entity.getConfig(ElasticSearchNode.TEMPLATE_CONFIGURATION_URL) != null) { commandBuilder.append(" -Des.config=" + Os.mergePaths(getRunDir(), getConfigFile())); } - appendConfigIfPresent(commandBuilder, ElasticSearchNode.DATA_DIR, "es.path.data", Os.mergePaths(getRunDir(), "data")); - appendConfigIfPresent(commandBuilder, ElasticSearchNode.LOG_DIR, "es.path.logs", Os.mergePaths(getRunDir(), "logs")); - appendConfigIfPresent(commandBuilder, ElasticSearchNode.NODE_NAME.getConfigKey(), "es.node.name"); - appendConfigIfPresent(commandBuilder, ElasticSearchNode.CLUSTER_NAME.getConfigKey(), "es.cluster.name"); - appendConfigIfPresent(commandBuilder, ElasticSearchNode.MULTICAST_ENABLED, "es.discovery.zen.ping.multicast.enabled"); - appendConfigIfPresent(commandBuilder, ElasticSearchNode.UNICAST_ENABLED, "es.discovery.zen.ping.unicast.enabled"); + appendConfigIfPresent(commandBuilder, "es.path.data", ElasticSearchNode.DATA_DIR, Os.mergePaths(getRunDir(), "data")); + appendConfigIfPresent(commandBuilder, "es.path.logs", ElasticSearchNode.LOG_DIR, Os.mergePaths(getRunDir(), "logs")); + appendConfigIfPresent(commandBuilder, "es.node.name", ElasticSearchNode.NODE_NAME.getConfigKey()); + appendConfigIfPresent(commandBuilder, "es.cluster.name", ElasticSearchNode.CLUSTER_NAME.getConfigKey()); + appendConfigIfPresent(commandBuilder, "es.discovery.zen.ping.multicast.enabled", ElasticSearchNode.MULTICAST_ENABLED); + appendConfigIfPresent(commandBuilder, "es.discovery.zen.ping.unicast.enabled", ElasticSearchNode.UNICAST_ENABLED); commandBuilder.append(" > out.log 2> err.log < /dev/null"); newScript(MutableMap.of("usePidFile", false), LAUNCHING) .updateTaskAndFailOnNonZeroResultCode() @@ -80,11 +80,11 @@ public class ElasticSearchNodeSshDriver extends AbstractSoftwareProcessSshDriver .execute(); } - private void appendConfigIfPresent(StringBuilder builder, ConfigKey<?> configKey, String parameter) { - appendConfigIfPresent(builder, configKey, parameter, null); + private void appendConfigIfPresent(StringBuilder builder, String parameter, ConfigKey<?> configKey) { + appendConfigIfPresent(builder, parameter, configKey, null); } - private void appendConfigIfPresent(StringBuilder builder, ConfigKey<?> configKey, String parameter, String defaultValue) { + private void appendConfigIfPresent(StringBuilder builder, String parameter, ConfigKey<?> configKey, String defaultValue) { String config = null; if (entity.getConfig(configKey) != null) { config = String.valueOf(entity.getConfig(configKey)); http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/25ef6932/software/nosql/src/test/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchClusterIntegrationTest.java ---------------------------------------------------------------------- diff --git a/software/nosql/src/test/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchClusterIntegrationTest.java b/software/nosql/src/test/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchClusterIntegrationTest.java new file mode 100644 index 0000000..4ec0260 --- /dev/null +++ b/software/nosql/src/test/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchClusterIntegrationTest.java @@ -0,0 +1,102 @@ +package brooklyn.entity.nosql.elasticsearch; + +import static org.testng.Assert.assertEquals; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.apache.http.client.methods.HttpGet; +import org.bouncycastle.util.Strings; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import brooklyn.entity.Entity; +import brooklyn.entity.basic.ApplicationBuilder; +import brooklyn.entity.basic.Attributes; +import brooklyn.entity.basic.Entities; +import brooklyn.entity.group.DynamicCluster; +import brooklyn.entity.proxying.EntitySpec; +import brooklyn.entity.trait.Startable; +import brooklyn.event.feed.http.HttpValueFunctions; +import brooklyn.location.Location; +import brooklyn.location.basic.LocalhostMachineProvisioningLocation; +import brooklyn.test.EntityTestUtils; +import brooklyn.test.entity.TestApplication; +import brooklyn.util.http.HttpTool; +import brooklyn.util.http.HttpToolResponse; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +public class ElasticSearchClusterIntegrationTest { + + protected TestApplication app; + protected Location testLocation; + protected ElasticSearchCluster elasticSearchCluster; + + @BeforeMethod(alwaysRun = true) + public void setup() throws Exception { + app = ApplicationBuilder.newManagedApp(TestApplication.class); + testLocation = new LocalhostMachineProvisioningLocation(); + } + + @AfterMethod(alwaysRun = true) + public void shutdown() { + Entities.destroyAll(app.getManagementContext()); + } + + @Test(groups = {"Integration"}) + public void testStartupAndShutdown() { + elasticSearchCluster = app.createAndManageChild(EntitySpec.create(ElasticSearchCluster.class) + .configure(DynamicCluster.INITIAL_SIZE, 3)); + app.start(ImmutableList.of(testLocation)); + + EntityTestUtils.assertAttributeEqualsEventually(elasticSearchCluster, Startable.SERVICE_UP, true); + + elasticSearchCluster.stop(); + + EntityTestUtils.assertAttributeEqualsEventually(elasticSearchCluster, Startable.SERVICE_UP, false); + } + + @Test(groups = {"Integration"}) + public void testPutAndGet() throws URISyntaxException { + elasticSearchCluster = app.createAndManageChild(EntitySpec.create(ElasticSearchCluster.class) + .configure(DynamicCluster.INITIAL_SIZE, 3)); + app.start(ImmutableList.of(testLocation)); + + EntityTestUtils.assertAttributeEqualsEventually(elasticSearchCluster, Startable.SERVICE_UP, true); + + assertEquals(elasticSearchCluster.getMembers().size(), 3); + + ElasticSearchNode anyNode = (ElasticSearchNode)elasticSearchCluster.getMembers().iterator().next(); + + String document = "{\"foo\" : \"bar\",\"baz\" : \"quux\"}"; + + String putBaseUri = "http://" + anyNode.getAttribute(Attributes.HOSTNAME) + ":" + anyNode.getAttribute(Attributes.HTTP_PORT); + + HttpToolResponse putResponse = HttpTool.httpPut( + HttpTool.httpClientBuilder() + .port(anyNode.getAttribute(Attributes.HTTP_PORT)) + .build(), + new URI(putBaseUri + "/mydocuments/docs/1"), + ImmutableMap.<String, String>of(), + Strings.toByteArray(document)); + assertEquals(putResponse.getResponseCode(), 201); + EntityTestUtils.assertAttributeEqualsEventually(anyNode, ElasticSearchNode.DOCUMENT_COUNT, 1); + + int totalDocumentCount = 0; + for (Entity entity : elasticSearchCluster.getMembers()) { + ElasticSearchNode node = (ElasticSearchNode)entity; + String getBaseUri = "http://" + node.getAttribute(Attributes.HOSTNAME) + ":" + node.getAttribute(Attributes.HTTP_PORT); + HttpToolResponse getResponse = HttpTool.execAndConsume( + HttpTool.httpClientBuilder().build(), + new HttpGet(getBaseUri + "/mydocuments/docs/1/_source")); + assertEquals(getResponse.getResponseCode(), 200); + assertEquals(HttpValueFunctions.jsonContents("foo", String.class).apply(getResponse), "bar"); + + totalDocumentCount += node.getAttribute(ElasticSearchNode.DOCUMENT_COUNT); + } + assertEquals(totalDocumentCount, 1); + } +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/25ef6932/software/nosql/src/test/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchNodeIntegrationTest.java ---------------------------------------------------------------------- diff --git a/software/nosql/src/test/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchNodeIntegrationTest.java b/software/nosql/src/test/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchNodeIntegrationTest.java new file mode 100644 index 0000000..25b9522 --- /dev/null +++ b/software/nosql/src/test/java/brooklyn/entity/nosql/elasticsearch/ElasticSearchNodeIntegrationTest.java @@ -0,0 +1,94 @@ +package brooklyn.entity.nosql.elasticsearch; + +import static org.testng.Assert.assertEquals; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.apache.http.client.methods.HttpGet; +import org.bouncycastle.util.Strings; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import brooklyn.entity.basic.ApplicationBuilder; +import brooklyn.entity.basic.Attributes; +import brooklyn.entity.basic.Entities; +import brooklyn.entity.proxying.EntitySpec; +import brooklyn.entity.trait.Startable; +import brooklyn.event.feed.http.HttpValueFunctions; +import brooklyn.location.Location; +import brooklyn.location.basic.LocalhostMachineProvisioningLocation; +import brooklyn.test.EntityTestUtils; +import brooklyn.test.entity.TestApplication; +import brooklyn.util.http.HttpTool; +import brooklyn.util.http.HttpToolResponse; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +public class ElasticSearchNodeIntegrationTest { + + protected TestApplication app; + protected Location testLocation; + protected ElasticSearchNode elasticSearchNode; + + @BeforeMethod(alwaysRun = true) + public void setup() throws Exception { + app = ApplicationBuilder.newManagedApp(TestApplication.class); + testLocation = new LocalhostMachineProvisioningLocation(); + } + + @AfterMethod(alwaysRun = true) + public void shutdown() { + Entities.destroyAll(app.getManagementContext()); + } + + @Test(groups = {"Integration"}) + public void testStartupAndShutdown() { + elasticSearchNode = app.createAndManageChild(EntitySpec.create(ElasticSearchNode.class)); + app.start(ImmutableList.of(testLocation)); + + EntityTestUtils.assertAttributeEqualsEventually(elasticSearchNode, Startable.SERVICE_UP, true); + + elasticSearchNode.stop(); + + EntityTestUtils.assertAttributeEqualsEventually(elasticSearchNode, Startable.SERVICE_UP, false); + } + + @Test(groups = {"Integration"}) + public void testDocumentCount() throws URISyntaxException { + elasticSearchNode = app.createAndManageChild(EntitySpec.create(ElasticSearchNode.class)); + app.start(ImmutableList.of(testLocation)); + + EntityTestUtils.assertAttributeEqualsEventually(elasticSearchNode, Startable.SERVICE_UP, true); + + EntityTestUtils.assertAttributeEquals(elasticSearchNode, ElasticSearchNode.DOCUMENT_COUNT, 0); + + String baseUri = "http://" + elasticSearchNode.getAttribute(Attributes.HOSTNAME) + ":" + elasticSearchNode.getAttribute(Attributes.HTTP_PORT); + + HttpToolResponse pingResponse = HttpTool.execAndConsume( + HttpTool.httpClientBuilder().build(), + new HttpGet(baseUri)); + assertEquals(pingResponse.getResponseCode(), 200); + + String document = "{\"foo\" : \"bar\",\"baz\" : \"quux\"}"; + + HttpToolResponse putResponse = HttpTool.httpPut( + HttpTool.httpClientBuilder() + .port(elasticSearchNode.getAttribute(Attributes.HTTP_PORT)) + .build(), + new URI(baseUri + "/mydocuments/docs/1"), + ImmutableMap.<String, String>of(), + Strings.toByteArray(document)); + assertEquals(putResponse.getResponseCode(), 201); + + HttpToolResponse getResponse = HttpTool.execAndConsume( + HttpTool.httpClientBuilder().build(), + new HttpGet(baseUri + "/mydocuments/docs/1/_source")); + assertEquals(getResponse.getResponseCode(), 200); + assertEquals(HttpValueFunctions.jsonContents("foo", String.class).apply(getResponse), "bar"); + + EntityTestUtils.assertAttributeEqualsEventually(elasticSearchNode, ElasticSearchNode.DOCUMENT_COUNT, 1); + } +}
