-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add container stats api client side
Signed-off-by: zhangyue <zy675793960@yeah.net>
- Loading branch information
zhangyue
committed
Dec 28, 2018
1 parent
daeca08
commit ac9fa5d
Showing
4 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/url" | ||
) | ||
|
||
// ContainerStats return the stats related a container in an io.ReadCloser. | ||
func (client *APIClient) ContainerStats(ctx context.Context, name string, stream bool) (io.ReadCloser, error) { | ||
query := url.Values{} | ||
if stream { | ||
query.Set("stream", "1") | ||
} | ||
resp, err := client.get(ctx, fmt.Sprintf("/containers/%s/stats", name), query, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return resp.Body, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
"github.com/alibaba/pouch/test/command" | ||
"github.com/alibaba/pouch/test/environment" | ||
"github.com/alibaba/pouch/test/request" | ||
|
||
"github.com/go-check/check" | ||
"github.com/gotestyourself/gotestyourself/icmd" | ||
) | ||
|
||
// APIContainerStatsSuite is the test suite for container stats API. | ||
type APIContainerStatsSuite struct{} | ||
|
||
func init() { | ||
check.Suite(&APIContainerStatsSuite{}) | ||
} | ||
|
||
// SetUpTest does common setup in the beginning of each test. | ||
func (suite *APIContainerStatsSuite) SetUpTest(c *check.C) { | ||
SkipIfFalse(c, environment.IsLinux) | ||
PullImage(c, busyboxImage125) | ||
} | ||
|
||
// TestNoSuchContainer tests a container that doesn't exits return error. | ||
func (suite *APIContainerStatsSuite) TestNoSuchContainer(c *check.C) { | ||
resp, err := request.Get("/containers/nosuchcontainer/stats") | ||
c.Assert(err, check.IsNil) | ||
CheckRespStatus(c, resp, http.StatusNotFound) | ||
} | ||
|
||
// TestNoStream tests stats api without stream | ||
func (suite *APIContainerStatsSuite) TestNoStream(c *check.C) { | ||
name := "test_no_stream" | ||
command.PouchRun("run", "-d", "--name", name, busyboxImage, "sh", "-c", "while true; do sleep 1; done").Assert(c, icmd.Success) | ||
defer DelContainerForceMultyTime(c, name) | ||
|
||
resp, err := request.Get(fmt.Sprintf("/containers/%s/stats", name)) | ||
c.Assert(err, check.IsNil) | ||
CheckRespStatus(c, resp, http.StatusOK) | ||
|
||
out := types.ContainerStats{} | ||
err = request.DecodeBody(&out, resp.Body) | ||
c.Assert(err, check.IsNil) | ||
|
||
c.Assert(out.Name, check.Equals, name) | ||
c.Assert(out.ID, check.NotNil) | ||
c.Assert(out.Read, check.NotNil) | ||
c.Assert(out.CPUStats, check.NotNil) | ||
c.Assert(out.MemoryStats, check.NotNil) | ||
c.Assert(out.BlkioStats, check.NotNil) | ||
c.Assert(out.PidsStats, check.NotNil) | ||
c.Assert(out.PrecpuStats, check.NotNil) | ||
} |