Skip to content

Commit

Permalink
Reduce false erroneous output:
Browse files Browse the repository at this point in the history
* ensure that liveness `exec` calls don't fire when a container is not running
* check that containers exist before trying to clean up in ResourceReaper
* filter out common ANSI control code in stderr output
* amend frame consumer result callback to suppress errors
  • Loading branch information
Richard North committed Nov 17, 2016
1 parent fd6542c commit 1ce1d9f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,10 @@ public ExecResult execInContainer(Charset outputCharset, String... command)

}

if (!isRunning()) {
throw new IllegalStateException("Container is not running so exec cannot be run");
}

this.dockerClient
.execCreateCmd(this.containerId)
.withCmd(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public void onNext(Frame frame) {
}
}

@Override
public void onError(Throwable throwable) {
// Sink any errors
try {
close();
} catch (IOException ignored) { }
}

@Override
public void close() throws IOException {
// send an END frame to every consumer... but only once per consumer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.slf4j.Logger;

import java.util.function.Consumer;
import java.util.regex.Pattern;

/**
* A consumer for container output that logs output to an SLF4J logger.
Expand All @@ -11,6 +12,8 @@ public class Slf4jLogConsumer implements Consumer<OutputFrame> {
private final Logger logger;
private String prefix = "";

private static final Pattern ANSI_CODE_PATTERN = Pattern.compile("\\[\\d[ABCD]");

public Slf4jLogConsumer(Logger logger) {
this.logger = logger;
}
Expand All @@ -28,6 +31,11 @@ public void accept(OutputFrame outputFrame) {
if (utf8String != null) {
OutputFrame.OutputType outputType = outputFrame.getType();
String message = utf8String.trim();

if (ANSI_CODE_PATTERN.matcher(message).matches()) {
return;
}

switch (outputType) {
case END:
break;
Expand Down
17 changes: 17 additions & 0 deletions core/src/main/java/org/testcontainers/utility/ResourceReaper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.InternalServerErrorException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Network;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -67,6 +68,8 @@ public void registerContainerForCleanup(String containerId, String imageName) {
*/
public void stopAndRemoveContainer(String containerId) {
stopContainer(containerId, registeredContainers.get(containerId));

registeredContainers.remove(containerId);
}

/**
Expand All @@ -83,6 +86,13 @@ public void stopAndRemoveContainer(String containerId, String imageName) {

private void stopContainer(String containerId, String imageName) {

List<Container> allContainers = dockerClient.listContainersCmd().withShowAll(true).exec();

if (allContainers.stream().noneMatch(it -> it.getId().equals(containerId))) {
LOGGER.trace("Was going to clean up container but it apparently no longer exists: {}");
return;
}

boolean running;
try {
InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(containerId).exec();
Expand All @@ -105,6 +115,13 @@ private void stopContainer(String containerId, String imageName) {
}
}

try {
InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(containerId).exec();
} catch (NotFoundException e) {
LOGGER.trace("Was going to kill container but it apparently no longer exists: {}");
return;
}

try {
LOGGER.trace("Removing container: {}", containerId);
try {
Expand Down

0 comments on commit 1ce1d9f

Please sign in to comment.