|
17 | 17 | package inspect
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "encoding/json" |
20 | 21 | "testing"
|
21 | 22 |
|
| 23 | + "gotest.tools/v3/assert" |
| 24 | + |
| 25 | + "github.com/containerd/nerdctl/mod/tigron/test" |
| 26 | + |
| 27 | + "github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat" |
22 | 28 | "github.com/containerd/nerdctl/v2/pkg/testutil"
|
| 29 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
23 | 30 | )
|
24 | 31 |
|
25 | 32 | func TestMain(m *testing.M) {
|
26 | 33 | testutil.M(m)
|
27 | 34 | }
|
| 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