github.com/goern/docker@v1.9.0-rc1/integration-cli/docker_cli_stats_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os/exec"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/docker/docker/pkg/integration/checker"
     9  	"github.com/go-check/check"
    10  )
    11  
    12  func (s *DockerSuite) TestStatsNoStream(c *check.C) {
    13  	testRequires(c, DaemonIsLinux)
    14  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
    15  	id := strings.TrimSpace(out)
    16  	c.Assert(waitRun(id), checker.IsNil)
    17  
    18  	statsCmd := exec.Command(dockerBinary, "stats", "--no-stream", id)
    19  	type output struct {
    20  		out []byte
    21  		err error
    22  	}
    23  
    24  	ch := make(chan output)
    25  	go func() {
    26  		out, err := statsCmd.Output()
    27  		ch <- output{out, err}
    28  	}()
    29  
    30  	select {
    31  	case outerr := <-ch:
    32  		c.Assert(outerr.err, checker.IsNil, check.Commentf("Error running stats: %v", outerr.err))
    33  		c.Assert(string(outerr.out), checker.Contains, id) //running container wasn't present in output
    34  	case <-time.After(3 * time.Second):
    35  		statsCmd.Process.Kill()
    36  		c.Fatalf("stats did not return immediately when not streaming")
    37  	}
    38  }
    39  
    40  func (s *DockerSuite) TestStatsContainerNotFound(c *check.C) {
    41  	testRequires(c, DaemonIsLinux)
    42  
    43  	out, _, err := dockerCmdWithError("stats", "notfound")
    44  	c.Assert(err, checker.NotNil)
    45  	c.Assert(out, checker.Contains, "no such id: notfound", check.Commentf("Expected to fail on not found container stats, got %q instead", out))
    46  
    47  	out, _, err = dockerCmdWithError("stats", "--no-stream", "notfound")
    48  	c.Assert(err, checker.NotNil)
    49  	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))
    50  }