|
17 | 17 | package namespace |
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 | + "github.com/containerd/nerdctl/mod/tigron/tig" |
| 27 | + |
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 | +// TestNamespaceInspect verifies that `nerdctl namespace inspect <namespace-name>` |
| 37 | +// returns correctly populated JSON for a namespace. |
| 38 | +func TestNamespaceInspect(t *testing.T) { |
| 39 | + nerdtest.Setup() |
| 40 | + |
| 41 | + testCase := &test.Case{ |
| 42 | + Description: "namespace inspect returns populated metadata for a namespace", |
| 43 | + |
| 44 | + Setup: func(data test.Data, helpers test.Helpers) { |
| 45 | + ns := data.Identifier() |
| 46 | + |
| 47 | + helpers.Ensure("namespace", "create", ns) |
| 48 | + |
| 49 | + helpers.Ensure("--namespace", ns, "run", "-d", "--name", "test-cnt", testutil.CommonImage, "sleep", "3600") |
| 50 | + |
| 51 | + helpers.Ensure("--namespace", ns, "ps", "-a") |
| 52 | + }, |
| 53 | + |
| 54 | + Cleanup: func(data test.Data, helpers test.Helpers) { |
| 55 | + ns := data.Identifier() |
| 56 | + helpers.Anyhow("--namespace", ns, "rm", "-f", "test-cnt") |
| 57 | + helpers.Anyhow("--namespace", ns, "rmi", "-f", testutil.CommonImage) |
| 58 | + helpers.Anyhow("namespace", "remove", ns) |
| 59 | + }, |
| 60 | + |
| 61 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 62 | + return helpers.Command("namespace", "inspect", "--format", "json", data.Identifier()) |
| 63 | + }, |
| 64 | + |
| 65 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 66 | + return &test.Expected{ |
| 67 | + Output: func(stdout string, t tig.T) { |
| 68 | + type NamespaceResult struct { |
| 69 | + Name string `json:"Name"` |
| 70 | + Containers []struct { |
| 71 | + ID string `json:"id"` |
| 72 | + Name string `json:"name"` |
| 73 | + Image string `json:"image"` |
| 74 | + } `json:"Containers"` |
| 75 | + } |
| 76 | + |
| 77 | + var results []NamespaceResult |
| 78 | + |
| 79 | + err := json.Unmarshal([]byte(stdout), &results) |
| 80 | + if err != nil { |
| 81 | + var single NamespaceResult |
| 82 | + if errObj := json.Unmarshal([]byte(stdout), &single); errObj == nil { |
| 83 | + results = []NamespaceResult{single} |
| 84 | + } else { |
| 85 | + assert.NilError(t, err, "CLI output is neither a JSON array nor a valid object: %s", stdout) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + assert.Assert(t, len(results) > 0, "Expected at least one namespace result") |
| 90 | + |
| 91 | + var target *NamespaceResult |
| 92 | + for i := range results { |
| 93 | + if results[i].Name == data.Identifier() { |
| 94 | + target = &results[i] |
| 95 | + break |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + assert.Assert(t, target != nil, "Namespace %s not found in results", data.Identifier()) |
| 100 | + |
| 101 | + assert.Assert(t, len(target.Containers) > 0, "Containers list should not be empty for namespace %s", target.Name) |
| 102 | + |
| 103 | + c := target.Containers[0] |
| 104 | + assert.Assert(t, c.ID != "", "Container ID should not be empty") |
| 105 | + assert.Equal(t, c.Image, testutil.CommonImage, "Image mismatch") |
| 106 | + }, |
| 107 | + } |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + testCase.Run(t) |
| 112 | +} |
0 commit comments