Skip to content
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
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<hamcrest.library.version>1.3</hamcrest.library.version>
<hamcrest.jpa-matchers>1.6</hamcrest.jpa-matchers>
<lambdaj.version>2.3.3</lambdaj.version>
<mockito.version>1.10.19</mockito.version>


<maven-jar-plugin.version>2.2</maven-jar-plugin.version>
Expand Down Expand Up @@ -195,6 +196,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public int read() throws IOException {
}

if (current != null && current.readableBytes() > 0) {
return current.readByte();
return current.readByte() & 0xff;
} else {
return read();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@

import java.io.InputStream;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.io.IOUtils;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
Expand All @@ -16,6 +23,7 @@

import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.core.util.CompressArchiveUtil;
import com.github.dockerjava.netty.AbstractNettyDockerClientTest;

@Test(groups = "integration")
Expand Down Expand Up @@ -70,4 +78,36 @@ public void copyFromNonExistingContainer() throws Exception {
} catch (NotFoundException ignored) {
}
}

@Test
public void copyFromContainerBinaryFile() throws Exception {
CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
.withName("docker-java-itest-copyFromContainerBinaryFile").exec();

LOG.info("Created container: {}", container);
assertThat(container.getId(), not(isEmptyOrNullString()));

Path temp = Files.createTempFile("", ".tar.gz");
Path binaryFile = Paths.get("src/test/resources/testCopyFromArchive/binary.dat");
CompressArchiveUtil.tar(binaryFile, temp, true, false);

try (InputStream uploadStream = Files.newInputStream(temp)) {
dockerClient.copyArchiveToContainerCmd(container.getId()).withTarInputStream(uploadStream).exec();
}

InputStream response = dockerClient.copyArchiveFromContainerCmd(container.getId(), "/binary.dat").exec();
Boolean bytesAvailable = response.available() > 0;
assertTrue(bytesAvailable, "The file was not copied from the container.");

try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(response)) {
TarArchiveEntry nextTarEntry = tarInputStream.getNextTarEntry();

assertEquals(nextTarEntry.getName(), "binary.dat");
try (InputStream binaryFileInputStream = Files.newInputStream(binaryFile, StandardOpenOption.READ)) {
assertTrue(IOUtils.contentEquals(binaryFileInputStream, tarInputStream));
}

assertNull(tarInputStream.getNextTarEntry(), "Nothing except binary.dat is expected to be copied.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.github.dockerjava.netty.handler;

import static org.testng.Assert.assertTrue;

import java.io.InputStream;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.io.IOUtils;
import org.mockito.Mockito;
import org.testng.annotations.Test;

import com.github.dockerjava.core.async.ResultCallbackTemplate;

/**
* @author Alexander Koshevoy
*/
public class HttpResponseStreamHandlerTest {
@Test
public void testNoBytesSkipped() throws Exception {
ResultCallbackTest callback = new ResultCallbackTest();
HttpResponseStreamHandler streamHandler = new HttpResponseStreamHandler(callback);
ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
ByteBuf buffer = generateByteBuf();
streamHandler.channelRead0(ctx, buffer);
streamHandler.channelReadComplete(ctx);

assertTrue(IOUtils.contentEquals(callback.getInputStream(), new ByteBufInputStream(buffer)));
}

private ByteBuf generateByteBuf() {
byte[] array = new byte[256];
for (int i = 0; i < array.length; i++) {
array[i] = (byte) i;
}
return Unpooled.copiedBuffer(array);
}

private static class ResultCallbackTest extends ResultCallbackTemplate<ResultCallbackTest, InputStream> {
private InputStream stream;

@Override
public void onNext(InputStream stream) {
this.stream = stream;
}

public InputStream getInputStream() {
return stream;
}
}
}
Binary file added src/test/resources/testCopyFromArchive/binary.dat
Binary file not shown.