From 249ea1f220b2d953cedbbd3a818e9f67457a2da0 Mon Sep 17 00:00:00 2001 From: mpl Date: Wed, 5 Jun 2019 23:36:29 +0200 Subject: [PATCH] test/dockertest: allow the syntax for images --- pkg/test/dockertest/docker.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/pkg/test/dockertest/docker.go b/pkg/test/dockertest/docker.go index 780757967..fa6f69aab 100644 --- a/pkg/test/dockertest/docker.go +++ b/pkg/test/dockertest/docker.go @@ -20,6 +20,7 @@ Package dockertest contains helper functions for setting up and tearing down doc package dockertest // import "perkeep.org/pkg/test/dockertest" import ( + "bufio" "bytes" "compress/gzip" "database/sql" @@ -121,7 +122,6 @@ func loadCamliHubImage(image string) error { } return <-errc1 } - return nil } // haveDocker returns whether the "docker" command was found. @@ -130,12 +130,30 @@ func haveDocker() bool { return err == nil } +// haveImage reports whether we have the the given docker image. The name can +// either be of the , or , or form. func haveImage(name string) (ok bool, err error) { out, err := exec.Command("docker", "images", "--no-trunc").Output() if err != nil { return } - return bytes.Contains(out, []byte(name)), nil + fields := strings.Split(name, ":") + if len(fields) < 2 { + return bytes.Contains(out, []byte(name)), nil + } + tag := fields[1] + image := fields[0] + sc := bufio.NewScanner(bytes.NewBuffer(out)) + for sc.Scan() { + l := sc.Text() + if !strings.HasPrefix(l, image) { + continue + } + if strings.HasPrefix(strings.TrimSpace(strings.TrimPrefix(l, image)), tag) { + return true, nil + } + } + return false, sc.Err() } func run(args ...string) (containerID string, err error) {