Skip to content

Commit 2d5d606

Browse files
committed
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 <[email protected]>
1 parent 4a2c373 commit 2d5d606

3 files changed

Lines changed: 29 additions & 17 deletions

File tree

api/server/container.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ func (s *Server) getContainersStats(ctx context.Context, w http.ResponseWriter,
5959
}
6060

6161
stream := boolValueOrDefault(r, "stream", true)
62-
63-
// If the container is not running and requires no stream, return an empty stats.
64-
container, err := s.daemon.Get(vars["name"])
65-
if err != nil {
66-
return err
67-
}
68-
if !container.IsRunning() && !stream {
69-
return writeJSON(w, http.StatusOK, &types.Stats{})
70-
}
71-
7262
var out io.Writer
7363
if !stream {
7464
w.Header().Set("Content-Type", "application/json")
@@ -90,7 +80,7 @@ func (s *Server) getContainersStats(ctx context.Context, w http.ResponseWriter,
9080
Version: version,
9181
}
9282

93-
return s.daemon.ContainerStats(container, config)
83+
return s.daemon.ContainerStats(vars["name"], config)
9484
}
9585

9686
func (s *Server) getContainersLogs(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

daemon/stats.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ type ContainerStatsConfig struct {
2222

2323
// ContainerStats writes information about the container to the stream
2424
// given in the config object.
25-
func (daemon *Daemon) ContainerStats(container *Container, config *ContainerStatsConfig) error {
25+
func (daemon *Daemon) ContainerStats(prefixOrName string, config *ContainerStatsConfig) error {
26+
27+
container, err := daemon.Get(prefixOrName)
28+
if err != nil {
29+
return err
30+
}
31+
32+
// If the container is not running and requires no stream, return an empty stats.
33+
if !container.IsRunning() && !config.Stream {
34+
return json.NewEncoder(config.OutStream).Encode(&types.Stats{})
35+
}
36+
2637
updates, err := daemon.subscribeToContainerStats(container)
2738
if err != nil {
2839
return err

integration-cli/docker_cli_stats_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"os/exec"
56
"strings"
67
"time"
@@ -15,18 +16,28 @@ func (s *DockerSuite) TestCliStatsNoStream(c *check.C) {
1516
c.Assert(waitRun(id), check.IsNil)
1617

1718
statsCmd := exec.Command(dockerBinary, "stats", "--no-stream", id)
18-
chErr := make(chan error)
19+
type output struct {
20+
out []byte
21+
err error
22+
}
23+
24+
ch := make(chan output)
1925
go func() {
20-
chErr <- statsCmd.Run()
26+
out, err := statsCmd.Output()
27+
ch <- output{out, err}
2128
}()
2229

2330
select {
24-
case err := <-chErr:
25-
if err != nil {
26-
c.Fatalf("Error running stats: %v", err)
31+
case outerr := <-ch:
32+
if outerr.err != nil {
33+
c.Fatalf("Error running stats: %v", outerr.err)
34+
}
35+
if !bytes.Contains(outerr.out, []byte(id)) {
36+
c.Fatalf("running container wasn't present in output")
2737
}
2838
case <-time.After(3 * time.Second):
2939
statsCmd.Process.Kill()
3040
c.Fatalf("stats did not return immediately when not streaming")
3141
}
42+
3243
}

0 commit comments

Comments
 (0)