From 2311bc62f7026a9dbc1b563475bfb7d6509c573d Mon Sep 17 00:00:00 2001 From: William Brafford Date: Fri, 14 Feb 2020 11:39:13 -0500 Subject: [PATCH] Filter empty lines from "docker ls" response (#52081) * Filter empty lines from docker ls response In order to cut down on test time, our docker/vagrant tests build the docker image outside of the vagrant VM. When we get around to launching the Vagrant VM, we mount that already-built docker image to a known location. At that point, we need to load the docker image. But we only want to load it once. As we're running tests, we use "docker ls" to check whether the local image is loaded for use. Empty output from the particular ls invocation means no image is loaded. There was a bug in how we checked this. In Java, splitting an empty string will yield an array containing one empty string. So when we're counting the output from the docker ls command, we need to filter out empty lines in order to proceed to loading the image for docker tests. --- .../java/org/elasticsearch/packaging/util/Docker.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/qa/os/src/test/java/org/elasticsearch/packaging/util/Docker.java b/qa/os/src/test/java/org/elasticsearch/packaging/util/Docker.java index d43c2219a03..08f230b2c38 100644 --- a/qa/os/src/test/java/org/elasticsearch/packaging/util/Docker.java +++ b/qa/os/src/test/java/org/elasticsearch/packaging/util/Docker.java @@ -31,6 +31,7 @@ import java.nio.file.Paths; import java.nio.file.attribute.PosixFileAttributes; import java.nio.file.attribute.PosixFilePermission; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -76,7 +77,12 @@ public class Docker { * @param distribution details about the docker image to potentially load. */ public static void ensureImageIsLoaded(Distribution distribution) { - final long count = sh.run("docker image ls --format '{{.Repository}}' " + distribution.flavor.name).stdout.split("\n").length; + Shell.Result result = sh.run("docker image ls --format '{{.Repository}}' " + distribution.flavor.name); + + final long count = Arrays.stream(result.stdout.split("\n")) + .map(String::trim) + .filter(s -> s.isEmpty() == false) + .count(); if (count != 0) { return;