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

Fix number overflow in the disk space detection. Closes #379 #380

Merged
merged 2 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.

## UNRELEASED
### Fixed
- Fixed the case when disk's size is bigger than Integer's max value (#379, #380)

### Changed
- Added support for Docker networks (#372)
Expand Down
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));
}
}