sijie closed pull request #1718: [integration tests] Dump container logs before stopping tests URL: https://github.com/apache/bookkeeper/pull/1718
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): 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 10829779f1..f155d74b4a 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 @@ -110,6 +110,18 @@ public void start() { log.info("Started bookie {} at cluster {}", hostname, clusterName); } + @Override + protected void beforeStop() { + super.beforeStop(); + if (null != containerId) { + DockerUtils.dumpContainerDirToTargetCompressed( + getDockerClient(), + getContainerName(), + "/opt/bookkeeper/logs" + ); + } + } + @Override public void stop() { super.stop(); 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 adc3e277f9..d6b146786c 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.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 @@ protected ChaosContainer(String clusterName, String image) { 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 9b9fb15811..891c1ce023 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 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.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.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 void onComplete() { } } + 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; ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services