|
| 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 TestTop(t *testing.T) { |
| 29 | + testCase := nerdtest.Setup() |
| 30 | + |
| 31 | + //more details https://github.com/containerd/nerdctl/pull/223#issuecomment-851395178 |
| 32 | + if runtime.GOOS == "linux" { |
| 33 | + testCase.Require = nerdtest.CgroupsAccessible |
| 34 | + } |
| 35 | + |
| 36 | + testCase.Setup = func(data test.Data, helpers test.Helpers) { |
| 37 | + // FIXME: busybox 1.36 on windows still appears to not support sleep inf. Unclear why. |
| 38 | + helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage, "sleep", "10") |
| 39 | + data.Set("cID", data.Identifier()) |
| 40 | + } |
| 41 | + |
| 42 | + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { |
| 43 | + helpers.Anyhow("rm", "-f", data.Identifier()) |
| 44 | + } |
| 45 | + |
| 46 | + testCase.SubTests = []*test.Case{ |
| 47 | + { |
| 48 | + Description: "with o pid,user,cmd", |
| 49 | + // Docker does not support top -o |
| 50 | + Require: test.Not(nerdtest.Docker), |
| 51 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 52 | + return helpers.Command("top", data.Get("cID"), "-o", "pid,user,cmd") |
| 53 | + }, |
| 54 | + |
| 55 | + Expected: test.Expects(0, nil, nil), |
| 56 | + }, |
| 57 | + { |
| 58 | + Description: "simple", |
| 59 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 60 | + return helpers.Command("top", data.Get("cID")) |
| 61 | + }, |
| 62 | + |
| 63 | + Expected: test.Expects(0, nil, nil), |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + testCase.Run(t) |
| 68 | +} |
| 69 | + |
| 70 | +func TestTopHyperVContainer(t *testing.T) { |
| 71 | + |
| 72 | + testCase := nerdtest.Setup() |
| 73 | + |
| 74 | + testCase.Require = nerdtest.HyperV |
| 75 | + |
| 76 | + testCase.Setup = func(data test.Data, helpers test.Helpers) { |
| 77 | + // FIXME: busybox 1.36 on windows still appears to not support sleep inf. Unclear why. |
| 78 | + helpers.Ensure("run", "--isolation", "hyperv", "-d", "--name", data.Identifier("container"), testutil.CommonImage, "sleep", "10") |
| 79 | + } |
| 80 | + |
| 81 | + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { |
| 82 | + helpers.Anyhow("rm", "-f", data.Identifier("container")) |
| 83 | + } |
| 84 | + |
| 85 | + testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 86 | + return helpers.Command("top", data.Identifier("container")) |
| 87 | + } |
| 88 | + |
| 89 | + testCase.Expected = test.Expects(0, nil, nil) |
| 90 | + |
| 91 | + testCase.Run(t) |
| 92 | +} |
0 commit comments