Repository: incubator-geode
Updated Branches:
  refs/heads/feature/e2e-testing [created] a2ce01d33


Exploring using docker for end-to-end tests


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

Branch: refs/heads/feature/e2e-testing
Commit: a2ce01d33047cc781064f1ba9493c7a5ba4b5533
Parents: 85e97c3
Author: Jens Deppe <[email protected]>
Authored: Thu Sep 29 08:26:40 2016 -0700
Committer: Jens Deppe <[email protected]>
Committed: Thu Sep 29 08:26:40 2016 -0700

----------------------------------------------------------------------
 .../test/java/org/apache/geode/DockerTest.java  |  45 +++++++
 .../apache/geode/container/DockerCluster.java   | 132 +++++++++++++++++++
 gradle/test.gradle                              |   3 +-
 3 files changed, 179 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/a2ce01d3/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
new file mode 100644
index 0000000..b5bc050
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/DockerTest.java
@@ -0,0 +1,45 @@
+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/a2ce01d3/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
new file mode 100644
index 0000000..fea2e1a
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/container/DockerCluster.java
@@ -0,0 +1,132 @@
+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/a2ce01d3/gradle/test.gradle
----------------------------------------------------------------------
diff --git a/gradle/test.gradle b/gradle/test.gradle
index 5b895ba..5b35db4 100644
--- a/gradle/test.gradle
+++ b/gradle/test.gradle
@@ -71,7 +71,8 @@ subprojects {
     testCompile 'org.jmock:jmock-junit4:' + project.'jmock.version'
     testCompile 'org.jmock:jmock-legacy:' + project.'jmock.version'
     testCompile 'pl.pragmatists:JUnitParams:' + project.'JUnitParams.version'
-
+    testCompile 'com.spotify:docker-client:5.0.2'
+    
     testRuntime 'cglib:cglib:' + project.'cglib.version'
     testRuntime 'org.ow2.asm:asm:' + project.'asm.version'
   }

Reply via email to