Skip to content

Commit aa5eb28

Browse files
committed
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 <[email protected]>
1 parent 6f34536 commit aa5eb28

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

cmd/nerdctl/image/image_inspect_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package image
1818

1919
import (
2020
"encoding/json"
21+
"errors"
22+
"fmt"
2123
"runtime"
2224
"strings"
2325
"testing"
@@ -61,6 +63,14 @@ func TestImageInspectSimpleCases(t *testing.T) {
6163
Command: test.Command("image", "inspect", testutil.CommonImage, "--format", "{{.ID}}"),
6264
Expected: test.Expects(0, nil, nil),
6365
},
66+
{
67+
Description: "Error for image not found",
68+
Command: test.Command("image", "inspect", "dne:latest", "dne2:latest"),
69+
Expected: test.Expects(1, []error{
70+
errors.New("no such image: dne:latest"),
71+
errors.New("no such image: dne2:latest"),
72+
}, nil),
73+
},
6474
},
6575
}
6676

@@ -171,7 +181,8 @@ func TestImageInspectDifferentValidReferencesForTheSameImage(t *testing.T) {
171181
for _, id := range []string{"doesnotexist", "doesnotexist:either", "busybox:bogustag"} {
172182
cmd := helpers.Command("image", "inspect", id+"@sha256:"+sha)
173183
cmd.Run(&test.Expected{
174-
Output: test.Equals(""),
184+
ExitCode: 1,
185+
Errors: []error{fmt.Errorf("no such image: %s@sha256:%s", id, sha)},
175186
})
176187
}
177188
},
@@ -192,7 +203,8 @@ func TestImageInspectDifferentValidReferencesForTheSameImage(t *testing.T) {
192203
for _, id := range []string{"∞∞∞∞∞∞∞∞∞∞", "busybox:∞∞∞∞∞∞∞∞∞∞"} {
193204
cmd := helpers.Command("image", "inspect", id)
194205
cmd.Run(&test.Expected{
195-
Output: test.Equals(""),
206+
ExitCode: 1,
207+
Errors: []error{fmt.Errorf("invalid reference format: %s", id)},
196208
})
197209
}
198210
},

cmd/nerdctl/ipfs/ipfs_simple_linux_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,13 @@ func TestIPFSSimple(t *testing.T) {
175175
helpers.Ensure("image", "encrypt", "--recipient=jwe:"+keyPair.Pub, data.Get(mainImageCIDKey), data.Identifier("encrypted"))
176176
cmd := helpers.Command("image", "inspect", "--mode=native", "--format={{len .Index.Manifests}}", data.Identifier("encrypted"))
177177
cmd.Run(&test.Expected{
178-
Output: test.Equals("1\n"),
178+
ExitCode: 1,
179+
Output: test.Equals("1\n"),
179180
})
180181
cmd = helpers.Command("image", "inspect", "--mode=native", "--format={{json (index .Manifest.Layers 0) }}", data.Identifier("encrypted"))
181182
cmd.Run(&test.Expected{
182-
Output: test.Contains("org.opencontainers.image.enc.keys.jwe"),
183+
ExitCode: 1,
184+
Output: test.Contains("org.opencontainers.image.enc.keys.jwe"),
183185
})
184186

185187
// Push the encrypted image and save the CID

pkg/cmd/image/inspect.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package image
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"regexp"
2324
"strings"
@@ -97,14 +98,15 @@ func Inspect(ctx context.Context, client *containerd.Client, identifiers []strin
9798
defer cancel()
9899

99100
// Will hold the final answers
101+
var errs []error
100102
var entries []interface{}
101103

102104
snapshotter := containerdutil.SnapshotService(client, options.GOptions.Snapshotter)
103105
// We have to query per provided identifier, as we need to post-process results for the case name + digest
104106
for _, identifier := range identifiers {
105107
candidateImageList, requestedName, requestedTag, err := inspectIdentifier(ctx, client, identifier)
106108
if err != nil {
107-
log.G(ctx).WithError(err).WithField("identifier", identifier).Error("failure calling inspect")
109+
errs = append(errs, fmt.Errorf("%w: %s", err, identifier))
108110
continue
109111
}
110112

@@ -185,6 +187,8 @@ func Inspect(ctx context.Context, client *containerd.Client, identifiers []strin
185187
// Store our image
186188
// foundImages[validatedDigest] = validatedImage
187189
entries = append(entries, validatedImage)
190+
} else {
191+
errs = append(errs, fmt.Errorf("no such image: %s", identifier))
188192
}
189193
}
190194

@@ -195,5 +199,9 @@ func Inspect(ctx context.Context, client *containerd.Client, identifiers []strin
195199
}
196200
}
197201

202+
if len(errs) > 0 {
203+
return fmt.Errorf("%d errors:\n%w", len(errs), errors.Join(errs...))
204+
}
205+
198206
return nil
199207
}

0 commit comments

Comments
 (0)