1
0
Fork 0
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:
Morgan Bauer 2015-09-16 14:16:55 -07:00
parent 4a2c3733bd
commit 2d5d606fd3
No known key found for this signature in database
GPG key ID: 23F15C502128F348
3 changed files with 29 additions and 17 deletions

View file

@ -59,16 +59,6 @@ func (s *Server) getContainersStats(ctx context.Context, w http.ResponseWriter,
}
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
if !stream {
w.Header().Set("Content-Type", "application/json")
@ -90,7 +80,7 @@ func (s *Server) getContainersStats(ctx context.Context, w http.ResponseWriter,
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 {

View file

@ -22,7 +22,18 @@ type ContainerStatsConfig struct {
// ContainerStats writes information about the container to the stream
// 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)
if err != nil {
return err

View file

@ -1,6 +1,7 @@
package main
import (
"bytes"
"os/exec"
"strings"
"time"
@ -15,18 +16,28 @@ func (s *DockerSuite) TestCliStatsNoStream(c *check.C) {
c.Assert(waitRun(id), check.IsNil)
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() {
chErr <- statsCmd.Run()
out, err := statsCmd.Output()
ch <- output{out, err}
}()
select {
case err := <-chErr:
if err != nil {
c.Fatalf("Error running stats: %v", err)
case outerr := <-ch:
if outerr.err != nil {
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):
statsCmd.Process.Kill()
c.Fatalf("stats did not return immediately when not streaming")
}
}