Able to execute gfsh commands

Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/4910c626
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/4910c626
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/4910c626

Branch: refs/heads/feature/e2e-testing
Commit: 4910c62604cf12b48556e532b7436aeb914fdd5b
Parents: a2ce01d
Author: Jens Deppe <jde...@pivotal.io>
Authored: Fri Sep 30 14:42:40 2016 -0700
Committer: Jens Deppe <jde...@pivotal.io>
Committed: Fri Sep 30 14:42:40 2016 -0700

----------------------------------------------------------------------
 .../test/java/org/apache/geode/DockerTest.java  |  45 -----
 .../apache/geode/container/DockerCluster.java   | 132 --------------
 .../java/org/apache/geode/e2e/DockerTest.java   |  43 +++++
 .../geode/e2e/container/DockerCluster.java      | 182 +++++++++++++++++++
 4 files changed, 225 insertions(+), 177 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4910c626/geode-core/src/test/java/org/apache/geode/DockerTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/DockerTest.java 
b/geode-core/src/test/java/org/apache/geode/DockerTest.java
deleted file mode 100644
index b5bc050..0000000
--- a/geode-core/src/test/java/org/apache/geode/DockerTest.java
+++ /dev/null
@@ -1,45 +0,0 @@
-package org.apache.geode;
-
-import static org.junit.Assert.*;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-import org.apache.geode.container.DockerCluster;
-
-public class DockerTest {
-
-  private DockerCluster cluster;
-
-  @Before
-  public void setup() throws Exception {
-    cluster = new DockerCluster("testy", 2);
-  }
-
-  @After
-  public void teardown() throws Exception {
-    cluster.stop();
-  }
-
-//  @Test
-  public void sanity() throws Exception {
-    cluster.start();
-    assertNotNull("Locator address is null", cluster.getLocatorAddress());
-  }
-
-//  @Test
-  public void testInvalidGfshCommand() throws Exception {
-    String id = cluster.startContainer(0);
-    int r = cluster.execCommand(id, new String[] { "/tmp/work/bin/gfsh", 
"startx" });
-    assertEquals(1, r);
-  }
-
-  @Test
-  public void testCreateRegion() throws Exception {
-    cluster.start();
-    cluster.gfshCommand("create region --name=FOO --type=REPLICATE");
-    cluster.gfshCommand("list regions");
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4910c626/geode-core/src/test/java/org/apache/geode/container/DockerCluster.java
----------------------------------------------------------------------
diff --git 
a/geode-core/src/test/java/org/apache/geode/container/DockerCluster.java 
b/geode-core/src/test/java/org/apache/geode/container/DockerCluster.java
deleted file mode 100644
index fea2e1a..0000000
--- a/geode-core/src/test/java/org/apache/geode/container/DockerCluster.java
+++ /dev/null
@@ -1,132 +0,0 @@
-package org.apache.geode.container;
-
-import static com.google.common.base.Charsets.*;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import com.spotify.docker.client.DefaultDockerClient;
-import com.spotify.docker.client.DockerClient;
-import com.spotify.docker.client.LogStream;
-import com.spotify.docker.client.exceptions.DockerException;
-import com.spotify.docker.client.messages.ContainerConfig;
-import com.spotify.docker.client.messages.ContainerCreation;
-import com.spotify.docker.client.messages.HostConfig;
-
-public class DockerCluster {
-
-  private DockerClient docker;
-  private int locatorCount;
-  private int serverCount;
-  private String name;
-  private List<String> nodeIds;
-  private String locatorAddress;
-
-  public DockerCluster(String name, int serverCount) {
-    this(name, 1, serverCount);
-  }
-
-  public DockerCluster(String name, int locatorCount, int serverCount) {
-    docker = DefaultDockerClient.builder().
-      uri("unix:///var/run/docker.sock").build();
-
-    this.name = name;
-    this.locatorCount = locatorCount;
-    this.serverCount = serverCount;
-    this.nodeIds = new ArrayList<>();
-  }
-
-  public void start() throws Exception {
-    startLocators();
-    startServers();
-  }
-
-  public String startContainer(int index) throws DockerException, 
InterruptedException {
-    String geodeHome = System.getenv("GEODE_HOME");
-    String vol = String.format("%s:/tmp/work", geodeHome);
-
-    HostConfig hostConfig = HostConfig.
-      builder().
-      appendBinds(vol).
-      build();
-
-    ContainerConfig config = ContainerConfig.builder().
-      image("gemfire/ubuntu-gradle").
-      openStdin(true).
-      hostname(String.format("%s-%d", name, index)).
-      hostConfig(hostConfig).
-      workingDir("/tmp").
-      build();
-
-    ContainerCreation creation = docker.createContainer(config);
-    String id = creation.id();
-    docker.startContainer(id);
-    docker.inspectContainer(id);
-
-    nodeIds.add(id);
-
-    return id;
-  }
-
-  public void startLocators() throws DockerException, InterruptedException {
-    for (int i = 0; i < locatorCount; i++) {
-      String[] command = {
-        "/tmp/work/bin/gfsh",
-        "start locator",
-        String.format("--name=%s-locator-%d", name, i)
-      };
-
-      String id = startContainer(i);
-      execCommand(id, command);
-    }
-
-    locatorAddress = String.format("%s[10334]", 
docker.inspectContainer(nodeIds.get(0)).networkSettings().ipAddress());
-  }
-
-  public void startServers() throws DockerException, InterruptedException {
-    for (int i = 0; i < serverCount+1; i++) {
-      String[] command = {
-        "/tmp/work/bin/gfsh",
-        "start server",
-        String.format("--name=%s-server-%d", name, i),
-        String.format("--locators=%s", locatorAddress)
-      };
-
-      String id = startContainer(i);
-      execCommand(id, command);
-    }
-  }
-
-  public int gfshCommand(String command) throws DockerException, 
InterruptedException {
-    String locatorId = nodeIds.get(0);
-    List<String> gfshCmd = Arrays.asList(command);
-    gfshCmd.add(0, "/tmp/work/bin/gfsh");
-    return execCommand(locatorId, gfshCmd.toArray(new String[]{}));
-  }
-
-  public int execCommand(String id, String... command) throws DockerException, 
InterruptedException {
-    String execId = docker.execCreate(id, command, 
DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam
-      .attachStderr());
-    LogStream output = docker.execStart(execId);
-
-    while (output.hasNext()) {
-      System.out.print(UTF_8.decode(output.next().content()));
-      System.out.flush();
-    }
-
-    return docker.execInspect(execId).exitCode();
-  }
-
-  public void stop() throws DockerException, InterruptedException {
-    for (String id : nodeIds) {
-      docker.killContainer(id);
-      docker.removeContainer(id);
-    }
-    docker.close();
-  }
-
-  public String getLocatorAddress() {
-    return locatorAddress;
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4910c626/geode-core/src/test/java/org/apache/geode/e2e/DockerTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/e2e/DockerTest.java 
b/geode-core/src/test/java/org/apache/geode/e2e/DockerTest.java
new file mode 100644
index 0000000..e8e04f0
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/e2e/DockerTest.java
@@ -0,0 +1,43 @@
+package org.apache.geode.e2e;
+
+import static org.junit.Assert.*;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.apache.geode.e2e.container.DockerCluster;
+
+public class DockerTest {
+
+  private DockerCluster cluster;
+
+  @Before
+  public void setup() throws Exception {
+    cluster = new DockerCluster("testy", 1);
+  }
+
+  @After
+  public void teardown() throws Exception {
+    cluster.stop();
+  }
+
+  @Test
+  public void sanity() throws Exception {
+    cluster.start();
+    assertNotNull("Locator address is null", cluster.getLocatorAddress());
+  }
+
+  @Test
+  public void testInvalidGfshCommand() throws Exception {
+    String id = cluster.startContainer(0);
+    int r = cluster.execCommand(id, false, null, new String[] { 
"/tmp/work/bin/gfsh", "startx" });
+    assertEquals(1, r);
+  }
+
+  @Test
+  public void testCreateRegion() throws Exception {
+    cluster.start();
+    cluster.gfshCommand("create region --name=FOO --type=REPLICATE", null);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4910c626/geode-core/src/test/java/org/apache/geode/e2e/container/DockerCluster.java
----------------------------------------------------------------------
diff --git 
a/geode-core/src/test/java/org/apache/geode/e2e/container/DockerCluster.java 
b/geode-core/src/test/java/org/apache/geode/e2e/container/DockerCluster.java
new file mode 100644
index 0000000..3be2feb
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/e2e/container/DockerCluster.java
@@ -0,0 +1,182 @@
+package org.apache.geode.e2e.container;
+
+import static com.google.common.base.Charsets.*;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import com.spotify.docker.client.DefaultDockerClient;
+import com.spotify.docker.client.DockerClient;
+import com.spotify.docker.client.LogStream;
+import com.spotify.docker.client.exceptions.DockerException;
+import com.spotify.docker.client.messages.ContainerConfig;
+import com.spotify.docker.client.messages.ContainerCreation;
+import com.spotify.docker.client.messages.HostConfig;
+
+public class DockerCluster {
+
+  private DockerClient docker;
+  private int locatorCount;
+  private int serverCount;
+  private String name;
+  private List<String> nodeIds;
+  private String locatorAddress;
+
+  public DockerCluster(String name, int serverCount) {
+    this(name, 1, serverCount);
+  }
+
+  public DockerCluster(String name, int locatorCount, int serverCount) {
+    docker = DefaultDockerClient.builder().
+      uri("unix:///var/run/docker.sock").build();
+
+    this.name = name;
+    this.locatorCount = locatorCount;
+    this.serverCount = serverCount;
+    this.nodeIds = new ArrayList<>();
+  }
+
+  public void start() throws Exception {
+    startLocators();
+    startServers();
+  }
+
+  public String startContainer(int index) throws DockerException, 
InterruptedException {
+    String geodeHome = System.getenv("GEODE_HOME");
+    String vol = String.format("%s:/tmp/work", geodeHome);
+
+    HostConfig hostConfig = HostConfig.
+      builder().
+      appendBinds(vol).
+      build();
+
+    ContainerConfig config = ContainerConfig.builder().
+      image("gemfire/ubuntu-gradle").
+      openStdin(true).
+      hostname(String.format("%s-%d", name, index)).
+      hostConfig(hostConfig).
+      workingDir("/tmp").
+      build();
+
+    ContainerCreation creation = docker.createContainer(config);
+    String id = creation.id();
+    docker.startContainer(id);
+    docker.inspectContainer(id);
+
+    nodeIds.add(id);
+
+    return id;
+  }
+
+  public void startLocators() throws DockerException, InterruptedException {
+    for (int i = 0; i < locatorCount; i++) {
+      String[] command = {
+        "/tmp/work/bin/gfsh",
+        "start locator",
+        String.format("--name=%s-locator-%d", name, i)
+      };
+
+      String id = startContainer(i);
+      execCommand(id, true, null, command);
+
+      while (gfshCommand(null, null) != 0) {
+        Thread.sleep(250);
+      }
+    }
+
+    locatorAddress = String.format("%s[10334]", 
docker.inspectContainer(nodeIds.get(0)).networkSettings().ipAddress());
+  }
+
+  public void startServers() throws DockerException, InterruptedException {
+    for (int i = 0; i < serverCount+1; i++) {
+      String[] command = {
+        "/tmp/work/bin/gfsh",
+        "start server",
+        String.format("--name=%s-server-%d", name, i),
+        String.format("--locators=%s", locatorAddress)
+      };
+
+      String id = startContainer(i);
+      execCommand(id, true, null, command);
+    }
+
+    int runningServers = 0;
+    while (runningServers != serverCount) {
+      Thread.sleep(200);
+
+      List<String> cmdOutput = new ArrayList<>();
+
+      ResultCallback cb = line -> cmdOutput.add(line);
+      gfshCommand("list members", cb);
+
+      runningServers = 0;
+      for (int i = 0; i < serverCount; i++) {
+        String server = String.format("%s-server-%d", name, i);
+        for (String s : cmdOutput) {
+          if (s.startsWith(server)) {
+            runningServers++;
+          }
+        }
+      }
+    }
+  }
+
+  public int gfshCommand(String command, ResultCallback callback) throws 
DockerException, InterruptedException {
+    String locatorId = nodeIds.get(0);
+    List<String> gfshCmd = new ArrayList<>();
+    Collections.addAll(gfshCmd, "/tmp/work/bin/gfsh", "-e", "connect 
--jmx-locator=localhost[1099]");
+
+    if (command != null) {
+      Collections.addAll(gfshCmd, "-e", command);
+    }
+
+    return execCommand(locatorId, false, callback, gfshCmd.toArray(new 
String[]{}));
+  }
+
+  public int execCommand(String id, boolean startDetached,
+                         ResultCallback callback, String... command) throws 
DockerException, InterruptedException {
+    List<DockerClient.ExecCreateParam> execParams = new ArrayList<>();
+    execParams.add(DockerClient.ExecCreateParam.attachStdout());
+    execParams.add(DockerClient.ExecCreateParam.attachStderr());
+    execParams.add(DockerClient.ExecCreateParam.detach(startDetached));
+
+    String execId = docker.execCreate(id, command, execParams.toArray(new 
DockerClient.ExecCreateParam[]{}));
+    LogStream output = docker.execStart(execId);
+
+    if (startDetached) {
+      return 0;
+    }
+
+    StringBuilder buffer = new StringBuilder();
+    while (output.hasNext()) {
+      String multiLine = UTF_8.decode(output.next().content()).toString();
+      buffer.append(multiLine);
+
+      if (buffer.indexOf("\n") >= 0) {
+        int n;
+        while ((n = buffer.indexOf("\n")) >=0 ) {
+          System.out.println("[gfsh]: " + buffer.substring(0, n));
+          if (callback != null) {
+            callback.call(buffer.substring(0, n));
+          }
+          buffer = new StringBuilder(buffer.substring(n + 1));
+        }
+      }
+    }
+
+    return docker.execInspect(execId).exitCode();
+  }
+
+  public void stop() throws DockerException, InterruptedException {
+    for (String id : nodeIds) {
+      docker.killContainer(id);
+      docker.removeContainer(id);
+    }
+    docker.close();
+  }
+
+  public String getLocatorAddress() {
+    return locatorAddress;
+  }
+}

Reply via email to