2018-02-05 16:05:59 -05:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
2018-01-16 11:35:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-02-10 18:01:37 -05:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2018-02-09 13:13:26 -05:00
|
|
|
"github.com/docker/docker/integration/internal/request"
|
2018-01-31 11:10:41 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/poll"
|
|
|
|
"github.com/gotestyourself/gotestyourself/skip"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-01-16 11:35:04 -05:00
|
|
|
)
|
|
|
|
|
2018-01-31 11:10:41 -05:00
|
|
|
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()
|
|
|
|
|
2018-02-10 18:01:37 -05:00
|
|
|
cID := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
|
|
|
|
c.HostConfig.Resources = containertypes.Resources{
|
|
|
|
Memory: 200 * 1024 * 1024,
|
|
|
|
}
|
|
|
|
})
|
2018-01-31 11:10:41 -05:00
|
|
|
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-01-31 11:10:41 -05:00
|
|
|
|
2018-02-15 14:59:53 -05:00
|
|
|
const (
|
|
|
|
setMemory int64 = 314572800
|
2018-02-20 16:31:03 -05:00
|
|
|
setMemorySwap int64 = 524288000
|
2018-02-15 14:59:53 -05:00
|
|
|
)
|
|
|
|
|
2018-02-10 18:01:37 -05:00
|
|
|
_, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
|
|
|
|
Resources: containertypes.Resources{
|
2018-02-15 14:59:53 -05:00
|
|
|
Memory: setMemory,
|
|
|
|
MemorySwap: setMemorySwap,
|
2018-01-31 11:10:41 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-02-10 18:01:37 -05:00
|
|
|
inspect, err := client.ContainerInspect(ctx, cID)
|
2018-01-31 11:10:41 -05:00
|
|
|
require.NoError(t, err)
|
2018-02-15 14:59:53 -05:00
|
|
|
assert.Equal(t, setMemory, inspect.HostConfig.Memory)
|
|
|
|
assert.Equal(t, setMemorySwap, inspect.HostConfig.MemorySwap)
|
2018-01-31 11:10:41 -05:00
|
|
|
|
2018-02-15 14:59:53 -05:00
|
|
|
res, err := container.Exec(ctx, client, cID,
|
|
|
|
[]string{"cat", "/sys/fs/cgroup/memory/memory.limit_in_bytes"})
|
2018-01-31 11:10:41 -05:00
|
|
|
require.NoError(t, err)
|
2018-02-15 14:59:53 -05:00
|
|
|
require.Empty(t, res.Stderr())
|
|
|
|
require.Equal(t, 0, res.ExitCode)
|
|
|
|
assert.Equal(t, strconv.FormatInt(setMemory, 10), strings.TrimSpace(res.Stdout()))
|
2018-01-31 11:10:41 -05:00
|
|
|
|
2018-02-15 14:59:53 -05:00
|
|
|
res, err = container.Exec(ctx, client, cID,
|
|
|
|
[]string{"cat", "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"})
|
2018-01-31 11:10:41 -05:00
|
|
|
require.NoError(t, err)
|
2018-02-15 14:59:53 -05:00
|
|
|
require.Empty(t, res.Stderr())
|
|
|
|
require.Equal(t, 0, res.ExitCode)
|
|
|
|
assert.Equal(t, strconv.FormatInt(setMemorySwap, 10), strings.TrimSpace(res.Stdout()))
|
2018-01-31 11:10:41 -05:00
|
|
|
}
|
|
|
|
|
2018-02-20 16:36:27 -05:00
|
|
|
func TestUpdateCPUQuota(t *testing.T) {
|
2018-01-16 11:35:04 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-02-10 18:01:37 -05:00
|
|
|
defer setupTest(t)()
|
2018-01-16 11:35:04 -05:00
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-02-10 18:01:37 -05:00
|
|
|
cID := container.Run(t, ctx, client)
|
2018-01-16 11:35:04 -05:00
|
|
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
desc string
|
|
|
|
update int64
|
|
|
|
}{
|
|
|
|
{desc: "some random value", update: 15000},
|
|
|
|
{desc: "a higher value", update: 20000},
|
|
|
|
{desc: "a lower value", update: 10000},
|
|
|
|
{desc: "unset value", update: -1},
|
|
|
|
} {
|
2018-02-10 18:01:37 -05:00
|
|
|
if _, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
|
|
|
|
Resources: containertypes.Resources{
|
2018-01-16 11:35:04 -05:00
|
|
|
CPUQuota: test.update,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-02-10 18:01:37 -05:00
|
|
|
inspect, err := client.ContainerInspect(ctx, cID)
|
2018-02-15 14:39:21 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, test.update, inspect.HostConfig.CPUQuota)
|
2018-01-16 11:35:04 -05:00
|
|
|
|
2018-02-15 14:39:21 -05:00
|
|
|
res, err := container.Exec(ctx, client, cID,
|
|
|
|
[]string{"/bin/cat", "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, res.Stderr())
|
|
|
|
require.Equal(t, 0, res.ExitCode)
|
2018-01-16 11:35:04 -05:00
|
|
|
|
2018-02-15 14:39:21 -05:00
|
|
|
assert.Equal(t, strconv.FormatInt(test.update, 10), strings.TrimSpace(res.Stdout()))
|
2018-01-16 11:35:04 -05:00
|
|
|
}
|
|
|
|
}
|