github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/integration-cli/docker_cli_stats_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"os/exec"
     6  	"regexp"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/docker/docker/pkg/integration/checker"
    11  	"github.com/go-check/check"
    12  )
    13  
    14  func (s *DockerSuite) TestStatsNoStream(c *check.C) {
    15  	testRequires(c, DaemonIsLinux)
    16  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
    17  	id := strings.TrimSpace(out)
    18  	c.Assert(waitRun(id), checker.IsNil)
    19  
    20  	statsCmd := exec.Command(dockerBinary, "stats", "--no-stream", id)
    21  	type output struct {
    22  		out []byte
    23  		err error
    24  	}
    25  
    26  	ch := make(chan output)
    27  	go func() {
    28  		out, err := statsCmd.Output()
    29  		ch <- output{out, err}
    30  	}()
    31  
    32  	select {
    33  	case outerr := <-ch:
    34  		c.Assert(outerr.err, checker.IsNil, check.Commentf("Error running stats: %v", outerr.err))
    35  		c.Assert(string(outerr.out), checker.Contains, id) //running container wasn't present in output
    36  	case <-time.After(3 * time.Second):
    37  		statsCmd.Process.Kill()
    38  		c.Fatalf("stats did not return immediately when not streaming")
    39  	}
    40  }
    41  
    42  func (s *DockerSuite) TestStatsContainerNotFound(c *check.C) {
    43  	testRequires(c, DaemonIsLinux)
    44  
    45  	out, _, err := dockerCmdWithError("stats", "notfound")
    46  	c.Assert(err, checker.NotNil)
    47  	c.Assert(out, checker.Contains, "No such container: notfound", check.Commentf("Expected to fail on not found container stats, got %q instead", out))
    48  
    49  	out, _, err = dockerCmdWithError("stats", "--no-stream", "notfound")
    50  	c.Assert(err, checker.NotNil)
    51  	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))
    52  }
    53  
    54  func (s *DockerSuite) TestStatsAllRunningNoStream(c *check.C) {
    55  	testRequires(c, DaemonIsLinux)
    56  
    57  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
    58  	id1 := strings.TrimSpace(out)[:12]
    59  	c.Assert(waitRun(id1), check.IsNil)
    60  	out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
    61  	id2 := strings.TrimSpace(out)[:12]
    62  	c.Assert(waitRun(id2), check.IsNil)
    63  	out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
    64  	id3 := strings.TrimSpace(out)[:12]
    65  	c.Assert(waitRun(id3), check.IsNil)
    66  	dockerCmd(c, "stop", id3)
    67  
    68  	out, _ = dockerCmd(c, "stats", "--no-stream")
    69  	if !strings.Contains(out, id1) || !strings.Contains(out, id2) {
    70  		c.Fatalf("Expected stats output to contain both %s and %s, got %s", id1, id2, out)
    71  	}
    72  	if strings.Contains(out, id3) {
    73  		c.Fatalf("Did not expect %s in stats, got %s", id3, out)
    74  	}
    75  }
    76  
    77  func (s *DockerSuite) TestStatsAllNoStream(c *check.C) {
    78  	testRequires(c, DaemonIsLinux)
    79  
    80  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
    81  	id1 := strings.TrimSpace(out)[:12]
    82  	c.Assert(waitRun(id1), check.IsNil)
    83  	dockerCmd(c, "stop", id1)
    84  	out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
    85  	id2 := strings.TrimSpace(out)[:12]
    86  	c.Assert(waitRun(id2), check.IsNil)
    87  
    88  	out, _ = dockerCmd(c, "stats", "--all", "--no-stream")
    89  	if !strings.Contains(out, id1) || !strings.Contains(out, id2) {
    90  		c.Fatalf("Expected stats output to contain both %s and %s, got %s", id1, id2, out)
    91  	}
    92  }
    93  
    94  func (s *DockerSuite) TestStatsAllNewContainersAdded(c *check.C) {
    95  	testRequires(c, DaemonIsLinux)
    96  
    97  	id := make(chan string)
    98  	addedChan := make(chan struct{})
    99  
   100  	dockerCmd(c, "run", "-d", "busybox", "top")
   101  	statsCmd := exec.Command(dockerBinary, "stats")
   102  	stdout, err := statsCmd.StdoutPipe()
   103  	c.Assert(err, check.IsNil)
   104  	c.Assert(statsCmd.Start(), check.IsNil)
   105  	defer statsCmd.Process.Kill()
   106  
   107  	go func() {
   108  		containerID := <-id
   109  		matchID := regexp.MustCompile(containerID)
   110  
   111  		scanner := bufio.NewScanner(stdout)
   112  		for scanner.Scan() {
   113  			switch {
   114  			case matchID.MatchString(scanner.Text()):
   115  				close(addedChan)
   116  			}
   117  		}
   118  	}()
   119  
   120  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
   121  	c.Assert(waitRun(strings.TrimSpace(out)), check.IsNil)
   122  	id <- strings.TrimSpace(out)[:12]
   123  
   124  	select {
   125  	case <-time.After(5 * time.Second):
   126  		c.Fatal("failed to observe new container created added to stats")
   127  	case <-addedChan:
   128  		// ignore, done
   129  	}
   130  }