use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project docker-client by spotify.
the class CompressedDirectoryTest method testFileWithEmptyDirectory.
@Test
public void testFileWithEmptyDirectory() throws Exception {
Path tempDir = Files.createTempDirectory("dockerDirectoryEmptySubdirectory");
tempDir.toFile().deleteOnExit();
assertThat(new File(tempDir.toFile(), "emptySubDir").mkdir(), is(true));
try (CompressedDirectory dir = CompressedDirectory.create(tempDir);
BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(dir.file()));
GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(fileIn);
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
final List<String> names = new ArrayList<>();
TarArchiveEntry entry;
while ((entry = tarIn.getNextTarEntry()) != null) {
final String name = entry.getName();
names.add(name);
}
assertThat(names, contains("emptySubDir/"));
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project docker-client by spotify.
the class CompressedDirectoryTest method testFile.
@Test
public void testFile() throws Exception {
// note: Paths.get(someURL.toUri()) is the platform-neutral way to convert a URL to a Path
final URL dockerDirectory = Resources.getResource("dockerDirectory");
try (CompressedDirectory dir = CompressedDirectory.create(Paths.get(dockerDirectory.toURI()));
BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(dir.file()));
GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(fileIn);
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
final List<String> names = new ArrayList<>();
TarArchiveEntry entry;
while ((entry = tarIn.getNextTarEntry()) != null) {
final String name = entry.getName();
names.add(name);
}
assertThat(names, containsInAnyOrder("Dockerfile", "bin/", "bin/date.sh", "innerDir/", "innerDir/innerDockerfile"));
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project docker-client by spotify.
the class DefaultDockerClientTest method integrationTest.
@Test
@SuppressWarnings("deprecation")
public void integrationTest() throws Exception {
// Pull image
sut.pull(BUSYBOX_LATEST);
// Create container
final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").build();
final String name = randomName();
final ContainerCreation creation = sut.createContainer(config, name);
final String id = creation.id();
assertThat(creation.warnings(), anyOf(is(empty()), is(nullValue())));
assertThat(id, is(any(String.class)));
// Inspect using container ID
{
final ContainerInfo info = sut.inspectContainer(id);
assertThat(info.id(), equalTo(id));
assertThat(info.config().image(), equalTo(config.image()));
assertThat(info.config().cmd(), equalTo(config.cmd()));
}
// Inspect using container name
{
final ContainerInfo info = sut.inspectContainer(name);
assertThat(info.config().image(), equalTo(config.image()));
assertThat(info.config().cmd(), equalTo(config.cmd()));
}
// Start container
sut.startContainer(id);
final Path dockerDirectory = getResource("dockerSslDirectory");
// to a directory in a container
if (dockerApiVersionAtLeast("1.20")) {
try {
sut.copyToContainer(dockerDirectory, id, "/tmp");
} catch (Exception e) {
fail("error copying files to container");
}
// Copy the same files from container
final ImmutableSet.Builder<String> filesDownloaded = ImmutableSet.builder();
try (TarArchiveInputStream tarStream = new TarArchiveInputStream(dockerApiVersionLessThan("1.24") ? sut.copyContainer(id, "/tmp") : sut.archiveContainer(id, "/tmp"))) {
TarArchiveEntry entry;
while ((entry = tarStream.getNextTarEntry()) != null) {
filesDownloaded.add(entry.getName());
}
}
// Check that we got back what we put in
final File folder = new File(dockerDirectory.toString());
final File[] files = folder.listFiles();
if (files != null) {
for (final File file : files) {
if (!file.isDirectory()) {
Boolean found = false;
for (final String fileDownloaded : filesDownloaded.build()) {
if (fileDownloaded.contains(file.getName())) {
found = true;
}
}
assertTrue(found);
}
}
}
}
// Kill container
sut.killContainer(id);
try {
// Remove the container
sut.removeContainer(id);
} catch (DockerRequestException e) {
// CircleCI doesn't let you remove a container :(
if (!CIRCLECI) {
// Verify that the container is gone
exception.expect(ContainerNotFoundException.class);
sut.inspectContainer(id);
}
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project docker-client by spotify.
the class DefaultDockerClientTest method testArchiveContainer.
@Test
public void testArchiveContainer() throws Exception {
requireDockerApiVersionAtLeast("1.20", "copyToContainer");
// Pull image
sut.pull(BUSYBOX_LATEST);
// Create container
final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).build();
final String name = randomName();
final ContainerCreation creation = sut.createContainer(config, name);
final String id = creation.id();
final ImmutableSet.Builder<String> files = ImmutableSet.builder();
try (final TarArchiveInputStream tarStream = new TarArchiveInputStream(sut.archiveContainer(id, "/bin"))) {
TarArchiveEntry entry;
while ((entry = tarStream.getNextTarEntry()) != null) {
files.add(entry.getName());
}
}
// Check that some common files exist
assertThat(files.build(), both(hasItem("bin/")).and(hasItem("bin/wc")));
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project bookkeeper by apache.
the class DockerUtils method dumpContainerLogDirToTarget.
public static void dumpContainerLogDirToTarget(DockerClient docker, String containerId, String path) {
final int READ_BLOCK_SIZE = 10000;
try (InputStream dockerStream = docker.copyArchiveFromContainerCmd(containerId, path).exec();
TarArchiveInputStream stream = new TarArchiveInputStream(dockerStream)) {
TarArchiveEntry entry = stream.getNextTarEntry();
while (entry != null) {
if (entry.isFile()) {
File output = new File(getTargetDirectory(containerId), entry.getName().replace("/", "-"));
try (FileOutputStream os = new FileOutputStream(output)) {
byte[] block = new byte[READ_BLOCK_SIZE];
int read = stream.read(block, 0, READ_BLOCK_SIZE);
while (read > -1) {
os.write(block, 0, read);
read = stream.read(block, 0, READ_BLOCK_SIZE);
}
}
}
entry = stream.getNextTarEntry();
}
} catch (RuntimeException | IOException e) {
LOG.error("Error reading bk logs from container {}", containerId, e);
}
}
Aggregations