Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions cmd/nerdctl/namespace/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,96 @@
package namespace

import (
"encoding/json"
"testing"

"gotest.tools/v3/assert"

"github.com/containerd/nerdctl/mod/tigron/test"
"github.com/containerd/nerdctl/mod/tigron/tig"

"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
)

func TestMain(m *testing.M) {
testutil.M(m)
}

// TestNamespaceInspect verifies that `nerdctl namespace inspect <namespace-name>`
// returns correctly populated JSON for a namespace.
func TestNamespaceInspect(t *testing.T) {
nerdtest.Setup()

testCase := &test.Case{
Description: "namespace inspect returns populated metadata for a namespace",

Setup: func(data test.Data, helpers test.Helpers) {
ns := data.Identifier()

helpers.Ensure("namespace", "create", ns)

helpers.Ensure("--namespace", ns, "run", "-d", "--name", "test-cnt", testutil.CommonImage, "sleep", "3600")

helpers.Ensure("--namespace", ns, "ps", "-a")
},

Cleanup: func(data test.Data, helpers test.Helpers) {
ns := data.Identifier()
helpers.Anyhow("--namespace", ns, "rm", "-f", "test-cnt")
helpers.Anyhow("--namespace", ns, "rmi", "-f", testutil.CommonImage)
helpers.Anyhow("namespace", "remove", ns)
},

Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("namespace", "inspect", "--format", "json", data.Identifier())
},

Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: func(stdout string, t tig.T) {
type NamespaceResult struct {
Name string `json:"Name"`
Containers []struct {
ID string `json:"id"`
Name string `json:"name"`
Image string `json:"image"`
} `json:"Containers"`
}

var results []NamespaceResult

err := json.Unmarshal([]byte(stdout), &results)
if err != nil {
var single NamespaceResult
if errObj := json.Unmarshal([]byte(stdout), &single); errObj == nil {
results = []NamespaceResult{single}
} else {
assert.NilError(t, err, "CLI output is neither a JSON array nor a valid object: %s", stdout)
}
}

assert.Assert(t, len(results) > 0, "Expected at least one namespace result")

var target *NamespaceResult
for i := range results {
if results[i].Name == data.Identifier() {
target = &results[i]
break
}
}

assert.Assert(t, target != nil, "Namespace %s not found in results", data.Identifier())

assert.Assert(t, len(target.Containers) > 0, "Containers list should not be empty for namespace %s", target.Name)

c := target.Containers[0]
assert.Assert(t, c.ID != "", "Container ID should not be empty")
assert.Equal(t, c.Image, testutil.CommonImage, "Image mismatch")
},
}
},
}

testCase.Run(t)
}
23 changes: 21 additions & 2 deletions pkg/cmd/namespace/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,28 @@ func Inspect(ctx context.Context, client *containerd.Client, inspectedNamespaces
if err != nil {
return err
}
containers, err := client.Containers(ctx)
if err != nil {
return err
}
var containersList []map[string]string
for _, container := range containers {
containerInfo, err := container.Info(ctx)
if err != nil {
return err
}
containersList = append(containersList,
map[string]string{
"id": containerInfo.ID,
"name": containerInfo.Runtime.Name,
"image": containerInfo.Image,
},
)
}
nsInspect := native.Namespace{
Name: ns,
Labels: &labels,
Name: ns,
Labels: &labels,
Containers: containersList,
}
result = append(result, nsInspect)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/inspecttypes/native/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package native

type Namespace struct {
Name string `json:"Name"`
Labels *map[string]string `json:"Labels,omitempty"`
Name string `json:"Name"`
Labels *map[string]string `json:"Labels,omitempty"`
Containers []map[string]string `json:"Containers,omitempty"`
}
Loading