From e7e8f526cc1669d8eb86a39ba613f9e315c6506e Mon Sep 17 00:00:00 2001 From: bsideup Date: Tue, 27 Jun 2017 20:32:28 +0200 Subject: [PATCH] Fix number overflow in the disk space detection. Closes #379 --- .../testcontainers/DockerClientFactory.java | 18 +++++++++++------- .../DockerClientFactoryTest.java | 11 +++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/testcontainers/DockerClientFactory.java b/core/src/main/java/org/testcontainers/DockerClientFactory.java index 816c058dce6..bb40de7fa3b 100644 --- a/core/src/main/java/org/testcontainers/DockerClientFactory.java +++ b/core/src/main/java/org/testcontainers/DockerClientFactory.java @@ -8,6 +8,7 @@ import com.github.dockerjava.api.model.*; import com.github.dockerjava.core.command.ExecStartResultCallback; import com.github.dockerjava.core.command.PullImageResultCallback; +import com.google.common.annotations.VisibleForTesting; import lombok.Synchronized; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; @@ -235,21 +236,24 @@ private T runInsideDocker(DockerClient client, Consumer } } } - - private static class DiskSpaceUsage { - Optional availableMB = Optional.empty(); + + @VisibleForTesting + static class DiskSpaceUsage { + Optional availableMB = Optional.empty(); Optional usedPercent = Optional.empty(); } - - private DiskSpaceUsage parseAvailableDiskSpace(String dfOutput) { + + @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("/")) { - int availableKB = Integer.valueOf(fields[3]); - df.availableMB = Optional.of(availableKB / 1024); + long availableKB = Long.valueOf(fields[3]); + df.availableMB = Optional.of(availableKB / 1024L); df.usedPercent = Optional.of(Integer.valueOf(fields[4].replace("%", ""))); + break; } } return df; diff --git a/core/src/test/java/org/testcontainers/DockerClientFactoryTest.java b/core/src/test/java/org/testcontainers/DockerClientFactoryTest.java index bf67cc6885a..a6cb304d4e3 100644 --- a/core/src/test/java/org/testcontainers/DockerClientFactoryTest.java +++ b/core/src/test/java/org/testcontainers/DockerClientFactoryTest.java @@ -1,6 +1,8 @@ package org.testcontainers; import org.junit.Test; +import org.rnorth.visibleassertions.VisibleAssertions; +import org.testcontainers.DockerClientFactory.DiskSpaceUsage; import org.testcontainers.dockerclient.LogToStringContainerCallback; import org.testcontainers.utility.TestcontainersConfiguration; @@ -27,4 +29,13 @@ public void runCommandInsideDockerShouldNotFailIfImageDoesNotExistsLocally() { .toString() ); } + + @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)); + } } \ No newline at end of file