Skip to content

Commit 86aa958

Browse files
author
Marcus Linke
committed
Merge branch 'tejksat-fixNettyResponseStreamSkipsFFBytes't push origin master
2 parents b3f8ddf + 3bde2a9 commit 86aa958

File tree

6 files changed

+142
-1
lines changed

6 files changed

+142
-1
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<hamcrest.library.version>1.3</hamcrest.library.version>
6868
<hamcrest.jpa-matchers>1.6</hamcrest.jpa-matchers>
6969
<lambdaj.version>2.3.3</lambdaj.version>
70+
<mockito.version>1.10.19</mockito.version>
7071

7172

7273
<maven-jar-plugin.version>2.2</maven-jar-plugin.version>
@@ -195,6 +196,13 @@
195196
<scope>test</scope>
196197
</dependency>
197198

199+
<dependency>
200+
<groupId>org.mockito</groupId>
201+
<artifactId>mockito-core</artifactId>
202+
<version>${mockito.version}</version>
203+
<scope>test</scope>
204+
</dependency>
205+
198206
<dependency>
199207
<groupId>com.google.code.findbugs</groupId>
200208
<artifactId>annotations</artifactId>

src/main/java/com/github/dockerjava/netty/handler/HttpResponseStreamHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public int read() throws IOException {
8181
}
8282

8383
if (current != null && current.readableBytes() > 0) {
84-
return current.readByte();
84+
return current.readByte() & 0xff;
8585
} else {
8686
return read();
8787
}

src/test/java/com/github/dockerjava/core/command/CopyArchiveFromContainerCmdImplTest.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;
@@ -17,6 +24,7 @@
1724
import com.github.dockerjava.api.exception.NotFoundException;
1825
import com.github.dockerjava.api.command.CreateContainerResponse;
1926
import com.github.dockerjava.client.AbstractDockerClientTest;
27+
import com.github.dockerjava.core.util.CompressArchiveUtil;
2028

2129
@Test(groups = "integration")
2230
public class CopyArchiveFromContainerCmdImplTest extends AbstractDockerClientTest {
@@ -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
}

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
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.github.dockerjava.netty.handler;
2+
3+
import static org.testng.Assert.assertTrue;
4+
5+
import java.io.InputStream;
6+
7+
import io.netty.buffer.ByteBuf;
8+
import io.netty.buffer.ByteBufInputStream;
9+
import io.netty.buffer.Unpooled;
10+
import io.netty.channel.ChannelHandlerContext;
11+
import org.apache.commons.io.IOUtils;
12+
import org.mockito.Mockito;
13+
import org.testng.annotations.Test;
14+
15+
import com.github.dockerjava.core.async.ResultCallbackTemplate;
16+
17+
/**
18+
* @author Alexander Koshevoy
19+
*/
20+
public class HttpResponseStreamHandlerTest {
21+
@Test
22+
public void testNoBytesSkipped() throws Exception {
23+
ResultCallbackTest callback = new ResultCallbackTest();
24+
HttpResponseStreamHandler streamHandler = new HttpResponseStreamHandler(callback);
25+
ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
26+
ByteBuf buffer = generateByteBuf();
27+
streamHandler.channelRead0(ctx, buffer);
28+
streamHandler.channelReadComplete(ctx);
29+
30+
assertTrue(IOUtils.contentEquals(callback.getInputStream(), new ByteBufInputStream(buffer)));
31+
}
32+
33+
private ByteBuf generateByteBuf() {
34+
byte[] array = new byte[256];
35+
for (int i = 0; i < array.length; i++) {
36+
array[i] = (byte) i;
37+
}
38+
return Unpooled.copiedBuffer(array);
39+
}
40+
41+
private static class ResultCallbackTest extends ResultCallbackTemplate<ResultCallbackTest, InputStream> {
42+
private InputStream stream;
43+
44+
@Override
45+
public void onNext(InputStream stream) {
46+
this.stream = stream;
47+
}
48+
49+
public InputStream getInputStream() {
50+
return stream;
51+
}
52+
}
53+
}
288 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)