Skip to content

Commit

Permalink
Fix image inspect exit code on image not found error
Browse files Browse the repository at this point in the history
This change fixes image inspect command to error when image is not
found.

Signed-off-by: Austin Vazquez <[email protected]>
  • Loading branch information
austinvazquez committed Dec 29, 2024
1 parent 6f34536 commit aa5eb28
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
16 changes: 14 additions & 2 deletions cmd/nerdctl/image/image_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package image

import (
"encoding/json"
"errors"
"fmt"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -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),
},
},
}

Expand Down Expand Up @@ -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)},
})
}
},
Expand All @@ -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)},
})
}
},
Expand Down
6 changes: 4 additions & 2 deletions cmd/nerdctl/ipfs/ipfs_simple_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion pkg/cmd/image/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package image

import (
"context"
"errors"
"fmt"
"regexp"
"strings"
Expand Down Expand Up @@ -97,14 +98,15 @@ 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)
// We have to query per provided identifier, as we need to post-process results for the case name + digest
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
}

Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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
}

0 comments on commit aa5eb28

Please sign in to comment.