Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-223 ignore failure at parsing df output #225

Merged
merged 1 commit into from
Sep 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions core/src/main/java/org/testcontainers/DockerClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import com.github.dockerjava.core.command.LogContainerResultCallback;
import com.github.dockerjava.core.command.PullImageResultCallback;
import com.github.dockerjava.core.command.WaitContainerResultCallback;

import lombok.Synchronized;
import org.slf4j.Logger;
import org.testcontainers.dockerclient.*;

import java.util.List;
import java.util.Optional;

import static java.util.Arrays.asList;
import static org.slf4j.LoggerFactory.getLogger;
Expand Down Expand Up @@ -150,21 +152,12 @@ private void checkDiskSpace(DockerClient client) {
callback.awaitCompletion();
String logResults = callback.toString();

int availableKB = 0;
int use = 0;
String[] lines = logResults.split("\n");
for (String line : lines) {
String[] fields = line.split("\\s+");
if (fields[5].equals("/")) {
availableKB = Integer.valueOf(fields[3]);
use = Integer.valueOf(fields[4].replace("%", ""));
}
}
int availableMB = availableKB / 1024;

LOGGER.info("Disk utilization in Docker environment is {}% ({} MB available)", use, availableMB);
DiskSpaceUsage df = parseAvailableDiskSpace(logResults);
LOGGER.info("Disk utilization in Docker environment is {} ({} )",
df.usedPercent.map(x -> x.toString() + "%").orElse("unknown"),
df.availableMB.map(x -> x.toString() + " MB available").orElse("unknown available"));

if (availableMB < 2048) {
if (df.availableMB.orElseThrow(NotAbleToGetDiskSpaceUsageException::new) < 2048) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this throw an exception if free space can't be found?

I think that would be too harsh - maybe the nicest behaviour if the df check returns nothing is to just let the process carry on. It would probably be annoying for a user to be blocked completely just because df doesn't work in their environment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's exactly what I did : the exception is caught into checkDiskSpaceAndHandleExceptions and it just logs a warning. Is that ok to you ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK cool - I'd missed the next stage. Thanks.

LOGGER.error("Docker environment has less than 2GB free - execution is unlikely to succeed so will be aborted.");
throw new NotEnoughDiskSpaceException("Not enough disk space in Docker environment");
}
Expand All @@ -178,6 +171,25 @@ private void checkDiskSpace(DockerClient client) {
}
}
}

private static class DiskSpaceUsage {
Optional<Integer> availableMB = Optional.empty();
Optional<Integer> usedPercent = Optional.empty();
}

private DiskSpaceUsage parseAvailableDiskSpace(String dfOutput) {
DiskSpaceUsage df = new DiskSpaceUsage();
String[] lines = dfOutput.split("\n");
for (String line : lines) {
String[] fields = line.split("\\s+");
if (fields[5].equals("/")) {
int availableKB = Integer.valueOf(fields[3]);
df.availableMB = Optional.of(availableKB / 1024);
df.usedPercent = Optional.of(Integer.valueOf(fields[4].replace("%", "")));
}
}
return df;
}

/**
* @return the docker API version of the daemon that we have connected to
Expand Down Expand Up @@ -212,6 +224,12 @@ private static class NotEnoughDiskSpaceException extends RuntimeException {
super(message);
}
}

private static class NotAbleToGetDiskSpaceUsageException extends RuntimeException {
NotAbleToGetDiskSpaceUsageException() {
super();
}
}
}

class LogContainerCallback extends LogContainerResultCallback {
Expand Down