Skip to content

Commit dea7eda

Browse files
authored
Passes 'docker load' error back to user. (GoogleContainerTools#334)
1 parent 2186553 commit dea7eda

2 files changed

Lines changed: 80 additions & 3 deletions

File tree

‎jib-core/src/main/java/com/google/cloud/tools/jib/docker/DockerClient.java‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,30 @@ public boolean isDockerInstalled() {
7575
* href="https://docs.docker.com/engine/reference/commandline/load/">https://docs.docker.com/engine/reference/commandline/load/</a>
7676
* @return stdout from {@code docker}
7777
*/
78-
public String load(Blob imageTarballBlob) throws IOException, InterruptedException {
78+
public String load(Blob imageTarballBlob) throws InterruptedException, IOException {
7979
// Runs 'docker load'.
8080
Process dockerProcess = docker("load");
8181

8282
try (OutputStream stdin = dockerProcess.getOutputStream()) {
83-
imageTarballBlob.writeTo(stdin);
83+
try {
84+
imageTarballBlob.writeTo(stdin);
85+
86+
} catch (IOException ex) {
87+
// Tries to read from stderr.
88+
String error;
89+
try (InputStreamReader stderr =
90+
new InputStreamReader(dockerProcess.getErrorStream(), StandardCharsets.UTF_8)) {
91+
error = CharStreams.toString(stderr);
92+
93+
} catch (IOException ignored) {
94+
// This ignores exceptions from reading stderr and throws the original exception from
95+
// writing to stdin.
96+
throw ex;
97+
}
98+
throw new IOException("'docker load' command failed with error: " + error, ex);
99+
}
84100
}
101+
85102
try (InputStreamReader stdout =
86103
new InputStreamReader(dockerProcess.getInputStream(), StandardCharsets.UTF_8)) {
87104
String output = CharStreams.toString(stdout);

‎jib-core/src/test/java/com/google/cloud/tools/jib/docker/DockerClientTest.java‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.ByteArrayInputStream;
2222
import java.io.ByteArrayOutputStream;
2323
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.io.OutputStream;
2426
import java.nio.charset.StandardCharsets;
2527
import java.nio.file.Paths;
2628
import org.junit.Assert;
@@ -85,7 +87,65 @@ public void testLoad() throws IOException, InterruptedException {
8587
}
8688

8789
@Test
88-
public void testLoad_fail() throws InterruptedException {
90+
public void testLoad_stdinFail() throws InterruptedException {
91+
DockerClient testDockerClient = new DockerClient(ignored -> mockProcessBuilder);
92+
93+
Mockito.when(mockProcess.getOutputStream())
94+
.thenReturn(
95+
new OutputStream() {
96+
97+
@Override
98+
public void write(int b) throws IOException {
99+
throw new IOException();
100+
}
101+
});
102+
Mockito.when(mockProcess.getErrorStream())
103+
.thenReturn(new ByteArrayInputStream("error".getBytes(StandardCharsets.UTF_8)));
104+
105+
try {
106+
testDockerClient.load(Blobs.from("jib"));
107+
Assert.fail("Write should have failed");
108+
109+
} catch (IOException ex) {
110+
Assert.assertEquals("'docker load' command failed with error: error", ex.getMessage());
111+
}
112+
}
113+
114+
@Test
115+
public void testLoad_stdinFail_stderrFail() throws InterruptedException {
116+
DockerClient testDockerClient = new DockerClient(ignored -> mockProcessBuilder);
117+
IOException expectedIOException = new IOException();
118+
119+
Mockito.when(mockProcess.getOutputStream())
120+
.thenReturn(
121+
new OutputStream() {
122+
123+
@Override
124+
public void write(int b) throws IOException {
125+
throw expectedIOException;
126+
}
127+
});
128+
Mockito.when(mockProcess.getErrorStream())
129+
.thenReturn(
130+
new InputStream() {
131+
132+
@Override
133+
public int read() throws IOException {
134+
throw new IOException();
135+
}
136+
});
137+
138+
try {
139+
testDockerClient.load(Blobs.from("jib"));
140+
Assert.fail("Write should have failed");
141+
142+
} catch (IOException ex) {
143+
Assert.assertTrue(expectedIOException == ex);
144+
}
145+
}
146+
147+
@Test
148+
public void testLoad_stdoutFail() throws InterruptedException {
89149
DockerClient testDockerClient = new DockerClient(ignored -> mockProcessBuilder);
90150
Mockito.when(mockProcess.waitFor()).thenReturn(1);
91151

0 commit comments

Comments
 (0)