integration-cli/docker_cli_stats_test.go
f3023a93
 package main
 
 import (
ae818a82
 	"bufio"
f3023a93
 	"os/exec"
ae818a82
 	"regexp"
f3023a93
 	"strings"
 	"time"
 
33968e6c
 	"github.com/docker/docker/integration-cli/checker"
db35c2a5
 	"github.com/docker/docker/integration-cli/cli"
f3023a93
 	"github.com/go-check/check"
 )
 
66be81b1
 func (s *DockerSuite) TestStatsNoStream(c *check.C) {
55268f4e
 	// Windows does not support stats
f9a3558a
 	testRequires(c, DaemonIsLinux)
eef6eda7
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
f3023a93
 	id := strings.TrimSpace(out)
90a81f6a
 	c.Assert(waitRun(id), checker.IsNil)
f3023a93
 
 	statsCmd := exec.Command(dockerBinary, "stats", "--no-stream", id)
2d5d606f
 	type output struct {
 		out []byte
 		err error
 	}
 
 	ch := make(chan output)
f3023a93
 	go func() {
2d5d606f
 		out, err := statsCmd.Output()
 		ch <- output{out, err}
f3023a93
 	}()
 
 	select {
2d5d606f
 	case outerr := <-ch:
90a81f6a
 		c.Assert(outerr.err, checker.IsNil, check.Commentf("Error running stats: %v", outerr.err))
8c10098e
 		c.Assert(string(outerr.out), checker.Contains, id[:12]) //running container wasn't present in output
96123a1f
 	case <-time.After(3 * time.Second):
f3023a93
 		statsCmd.Process.Kill()
 		c.Fatalf("stats did not return immediately when not streaming")
 	}
66be81b1
 }
 
 func (s *DockerSuite) TestStatsContainerNotFound(c *check.C) {
55268f4e
 	// Windows does not support stats
66be81b1
 	testRequires(c, DaemonIsLinux)
2d5d606f
 
66be81b1
 	out, _, err := dockerCmdWithError("stats", "notfound")
90a81f6a
 	c.Assert(err, checker.NotNil)
c424c8c3
 	c.Assert(out, checker.Contains, "No such container: notfound", check.Commentf("Expected to fail on not found container stats, got %q instead", out))
66be81b1
 
 	out, _, err = dockerCmdWithError("stats", "--no-stream", "notfound")
90a81f6a
 	c.Assert(err, checker.NotNil)
c424c8c3
 	c.Assert(out, checker.Contains, "No such container: notfound", check.Commentf("Expected to fail on not found container stats with --no-stream, got %q instead", out))
f3023a93
 }
ae818a82
 
 func (s *DockerSuite) TestStatsAllRunningNoStream(c *check.C) {
55268f4e
 	// Windows does not support stats
ae818a82
 	testRequires(c, DaemonIsLinux)
 
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
 	id1 := strings.TrimSpace(out)[:12]
 	c.Assert(waitRun(id1), check.IsNil)
 	out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
 	id2 := strings.TrimSpace(out)[:12]
 	c.Assert(waitRun(id2), check.IsNil)
 	out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
 	id3 := strings.TrimSpace(out)[:12]
 	c.Assert(waitRun(id3), check.IsNil)
 	dockerCmd(c, "stop", id3)
 
 	out, _ = dockerCmd(c, "stats", "--no-stream")
 	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)
 	}
ea86c30a
 
 	// 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:]))
 	c.Assert(realData, checker.NotNil, check.Commentf("stat result are empty: %s", out))
 	// check stat result of id1 contains real data
 	realData = reg.Find([]byte(outLines[2][12:]))
 	c.Assert(realData, checker.NotNil, check.Commentf("stat result are empty: %s", out))
ae818a82
 }
 
 func (s *DockerSuite) TestStatsAllNoStream(c *check.C) {
55268f4e
 	// Windows does not support stats
ae818a82
 	testRequires(c, DaemonIsLinux)
 
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
 	id1 := strings.TrimSpace(out)[:12]
 	c.Assert(waitRun(id1), check.IsNil)
 	dockerCmd(c, "stop", id1)
 	out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
 	id2 := strings.TrimSpace(out)[:12]
 	c.Assert(waitRun(id2), check.IsNil)
 
 	out, _ = dockerCmd(c, "stats", "--all", "--no-stream")
 	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)
 	}
ea86c30a
 
 	// 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:]))
 	c.Assert(realData, checker.NotNil, check.Commentf("stat result of %s is empty: %s", id2, out))
 	// check stat result of id1 contains all zero
 	realData = reg.Find([]byte(outLines[2][12:]))
 	c.Assert(realData, checker.IsNil, check.Commentf("stat result of %s should be empty : %s", id1, out))
ae818a82
 }
 
 func (s *DockerSuite) TestStatsAllNewContainersAdded(c *check.C) {
55268f4e
 	// Windows does not support stats
ff08036c
 	testRequires(c, DaemonIsLinux)
ae818a82
 
 	id := make(chan string)
 	addedChan := make(chan struct{})
 
efd281d6
 	runSleepingContainer(c, "-d")
ae818a82
 	statsCmd := exec.Command(dockerBinary, "stats")
 	stdout, err := statsCmd.StdoutPipe()
 	c.Assert(err, check.IsNil)
 	c.Assert(statsCmd.Start(), check.IsNil)
617f89b9
 	go statsCmd.Wait()
ae818a82
 	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)
efd281d6
 				return
ae818a82
 			}
 		}
 	}()
 
a899aa67
 	out := runSleepingContainer(c, "-d")
71d6e71c
 	c.Assert(waitRun(strings.TrimSpace(out)), check.IsNil)
ae818a82
 	id <- strings.TrimSpace(out)[:12]
 
 	select {
efd281d6
 	case <-time.After(30 * time.Second):
ae818a82
 		c.Fatal("failed to observe new container created added to stats")
 	case <-addedChan:
 		// ignore, done
 	}
 }
eb3a7c23
 
 func (s *DockerSuite) TestStatsFormatAll(c *check.C) {
 	// Windows does not support stats
 	testRequires(c, DaemonIsLinux)
 
db35c2a5
 	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)
eb3a7c23
 
db35c2a5
 	out := cli.DockerCmd(c, "stats", "--no-stream", "--format", "{{.Name}}").Combined()
eb3a7c23
 	c.Assert(out, checker.Contains, "RunningOne")
 	c.Assert(out, checker.Not(checker.Contains), "ExitedOne")
 
db35c2a5
 	out = cli.DockerCmd(c, "stats", "--all", "--no-stream", "--format", "{{.Name}}").Combined()
eb3a7c23
 	c.Assert(out, checker.Contains, "RunningOne")
 	c.Assert(out, checker.Contains, "ExitedOne")
 }