2015-02-13 16:45:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-03 12:53:25 +00:00
|
|
|
"bufio"
|
2023-07-14 18:02:38 +00:00
|
|
|
"context"
|
2015-02-13 16:45:04 +00:00
|
|
|
"os/exec"
|
2015-10-03 12:53:25 +00:00
|
|
|
"regexp"
|
2015-02-13 16:45:04 +00:00
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2015-02-13 16:45:04 +00:00
|
|
|
"time"
|
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2015-02-13 16:45:04 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
type DockerCLIStatsSuite struct {
|
|
|
|
ds *DockerSuite
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
func (s *DockerCLIStatsSuite) TearDownTest(ctx context.Context, c *testing.T) {
|
|
|
|
s.ds.TearDownTest(ctx, c)
|
2022-06-16 21:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIStatsSuite) OnTimeout(c *testing.T) {
|
|
|
|
s.ds.OnTimeout(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIStatsSuite) TestStatsNoStream(c *testing.T) {
|
2016-02-03 01:59:11 +00:00
|
|
|
// Windows does not support stats
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2023-07-27 22:31:51 +00:00
|
|
|
id := cli.DockerCmd(c, "run", "-d", "busybox", "top").Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
|
|
|
cli.WaitRun(c, id)
|
2015-02-13 16:45:04 +00:00
|
|
|
|
|
|
|
statsCmd := exec.Command(dockerBinary, "stats", "--no-stream", id)
|
2015-09-16 21:16:55 +00:00
|
|
|
type output struct {
|
|
|
|
out []byte
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2020-02-25 22:13:25 +00:00
|
|
|
ch := make(chan output, 1)
|
2015-02-13 16:45:04 +00:00
|
|
|
go func() {
|
2015-09-16 21:16:55 +00:00
|
|
|
out, err := statsCmd.Output()
|
|
|
|
ch <- output{out, err}
|
2015-02-13 16:45:04 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2015-09-16 21:16:55 +00:00
|
|
|
case outerr := <-ch:
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, outerr.err, "Error running stats: %v", outerr.err)
|
|
|
|
assert.Assert(c, is.Contains(string(outerr.out), id[:12]), "running container wasn't present in output")
|
2015-05-30 17:25:51 +00:00
|
|
|
case <-time.After(3 * time.Second):
|
2015-02-13 16:45:04 +00:00
|
|
|
statsCmd.Process.Kill()
|
|
|
|
c.Fatalf("stats did not return immediately when not streaming")
|
|
|
|
}
|
2015-09-17 16:20:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStatsSuite) TestStatsContainerNotFound(c *testing.T) {
|
2016-02-03 01:59:11 +00:00
|
|
|
// Windows does not support stats
|
2015-09-17 16:20:25 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-09-16 21:16:55 +00:00
|
|
|
|
2015-09-17 16:20:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("stats", "notfound")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.ErrorContains(c, err, "")
|
|
|
|
assert.Assert(c, is.Contains(out, "No such container: notfound"), "Expected to fail on not found container stats, got %q instead", out)
|
2015-09-17 16:20:25 +00:00
|
|
|
|
|
|
|
out, _, err = dockerCmdWithError("stats", "--no-stream", "notfound")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.ErrorContains(c, err, "")
|
|
|
|
assert.Assert(c, is.Contains(out, "No such container: notfound"), "Expected to fail on not found container stats with --no-stream, got %q instead", out)
|
2015-02-13 16:45:04 +00:00
|
|
|
}
|
2015-10-03 12:53:25 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStatsSuite) TestStatsAllRunningNoStream(c *testing.T) {
|
2016-02-03 01:59:11 +00:00
|
|
|
// Windows does not support stats
|
2015-10-03 12:53:25 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
2023-07-27 22:31:51 +00:00
|
|
|
id1 := cli.DockerCmd(c, "run", "-d", "busybox", "top").Stdout()
|
|
|
|
id1 = strings.TrimSpace(id1)[:12]
|
|
|
|
cli.WaitRun(c, id1)
|
|
|
|
id2 := cli.DockerCmd(c, "run", "-d", "busybox", "top").Stdout()
|
|
|
|
id2 = strings.TrimSpace(id2)[:12]
|
|
|
|
cli.WaitRun(c, id2)
|
|
|
|
id3 := cli.DockerCmd(c, "run", "-d", "busybox", "top").Stdout()
|
|
|
|
id3 = strings.TrimSpace(id3)[:12]
|
|
|
|
cli.WaitRun(c, id3)
|
|
|
|
cli.DockerCmd(c, "stop", id3)
|
|
|
|
|
|
|
|
out := cli.DockerCmd(c, "stats", "--no-stream").Combined()
|
2015-10-03 12:53:25 +00:00
|
|
|
if !strings.Contains(out, id1) || !strings.Contains(out, id2) {
|
|
|
|
c.Fatalf("Expected stats output to contain both %s and %s, got %s", id1, id2, out)
|
|
|
|
}
|
|
|
|
if strings.Contains(out, id3) {
|
|
|
|
c.Fatalf("Did not expect %s in stats, got %s", id3, out)
|
|
|
|
}
|
2016-03-01 07:09:48 +00:00
|
|
|
|
|
|
|
// check output contains real data, but not all zeros
|
|
|
|
reg, _ := regexp.Compile("[1-9]+")
|
|
|
|
// split output with "\n", outLines[1] is id2's output
|
|
|
|
// outLines[2] is id1's output
|
|
|
|
outLines := strings.Split(out, "\n")
|
|
|
|
// check stat result of id2 contains real data
|
|
|
|
realData := reg.Find([]byte(outLines[1][12:]))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, realData != nil, "stat result are empty: %s", out)
|
2016-03-01 07:09:48 +00:00
|
|
|
// check stat result of id1 contains real data
|
|
|
|
realData = reg.Find([]byte(outLines[2][12:]))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, realData != nil, "stat result are empty: %s", out)
|
2015-10-03 12:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStatsSuite) TestStatsAllNoStream(c *testing.T) {
|
2016-02-03 01:59:11 +00:00
|
|
|
// Windows does not support stats
|
2015-10-03 12:53:25 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
2023-07-27 22:31:51 +00:00
|
|
|
id1 := cli.DockerCmd(c, "run", "-d", "busybox", "top").Stdout()
|
|
|
|
id1 = strings.TrimSpace(id1)[:12]
|
|
|
|
cli.WaitRun(c, id1)
|
|
|
|
cli.DockerCmd(c, "stop", id1)
|
|
|
|
id2 := cli.DockerCmd(c, "run", "-d", "busybox", "top").Stdout()
|
|
|
|
id2 = strings.TrimSpace(id2)[:12]
|
|
|
|
cli.WaitRun(c, id2)
|
2015-10-03 12:53:25 +00:00
|
|
|
|
2023-07-27 22:31:51 +00:00
|
|
|
out := cli.DockerCmd(c, "stats", "--all", "--no-stream").Combined()
|
2015-10-03 12:53:25 +00:00
|
|
|
if !strings.Contains(out, id1) || !strings.Contains(out, id2) {
|
|
|
|
c.Fatalf("Expected stats output to contain both %s and %s, got %s", id1, id2, out)
|
|
|
|
}
|
2016-03-01 07:09:48 +00:00
|
|
|
|
|
|
|
// check output contains real data, but not all zeros
|
|
|
|
reg, _ := regexp.Compile("[1-9]+")
|
|
|
|
// split output with "\n", outLines[1] is id2's output
|
|
|
|
outLines := strings.Split(out, "\n")
|
|
|
|
// check stat result of id2 contains real data
|
|
|
|
realData := reg.Find([]byte(outLines[1][12:]))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, realData != nil, "stat result of %s is empty: %s", id2, out)
|
|
|
|
|
2016-03-01 07:09:48 +00:00
|
|
|
// check stat result of id1 contains all zero
|
|
|
|
realData = reg.Find([]byte(outLines[2][12:]))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, realData == nil, "stat result of %s should be empty : %s", id1, out)
|
2015-10-03 12:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStatsSuite) TestStatsAllNewContainersAdded(c *testing.T) {
|
2016-02-03 01:59:11 +00:00
|
|
|
// Windows does not support stats
|
2016-03-01 22:10:13 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-10-03 12:53:25 +00:00
|
|
|
|
|
|
|
id := make(chan string)
|
|
|
|
addedChan := make(chan struct{})
|
|
|
|
|
2016-02-26 21:50:50 +00:00
|
|
|
runSleepingContainer(c, "-d")
|
2015-10-03 12:53:25 +00:00
|
|
|
statsCmd := exec.Command(dockerBinary, "stats")
|
|
|
|
stdout, err := statsCmd.StdoutPipe()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.NilError(c, statsCmd.Start())
|
2017-09-07 17:11:25 +00:00
|
|
|
go statsCmd.Wait()
|
2015-10-03 12:53:25 +00:00
|
|
|
defer statsCmd.Process.Kill()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
containerID := <-id
|
|
|
|
matchID := regexp.MustCompile(containerID)
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(stdout)
|
|
|
|
for scanner.Scan() {
|
|
|
|
switch {
|
|
|
|
case matchID.MatchString(scanner.Text()):
|
|
|
|
close(addedChan)
|
2016-02-26 21:50:50 +00:00
|
|
|
return
|
2015-10-03 12:53:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-04-16 21:39:30 +00:00
|
|
|
out := runSleepingContainer(c, "-d")
|
2023-07-27 22:31:51 +00:00
|
|
|
cli.WaitRun(c, out)
|
2015-10-03 12:53:25 +00:00
|
|
|
id <- strings.TrimSpace(out)[:12]
|
|
|
|
|
|
|
|
select {
|
2016-02-26 21:50:50 +00:00
|
|
|
case <-time.After(30 * time.Second):
|
2015-10-03 12:53:25 +00:00
|
|
|
c.Fatal("failed to observe new container created added to stats")
|
|
|
|
case <-addedChan:
|
|
|
|
// ignore, done
|
|
|
|
}
|
|
|
|
}
|
2016-12-26 09:43:18 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStatsSuite) TestStatsFormatAll(c *testing.T) {
|
2016-12-26 09:43:18 +00:00
|
|
|
// Windows does not support stats
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "--name=RunningOne", "busybox", "top")
|
|
|
|
cli.WaitRun(c, "RunningOne")
|
|
|
|
cli.DockerCmd(c, "run", "-d", "--name=ExitedOne", "busybox", "top")
|
|
|
|
cli.DockerCmd(c, "stop", "ExitedOne")
|
|
|
|
cli.WaitExited(c, "ExitedOne", 5*time.Second)
|
2016-12-26 09:43:18 +00:00
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out := cli.DockerCmd(c, "stats", "--no-stream", "--format", "{{.Name}}").Combined()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, is.Contains(out, "RunningOne"))
|
|
|
|
assert.Assert(c, !strings.Contains(out, "ExitedOne"))
|
2016-12-26 09:43:18 +00:00
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
out = cli.DockerCmd(c, "stats", "--all", "--no-stream", "--format", "{{.Name}}").Combined()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, is.Contains(out, "RunningOne"))
|
|
|
|
assert.Assert(c, is.Contains(out, "ExitedOne"))
|
2016-12-26 09:43:18 +00:00
|
|
|
}
|