Skip to content

Commit c3ab3d2

Browse files
committed
Integration test for copy archive cmd checking integrity of received binary file
1 parent 3b3399d commit c3ab3d2

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/test/java/com/github/dockerjava/netty/exec/CopyArchiveFromContainerCmdExecTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66

77
import java.io.InputStream;
88
import java.lang.reflect.Method;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.nio.file.StandardOpenOption;
913

14+
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
15+
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
16+
import org.apache.commons.io.IOUtils;
1017
import org.testng.ITestResult;
1118
import org.testng.annotations.AfterMethod;
1219
import org.testng.annotations.AfterTest;
@@ -16,6 +23,7 @@
1623

1724
import com.github.dockerjava.api.command.CreateContainerResponse;
1825
import com.github.dockerjava.api.exception.NotFoundException;
26+
import com.github.dockerjava.core.util.CompressArchiveUtil;
1927
import com.github.dockerjava.netty.AbstractNettyDockerClientTest;
2028

2129
@Test(groups = "integration")
@@ -70,4 +78,36 @@ public void copyFromNonExistingContainer() throws Exception {
7078
} catch (NotFoundException ignored) {
7179
}
7280
}
81+
82+
@Test
83+
public void copyFromContainerBinaryFile() throws Exception {
84+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
85+
.withName("docker-java-itest-copyFromContainerBinaryFile").exec();
86+
87+
LOG.info("Created container: {}", container);
88+
assertThat(container.getId(), not(isEmptyOrNullString()));
89+
90+
Path temp = Files.createTempFile("", ".tar.gz");
91+
Path binaryFile = Paths.get("src/test/resources/testCopyFromArchive/binary.dat");
92+
CompressArchiveUtil.tar(binaryFile, temp, true, false);
93+
94+
try (InputStream uploadStream = Files.newInputStream(temp)) {
95+
dockerClient.copyArchiveToContainerCmd(container.getId()).withTarInputStream(uploadStream).exec();
96+
}
97+
98+
InputStream response = dockerClient.copyArchiveFromContainerCmd(container.getId(), "/binary.dat").exec();
99+
Boolean bytesAvailable = response.available() > 0;
100+
assertTrue(bytesAvailable, "The file was not copied from the container.");
101+
102+
try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(response)) {
103+
TarArchiveEntry nextTarEntry = tarInputStream.getNextTarEntry();
104+
105+
assertEquals(nextTarEntry.getName(), "binary.dat");
106+
try (InputStream binaryFileInputStream = Files.newInputStream(binaryFile, StandardOpenOption.READ)) {
107+
assertTrue(IOUtils.contentEquals(binaryFileInputStream, tarInputStream));
108+
}
109+
110+
assertNull(tarInputStream.getNextTarEntry(), "Nothing except binary.dat is expected to be copied.");
111+
}
112+
}
73113
}
288 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)