From c6c2e426e0ad801ec06ea9c4fbfd1a9a1bbe7c99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20M=C3=ADchal?= Date: Tue, 22 Jun 2021 15:47:36 +0200 Subject: [PATCH] cmd/list: Support images without names Some people create images manually. If such created images are recognize as toolbox images (they have the proper labels) but do not have a name/tag then 'toolbox list' will panic due to index being out of range. https://github.com/containers/toolbox/pull/800 --- src/cmd/list.go | 7 ++++++- test/system/102-list.bats | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/cmd/list.go b/src/cmd/list.go index 9b9c913..e9f676a 100644 --- a/src/cmd/list.go +++ b/src/cmd/list.go @@ -237,9 +237,14 @@ func listOutput(images []toolboxImage, containers []toolboxContainer) { fmt.Fprintf(writer, "%s\t%s\t%s\n", "IMAGE ID", "IMAGE NAME", "CREATED") for _, image := range images { + imageName := "" + if len(image.Names) != 0 { + imageName = image.Names[0] + } + fmt.Fprintf(writer, "%s\t%s\t%s\n", utils.ShortID(image.ID), - image.Names[0], + imageName, image.Created) } diff --git a/test/system/102-list.bats b/test/system/102-list.bats index eeff007..ea74645 100644 --- a/test/system/102-list.bats +++ b/test/system/102-list.bats @@ -82,3 +82,22 @@ teardown() { assert_output --partial "non-default-one" assert_output --partial "non-default-two" } + +@test "list: List an image without a name" { + echo -e "FROM scratch\n\nLABEL com.github.containers.toolbox=\"true\"" > "$BATS_TMPDIR"/Containerfile + + run $PODMAN build "$BATS_TMPDIR" + + assert_success + assert_line --index 0 "STEP 1: FROM scratch" + assert_line --index 1 "STEP 2: LABEL com.github.containers.toolbox=\"true\"" + assert_line --index 2 "STEP 3: COMMIT" + assert_line --index 3 --regexp "^--> [a-z0-9]*$" + + run $TOOLBOX list + + assert_success + assert_line --index 1 --partial "" + + rm -f "$BATS_TMPDIR"/Containerfile +}