Skip to content
Merged
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
44 changes: 44 additions & 0 deletions cmd/nerdctl/container/container_top_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package container

import (
"errors"
"runtime"
"testing"

"github.com/containerd/nerdctl/mod/tigron/expect"
"github.com/containerd/nerdctl/mod/tigron/require"
"github.com/containerd/nerdctl/mod/tigron/test"

Expand Down Expand Up @@ -69,6 +71,48 @@ func TestTop(t *testing.T) {
testCase.Run(t)
}

func TestTopStoppedContainer(t *testing.T) {
testCase := nerdtest.Setup()
testCase.Require = require.All(require.Linux, nerdtest.CgroupsAccessible)

testCase.Setup = func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--network", "none", "--name", data.Identifier(), testutil.CommonImage, "sleep", nerdtest.Infinity)
helpers.Ensure("stop", "--time", "1", data.Identifier())
}

testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
}

testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("top", data.Identifier())
}

testCase.Expected = test.Expects(1, []error{errors.New("is not running")}, nil)
testCase.Run(t)
}

func TestTopPausedContainer(t *testing.T) {
testCase := nerdtest.Setup()
testCase.Require = require.All(require.Linux, nerdtest.CgroupsAccessible)

testCase.Setup = func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--network", "none", "--name", data.Identifier(), testutil.CommonImage, "sleep", nerdtest.Infinity)
helpers.Ensure("pause", data.Identifier())
}

testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
}

testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("top", data.Identifier())
}

testCase.Expected = test.Expects(0, nil, expect.Contains("sleep"))
testCase.Run(t)
}

func TestTopHyperVContainer(t *testing.T) {

testCase := nerdtest.Setup()
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/container/top_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func containerTop(ctx context.Context, stdio io.Writer, client *containerd.Clien
return err
}

if status.Status != containerd.Running {
return nil
if status.Status != containerd.Running && status.Status != containerd.Paused {
return fmt.Errorf("container %s is not running (status: %s)", id, status.Status)
}

//TO DO handle restarting case: wait for container to restart and then launch top command
Expand Down
Loading