Skip to content

Commit 31ff7f1

Browse files
committed
Tolerate Successfully built being found in response other than last
Different versions of Docker produce different responses when building and tagging an image. On CI, a response with a stream like "Successfully built 185991ffe24a" followed by a response with a stream like "Successfully tagged spring-boot-it/centos:6.9-a23bced6" is received. By default, for the building of an image to be considered successful, the Docker Java client requires the stream for the last response item to contain "Successfully built". This means that, on the CI server, it incorrectly believes that the building of the tagged image has failed. This commit uses a custom BuildImageResultCallback that doesn't require the last response to be the one that has a stream containing "Successfully built". Instead, it looks back through the error-free responses (newest to oldest) looking for one with a stream containing "Successfully built".
1 parent 0270cca commit 31ff7f1

File tree

1 file changed

+52
-1
lines changed
  • spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/java/org/springframework/boot/launchscript

1 file changed

+52
-1
lines changed

spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/java/org/springframework/boot/launchscript/SysVinitLaunchScriptIT.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.InputStream;
2323
import java.util.ArrayList;
2424
import java.util.Arrays;
25+
import java.util.Collections;
2526
import java.util.List;
2627
import java.util.concurrent.TimeUnit;
2728
import java.util.regex.Pattern;
@@ -32,7 +33,9 @@
3233
import javax.ws.rs.client.WebTarget;
3334

3435
import com.github.dockerjava.api.DockerClient;
36+
import com.github.dockerjava.api.DockerClientException;
3537
import com.github.dockerjava.api.command.DockerCmd;
38+
import com.github.dockerjava.api.model.BuildResponseItem;
3639
import com.github.dockerjava.api.model.Frame;
3740
import com.github.dockerjava.core.CompressArchiveUtil;
3841
import com.github.dockerjava.core.DockerClientBuilder;
@@ -257,10 +260,58 @@ private DockerClient createClient() {
257260
}
258261

259262
private String buildImage(DockerClient docker) {
260-
BuildImageResultCallback resultCallback = new BuildImageResultCallback();
261263
String dockerfile = "src/test/resources/conf/" + this.os + "/" + this.version
262264
+ "/Dockerfile";
263265
String tag = "spring-boot-it/" + this.os.toLowerCase() + ":" + this.version;
266+
BuildImageResultCallback resultCallback = new BuildImageResultCallback() {
267+
268+
private List<BuildResponseItem> items = new ArrayList<BuildResponseItem>();
269+
270+
@Override
271+
public void onNext(BuildResponseItem item) {
272+
super.onNext(item);
273+
this.items.add(item);
274+
}
275+
276+
@Override
277+
public String awaitImageId() {
278+
try {
279+
awaitCompletion();
280+
}
281+
catch (InterruptedException ex) {
282+
throw new DockerClientException(
283+
"Interrupted while waiting for image id", ex);
284+
}
285+
return getImageId();
286+
}
287+
288+
@SuppressWarnings("deprecation")
289+
private String getImageId() {
290+
if (this.items.isEmpty()) {
291+
throw new DockerClientException("Could not build image");
292+
}
293+
String imageId = extractImageId();
294+
if (imageId == null) {
295+
throw new DockerClientException("Could not build image: "
296+
+ this.items.get(this.items.size() - 1).getError());
297+
}
298+
return imageId;
299+
}
300+
301+
private String extractImageId() {
302+
Collections.reverse(this.items);
303+
for (BuildResponseItem item : this.items) {
304+
if (item.isErrorIndicated() || item.getStream() == null) {
305+
return null;
306+
}
307+
if (item.getStream().contains("Successfully built")) {
308+
return item.getStream().replace("Successfully built", "").trim();
309+
}
310+
}
311+
return null;
312+
}
313+
314+
};
264315
docker.buildImageCmd(new File(dockerfile)).withTag(tag).exec(resultCallback);
265316
String imageId = resultCallback.awaitImageId();
266317
return imageId;

0 commit comments

Comments
 (0)