Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ import (
"github.com/ngageoint/seed-common/util"
)

//DockerList - Simplified version of dockerlist - relies on name filter of
// docker images command to search for images ending in '-seed'
// DockerList - Simplified version of dockerlist - relies on name filter of
//
// docker images command to search for images ending in '-seed'
func DockerList() (string, error) {
var errs, out bytes.Buffer
var cmd *exec.Cmd
reference := util.DockerVersionHasReferenceFilter()
var buildArgs, dockerCommand = cliutil.DockerCommandArgsInit()
if reference {
buildArgs = append(buildArgs, "images", "--filter=reference=*-seed*")
buildArgs = append(buildArgs,
"images",
"--filter=reference=*-seed*",
"--filter=reference=*/*-seed*",
"--filter=reference=*/*/*-seed*",
)
cmd = exec.Command(dockerCommand, buildArgs...)
} else {
buildArgs = append(buildArgs, "images")
Expand Down Expand Up @@ -70,7 +76,7 @@ func DockerList() (string, error) {
return out.String(), nil
}

//PrintListUsage prints the seed list usage information, then exits the program
// PrintListUsage prints the seed list usage information, then exits the program
func PrintListUsage() {
util.PrintUtil("\nUsage:\tseed list\n")
util.PrintUtil("\nLists all Seed compliant docker images residing on the local system.\n")
Expand Down
64 changes: 64 additions & 0 deletions commands/list_reference_filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package commands

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)

func TestDockerListIncludesPulledImagesWithReferenceFilter(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "seed-list-reference-filter")
if err != nil {
t.Fatalf("unable to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)

dockerScript := `#!/bin/sh
if [ "$1" = "info" ]; then
exit 0
fi

if [ "$1" = "version" ]; then
echo "20.10.0"
exit 0
fi

if [ "$1" = "images" ]; then
for arg in "$@"; do
if [ "$arg" = "--filter=reference=*/*/*-seed*" ]; then
echo "REPOSITORY TAG"
echo "my-registry/my-org/my-job-1.0.0-seed 1.0.0"
exit 0
fi
done

echo "REPOSITORY TAG"
exit 0
fi

echo "unexpected docker command: $*" >&2
exit 1
`

dockerPath := filepath.Join(tmpDir, "docker")
if err = ioutil.WriteFile(dockerPath, []byte(dockerScript), 0755); err != nil {
t.Fatalf("unable to write fake docker script: %v", err)
}

originalPath := os.Getenv("PATH")
if err = os.Setenv("PATH", tmpDir+string(os.PathListSeparator)+originalPath); err != nil {
t.Fatalf("unable to set PATH: %v", err)
}
defer os.Setenv("PATH", originalPath)

output, err := DockerList()
if err != nil {
t.Fatalf("DockerList returned an error: %v", err)
}

if !strings.Contains(output, "my-registry/my-org/my-job-1.0.0-seed") {
t.Fatalf("DockerList output did not include pulled image: %s", output)
}
}