aahmed-se closed pull request #2611: Make dockerUtils use container name for
test exec
URL: https://github.com/apache/pulsar/pull/2611
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/src/test/java/org/apache/pulsar/tests/integration/utils/DockerUtils.java
b/tests/integration/src/test/java/org/apache/pulsar/tests/integration/utils/DockerUtils.java
index 5148a36912..b685b3cc80 100644
---
a/tests/integration/src/test/java/org/apache/pulsar/tests/integration/utils/DockerUtils.java
+++
b/tests/integration/src/test/java/org/apache/pulsar/tests/integration/utils/DockerUtils.java
@@ -42,6 +42,7 @@
import java.util.stream.Collectors;
import java.util.zip.GZIPOutputStream;
+import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
@@ -50,8 +51,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+@Slf4j
public class DockerUtils {
- private static final Logger LOG =
LoggerFactory.getLogger(DockerUtils.class);
private static File getTargetDirectory(String containerId) {
String base = System.getProperty("maven.buildDirectory");
@@ -60,7 +61,7 @@ private static File getTargetDirectory(String containerId) {
}
File directory = new File(base + "/container-logs/" + containerId);
if (!directory.exists() && !directory.mkdirs()) {
- LOG.error("Error creating directory for container logs.");
+ log.error("Error creating directory for container logs.");
}
return directory;
}
@@ -102,10 +103,10 @@ public void onComplete() {
});
future.get();
} catch (RuntimeException|ExecutionException|IOException e) {
- LOG.error("Error dumping log for {}", containerName, e);
+ log.error("Error dumping log for {}", containerName, e);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
- LOG.info("Interrupted dumping log from container {}",
containerName, ie);
+ log.info("Interrupted dumping log from container {}",
containerName, ie);
}
}
@@ -127,7 +128,7 @@ public static void
dumpContainerDirToTargetCompressed(DockerClient dockerClient,
read = dockerStream.read(block, 0, READ_BLOCK_SIZE);
}
} catch (RuntimeException|IOException e) {
- LOG.error("Error reading dir from container {}", containerName, e);
+ log.error("Error reading dir from container {}", containerName, e);
}
}
@@ -153,7 +154,7 @@ public static void dumpContainerLogDirToTarget(DockerClient
docker, String conta
entry = stream.getNextTarEntry();
}
} catch (RuntimeException|IOException e) {
- LOG.error("Error reading logs from container {}", containerId, e);
+ log.error("Error reading logs from container {}", containerId, e);
}
}
@@ -165,12 +166,16 @@ public static String getContainerIP(DockerClient docker,
String containerId) {
throw new IllegalArgumentException("Container " + containerId + " has
no networks");
}
- public static ContainerExecResult runCommand(DockerClient docker,
+ public static ContainerExecResult runCommand(DockerClient dockerClient,
String containerId,
String... cmd)
throws ContainerExecException {
+ 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("/","");
CompletableFuture<Boolean> future = new CompletableFuture<>();
- String execid = docker.execCreateCmd(containerId)
+ String execid = dockerClient.execCreateCmd(containerId)
.withCmd(cmd)
.withAttachStderr(true)
.withAttachStdout(true)
@@ -179,19 +184,19 @@ public static ContainerExecResult runCommand(DockerClient
docker,
String cmdString = Arrays.stream(cmd).collect(Collectors.joining(" "));
StringBuilder stdout = new StringBuilder();
StringBuilder stderr = new StringBuilder();
- docker.execStartCmd(execid).withDetach(false)
+ dockerClient.execStartCmd(execid).withDetach(false)
.exec(new ResultCallback<Frame>() {
@Override
public void close() {}
@Override
public void onStart(Closeable closeable) {
- LOG.info("DOCKER.exec({}:{}): Executing...", containerId,
cmdString);
+ log.info("DOCKER.exec({}:{}): Executing...",
containerName, cmdString);
}
@Override
public void onNext(Frame object) {
- LOG.info("DOCKER.exec({}:{}): {}", containerId, cmdString,
object);
+ log.info("DOCKER.exec({}:{}): {}", containerName,
cmdString, object);
if (StreamType.STDOUT == object.getStreamType()) {
stdout.append(new String(object.getPayload(), UTF_8));
} else if (StreamType.STDERR == object.getStreamType()) {
@@ -206,13 +211,13 @@ public void onError(Throwable throwable) {
@Override
public void onComplete() {
- LOG.info("DOCKER.exec({}:{}): Done", containerId,
cmdString);
+ log.info("DOCKER.exec({}:{}): Done", containerName,
cmdString);
future.complete(true);
}
});
future.join();
- InspectExecResponse resp = docker.inspectExecCmd(execid).exec();
+ InspectExecResponse resp = dockerClient.inspectExecCmd(execid).exec();
while (resp.isRunning()) {
try {
Thread.sleep(200);
@@ -220,7 +225,7 @@ public void onComplete() {
Thread.currentThread().interrupt();
throw new RuntimeException(ie);
}
- resp = docker.inspectExecCmd(execid).exec();
+ resp = dockerClient.inspectExecCmd(execid).exec();
}
int retCode = resp.getExitCode();
ContainerExecResult result = ContainerExecResult.of(
@@ -228,7 +233,7 @@ public void onComplete() {
stdout.toString(),
stderr.toString()
);
- LOG.info("DOCKER.exec({}:{}): completed with {}", containerId,
cmdString, retCode);
+ log.info("DOCKER.exec({}:{}): completed with {}", containerName,
cmdString, retCode);
if (retCode != 0) {
throw new ContainerExecException(cmdString, containerId, result);
diff --git a/tests/integration/src/test/resources/log4j2.xml
b/tests/integration/src/test/resources/log4j2.xml
new file mode 100644
index 0000000000..3332b39074
--- /dev/null
+++ b/tests/integration/src/test/resources/log4j2.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+<Configuration status="INFO">
+ <Appenders>
+ <Console name="Console" target="SYSTEM_OUT">
+ <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level- %msg%n" />
+ </Console>
+ </Appenders>
+ <Loggers>
+ <Root level="info">
+ <AppenderRef ref="Console" />
+ </Root>
+ </Loggers>
+</Configuration>
\ No newline at end of file
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services