Skip to content

Commit

Permalink
Fix number overflow in the disk space detection. Closes #379
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed Jun 27, 2017
1 parent 3e513be commit e7e8f52
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
18 changes: 11 additions & 7 deletions core/src/main/java/org/testcontainers/DockerClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -235,21 +236,24 @@ private <T> T runInsideDocker(DockerClient client, Consumer<CreateContainerCmd>
}
}
}

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

@VisibleForTesting
static class DiskSpaceUsage {
Optional<Long> availableMB = Optional.empty();
Optional<Integer> 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;
Expand Down
11 changes: 11 additions & 0 deletions core/src/test/java/org/testcontainers/DockerClientFactoryTest.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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));
}
}

0 comments on commit e7e8f52

Please sign in to comment.