diff --git a/cmd/nerdctl/container/container_commit_test.go b/cmd/nerdctl/container/container_commit_test.go index 144180f79c2..cc3f1f45e0e 100644 --- a/cmd/nerdctl/container/container_commit_test.go +++ b/cmd/nerdctl/container/container_commit_test.go @@ -36,14 +36,7 @@ func TestCommit(t *testing.T) { helpers.Anyhow("rmi", "-f", data.Identifier()) }, Setup: func(data test.Data, helpers test.Helpers) { - // FIXME: short of pulling first, docker will fail to start the container. - // Debugging shows container exited immediately. Nothing in the container logs. Not much in journalctl. - // It is not clear what is happening. - if nerdtest.IsDocker() { - helpers.Ensure("pull", testutil.CommonImage) - } helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage, "sleep", "infinity") - nerdtest.EnsureContainerStarted(helpers, data.Identifier()) helpers.Ensure("exec", data.Identifier(), "sh", "-euxc", `echo hello-test-commit > /foo`) }, Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { diff --git a/cmd/nerdctl/container/container_top_test.go b/cmd/nerdctl/container/container_top_test.go new file mode 100644 index 00000000000..5f7a6e0a4b5 --- /dev/null +++ b/cmd/nerdctl/container/container_top_test.go @@ -0,0 +1,92 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package container + +import ( + "runtime" + "testing" + + "github.com/containerd/nerdctl/v2/pkg/testutil" + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" + "github.com/containerd/nerdctl/v2/pkg/testutil/test" +) + +func TestTop(t *testing.T) { + testCase := nerdtest.Setup() + + //more details https://github.com/containerd/nerdctl/pull/223#issuecomment-851395178 + if runtime.GOOS == "linux" { + testCase.Require = nerdtest.CgroupsAccessible + } + + testCase.Setup = func(data test.Data, helpers test.Helpers) { + // FIXME: busybox 1.36 on windows still appears to not support sleep inf. Unclear why. + helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage, "sleep", "10") + data.Set("cID", data.Identifier()) + } + + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { + helpers.Anyhow("rm", "-f", data.Identifier()) + } + + testCase.SubTests = []*test.Case{ + { + Description: "with o pid,user,cmd", + // Docker does not support top -o + Require: test.Not(nerdtest.Docker), + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { + return helpers.Command("top", data.Get("cID"), "-o", "pid,user,cmd") + }, + + Expected: test.Expects(0, nil, nil), + }, + { + Description: "simple", + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { + return helpers.Command("top", data.Get("cID")) + }, + + Expected: test.Expects(0, nil, nil), + }, + } + + testCase.Run(t) +} + +func TestTopHyperVContainer(t *testing.T) { + + testCase := nerdtest.Setup() + + testCase.Require = nerdtest.HyperV + + testCase.Setup = func(data test.Data, helpers test.Helpers) { + // FIXME: busybox 1.36 on windows still appears to not support sleep inf. Unclear why. + helpers.Ensure("run", "--isolation", "hyperv", "-d", "--name", data.Identifier("container"), testutil.CommonImage, "sleep", "10") + } + + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { + helpers.Anyhow("rm", "-f", data.Identifier("container")) + } + + testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand { + return helpers.Command("top", data.Identifier("container")) + } + + testCase.Expected = test.Expects(0, nil, nil) + + testCase.Run(t) +} diff --git a/cmd/nerdctl/container/container_top_unix_test.go b/cmd/nerdctl/container/container_top_unix_test.go deleted file mode 100644 index d68d42302ee..00000000000 --- a/cmd/nerdctl/container/container_top_unix_test.go +++ /dev/null @@ -1,43 +0,0 @@ -//go:build unix - -/* - Copyright The containerd Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package container - -import ( - "testing" - - "github.com/containerd/nerdctl/v2/pkg/infoutil" - "github.com/containerd/nerdctl/v2/pkg/rootlessutil" - "github.com/containerd/nerdctl/v2/pkg/testutil" -) - -func TestTop(t *testing.T) { - t.Parallel() - //more details https://github.com/containerd/nerdctl/pull/223#issuecomment-851395178 - if rootlessutil.IsRootless() && infoutil.CgroupsVersion() == "1" { - t.Skip("test skipped for rootless containers on cgroup v1") - } - testContainerName := testutil.Identifier(t) - - base := testutil.NewBase(t) - defer base.Cmd("rm", "-f", testContainerName).Run() - - base.Cmd("run", "-d", "--name", testContainerName, testutil.AlpineImage, "sleep", "5").AssertOK() - base.Cmd("top", testContainerName, "-o", "pid,user,cmd").AssertOK() - -} diff --git a/cmd/nerdctl/container/container_top_windows_test.go b/cmd/nerdctl/container/container_top_windows_test.go deleted file mode 100644 index 690e52d50f4..00000000000 --- a/cmd/nerdctl/container/container_top_windows_test.go +++ /dev/null @@ -1,47 +0,0 @@ -/* - Copyright The containerd Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package container - -import ( - "testing" - - "github.com/containerd/nerdctl/v2/pkg/testutil" -) - -func TestTopProcessContainer(t *testing.T) { - testContainerName := testutil.Identifier(t) - - base := testutil.NewBase(t) - defer base.Cmd("rm", "-f", testContainerName).Run() - - base.Cmd("run", "-d", "--name", testContainerName, testutil.WindowsNano, "sleep", "5").AssertOK() - base.Cmd("top", testContainerName).AssertOK() -} - -func TestTopHyperVContainer(t *testing.T) { - if !testutil.HyperVSupported() { - t.Skip("HyperV is not enabled, skipping test") - } - - testContainerName := testutil.Identifier(t) - - base := testutil.NewBase(t) - defer base.Cmd("rm", "-f", testContainerName).Run() - - base.Cmd("run", "--isolation", "hyperv", "-d", "--name", testContainerName, testutil.WindowsNano, "sleep", "5").AssertOK() - base.Cmd("top", testContainerName).AssertOK() -}