github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_stats_linux_test.go (about) 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 main 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/containerd/nerdctl/pkg/infoutil" 24 "github.com/containerd/nerdctl/pkg/rootlessutil" 25 "github.com/containerd/nerdctl/pkg/testutil" 26 ) 27 28 func TestStats(t *testing.T) { 29 // this comment is for `nerdctl ps` but it also valid for `nerdctl stats` : 30 // https://github.com/containerd/nerdctl/pull/223#issuecomment-851395178 31 if rootlessutil.IsRootless() && infoutil.CgroupsVersion() == "1" { 32 t.Skip("test skipped for rootless containers on cgroup v1") 33 } 34 testContainerName := testutil.Identifier(t)[:12] 35 exitedTestContainerName := fmt.Sprintf("%s-exited", testContainerName) 36 37 base := testutil.NewBase(t) 38 defer base.Cmd("rm", "-f", testContainerName).Run() 39 defer base.Cmd("rm", "-f", exitedTestContainerName).Run() 40 base.Cmd("run", "--name", exitedTestContainerName, testutil.AlpineImage, "echo", "'exited'").AssertOK() 41 42 base.Cmd("run", "-d", "--name", testContainerName, testutil.AlpineImage, "sleep", "10").AssertOK() 43 base.Cmd("stats", "--no-stream").AssertOutContains(testContainerName) 44 base.Cmd("stats", "--no-stream", testContainerName).AssertOK() 45 base.Cmd("container", "stats", "--no-stream").AssertOutContains(testContainerName) 46 base.Cmd("container", "stats", "--no-stream", testContainerName).AssertOK() 47 }