Skip to content

Commit c031c5f

Browse files
feat: Add image info to inspect commands
Signed-off-by: Shubhranshu Mahapatra <[email protected]>
1 parent 6aa6b72 commit c031c5f

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package container
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
2122
"os"
2223
"os/exec"
@@ -29,6 +30,7 @@ import (
2930

3031
"github.com/containerd/nerdctl/mod/tigron/expect"
3132
"github.com/containerd/nerdctl/mod/tigron/test"
33+
"github.com/containerd/nerdctl/mod/tigron/tig"
3234

3335
"github.com/containerd/nerdctl/v2/pkg/infoutil"
3436
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
@@ -185,6 +187,35 @@ func TestContainerInspectContainsInternalLabel(t *testing.T) {
185187
assert.Equal(base.T, expectedLabelMount, labelMount)
186188
}
187189

190+
func TestContainerInspectConfigImage(t *testing.T) {
191+
nerdtest.Setup()
192+
193+
testCase := &test.Case{
194+
Description: "Container inspect contains Config.Image field",
195+
Setup: func(data test.Data, helpers test.Helpers) {
196+
helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.AlpineImage, "sleep", "infinity")
197+
},
198+
Cleanup: func(data test.Data, helpers test.Helpers) {
199+
helpers.Anyhow("rm", "-f", data.Identifier())
200+
},
201+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
202+
return helpers.Command("inspect", data.Identifier())
203+
},
204+
Expected: test.Expects(0, nil, func(stdout string, t tig.T) {
205+
var containers []dockercompat.Container
206+
err := json.Unmarshal([]byte(stdout), &containers)
207+
assert.NilError(t, err, "Unable to unmarshal output\n")
208+
assert.Equal(t, 1, len(containers), "Expected exactly one container in inspect output")
209+
210+
container := containers[0]
211+
assert.Assert(t, container.Config != nil, "container Config should not be nil")
212+
assert.Assert(t, container.Config.Image != "", "Config.Image should not be empty")
213+
}),
214+
}
215+
216+
testCase.Run(t)
217+
}
218+
188219
func TestContainerInspectState(t *testing.T) {
189220
t.Parallel()
190221
testContainer := testutil.Identifier(t)

cmd/nerdctl/image/image_inspect_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ func TestImageInspectSimpleCases(t *testing.T) {
6666
Command: test.Command("image", "inspect", testutil.CommonImage, "--format", "{{.ID}}"),
6767
Expected: test.Expects(0, nil, nil),
6868
},
69+
{
70+
Description: "Config.Image field is set",
71+
Command: test.Command("image", "inspect", testutil.CommonImage),
72+
Expected: test.Expects(0, nil, func(stdout string, t tig.T) {
73+
var dc []dockercompat.Image
74+
err := json.Unmarshal([]byte(stdout), &dc)
75+
assert.NilError(t, err, "Unable to unmarshal output\n")
76+
assert.Equal(t, 1, len(dc), "Unexpectedly got multiple results\n")
77+
assert.Assert(t, dc[0].Config != nil, "image Config should not be nil")
78+
assert.Assert(t, dc[0].Config.Image != "", "Config.Image should not be empty")
79+
}),
80+
},
6981
{
7082
Description: "Error for image not found",
7183
Command: test.Command("image", "inspect", "dne:latest", "dne2:latest"),

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ type Config struct {
215215
Cmd []string `json:",omitempty"` // Command to run when starting the container
216216
Healthcheck *healthcheck.Healthcheck `json:",omitempty"` // Healthcheck describes how to check the container is healthy
217217
// TODO: ArgsEscaped bool `json:",omitempty"` // True if command is already escaped (meaning treat as a command line) (Windows specific).
218-
// TODO: Image string // Name of the image as it was passed by the operator (e.g. could be symbolic)
218+
Image string `json:",omitempty"` // Name of the image as it was passed by the operator (e.g. could be symbolic)
219219
Volumes map[string]struct{} `json:",omitempty"` // List of volumes (mounts) used for the container
220220
WorkingDir string `json:",omitempty"` // Current directory (PWD) in the command will be launched
221221
Entrypoint []string `json:",omitempty"` // Entrypoint to run when starting the container
@@ -549,6 +549,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
549549
c.State = cs
550550
c.Config = &Config{
551551
Labels: n.Labels,
552+
Image: c.Image,
552553
}
553554
if n.Labels[labels.Hostname] != "" {
554555
hostname = n.Labels[labels.Hostname]
@@ -648,6 +649,7 @@ func ImageFromNative(nativeImage *native.Image) (*Image, error) {
648649
Entrypoint: imgOCI.Config.Entrypoint,
649650
Labels: imgOCI.Config.Labels,
650651
ExposedPorts: portSet,
652+
Image: nativeImage.Image.Name,
651653
}
652654

653655
// Add health check if present in labels

0 commit comments

Comments
 (0)