brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [1/6] brooklyn-library git commit: ZooKeeper tests: assert data can be written and read back
Date Wed, 16 Nov 2016 17:20:25 GMT
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 <sam.corbett@cloudsoftcorp.com>
Authored: Mon Nov 14 18:35:54 2016 +0000
Committer: Sam Corbett <sam.corbett@cloudsoftcorp.com>
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<Integer> 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<String> 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<String>() {
+                @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));
+    }
+
+}


Mime
View raw message