github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/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/integration-cli/checker"
    11  	"github.com/docker/docker/integration-cli/cli"
    12  	"github.com/go-check/check"
    13  )
    14  
    15  func (s *DockerSuite) TestStatsNoStream(c *check.C) {
    16  	// Windows does not support stats
    17  	testRequires(c, DaemonIsLinux)
    18  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
    19  	id := strings.TrimSpace(out)
    20  	c.Assert(waitRun(id), checker.IsNil)
    21  
    22  	statsCmd := exec.Command(dockerBinary, "stats", "--no-stream", id)
    23  	type output struct {
    24  		out []byte
    25  		err error
    26  	}
    27  
    28  	ch := make(chan output)
    29  	go func() {
    30  		out, err := statsCmd.Output()
    31  		ch <- output{out, err}
    32  	}()
    33  
    34  	select {
    35  	case outerr := <-ch:
    36  		c.Assert(outerr.err, checker.IsNil, check.Commentf("Error running stats: %v", outerr.err))
    37  		c.Assert(string(outerr.out), checker.Contains, id[:12]) //running container wasn't present in output
    38  	case <-time.After(3 * time.Second):
    39  		statsCmd.Process.Kill()
    40  		c.Fatalf("stats did not return immediately when not streaming")
    41  	}
    42  }
    43  
    44  func (s *DockerSuite) TestStatsContainerNotFound(c *check.C) {
    45  	// Windows does not support stats
    46  	testRequires(c, DaemonIsLinux)
    47  
    48  	out, _, err := dockerCmdWithError("stats", "notfound")
    49  	c.Assert(err, checker.NotNil)
    50  	c.Assert(out, checker.Contains, "No such container: notfound", check.Commentf("Expected to fail on not found container stats, got %q instead", out))
    51  
    52  	out, _, err = dockerCmdWithError("stats", "--no-stream", "notfound")
    53  	c.Assert(err, checker.NotNil)
    54  	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))
    55  }
    56  
    57  func (s *DockerSuite) TestStatsAllRunningNoStream(c *check.C) {
    58  	// Windows does not support stats
    59  	testRequires(c, DaemonIsLinux)
    60  
    61  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
    62  	id1 := strings.TrimSpace(out)[:12]
    63  	c.Assert(waitRun(id1), check.IsNil)
    64  	out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
    65  	id2 := strings.TrimSpace(out)[:12]
    66  	c.Assert(waitRun(id2), check.IsNil)
    67  	out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
    68  	id3 := strings.TrimSpace(out)[:12]
    69  	c.Assert(waitRun(id3), check.IsNil)
    70  	dockerCmd(c, "stop", id3)
    71  
    72  	out, _ = dockerCmd(c, "stats", "--no-stream")
    73  	if !strings.Contains(out, id1) || !strings.Contains(out, id2) {
    74  		c.Fatalf("Expected stats output to contain both %s and %s, got %s", id1, id2, out)
    75  	}
    76  	if strings.Contains(out, id3) {
    77  		c.Fatalf("Did not expect %s in stats, got %s", id3, out)
    78  	}
    79  
    80  	// check output contains real data, but not all zeros
    81  	reg, _ := regexp.Compile("[1-9]+")
    82  	// split output with "\n", outLines[1] is id2's output
    83  	// outLines[2] is id1's output
    84  	outLines := strings.Split(out, "\n")
    85  	// check stat result of id2 contains real data
    86  	realData := reg.Find([]byte(outLines[1][12:]))
    87  	c.Assert(realData, checker.NotNil, check.Commentf("stat result are empty: %s", out))
    88  	// check stat result of id1 contains real data
    89  	realData = reg.Find([]byte(outLines[2][12:]))
    90  	c.Assert(realData, checker.NotNil, check.Commentf("stat result are empty: %s", out))
    91  }
    92  
    93  func (s *DockerSuite) TestStatsAllNoStream(c *check.C) {
    94  	// Windows does not support stats
    95  	testRequires(c, DaemonIsLinux)
    96  
    97  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
    98  	id1 := strings.TrimSpace(out)[:12]
    99  	c.Assert(waitRun(id1), check.IsNil)
   100  	dockerCmd(c, "stop", id1)
   101  	out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
   102  	id2 := strings.TrimSpace(out)[:12]
   103  	c.Assert(waitRun(id2), check.IsNil)
   104  
   105  	out, _ = dockerCmd(c, "stats", "--all", "--no-stream")
   106  	if !strings.Contains(out, id1) || !strings.Contains(out, id2) {
   107  		c.Fatalf("Expected stats output to contain both %s and %s, got %s", id1, id2, out)
   108  	}
   109  
   110  	// check output contains real data, but not all zeros
   111  	reg, _ := regexp.Compile("[1-9]+")
   112  	// split output with "\n", outLines[1] is id2's output
   113  	outLines := strings.Split(out, "\n")
   114  	// check stat result of id2 contains real data
   115  	realData := reg.Find([]byte(outLines[1][12:]))
   116  	c.Assert(realData, checker.NotNil, check.Commentf("stat result of %s is empty: %s", id2, out))
   117  	// check stat result of id1 contains all zero
   118  	realData = reg.Find([]byte(outLines[2][12:]))
   119  	c.Assert(realData, checker.IsNil, check.Commentf("stat result of %s should be empty : %s", id1, out))
   120  }
   121  
   122  func (s *DockerSuite) TestStatsAllNewContainersAdded(c *check.C) {
   123  	// Windows does not support stats
   124  	testRequires(c, DaemonIsLinux)
   125  
   126  	id := make(chan string)
   127  	addedChan := make(chan struct{})
   128  
   129  	runSleepingContainer(c, "-d")
   130  	statsCmd := exec.Command(dockerBinary, "stats")
   131  	stdout, err := statsCmd.StdoutPipe()
   132  	c.Assert(err, check.IsNil)
   133  	c.Assert(statsCmd.Start(), check.IsNil)
   134  	go statsCmd.Wait()
   135  	defer statsCmd.Process.Kill()
   136  
   137  	go func() {
   138  		containerID := <-id
   139  		matchID := regexp.MustCompile(containerID)
   140  
   141  		scanner := bufio.NewScanner(stdout)
   142  		for scanner.Scan() {
   143  			switch {
   144  			case matchID.MatchString(scanner.Text()):
   145  				close(addedChan)
   146  				return
   147  			}
   148  		}
   149  	}()
   150  
   151  	out := runSleepingContainer(c, "-d")
   152  	c.Assert(waitRun(strings.TrimSpace(out)), check.IsNil)
   153  	id <- strings.TrimSpace(out)[:12]
   154  
   155  	select {
   156  	case <-time.After(30 * time.Second):
   157  		c.Fatal("failed to observe new container created added to stats")
   158  	case <-addedChan:
   159  		// ignore, done
   160  	}
   161  }
   162  
   163  func (s *DockerSuite) TestStatsFormatAll(c *check.C) {
   164  	// Windows does not support stats
   165  	testRequires(c, DaemonIsLinux)
   166  
   167  	cli.DockerCmd(c, "run", "-d", "--name=RunningOne", "busybox", "top")
   168  	cli.WaitRun(c, "RunningOne")
   169  	cli.DockerCmd(c, "run", "-d", "--name=ExitedOne", "busybox", "top")
   170  	cli.DockerCmd(c, "stop", "ExitedOne")
   171  	cli.WaitExited(c, "ExitedOne", 5*time.Second)
   172  
   173  	out := cli.DockerCmd(c, "stats", "--no-stream", "--format", "{{.Name}}").Combined()
   174  	c.Assert(out, checker.Contains, "RunningOne")
   175  	c.Assert(out, checker.Not(checker.Contains), "ExitedOne")
   176  
   177  	out = cli.DockerCmd(c, "stats", "--all", "--no-stream", "--format", "{{.Name}}").Combined()
   178  	c.Assert(out, checker.Contains, "RunningOne")
   179  	c.Assert(out, checker.Contains, "ExitedOne")
   180  }