From 04ec4a3c4db182444f0e165accb16fd6d447412e Mon Sep 17 00:00:00 2001 From: apostasie Date: Sun, 20 Oct 2024 19:20:53 -0700 Subject: [PATCH] Rewrite container_stats tests Signed-off-by: apostasie --- .../container/container_stats_linux_test.go | 75 ------------ cmd/nerdctl/container/container_stats_test.go | 108 ++++++++++++++++++ 2 files changed, 108 insertions(+), 75 deletions(-) delete mode 100644 cmd/nerdctl/container/container_stats_linux_test.go create mode 100644 cmd/nerdctl/container/container_stats_test.go diff --git a/cmd/nerdctl/container/container_stats_linux_test.go b/cmd/nerdctl/container/container_stats_linux_test.go deleted file mode 100644 index 42b2861aec4..00000000000 --- a/cmd/nerdctl/container/container_stats_linux_test.go +++ /dev/null @@ -1,75 +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 ( - "fmt" - "testing" - - "github.com/containerd/nerdctl/v2/pkg/infoutil" - "github.com/containerd/nerdctl/v2/pkg/rootlessutil" - "github.com/containerd/nerdctl/v2/pkg/testutil" -) - -func TestStats(t *testing.T) { - // this comment is for `nerdctl ps` but it also valid for `nerdctl stats` : - // 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)[:12] - exitedTestContainerName := fmt.Sprintf("%s-exited", testContainerName) - - base := testutil.NewBase(t) - defer base.Cmd("rm", "-f", testContainerName).Run() - defer base.Cmd("rm", "-f", exitedTestContainerName).Run() - base.Cmd("run", "--name", exitedTestContainerName, testutil.AlpineImage, "echo", "'exited'").AssertOK() - - base.Cmd("run", "-d", "--name", testContainerName, testutil.AlpineImage, "sleep", "10").AssertOK() - base.Cmd("stats", "--no-stream").AssertOutContains(testContainerName) - base.Cmd("stats", "--no-stream", testContainerName).AssertOK() - base.Cmd("container", "stats", "--no-stream").AssertOutContains(testContainerName) - base.Cmd("container", "stats", "--no-stream", testContainerName).AssertOK() -} - -func TestStatsMemoryLimitNotSet(t *testing.T) { - if rootlessutil.IsRootless() && infoutil.CgroupsVersion() == "1" { - t.Skip("test skipped for rootless containers on cgroup v1") - } - testContainerName := testutil.Identifier(t)[:12] - - base := testutil.NewBase(t) - defer base.Cmd("rm", "-f", testContainerName).Run() - - base.Cmd("run", "-d", "--name", testContainerName, testutil.AlpineImage, "sleep", "10").AssertOK() - base.Cmd("stats", "--no-stream").AssertOutNotContains("16EiB") - base.Cmd("stats", "--no-stream", testContainerName).AssertOK() -} - -func TestStatsMemoryLimitSet(t *testing.T) { - if rootlessutil.IsRootless() && infoutil.CgroupsVersion() == "1" { - t.Skip("test skipped for rootless containers on cgroup v1") - } - testContainerName := testutil.Identifier(t)[:12] - - base := testutil.NewBase(t) - defer base.Cmd("rm", "-f", testContainerName).Run() - - base.Cmd("run", "-d", "--name", testContainerName, "--memory", "1g", testutil.AlpineImage, "sleep", "10").AssertOK() - base.Cmd("stats", "--no-stream").AssertOutContains("1GiB") - base.Cmd("stats", "--no-stream", testContainerName).AssertOK() -} diff --git a/cmd/nerdctl/container/container_stats_test.go b/cmd/nerdctl/container/container_stats_test.go new file mode 100644 index 00000000000..e3c9b3a8c2c --- /dev/null +++ b/cmd/nerdctl/container/container_stats_test.go @@ -0,0 +1,108 @@ +/* + 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 TestStats(t *testing.T) { + testCase := nerdtest.Setup() + + // FIXME: does not seem to work on windows + testCase.Require = test.Not(test.Windows) + + if runtime.GOOS == "linux" { + // this comment is for `nerdctl ps` but it also valid for `nerdctl stats` : + // https://github.com/containerd/nerdctl/pull/223#issuecomment-851395178 + testCase.Require = test.Require( + testCase.Require, + nerdtest.CgroupsAccessible, + ) + } + + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { + helpers.Anyhow("rm", "-f", data.Identifier("container")) + helpers.Anyhow("rm", "-f", data.Identifier("memlimited")) + helpers.Anyhow("rm", "-f", data.Identifier("exited")) + } + + testCase.Setup = func(data test.Data, helpers test.Helpers) { + helpers.Ensure("run", "-d", "--name", data.Identifier("container"), testutil.CommonImage, "sleep", "10") + helpers.Ensure("run", "-d", "--name", data.Identifier("memlimited"), "--memory", "1g", testutil.CommonImage, "sleep", "10") + helpers.Ensure("run", "--name", data.Identifier("exited"), testutil.CommonImage, "echo", "'exited'") + data.Set("id", data.Identifier("container")) + } + + testCase.SubTests = []*test.Case{ + { + Description: "stats", + Command: test.Command("stats", "--no-stream", "--no-trunc"), + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { + return &test.Expected{ + Output: test.Contains(data.Get("id")), + } + }, + }, + { + Description: "container stats", + Command: test.Command("container", "stats", "--no-stream", "--no-trunc"), + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { + return &test.Expected{ + Output: test.Contains(data.Get("id")), + } + }, + }, + { + Description: "stats ID", + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { + return helpers.Command("stats", "--no-stream", data.Get("id")) + }, + Expected: test.Expects(0, nil, nil), + }, + { + Description: "container stats ID", + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { + return helpers.Command("container", "stats", "--no-stream", data.Get("id")) + }, + Expected: test.Expects(0, nil, nil), + }, + { + Description: "no mem limit set", + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { + return helpers.Command("stats", "--no-stream") + }, + // https://github.com/containerd/nerdctl/issues/1240 + // nerdctl used to print UINT64_MAX as the memory limit, so, ensure it does no more + Expected: test.Expects(0, nil, test.DoesNotContain("16EiB")), + }, + { + Description: "mem limit set", + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { + return helpers.Command("stats", "--no-stream") + }, + Expected: test.Expects(0, nil, test.Contains("1GiB")), + }, + } + + testCase.Run(t) +}