Skip to content

Commit

Permalink
Filter empty lines from "docker ls" response (#52081) (#52373)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
williamrandolph committed Feb 14, 2020
1 parent ba81008 commit 317d146
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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;
Expand Down Expand Up @@ -74,7 +75,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;
Expand Down

0 comments on commit 317d146

Please sign in to comment.