From aa5eb28ade74afcd6ff1cd29ae3b937d5fd62652 Mon Sep 17 00:00:00 2001 From: Austin Vazquez Date: Thu, 26 Dec 2024 22:40:31 -0700 Subject: [PATCH] Fix image inspect exit code on image not found error This change fixes image inspect command to error when image is not found. Signed-off-by: Austin Vazquez --- cmd/nerdctl/image/image_inspect_test.go | 16 ++++++++++++++-- cmd/nerdctl/ipfs/ipfs_simple_linux_test.go | 6 ++++-- pkg/cmd/image/inspect.go | 10 +++++++++- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/cmd/nerdctl/image/image_inspect_test.go b/cmd/nerdctl/image/image_inspect_test.go index 4ac86996bd8..808fd199d46 100644 --- a/cmd/nerdctl/image/image_inspect_test.go +++ b/cmd/nerdctl/image/image_inspect_test.go @@ -18,6 +18,8 @@ package image import ( "encoding/json" + "errors" + "fmt" "runtime" "strings" "testing" @@ -61,6 +63,14 @@ func TestImageInspectSimpleCases(t *testing.T) { Command: test.Command("image", "inspect", testutil.CommonImage, "--format", "{{.ID}}"), Expected: test.Expects(0, nil, nil), }, + { + Description: "Error for image not found", + Command: test.Command("image", "inspect", "dne:latest", "dne2:latest"), + Expected: test.Expects(1, []error{ + errors.New("no such image: dne:latest"), + errors.New("no such image: dne2:latest"), + }, nil), + }, }, } @@ -171,7 +181,8 @@ func TestImageInspectDifferentValidReferencesForTheSameImage(t *testing.T) { for _, id := range []string{"doesnotexist", "doesnotexist:either", "busybox:bogustag"} { cmd := helpers.Command("image", "inspect", id+"@sha256:"+sha) cmd.Run(&test.Expected{ - Output: test.Equals(""), + ExitCode: 1, + Errors: []error{fmt.Errorf("no such image: %s@sha256:%s", id, sha)}, }) } }, @@ -192,7 +203,8 @@ func TestImageInspectDifferentValidReferencesForTheSameImage(t *testing.T) { for _, id := range []string{"∞∞∞∞∞∞∞∞∞∞", "busybox:∞∞∞∞∞∞∞∞∞∞"} { cmd := helpers.Command("image", "inspect", id) cmd.Run(&test.Expected{ - Output: test.Equals(""), + ExitCode: 1, + Errors: []error{fmt.Errorf("invalid reference format: %s", id)}, }) } }, diff --git a/cmd/nerdctl/ipfs/ipfs_simple_linux_test.go b/cmd/nerdctl/ipfs/ipfs_simple_linux_test.go index f2d59a15618..48bc874049b 100644 --- a/cmd/nerdctl/ipfs/ipfs_simple_linux_test.go +++ b/cmd/nerdctl/ipfs/ipfs_simple_linux_test.go @@ -175,11 +175,13 @@ func TestIPFSSimple(t *testing.T) { helpers.Ensure("image", "encrypt", "--recipient=jwe:"+keyPair.Pub, data.Get(mainImageCIDKey), data.Identifier("encrypted")) cmd := helpers.Command("image", "inspect", "--mode=native", "--format={{len .Index.Manifests}}", data.Identifier("encrypted")) cmd.Run(&test.Expected{ - Output: test.Equals("1\n"), + ExitCode: 1, + Output: test.Equals("1\n"), }) cmd = helpers.Command("image", "inspect", "--mode=native", "--format={{json (index .Manifest.Layers 0) }}", data.Identifier("encrypted")) cmd.Run(&test.Expected{ - Output: test.Contains("org.opencontainers.image.enc.keys.jwe"), + ExitCode: 1, + Output: test.Contains("org.opencontainers.image.enc.keys.jwe"), }) // Push the encrypted image and save the CID diff --git a/pkg/cmd/image/inspect.go b/pkg/cmd/image/inspect.go index 1627cc4b14d..ec96ddb7cba 100644 --- a/pkg/cmd/image/inspect.go +++ b/pkg/cmd/image/inspect.go @@ -18,6 +18,7 @@ package image import ( "context" + "errors" "fmt" "regexp" "strings" @@ -97,6 +98,7 @@ func Inspect(ctx context.Context, client *containerd.Client, identifiers []strin defer cancel() // Will hold the final answers + var errs []error var entries []interface{} snapshotter := containerdutil.SnapshotService(client, options.GOptions.Snapshotter) @@ -104,7 +106,7 @@ func Inspect(ctx context.Context, client *containerd.Client, identifiers []strin for _, identifier := range identifiers { candidateImageList, requestedName, requestedTag, err := inspectIdentifier(ctx, client, identifier) if err != nil { - log.G(ctx).WithError(err).WithField("identifier", identifier).Error("failure calling inspect") + errs = append(errs, fmt.Errorf("%w: %s", err, identifier)) continue } @@ -185,6 +187,8 @@ func Inspect(ctx context.Context, client *containerd.Client, identifiers []strin // Store our image // foundImages[validatedDigest] = validatedImage entries = append(entries, validatedImage) + } else { + errs = append(errs, fmt.Errorf("no such image: %s", identifier)) } } @@ -195,5 +199,9 @@ func Inspect(ctx context.Context, client *containerd.Client, identifiers []strin } } + if len(errs) > 0 { + return fmt.Errorf("%d errors:\n%w", len(errs), errors.Join(errs...)) + } + return nil }