|
| 1 | +package com.github.dockerjava.cmd; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.command.CreateContainerResponse; |
| 4 | +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
| 5 | +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; |
| 6 | +import org.junit.Test; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +import java.io.InputStream; |
| 11 | + |
| 12 | +import static com.github.dockerjava.core.DockerRule.DEFAULT_IMAGE; |
| 13 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 14 | +import static org.hamcrest.Matchers.is; |
| 15 | +import static org.hamcrest.Matchers.not; |
| 16 | +import static org.hamcrest.Matchers.emptyString; |
| 17 | + |
| 18 | +public class ExportContainerCmdIT extends CmdIT { |
| 19 | + |
| 20 | + private static final Logger LOG = LoggerFactory.getLogger(ExportContainerCmdIT.class); |
| 21 | + |
| 22 | + @Test |
| 23 | + public void exportContainerHasCreatedFile() throws Exception { |
| 24 | + CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) |
| 25 | + .withCmd("touch", "/myExportedFile") |
| 26 | + .exec(); |
| 27 | + |
| 28 | + LOG.info("Created container: {}", container.toString()); |
| 29 | + assertThat(container.getId(), not(is(emptyString()))); |
| 30 | + |
| 31 | + dockerRule.getClient().startContainerCmd(container.getId()).exec(); |
| 32 | + |
| 33 | + int exitCode = dockerRule.getClient().waitContainerCmd(container.getId()).start() |
| 34 | + .awaitStatusCode(); |
| 35 | + assertThat(exitCode, is(0)); |
| 36 | + |
| 37 | + try (InputStream response = dockerRule.getClient().exportContainerCmd(container.getId()).exec()) { |
| 38 | + boolean foundFile = false; |
| 39 | + try (TarArchiveInputStream tarStream = new TarArchiveInputStream(response)) { |
| 40 | + TarArchiveEntry entry; |
| 41 | + while ((entry = tarStream.getNextTarEntry()) != null) { |
| 42 | + if (entry.getName().contains("myExportedFile")) { |
| 43 | + foundFile = true; |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + assertThat("Exported archive should contain the created file", foundFile, is(true)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | +} |
0 commit comments