You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by si...@apache.org on 2018/09/30 18:37:45 UTC

[bookkeeper] branch master updated: [INTEGRATION TESTS] Dump container logs before stopping tests

This is an automated email from the ASF dual-hosted git repository.

sijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new dd1c8e0  [INTEGRATION TESTS] Dump container logs before stopping tests
dd1c8e0 is described below

commit dd1c8e0794786ea2e87918df451f912d84098a67
Author: Sijie Guo <gu...@gmail.com>
AuthorDate: Sun Sep 30 11:37:41 2018 -0700

    [INTEGRATION TESTS] Dump container logs before stopping tests
    
    Descriptions of the changes in this PR:
    
    *Motivation*
    
    For debugging purpose, it would be great to dump container logs before
    stopping tests.
    
    *Changes*
    
    Copying container and bookkeeper logs before stopping the tests.
    
    
    
    
    
    Author: Sijie Guo <gu...@gmail.com>
    Author: Enrico Olivelli <eo...@gmail.com>
    Author: Sijie Guo <si...@apache.org>
    
    Reviewers: Enrico Olivelli <eo...@gmail.com>
    
    This closes #1718 from sijie/dump_container_logs
---
 .../tests/containers/BookieContainer.java          | 12 ++++++++++
 .../tests/containers/ChaosContainer.java           | 18 +++++++++++++++
 .../tests/integration/utils/DockerUtils.java       | 26 ++++++++++++++++++++++
 3 files changed, 56 insertions(+)

diff --git a/tests/integration-tests-topologies/src/main/java/org/apache/bookkeeper/tests/containers/BookieContainer.java b/tests/integration-tests-topologies/src/main/java/org/apache/bookkeeper/tests/containers/BookieContainer.java
index 1082977..f155d74 100644
--- a/tests/integration-tests-topologies/src/main/java/org/apache/bookkeeper/tests/containers/BookieContainer.java
+++ b/tests/integration-tests-topologies/src/main/java/org/apache/bookkeeper/tests/containers/BookieContainer.java
@@ -111,6 +111,18 @@ public class BookieContainer<SelfT extends BookieContainer<SelfT>> extends Chaos
     }
 
     @Override
+    protected void beforeStop() {
+        super.beforeStop();
+        if (null != containerId) {
+            DockerUtils.dumpContainerDirToTargetCompressed(
+                getDockerClient(),
+                getContainerName(),
+                "/opt/bookkeeper/logs"
+            );
+        }
+    }
+
+    @Override
     public void stop() {
         super.stop();
         log.info("Stopped bookie {} at cluster {}", hostname, clusterName);
diff --git a/tests/integration-tests-topologies/src/main/java/org/apache/bookkeeper/tests/containers/ChaosContainer.java b/tests/integration-tests-topologies/src/main/java/org/apache/bookkeeper/tests/containers/ChaosContainer.java
index adc3e27..d6b1467 100644
--- a/tests/integration-tests-topologies/src/main/java/org/apache/bookkeeper/tests/containers/ChaosContainer.java
+++ b/tests/integration-tests-topologies/src/main/java/org/apache/bookkeeper/tests/containers/ChaosContainer.java
@@ -27,6 +27,7 @@ import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.TimeUnit;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.tests.integration.utils.DockerUtils;
 import org.apache.commons.lang.StringUtils;
 import org.testcontainers.containers.GenericContainer;
 
@@ -43,6 +44,23 @@ public class ChaosContainer<SelfT extends ChaosContainer<SelfT>> extends Generic
         this.clusterName = clusterName;
     }
 
+    protected void beforeStop() {
+        if (null == containerId) {
+            return;
+        }
+
+        DockerUtils.dumpContainerLogToTarget(
+            getDockerClient(),
+            getContainerName()
+        );
+    }
+
+    @Override
+    public void stop() {
+        beforeStop();
+        super.stop();
+    }
+
     public void tailContainerLog() {
         CompletableFuture.runAsync(() -> {
             while (null == containerId) {
diff --git a/tests/integration-tests-utils/src/main/java/org/apache/bookkeeper/tests/integration/utils/DockerUtils.java b/tests/integration-tests-utils/src/main/java/org/apache/bookkeeper/tests/integration/utils/DockerUtils.java
index 9b9fb15..891c1ce 100644
--- a/tests/integration-tests-utils/src/main/java/org/apache/bookkeeper/tests/integration/utils/DockerUtils.java
+++ b/tests/integration-tests-utils/src/main/java/org/apache/bookkeeper/tests/integration/utils/DockerUtils.java
@@ -21,6 +21,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
 
 import com.github.dockerjava.api.DockerClient;
 import com.github.dockerjava.api.async.ResultCallback;
+import com.github.dockerjava.api.command.InspectContainerResponse;
 import com.github.dockerjava.api.command.InspectExecResponse;
 import com.github.dockerjava.api.model.ContainerNetwork;
 import com.github.dockerjava.api.model.Frame;
@@ -30,6 +31,7 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.Arrays;
 import java.util.Map;
 import java.util.Set;
@@ -38,6 +40,7 @@ import java.util.concurrent.ExecutionException;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
+import java.util.zip.GZIPOutputStream;
 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
 
@@ -102,6 +105,29 @@ public class DockerUtils {
         }
     }
 
+    public static void dumpContainerDirToTargetCompressed(DockerClient dockerClient, String containerId,
+                                                          String path) {
+        final int readBlockSize = 10000;
+        InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(containerId).exec();
+        // docker api returns names prefixed with "/", it's part of it's legacy design,
+        // this removes it to be consistent with what docker ps shows.
+        final String containerName = inspectContainerResponse.getName().replace("/", "");
+        File output = new File(getTargetDirectory(containerName),
+                               (path.replace("/", "-") + ".tar.gz")
+                                   .replaceAll("^-", ""));
+        try (InputStream dockerStream = dockerClient.copyArchiveFromContainerCmd(containerId, path).exec();
+             OutputStream os = new GZIPOutputStream(new FileOutputStream(output))) {
+            byte[] block = new byte[readBlockSize];
+            int read = dockerStream.read(block, 0, readBlockSize);
+            while (read > -1) {
+                os.write(block, 0, read);
+                read = dockerStream.read(block, 0, readBlockSize);
+            }
+        } catch (RuntimeException | IOException e) {
+            LOG.error("Error reading dir from container {}", containerName, e);
+        }
+    }
+
     public static void dumpContainerLogDirToTarget(DockerClient docker, String containerId, String path) {
         final int readBlockSize = 10000;