integration-cli/docker_cli_stats_test.go
f3023a93
 package main
 
 import (
 	"os/exec"
 	"strings"
 	"time"
 
90a81f6a
 	"github.com/docker/docker/pkg/integration/checker"
f3023a93
 	"github.com/go-check/check"
 )
 
66be81b1
 func (s *DockerSuite) TestStatsNoStream(c *check.C) {
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))
 		c.Assert(string(outerr.out), checker.Contains, id) //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) {
 	testRequires(c, DaemonIsLinux)
2d5d606f
 
66be81b1
 	out, _, err := dockerCmdWithError("stats", "notfound")
90a81f6a
 	c.Assert(err, checker.NotNil)
 	c.Assert(out, checker.Contains, "no such id: 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)
 	c.Assert(out, checker.Contains, "no such id: notfound", check.Commentf("Expected to fail on not found container stats with --no-stream, got %q instead", out))
f3023a93
 }