Skip to content

Commit

Permalink
Remove the disk space check (#5381)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed May 17, 2022
1 parent 8bc0b26 commit 7a957d5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 117 deletions.
4 changes: 3 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ tasks.japicmp {
"org.testcontainers.shaded.*",
]

classExcludes = []
classExcludes = [
"org.testcontainers.utility.RyukResourceReaper",
]

methodExcludes = [
"org.testcontainers.containers.Container#getDockerClient()",
Expand Down
102 changes: 4 additions & 98 deletions core/src/main/java/org/testcontainers/DockerClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.DockerClientDelegate;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.PullImageCmd;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.exception.InternalServerErrorException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.AccessMode;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.Info;
import com.github.dockerjava.api.model.Version;
import com.github.dockerjava.api.model.Volume;
Expand All @@ -31,18 +29,14 @@
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;
import org.testcontainers.utility.ResourceReaper;
import org.testcontainers.utility.RyukResourceReaper;
import org.testcontainers.utility.TestcontainersConfiguration;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.UUID;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -80,16 +74,10 @@ public class DockerClientFactory {
RuntimeException cachedClientFailure;

private String activeApiVersion;
private String activeExecutionDriver;

@Getter(lazy = true)
private final boolean fileMountingSupported = checkMountableFile();


static {
System.setProperty("org.testcontainers.shaded.io.netty.packagePrefix", "org.testcontainers.shaded.");
}

@VisibleForTesting
DockerClientFactory() {

Expand Down Expand Up @@ -200,20 +188,19 @@ public void close() {
log.info("Docker host IP address is {}", strategy.getDockerHostIpAddress());

Info dockerInfo = strategy.getInfo();
log.debug("Docker info: {}", dockerInfo.getRawValues());
Version version = client.versionCmd().exec();
log.debug("Docker version: {}", version.getRawValues());
activeApiVersion = version.getApiVersion();
activeExecutionDriver = dockerInfo.getExecutionDriver();
log.info("Connected to docker: \n" +
" Server Version: " + dockerInfo.getServerVersion() + "\n" +
" API Version: " + activeApiVersion + "\n" +
" Operating System: " + dockerInfo.getOperatingSystem() + "\n" +
" Total Memory: " + dockerInfo.getMemTotal() / (1024 * 1024) + " MB");

final ResourceReaper resourceReaper;
try {
resourceReaper = ResourceReaper.instance();
//noinspection deprecation
resourceReaper.init();
ResourceReaper.instance().init();
} catch (RuntimeException e) {
cachedClientFailure = e;
throw e;
Expand All @@ -226,27 +213,6 @@ public void close() {
try {
log.info("Checking the system...");
checkDockerVersion(version.getVersion());

//noinspection deprecation
String ryukContainerId = resourceReaper instanceof RyukResourceReaper
? ((RyukResourceReaper) resourceReaper).getContainerId()
: null;

if (ryukContainerId != null) {
checkDiskSpace(client, ryukContainerId);
} else {
runInsideDocker(
createContainerCmd -> {
createContainerCmd.withName("testcontainers-checks-" + SESSION_ID);
createContainerCmd.getHostConfig().withAutoRemove(true);
createContainerCmd.withCmd("tail", "-f", "/dev/null");
},
(__, containerId) -> {
checkDiskSpace(client, containerId);
return "";
}
);
}
} catch (RuntimeException e) {
cachedClientFailure = e;
throw e;
Expand All @@ -263,43 +229,6 @@ private void checkDockerVersion(String dockerVersion) {
check("Docker server version should be at least 1.6.0", versionIsSufficient);
}

private void checkDiskSpace(DockerClient dockerClient, String id) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

try {
dockerClient
.execStartCmd(dockerClient.execCreateCmd(id).withAttachStdout(true).withCmd("df", "-P").exec().getId())
.exec(new ResultCallback.Adapter<Frame>() {
@Override
public void onNext(Frame frame) {
if (frame == null) {
return;
}
switch (frame.getStreamType()) {
case RAW:
case STDOUT:
try {
outputStream.write(frame.getPayload());
outputStream.flush();
} catch (IOException e) {
onError(e);
}
}
}
})
.awaitCompletion();
} catch (Exception e) {
log.debug("Can't exec disk checking command", e);
}

DiskSpaceUsage df = parseAvailableDiskSpace(outputStream.toString());

check(
"Docker environment should have more than 2GB free disk space",
df.availableMB.map(it -> it >= 2048).orElse(true)
);
}

private void check(String message, boolean isSuccessful) {
if (isSuccessful) {
log.info("\u2714\ufe0e {}", message);
Expand Down Expand Up @@ -389,28 +318,6 @@ <T> T runInsideDocker(DockerImageName imageName, Consumer<CreateContainerCmd> cr
}
}

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

@VisibleForTesting
DiskSpaceUsage parseAvailableDiskSpace(String dfOutput) {
DiskSpaceUsage df = new DiskSpaceUsage();
String[] lines = dfOutput.split("\n");
for (String line : lines) {
String[] fields = line.split("\\s+");
if (fields.length > 5 && fields[5].equals("/")) {
long availableKB = Long.parseLong(fields[3]);
df.availableMB = Optional.of(availableKB / 1024L);
df.usedPercent = Optional.of(Integer.valueOf(fields[4].replace("%", "")));
break;
}
}
return df;
}

/**
* @return the docker API version of the daemon that we have connected to
*/
Expand All @@ -423,8 +330,7 @@ public String getActiveApiVersion() {
* @return the docker execution driver of the daemon that we have connected to
*/
public String getActiveExecutionDriver() {
client();
return activeExecutionDriver;
return getInfo().getExecutionDriver();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
* Ryuk-based {@link ResourceReaper} implementation.
*
* @see <a href="https://github.com/testcontainers/moby-ryuk">moby-ryuk</a>
* @deprecated internal API
*/
@Deprecated
@Slf4j
public class RyukResourceReaper extends ResourceReaper {
class RyukResourceReaper extends ResourceReaper {

private static final RateLimiter RYUK_ACK_RATE_LIMITER = RateLimiterBuilder
.newBuilder()
Expand All @@ -37,10 +35,6 @@ public class RyukResourceReaper extends ResourceReaper {

private final RyukContainer ryukContainer = new RyukContainer();

public String getContainerId() {
return ryukContainer.getContainerId();
}

@Override
public void init() {
if (!TestcontainersConfiguration.getInstance().environmentSupportsReuse()) {
Expand Down
11 changes: 0 additions & 11 deletions core/src/test/java/org/testcontainers/DockerClientFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.junit.Rule;
import org.junit.Test;
import org.rnorth.visibleassertions.VisibleAssertions;
import org.testcontainers.DockerClientFactory.DiskSpaceUsage;
import org.testcontainers.dockerclient.LogToStringContainerCallback;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MockTestcontainersConfigurationRule;
Expand Down Expand Up @@ -39,15 +37,6 @@ public void runCommandInsideDockerShouldNotFailIfImageDoesNotExistsLocally() {
}
}

@Test
public void shouldHandleBigDiskSize() throws Exception {
String dfOutput = "/dev/disk1 2982480572 1491240286 2982480572 31% /";
DiskSpaceUsage usage = DockerClientFactory.instance().parseAvailableDiskSpace(dfOutput);

VisibleAssertions.assertEquals("Available MB is correct", 2982480572L / 1024L, usage.availableMB.orElse(0L));
VisibleAssertions.assertEquals("Available percentage is correct", 31, usage.usedPercent.orElse(0));
}

@Test
public void dockerHostIpAddress() {
DockerClientFactory instance = new DockerClientFactory();
Expand Down

0 comments on commit 7a957d5

Please sign in to comment.