Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 9A88B200BE3 for ; Wed, 16 Nov 2016 18:20:27 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 994BF160B13; Wed, 16 Nov 2016 17:20:27 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id A782E160B02 for ; Wed, 16 Nov 2016 18:20:26 +0100 (CET) Received: (qmail 41914 invoked by uid 500); 16 Nov 2016 17:20:25 -0000 Mailing-List: contact commits-help@brooklyn.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.apache.org Delivered-To: mailing list commits@brooklyn.apache.org Received: (qmail 41903 invoked by uid 99); 16 Nov 2016 17:20:25 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 16 Nov 2016 17:20:25 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C074AE00C7; Wed, 16 Nov 2016 17:20:25 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: svet@apache.org To: commits@brooklyn.apache.org Date: Wed, 16 Nov 2016 17:20:25 -0000 Message-Id: <414fca6a78e3400bba27b1dd2fe0bd48@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/6] brooklyn-library git commit: ZooKeeper tests: assert data can be written and read back archived-at: Wed, 16 Nov 2016 17:20:27 -0000 Repository: brooklyn-library Updated Branches: refs/heads/master fdad773c5 -> 4bcc6ef13 ZooKeeper tests: assert data can be written and read back Project: http://git-wip-us.apache.org/repos/asf/brooklyn-library/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-library/commit/9ce90b84 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-library/tree/9ce90b84 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-library/diff/9ce90b84 Branch: refs/heads/master Commit: 9ce90b844453e1cd0544a70a97d5bb0f2140cae5 Parents: 6d08cb7 Author: Sam Corbett Authored: Mon Nov 14 18:35:54 2016 +0000 Committer: Sam Corbett Committed: Wed Nov 16 14:34:01 2016 +0000 ---------------------------------------------------------------------- .../zookeeper/ZooKeeperEc2LiveTest.java | 17 ++- .../zookeeper/ZooKeeperEnsembleLiveTest.java | 150 +++++++++++++------ .../zookeeper/ZooKeeperTestSupport.java | 81 ++++++++++ 3 files changed, 199 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/9ce90b84/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperEc2LiveTest.java ---------------------------------------------------------------------- diff --git a/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperEc2LiveTest.java b/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperEc2LiveTest.java index f3557d1..5a435f0 100644 --- a/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperEc2LiveTest.java +++ b/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperEc2LiveTest.java @@ -18,16 +18,18 @@ */ package org.apache.brooklyn.entity.messaging.zookeeper; +import static org.testng.Assert.assertEquals; + import org.apache.brooklyn.api.entity.EntitySpec; import org.apache.brooklyn.api.location.Location; -import org.apache.brooklyn.core.entity.Entities; import org.apache.brooklyn.core.entity.EntityAsserts; import org.apache.brooklyn.core.entity.trait.Startable; -import org.testng.annotations.Test; import org.apache.brooklyn.entity.AbstractEc2LiveTest; import org.apache.brooklyn.entity.zookeeper.ZooKeeperNode; +import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; +import com.google.common.net.HostAndPort; public class ZooKeeperEc2LiveTest extends AbstractEc2LiveTest { @@ -36,10 +38,17 @@ public class ZooKeeperEc2LiveTest extends AbstractEc2LiveTest { */ @Override protected void doTest(Location loc) throws Exception { - ZooKeeperNode zookeeper = app.createAndManageChild(EntitySpec.create(ZooKeeperNode.class).configure("jmxPort", "31001+")); + ZooKeeperNode zookeeper = app.createAndManageChild(EntitySpec.create(ZooKeeperNode.class) + .configure("jmxPort", "31001+")); app.start(ImmutableList.of(loc)); - Entities.dumpInfo(zookeeper); EntityAsserts.assertAttributeEqualsEventually(zookeeper, Startable.SERVICE_UP, true); + HostAndPort conn = HostAndPort.fromParts( + zookeeper.sensors().get(ZooKeeperNode.HOSTNAME), + zookeeper.sensors().get(ZooKeeperNode.ZOOKEEPER_PORT)); + try (ZooKeeperTestSupport zkts = new ZooKeeperTestSupport(conn)) { + zkts.create("/ec2livetest", "data".getBytes()); + assertEquals(new String(zkts.get("/ec2livetest")), "data"); + } } @Test(enabled=false) http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/9ce90b84/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperEnsembleLiveTest.java ---------------------------------------------------------------------- diff --git a/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperEnsembleLiveTest.java b/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperEnsembleLiveTest.java index 2721976..1015c76 100644 --- a/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperEnsembleLiveTest.java +++ b/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperEnsembleLiveTest.java @@ -18,33 +18,45 @@ */ package org.apache.brooklyn.entity.messaging.zookeeper; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +import java.net.Socket; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.api.location.Location; import org.apache.brooklyn.core.entity.Attributes; import org.apache.brooklyn.core.entity.Entities; import org.apache.brooklyn.core.entity.EntityAsserts; -import org.apache.brooklyn.core.entity.factory.ApplicationBuilder; import org.apache.brooklyn.core.entity.trait.Startable; -import org.apache.brooklyn.core.test.entity.TestApplication; +import org.apache.brooklyn.core.test.BrooklynAppLiveTestSupport; +import org.apache.brooklyn.entity.group.DynamicCluster; import org.apache.brooklyn.entity.zookeeper.ZooKeeperEnsemble; import org.apache.brooklyn.entity.zookeeper.ZooKeeperNode; - -import com.google.common.base.Throwables; -import com.google.common.collect.ImmutableList; -import com.google.common.util.concurrent.Uninterruptibles; - -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.api.location.Location; +import org.apache.brooklyn.test.Asserts; +import org.apache.brooklyn.util.exceptions.Exceptions; +import org.apache.brooklyn.util.text.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Optional; +import org.testng.annotations.Parameters; import org.testng.annotations.Test; -import java.net.Socket; -import java.util.concurrent.TimeUnit; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; +import com.google.common.base.Predicates; +import com.google.common.base.Supplier; +import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +import com.google.common.collect.Sets; +import com.google.common.net.HostAndPort; +import com.google.common.util.concurrent.Uninterruptibles; /** * A live test of the {@link org.apache.brooklyn.entity.zookeeper.ZooKeeperEnsemble} entity. @@ -52,29 +64,29 @@ import static org.testng.Assert.assertTrue; * Tests that a 3 node cluster can be started on Amazon EC2 and data written on one {@link org.apache.brooklyn.entity.zookeeper.ZooKeeperEnsemble} * can be read from another, using the Astyanax API. */ -public class ZooKeeperEnsembleLiveTest { +public class ZooKeeperEnsembleLiveTest extends BrooklynAppLiveTestSupport { private static final Logger log = LoggerFactory.getLogger(ZooKeeperEnsembleLiveTest.class); - - private String provider = - "gce-europe-west1"; -// "aws-ec2:eu-west-1"; -// "named:hpcloud-compute-at"; -// "localhost"; + private static final String DEFAULT_LOCATION = "jclouds:aws-ec2:eu-west-1"; - protected TestApplication app; - protected Location testLocation; - protected ZooKeeperEnsemble cluster; + private Location testLocation; + private ZooKeeperEnsemble cluster; + private String locationSpec; - @BeforeMethod(alwaysRun = true) - public void setup() { - app = ApplicationBuilder.newManagedApp(TestApplication.class); - testLocation = app.getManagementContext().getLocationRegistry().getLocationManaged(provider); + @BeforeClass(groups = "Live") + @Parameters({"locationSpec"}) + public void setLocationSpec(@Optional String locationSpec) { + this.locationSpec = !Strings.isBlank(locationSpec) + ? locationSpec + : DEFAULT_LOCATION; + log.info("Running {} with in {}", this, this.locationSpec); } - @AfterMethod(alwaysRun = true) - public void shutdown() { - Entities.destroyAll(app.getManagementContext()); + @Override + @BeforeMethod(alwaysRun = true) + public void setUp() throws Exception { + super.setUp(); + testLocation = app.getManagementContext().getLocationRegistry().getLocationManaged(locationSpec); } /** @@ -82,38 +94,86 @@ public class ZooKeeperEnsembleLiveTest { */ @Test(groups = "Live") public void testStartUpConnectAndResize() throws Exception { + final String zkDataPath = "/ensembletest"; + final int initialSize = 3; try { cluster = app.createAndManageChild(EntitySpec.create(ZooKeeperEnsemble.class) - .configure("initialSize", 3) - .configure("clusterName", "ZooKeeperEnsembleLiveTest")); - assertEquals(cluster.getCurrentSize().intValue(), 0); + .configure(DynamicCluster.INITIAL_SIZE, initialSize) + .configure(ZooKeeperEnsemble.CLUSTER_NAME, "ZooKeeperEnsembleLiveTest")); app.start(ImmutableList.of(testLocation)); - EntityAsserts.assertAttributeEqualsEventually(cluster, ZooKeeperEnsemble.GROUP_SIZE, 3); Entities.dumpInfo(app); - + EntityAsserts.assertAttributeEqualsEventually(cluster, ZooKeeperEnsemble.GROUP_SIZE, 3); EntityAsserts.assertAttributeEqualsEventually(cluster, Startable.SERVICE_UP, true); - for(Entity zkNode : cluster.getMembers()) { - assertTrue(isSocketOpen((ZooKeeperNode) zkNode)); + Set nodeIds = Sets.newHashSet(); + for (Entity zkNode : cluster.getMembers()) { + assertSocketOpen(zkNode); + nodeIds.add(zkNode.sensors().get(ZooKeeperNode.MY_ID)); + } + assertEquals(nodeIds.size(), initialSize, "expected " + initialSize + " node ids, found " + Iterables.toString(nodeIds)); + + // Write data to one and read from the others. + List servers = cluster.sensors().get(ZooKeeperEnsemble.ZOOKEEPER_SERVERS); + assertNotNull(servers, "value for sensor should not be null: " + ZooKeeperEnsemble.ZOOKEEPER_SERVERS); + assertEquals(servers.size(), initialSize, "expected " + initialSize + " entries in " + servers); + + // Write to one + String firstServer = servers.get(0); + HostAndPort conn = HostAndPort.fromString(firstServer); + log.info("Writing data to {}", conn); + try (ZooKeeperTestSupport zkts = new ZooKeeperTestSupport(conn)) { + zkts.create(zkDataPath, "data".getBytes()); + assertEquals(new String(zkts.get(zkDataPath)), "data"); } + + // And read from the others. + for (int i = 1; i < servers.size(); i++) { + conn = HostAndPort.fromString(servers.get(i)); + log.info("Asserting that data can be read from {}", conn); + assertPathDataEventually(conn, zkDataPath, "data"); + } + cluster.resize(1); EntityAsserts.assertAttributeEqualsEventually(cluster, ZooKeeperEnsemble.GROUP_SIZE, 1); - Entities.dumpInfo(app); - EntityAsserts.assertAttributeEqualsEventually(cluster, Startable.SERVICE_UP, true); + EntityAsserts.assertAttributeEqualsContinually(cluster, Startable.SERVICE_UP, true); + + // TODO: assert that data can still be read. for (Entity zkNode : cluster.getMembers()) { - assertTrue(isSocketOpen((ZooKeeperNode) zkNode)); + assertSocketOpen(zkNode); } } catch (Throwable e) { throw Throwables.propagate(e); } } - protected static boolean isSocketOpen(ZooKeeperNode node) { + protected void assertPathDataEventually(HostAndPort hostAndPort, final String path, String expected) throws Exception { + try (ZooKeeperTestSupport zkts = new ZooKeeperTestSupport(hostAndPort)) { + Asserts.eventually(new Supplier() { + @Override + public String get() { + try { + return new String(zkts.get(path)); + } catch (Exception e) { + throw Exceptions.propagate(e); + } + } + }, Predicates.equalTo(expected)); + } + + } + + protected void assertSocketOpen(Entity node) { + assertTrue(isSocketOpen(node)); + } + + protected static boolean isSocketOpen(Entity node) { int attempt = 0, maxAttempts = 20; while(attempt < maxAttempts) { try { - Socket s = new Socket(node.getAttribute(Attributes.HOSTNAME), node.getZookeeperPort()); + final String host = node.sensors().get(Attributes.HOSTNAME); + final int port = node.sensors().get(ZooKeeperNode.ZOOKEEPER_PORT); + Socket s = new Socket(host, port); s.close(); return true; } catch (Exception e) { http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/9ce90b84/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperTestSupport.java ---------------------------------------------------------------------- diff --git a/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperTestSupport.java b/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperTestSupport.java new file mode 100644 index 0000000..042e9bf --- /dev/null +++ b/software/messaging/src/test/java/org/apache/brooklyn/entity/messaging/zookeeper/ZooKeeperTestSupport.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.brooklyn.entity.messaging.zookeeper; + +import java.io.Closeable; +import java.util.concurrent.CountDownLatch; + +import org.apache.brooklyn.util.exceptions.Exceptions; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; + +import com.google.common.net.HostAndPort; + +/** + * Useful methods for writing to and reading from ZooKeeper nodes. + */ +public class ZooKeeperTestSupport implements Closeable { + + private final ZooKeeper zk; + private final CountDownLatch connSignal = new CountDownLatch(1); + + public ZooKeeperTestSupport(HostAndPort hostAndPort) throws Exception { + final int sessionTimeout = 3000; + zk = new ZooKeeper(hostAndPort.toString(), sessionTimeout, new Watcher() { + @Override + public void process(WatchedEvent event) { + if (event.getState() == Event.KeeperState.SyncConnected) { + connSignal.countDown(); + } + } + }); + connSignal.await(); + } + + @Override + public void close() { + try { + zk.close(); + } catch (InterruptedException e) { + throw Exceptions.propagate(e); + } + } + + public String create(String path, byte[] data) throws Exception { + return zk.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + + public Stat update(String path, byte[] data) throws Exception { + return zk.setData(path, data, zk.exists(path, true).getVersion()); + } + + public void delete(String path) throws Exception { + zk.delete(path, zk.exists(path, true).getVersion()); + } + + public byte[] get(String path) throws Exception { + return zk.getData(path, false, zk.exists(path, false)); + } + +}