mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Migrate TestAPIUpdateContainer from integration-cli to api tests
This fix migrates TestAPIUpdateContainer from integration-cli to api tests Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
parent
8786b09c81
commit
490edd3582
1 changed files with 88 additions and 0 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
@ -11,10 +12,67 @@ import (
|
|||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/strslice"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/integration/util/request"
|
||||
"github.com/docker/docker/pkg/stdcopy"
|
||||
"github.com/gotestyourself/gotestyourself/poll"
|
||||
"github.com/gotestyourself/gotestyourself/skip"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestUpdateMemory(t *testing.T) {
|
||||
skip.If(t, testEnv.DaemonInfo.OSType != "linux")
|
||||
skip.If(t, !testEnv.DaemonInfo.MemoryLimit)
|
||||
skip.If(t, !testEnv.DaemonInfo.SwapLimit)
|
||||
|
||||
defer setupTest(t)()
|
||||
client := request.NewAPIClient(t)
|
||||
ctx := context.Background()
|
||||
|
||||
c, err := client.ContainerCreate(ctx,
|
||||
&container.Config{
|
||||
Cmd: []string{"top"},
|
||||
Image: "busybox",
|
||||
},
|
||||
&container.HostConfig{
|
||||
Resources: container.Resources{
|
||||
Memory: 200 * 1024 * 1024,
|
||||
},
|
||||
},
|
||||
nil,
|
||||
"",
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
poll.WaitOn(t, containerIsInState(ctx, client, c.ID, "running"), poll.WithDelay(100*time.Millisecond))
|
||||
|
||||
_, err = client.ContainerUpdate(ctx, c.ID, container.UpdateConfig{
|
||||
Resources: container.Resources{
|
||||
Memory: 314572800,
|
||||
MemorySwap: 524288000,
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
inspect, err := client.ContainerInspect(ctx, c.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, inspect.HostConfig.Memory, int64(314572800))
|
||||
assert.Equal(t, inspect.HostConfig.MemorySwap, int64(524288000))
|
||||
|
||||
body, err := getContainerSysFSValue(ctx, client, c.ID, "/sys/fs/cgroup/memory/memory.limit_in_bytes")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(body), "314572800")
|
||||
|
||||
body, err = getContainerSysFSValue(ctx, client, c.ID, "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(body), "524288000")
|
||||
}
|
||||
|
||||
func TestUpdateCPUQUota(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
@ -106,3 +164,33 @@ func TestUpdateCPUQUota(t *testing.T) {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
func getContainerSysFSValue(ctx context.Context, client client.APIClient, cID string, path string) (string, error) {
|
||||
var b bytes.Buffer
|
||||
|
||||
ex, err := client.ContainerExecCreate(ctx, cID,
|
||||
types.ExecConfig{
|
||||
AttachStdout: true,
|
||||
Cmd: strslice.StrSlice([]string{"cat", path}),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
resp, err := client.ContainerExecAttach(ctx, ex.ID,
|
||||
types.ExecStartCheck{
|
||||
Detach: false,
|
||||
Tty: false,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
defer resp.Close()
|
||||
|
||||
b.Reset()
|
||||
_, err = stdcopy.StdCopy(&b, ioutil.Discard, resp.Reader)
|
||||
return b.String(), err
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue