|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package container |
| 18 | + |
| 19 | +import ( |
| 20 | + "runtime" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 24 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 25 | + "github.com/containerd/nerdctl/v2/pkg/testutil/test" |
| 26 | +) |
| 27 | + |
| 28 | +func TestStats(t *testing.T) { |
| 29 | + testCase := nerdtest.Setup() |
| 30 | + |
| 31 | + // FIXME: does not seem to work on windows |
| 32 | + testCase.Require = test.Not(test.Windows) |
| 33 | + |
| 34 | + if runtime.GOOS == "linux" { |
| 35 | + // this comment is for `nerdctl ps` but it also valid for `nerdctl stats` : |
| 36 | + // https://github.com/containerd/nerdctl/pull/223#issuecomment-851395178 |
| 37 | + testCase.Require = test.Require( |
| 38 | + testCase.Require, |
| 39 | + nerdtest.CgroupsAccessible, |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { |
| 44 | + helpers.Anyhow("rm", "-f", data.Identifier("container")) |
| 45 | + helpers.Anyhow("rm", "-f", data.Identifier("memlimited")) |
| 46 | + helpers.Anyhow("rm", "-f", data.Identifier("exited")) |
| 47 | + } |
| 48 | + |
| 49 | + testCase.Setup = func(data test.Data, helpers test.Helpers) { |
| 50 | + helpers.Ensure("run", "-d", "--name", data.Identifier("container"), testutil.CommonImage, "sleep", "10") |
| 51 | + helpers.Ensure("run", "-d", "--name", data.Identifier("memlimited"), "--memory", "1g", testutil.CommonImage, "sleep", "10") |
| 52 | + helpers.Ensure("run", "--name", data.Identifier("exited"), testutil.CommonImage, "echo", "'exited'") |
| 53 | + data.Set("id", data.Identifier("container")) |
| 54 | + } |
| 55 | + |
| 56 | + testCase.SubTests = []*test.Case{ |
| 57 | + { |
| 58 | + Description: "stats", |
| 59 | + Command: test.Command("stats", "--no-stream", "--no-trunc"), |
| 60 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 61 | + return &test.Expected{ |
| 62 | + Output: test.Contains(data.Get("id")), |
| 63 | + } |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + Description: "container stats", |
| 68 | + Command: test.Command("container", "stats", "--no-stream", "--no-trunc"), |
| 69 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 70 | + return &test.Expected{ |
| 71 | + Output: test.Contains(data.Get("id")), |
| 72 | + } |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + Description: "stats ID", |
| 77 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 78 | + return helpers.Command("stats", "--no-stream", data.Get("id")) |
| 79 | + }, |
| 80 | + Expected: test.Expects(0, nil, nil), |
| 81 | + }, |
| 82 | + { |
| 83 | + Description: "container stats ID", |
| 84 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 85 | + return helpers.Command("container", "stats", "--no-stream", data.Get("id")) |
| 86 | + }, |
| 87 | + Expected: test.Expects(0, nil, nil), |
| 88 | + }, |
| 89 | + { |
| 90 | + Description: "no mem limit set", |
| 91 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 92 | + return helpers.Command("stats", "--no-stream") |
| 93 | + }, |
| 94 | + // https://github.com/containerd/nerdctl/issues/1240 |
| 95 | + // nerdctl used to print UINT64_MAX as the memory limit, so, ensure it does no more |
| 96 | + Expected: test.Expects(0, nil, test.DoesNotContain("16EiB")), |
| 97 | + }, |
| 98 | + { |
| 99 | + Description: "mem limit set", |
| 100 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 101 | + return helpers.Command("stats", "--no-stream") |
| 102 | + }, |
| 103 | + Expected: test.Expects(0, nil, test.Contains("1GiB")), |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + testCase.Run(t) |
| 108 | +} |
0 commit comments