This fix migrates TestAPIStatsContainerGetMemoryLimit from
integration-cli to api test.
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,42 +0,0 @@ |
| 1 |
-// +build !windows |
|
| 2 |
- |
|
| 3 |
-package main |
|
| 4 |
- |
|
| 5 |
-import ( |
|
| 6 |
- "encoding/json" |
|
| 7 |
- "fmt" |
|
| 8 |
- "net/http" |
|
| 9 |
- |
|
| 10 |
- "github.com/docker/docker/api/types" |
|
| 11 |
- "github.com/docker/docker/integration-cli/checker" |
|
| 12 |
- "github.com/docker/docker/integration-cli/request" |
|
| 13 |
- "github.com/go-check/check" |
|
| 14 |
-) |
|
| 15 |
- |
|
| 16 |
-func (s *DockerSuite) TestAPIStatsContainerGetMemoryLimit(c *check.C) {
|
|
| 17 |
- testRequires(c, DaemonIsLinux, memoryLimitSupport) |
|
| 18 |
- |
|
| 19 |
- resp, body, err := request.Get("/info", request.JSON)
|
|
| 20 |
- c.Assert(err, checker.IsNil) |
|
| 21 |
- c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) |
|
| 22 |
- var info types.Info |
|
| 23 |
- err = json.NewDecoder(body).Decode(&info) |
|
| 24 |
- c.Assert(err, checker.IsNil) |
|
| 25 |
- body.Close() |
|
| 26 |
- |
|
| 27 |
- // don't set a memory limit, the memory limit should be system memory |
|
| 28 |
- conName := "foo" |
|
| 29 |
- dockerCmd(c, "run", "-d", "--name", conName, "busybox", "top") |
|
| 30 |
- c.Assert(waitRun(conName), checker.IsNil) |
|
| 31 |
- |
|
| 32 |
- resp, body, err = request.Get(fmt.Sprintf("/containers/%s/stats?stream=false", conName))
|
|
| 33 |
- c.Assert(err, checker.IsNil) |
|
| 34 |
- c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) |
|
| 35 |
- c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json")
|
|
| 36 |
- |
|
| 37 |
- var v *types.Stats |
|
| 38 |
- err = json.NewDecoder(body).Decode(&v) |
|
| 39 |
- c.Assert(err, checker.IsNil) |
|
| 40 |
- body.Close() |
|
| 41 |
- c.Assert(fmt.Sprintf("%d", v.MemoryStats.Limit), checker.Equals, fmt.Sprintf("%d", info.MemTotal))
|
|
| 42 |
-} |
| 43 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,56 @@ |
| 0 |
+package container |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "context" |
|
| 4 |
+ "encoding/json" |
|
| 5 |
+ "io" |
|
| 6 |
+ "testing" |
|
| 7 |
+ "time" |
|
| 8 |
+ |
|
| 9 |
+ "github.com/docker/docker/api/types" |
|
| 10 |
+ "github.com/docker/docker/api/types/container" |
|
| 11 |
+ "github.com/docker/docker/api/types/network" |
|
| 12 |
+ "github.com/docker/docker/integration/util/request" |
|
| 13 |
+ "github.com/gotestyourself/gotestyourself/poll" |
|
| 14 |
+ "github.com/gotestyourself/gotestyourself/skip" |
|
| 15 |
+ "github.com/stretchr/testify/assert" |
|
| 16 |
+ "github.com/stretchr/testify/require" |
|
| 17 |
+) |
|
| 18 |
+ |
|
| 19 |
+func TestStats(t *testing.T) {
|
|
| 20 |
+ skip.If(t, !testEnv.DaemonInfo.MemoryLimit) |
|
| 21 |
+ |
|
| 22 |
+ defer setupTest(t)() |
|
| 23 |
+ client := request.NewAPIClient(t) |
|
| 24 |
+ ctx := context.Background() |
|
| 25 |
+ |
|
| 26 |
+ info, err := client.Info(ctx) |
|
| 27 |
+ require.NoError(t, err) |
|
| 28 |
+ |
|
| 29 |
+ c, err := client.ContainerCreate(ctx, |
|
| 30 |
+ &container.Config{
|
|
| 31 |
+ Cmd: []string{"top"},
|
|
| 32 |
+ Image: "busybox", |
|
| 33 |
+ }, |
|
| 34 |
+ &container.HostConfig{},
|
|
| 35 |
+ &network.NetworkingConfig{},
|
|
| 36 |
+ "", |
|
| 37 |
+ ) |
|
| 38 |
+ require.NoError(t, err) |
|
| 39 |
+ |
|
| 40 |
+ err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
|
|
| 41 |
+ require.NoError(t, err) |
|
| 42 |
+ |
|
| 43 |
+ poll.WaitOn(t, containerIsInState(ctx, client, c.ID, "running"), poll.WithDelay(100*time.Millisecond)) |
|
| 44 |
+ |
|
| 45 |
+ resp, err := client.ContainerStats(context.Background(), c.ID, false) |
|
| 46 |
+ require.NoError(t, err) |
|
| 47 |
+ defer resp.Body.Close() |
|
| 48 |
+ |
|
| 49 |
+ var v *types.Stats |
|
| 50 |
+ err = json.NewDecoder(resp.Body).Decode(&v) |
|
| 51 |
+ require.NoError(t, err) |
|
| 52 |
+ assert.Equal(t, int64(v.MemoryStats.Limit), info.MemTotal) |
|
| 53 |
+ err = json.NewDecoder(resp.Body).Decode(&v) |
|
| 54 |
+ require.Error(t, err, io.EOF) |
|
| 55 |
+} |