Skip to content

Commit a46b567

Browse files
committed
test(inspect): validate combined container and image inspect output
`nerdctl inspect <container> <image>` should return a single JSON array containing both the container and image inspect results. This unit test parses the output and compares it with the results of separate `inspect` commands to ensure correctness. Signed-off-by: Yuhang Wei <[email protected]>
1 parent 609fe3b commit a46b567

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

cmd/nerdctl/inspect/inspect_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,60 @@
1717
package inspect
1818

1919
import (
20+
"encoding/json"
2021
"testing"
2122

23+
"gotest.tools/v3/assert"
24+
25+
"github.com/containerd/nerdctl/mod/tigron/test"
26+
27+
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
2228
"github.com/containerd/nerdctl/v2/pkg/testutil"
29+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
2330
)
2431

2532
func TestMain(m *testing.M) {
2633
testutil.M(m)
2734
}
35+
36+
func TestInspectSimpleCase(t *testing.T) {
37+
nerdtest.Setup()
38+
testCase := &test.Case{
39+
Description: "inspect container and image return one single json array",
40+
Setup: func(data test.Data, helpers test.Helpers) {
41+
identifier := data.Identifier()
42+
helpers.Ensure("run", "-d", "--quiet", "--name", identifier, testutil.CommonImage, "sleep", nerdtest.Infinity)
43+
},
44+
Cleanup: func(data test.Data, helpers test.Helpers) {
45+
identifier := data.Identifier()
46+
helpers.Anyhow("rm", "-f", identifier)
47+
},
48+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
49+
return helpers.Command("inspect", testutil.CommonImage, data.Identifier())
50+
},
51+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
52+
return &test.Expected{
53+
Output: func(stdout string, info string, t *testing.T) {
54+
var inspectResult []json.RawMessage
55+
err := json.Unmarshal([]byte(stdout), &inspectResult)
56+
assert.NilError(t, err, "Unable to unmarshal output\n"+info)
57+
assert.Equal(t, len(inspectResult), 2, "Unexpectedly got multiple results\n"+info)
58+
59+
var dci dockercompat.Image
60+
err = json.Unmarshal(inspectResult[0], &dci)
61+
assert.NilError(t, err, "Unable to unmarshal output\n"+info)
62+
inspecti := nerdtest.InspectImage(helpers, testutil.CommonImage)
63+
assert.Equal(t, dci.ID, inspecti.ID, info)
64+
65+
var dcc dockercompat.Container
66+
err = json.Unmarshal(inspectResult[1], &dcc)
67+
assert.NilError(t, err, "Unable to unmarshal output\n"+info)
68+
inspectc := nerdtest.InspectContainer(helpers, data.Identifier())
69+
assert.Assert(t, dcc.ID == inspectc.ID, info)
70+
},
71+
}
72+
},
73+
}
74+
75+
testCase.Run(t)
76+
}

0 commit comments

Comments
 (0)