|
6 | 6 |
|
7 | 7 | import java.io.InputStream; |
8 | 8 | 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; |
9 | 13 |
|
| 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; |
10 | 17 | import org.testng.ITestResult; |
11 | 18 | import org.testng.annotations.AfterMethod; |
12 | 19 | import org.testng.annotations.AfterTest; |
|
16 | 23 |
|
17 | 24 | import com.github.dockerjava.api.command.CreateContainerResponse; |
18 | 25 | import com.github.dockerjava.api.exception.NotFoundException; |
| 26 | +import com.github.dockerjava.core.util.CompressArchiveUtil; |
19 | 27 | import com.github.dockerjava.netty.AbstractNettyDockerClientTest; |
20 | 28 |
|
21 | 29 | @Test(groups = "integration") |
@@ -70,4 +78,36 @@ public void copyFromNonExistingContainer() throws Exception { |
70 | 78 | } catch (NotFoundException ignored) { |
71 | 79 | } |
72 | 80 | } |
| 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 | + } |
73 | 113 | } |
0 commit comments