From 86c86fc1663049374d6cf86d9a27f812b6691683 Mon Sep 17 00:00:00 2001 From: John Howard Date: Wed, 7 Sep 2016 16:08:51 -0700 Subject: [PATCH] Windows: stats support Signed-off-by: John Howard --- container_stats.go | 10 ++++++---- container_stats_test.go | 6 +++--- image_build.go | 5 +++-- image_build_test.go | 2 +- interface.go | 2 +- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/container_stats.go b/container_stats.go index 2cc67c3af1..3be7a988f4 100644 --- a/container_stats.go +++ b/container_stats.go @@ -1,15 +1,15 @@ package client import ( - "io" "net/url" + "github.com/docker/docker/api/types" "golang.org/x/net/context" ) // ContainerStats returns near realtime stats for a given container. // It's up to the caller to close the io.ReadCloser returned. -func (cli *Client) ContainerStats(ctx context.Context, containerID string, stream bool) (io.ReadCloser, error) { +func (cli *Client) ContainerStats(ctx context.Context, containerID string, stream bool) (types.ContainerStats, error) { query := url.Values{} query.Set("stream", "0") if stream { @@ -18,7 +18,9 @@ func (cli *Client) ContainerStats(ctx context.Context, containerID string, strea resp, err := cli.get(ctx, "/containers/"+containerID+"/stats", query, nil) if err != nil { - return nil, err + return types.ContainerStats{}, err } - return resp.body, err + + osType := GetDockerOS(resp.header.Get("Server")) + return types.ContainerStats{Body: resp.body, OSType: osType}, err } diff --git a/container_stats_test.go b/container_stats_test.go index 22ecd6170f..dc7c56492b 100644 --- a/container_stats_test.go +++ b/container_stats_test.go @@ -54,12 +54,12 @@ func TestContainerStats(t *testing.T) { }, nil }), } - body, err := client.ContainerStats(context.Background(), "container_id", c.stream) + resp, err := client.ContainerStats(context.Background(), "container_id", c.stream) if err != nil { t.Fatal(err) } - defer body.Close() - content, err := ioutil.ReadAll(body) + defer resp.Body.Close() + content, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatal(err) } diff --git a/image_build.go b/image_build.go index 8dd6744859..a84bf57821 100644 --- a/image_build.go +++ b/image_build.go @@ -39,7 +39,7 @@ func (cli *Client) ImageBuild(ctx context.Context, buildContext io.Reader, optio return types.ImageBuildResponse{}, err } - osType := getDockerOS(serverResp.header.Get("Server")) + osType := GetDockerOS(serverResp.header.Get("Server")) return types.ImageBuildResponse{ Body: serverResp.body, @@ -113,7 +113,8 @@ func imageBuildOptionsToQuery(options types.ImageBuildOptions) (url.Values, erro return query, nil } -func getDockerOS(serverHeader string) string { +// GetDockerOS returns the operating system based on the server header from the daemon. +func GetDockerOS(serverHeader string) string { var osType string matches := headerRegexp.FindStringSubmatch(serverHeader) if len(matches) > 0 { diff --git a/image_build_test.go b/image_build_test.go index 8261c54854..def88c3cb6 100644 --- a/image_build_test.go +++ b/image_build_test.go @@ -222,7 +222,7 @@ func TestGetDockerOS(t *testing.T) { "Foo/v1.22 (bar)": "", } for header, os := range cases { - g := getDockerOS(header) + g := GetDockerOS(header) if g != os { t.Fatalf("Expected %s, got %s", os, g) } diff --git a/interface.go b/interface.go index 1bfeb6aeb6..2d5555ff06 100644 --- a/interface.go +++ b/interface.go @@ -51,7 +51,7 @@ type ContainerAPIClient interface { ContainerResize(ctx context.Context, container string, options types.ResizeOptions) error ContainerRestart(ctx context.Context, container string, timeout *time.Duration) error ContainerStatPath(ctx context.Context, container, path string) (types.ContainerPathStat, error) - ContainerStats(ctx context.Context, container string, stream bool) (io.ReadCloser, error) + ContainerStats(ctx context.Context, container string, stream bool) (types.ContainerStats, error) ContainerStart(ctx context.Context, container string, options types.ContainerStartOptions) error ContainerStop(ctx context.Context, container string, timeout *time.Duration) error ContainerTop(ctx context.Context, container string, arguments []string) (types.ContainerProcessList, error)