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