mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
refactor stats to not use internal data structures
- refactor to make it easier to split the api in the future - addition to check the existing test case and make sure it contains some expected output Signed-off-by: Morgan Bauer <mbauer@us.ibm.com>
This commit is contained in:
parent
4a2c3733bd
commit
2d5d606fd3
3 changed files with 29 additions and 17 deletions
|
@ -59,16 +59,6 @@ func (s *Server) getContainersStats(ctx context.Context, w http.ResponseWriter,
|
||||||
}
|
}
|
||||||
|
|
||||||
stream := boolValueOrDefault(r, "stream", true)
|
stream := boolValueOrDefault(r, "stream", true)
|
||||||
|
|
||||||
// If the container is not running and requires no stream, return an empty stats.
|
|
||||||
container, err := s.daemon.Get(vars["name"])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !container.IsRunning() && !stream {
|
|
||||||
return writeJSON(w, http.StatusOK, &types.Stats{})
|
|
||||||
}
|
|
||||||
|
|
||||||
var out io.Writer
|
var out io.Writer
|
||||||
if !stream {
|
if !stream {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
@ -90,7 +80,7 @@ func (s *Server) getContainersStats(ctx context.Context, w http.ResponseWriter,
|
||||||
Version: version,
|
Version: version,
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.daemon.ContainerStats(container, config)
|
return s.daemon.ContainerStats(vars["name"], config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) getContainersLogs(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
func (s *Server) getContainersLogs(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
||||||
|
|
|
@ -22,7 +22,18 @@ type ContainerStatsConfig struct {
|
||||||
|
|
||||||
// ContainerStats writes information about the container to the stream
|
// ContainerStats writes information about the container to the stream
|
||||||
// given in the config object.
|
// given in the config object.
|
||||||
func (daemon *Daemon) ContainerStats(container *Container, config *ContainerStatsConfig) error {
|
func (daemon *Daemon) ContainerStats(prefixOrName string, config *ContainerStatsConfig) error {
|
||||||
|
|
||||||
|
container, err := daemon.Get(prefixOrName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the container is not running and requires no stream, return an empty stats.
|
||||||
|
if !container.IsRunning() && !config.Stream {
|
||||||
|
return json.NewEncoder(config.OutStream).Encode(&types.Stats{})
|
||||||
|
}
|
||||||
|
|
||||||
updates, err := daemon.subscribeToContainerStats(container)
|
updates, err := daemon.subscribeToContainerStats(container)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -15,18 +16,28 @@ func (s *DockerSuite) TestCliStatsNoStream(c *check.C) {
|
||||||
c.Assert(waitRun(id), check.IsNil)
|
c.Assert(waitRun(id), check.IsNil)
|
||||||
|
|
||||||
statsCmd := exec.Command(dockerBinary, "stats", "--no-stream", id)
|
statsCmd := exec.Command(dockerBinary, "stats", "--no-stream", id)
|
||||||
chErr := make(chan error)
|
type output struct {
|
||||||
|
out []byte
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := make(chan output)
|
||||||
go func() {
|
go func() {
|
||||||
chErr <- statsCmd.Run()
|
out, err := statsCmd.Output()
|
||||||
|
ch <- output{out, err}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case err := <-chErr:
|
case outerr := <-ch:
|
||||||
if err != nil {
|
if outerr.err != nil {
|
||||||
c.Fatalf("Error running stats: %v", err)
|
c.Fatalf("Error running stats: %v", outerr.err)
|
||||||
|
}
|
||||||
|
if !bytes.Contains(outerr.out, []byte(id)) {
|
||||||
|
c.Fatalf("running container wasn't present in output")
|
||||||
}
|
}
|
||||||
case <-time.After(3 * time.Second):
|
case <-time.After(3 * time.Second):
|
||||||
statsCmd.Process.Kill()
|
statsCmd.Process.Kill()
|
||||||
c.Fatalf("stats did not return immediately when not streaming")
|
c.Fatalf("stats did not return immediately when not streaming")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue