|
| 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 | + if runtime.GOOS == "linux" { |
| 32 | + // this comment is for `nerdctl ps` but it also valid for `nerdctl stats` : |
| 33 | + // https://github.com/containerd/nerdctl/pull/223#issuecomment-851395178 |
| 34 | + testCase.Require = nerdtest.RootfulOrCGroupV2 |
| 35 | + } |
| 36 | + |
| 37 | + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { |
| 38 | + helpers.Anyhow("rm", "-f", data.Identifier("container")) |
| 39 | + helpers.Anyhow("rm", "-f", data.Identifier("memlimited")) |
| 40 | + helpers.Anyhow("rm", "-f", data.Identifier("exited")) |
| 41 | + } |
| 42 | + |
| 43 | + testCase.Setup = func(data test.Data, helpers test.Helpers) { |
| 44 | + helpers.Ensure("run", "-d", "--name", data.Identifier("container"), testutil.CommonImage, "sleep", "infinity") |
| 45 | + helpers.Ensure("run", "-d", "--name", data.Identifier("memlimited"), "--memory", "1g", testutil.CommonImage, "sleep", "infinity") |
| 46 | + helpers.Ensure("run", "--name", data.Identifier("exited"), testutil.CommonImage, "echo", "'exited'") |
| 47 | + data.Set("id", data.Identifier("container")) |
| 48 | + } |
| 49 | + |
| 50 | + testCase.SubTests = []*test.Case{ |
| 51 | + { |
| 52 | + Description: "stats", |
| 53 | + Command: test.Command("stats", "--no-stream", "--no-trunc"), |
| 54 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 55 | + return &test.Expected{ |
| 56 | + Output: test.Contains(data.Get("id")), |
| 57 | + } |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + Description: "container stats", |
| 62 | + Command: test.Command("container", "stats", "--no-stream", "--no-trunc"), |
| 63 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 64 | + return &test.Expected{ |
| 65 | + Output: test.Contains(data.Get("id")), |
| 66 | + } |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + Description: "stats ID", |
| 71 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 72 | + return helpers.Command("stats", "--no-stream", data.Get("id")) |
| 73 | + }, |
| 74 | + Expected: test.Expects(0, nil, nil), |
| 75 | + }, |
| 76 | + { |
| 77 | + Description: "container stats ID", |
| 78 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 79 | + return helpers.Command("container", "stats", "--no-stream", data.Get("id")) |
| 80 | + }, |
| 81 | + Expected: test.Expects(0, nil, nil), |
| 82 | + }, |
| 83 | + { |
| 84 | + Description: "no mem limit set", |
| 85 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 86 | + return helpers.Command("stats", "--no-stream") |
| 87 | + }, |
| 88 | + // FIXME: why? what is this testing? |
| 89 | + Expected: test.Expects(0, nil, test.DoesNotContain("16EiB")), |
| 90 | + }, |
| 91 | + { |
| 92 | + Description: "mem limit set", |
| 93 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 94 | + return helpers.Command("stats", "--no-stream") |
| 95 | + }, |
| 96 | + Expected: test.Expects(0, nil, test.Contains("1GiB")), |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + testCase.Run(t) |
| 101 | +} |
0 commit comments