2018-02-05 21:05:59 +00:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
2018-01-16 16:35:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-02-10 23:01:37 +00:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2019-02-24 13:11:00 +00:00
|
|
|
"github.com/docker/docker/client"
|
2018-02-10 23:01:37 +00:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2023-07-14 18:02:38 +00:00
|
|
|
"github.com/docker/docker/testutil"
|
2019-08-29 20:52:40 +00:00
|
|
|
"github.com/docker/docker/testutil/request"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-01-16 16:35:04 +00:00
|
|
|
)
|
|
|
|
|
2018-01-31 16:10:41 +00:00
|
|
|
func TestUpdateMemory(t *testing.T) {
|
2018-04-19 09:14:15 +00:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2020-02-18 09:43:56 +00:00
|
|
|
skip.If(t, testEnv.DaemonInfo.CgroupDriver == "none")
|
2018-01-31 16:10:41 +00:00
|
|
|
skip.If(t, !testEnv.DaemonInfo.MemoryLimit)
|
|
|
|
skip.If(t, !testEnv.DaemonInfo.SwapLimit)
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := setupTest(t)
|
2023-08-11 11:45:45 +00:00
|
|
|
apiClient := testEnv.APIClient()
|
2018-01-31 16:10:41 +00:00
|
|
|
|
2023-08-11 11:45:45 +00:00
|
|
|
cID := container.Run(ctx, t, apiClient, func(c *container.TestContainerConfig) {
|
2018-02-10 23:01:37 +00:00
|
|
|
c.HostConfig.Resources = containertypes.Resources{
|
|
|
|
Memory: 200 * 1024 * 1024,
|
|
|
|
}
|
|
|
|
})
|
2018-01-31 16:10:41 +00:00
|
|
|
|
2018-02-15 19:59:53 +00:00
|
|
|
const (
|
|
|
|
setMemory int64 = 314572800
|
2018-02-20 21:31:03 +00:00
|
|
|
setMemorySwap int64 = 524288000
|
2018-02-15 19:59:53 +00:00
|
|
|
)
|
|
|
|
|
2023-08-11 11:45:45 +00:00
|
|
|
_, err := apiClient.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
|
2018-02-10 23:01:37 +00:00
|
|
|
Resources: containertypes.Resources{
|
2018-02-15 19:59:53 +00:00
|
|
|
Memory: setMemory,
|
|
|
|
MemorySwap: setMemorySwap,
|
2018-01-31 16:10:41 +00:00
|
|
|
},
|
|
|
|
})
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2018-01-31 16:10:41 +00:00
|
|
|
|
2023-08-11 11:45:45 +00:00
|
|
|
inspect, err := apiClient.ContainerInspect(ctx, cID)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(setMemory, inspect.HostConfig.Memory))
|
|
|
|
assert.Check(t, is.Equal(setMemorySwap, inspect.HostConfig.MemorySwap))
|
2018-01-31 16:10:41 +00:00
|
|
|
|
2020-06-03 13:37:14 +00:00
|
|
|
memoryFile := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
|
|
|
|
if testEnv.DaemonInfo.CgroupVersion == "2" {
|
|
|
|
memoryFile = "/sys/fs/cgroup/memory.max"
|
|
|
|
}
|
2023-08-11 11:45:45 +00:00
|
|
|
res, err := container.Exec(ctx, apiClient, cID,
|
2020-06-03 13:37:14 +00:00
|
|
|
[]string{"cat", memoryFile})
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.Len(res.Stderr(), 0))
|
|
|
|
assert.Equal(t, 0, res.ExitCode)
|
|
|
|
assert.Check(t, is.Equal(strconv.FormatInt(setMemory, 10), strings.TrimSpace(res.Stdout())))
|
2018-01-31 16:10:41 +00:00
|
|
|
|
2020-06-03 13:37:14 +00:00
|
|
|
// see ConvertMemorySwapToCgroupV2Value() for the convention:
|
|
|
|
// https://github.com/opencontainers/runc/commit/c86be8a2c118ca7bad7bbe9eaf106c659a83940d
|
|
|
|
if testEnv.DaemonInfo.CgroupVersion == "2" {
|
2023-08-11 11:45:45 +00:00
|
|
|
res, err = container.Exec(ctx, apiClient, cID,
|
2020-06-03 13:37:14 +00:00
|
|
|
[]string{"cat", "/sys/fs/cgroup/memory.swap.max"})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.Len(res.Stderr(), 0))
|
|
|
|
assert.Equal(t, 0, res.ExitCode)
|
|
|
|
assert.Check(t, is.Equal(strconv.FormatInt(setMemorySwap-setMemory, 10), strings.TrimSpace(res.Stdout())))
|
|
|
|
} else {
|
2023-08-11 11:45:45 +00:00
|
|
|
res, err = container.Exec(ctx, apiClient, cID,
|
2020-06-03 13:37:14 +00:00
|
|
|
[]string{"cat", "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.Len(res.Stderr(), 0))
|
|
|
|
assert.Equal(t, 0, res.ExitCode)
|
|
|
|
assert.Check(t, is.Equal(strconv.FormatInt(setMemorySwap, 10), strings.TrimSpace(res.Stdout())))
|
|
|
|
}
|
2018-01-31 16:10:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-20 21:36:27 +00:00
|
|
|
func TestUpdateCPUQuota(t *testing.T) {
|
2020-02-18 09:43:56 +00:00
|
|
|
skip.If(t, testEnv.DaemonInfo.CgroupDriver == "none")
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := setupTest(t)
|
2023-08-11 11:45:45 +00:00
|
|
|
apiClient := testEnv.APIClient()
|
2018-01-16 16:35:04 +00:00
|
|
|
|
2023-08-11 11:45:45 +00:00
|
|
|
cID := container.Run(ctx, t, apiClient)
|
2018-01-16 16:35:04 +00: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},
|
|
|
|
} {
|
2020-06-03 13:37:14 +00:00
|
|
|
if testEnv.DaemonInfo.CgroupVersion == "2" {
|
|
|
|
// On v2, specifying CPUQuota without CPUPeriod is currently broken:
|
|
|
|
// https://github.com/opencontainers/runc/issues/2456
|
|
|
|
// As a workaround we set them together.
|
2023-08-11 11:45:45 +00:00
|
|
|
_, err := apiClient.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
|
2020-06-03 13:37:14 +00:00
|
|
|
Resources: containertypes.Resources{
|
|
|
|
CPUQuota: test.update,
|
|
|
|
CPUPeriod: 100000,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
} else {
|
2023-08-11 11:45:45 +00:00
|
|
|
_, err := apiClient.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
|
2020-06-03 13:37:14 +00:00
|
|
|
Resources: containertypes.Resources{
|
|
|
|
CPUQuota: test.update,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
2018-01-16 16:35:04 +00:00
|
|
|
|
2023-08-11 11:45:45 +00:00
|
|
|
inspect, err := apiClient.ContainerInspect(ctx, cID)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(test.update, inspect.HostConfig.CPUQuota))
|
2018-01-16 16:35:04 +00:00
|
|
|
|
2020-06-03 13:37:14 +00:00
|
|
|
if testEnv.DaemonInfo.CgroupVersion == "2" {
|
2023-08-11 11:45:45 +00:00
|
|
|
res, err := container.Exec(ctx, apiClient, cID,
|
2020-06-03 13:37:14 +00:00
|
|
|
[]string{"/bin/cat", "/sys/fs/cgroup/cpu.max"})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.Len(res.Stderr(), 0))
|
|
|
|
assert.Equal(t, 0, res.ExitCode)
|
|
|
|
|
|
|
|
quotaPeriodPair := strings.Fields(res.Stdout())
|
|
|
|
quota := quotaPeriodPair[0]
|
|
|
|
if test.update == -1 {
|
|
|
|
assert.Check(t, is.Equal("max", quota))
|
|
|
|
} else {
|
|
|
|
assert.Check(t, is.Equal(strconv.FormatInt(test.update, 10), quota))
|
|
|
|
}
|
|
|
|
} else {
|
2023-08-11 11:45:45 +00:00
|
|
|
res, err := container.Exec(ctx, apiClient, cID,
|
2020-06-03 13:37:14 +00:00
|
|
|
[]string{"/bin/cat", "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.Len(res.Stderr(), 0))
|
|
|
|
assert.Equal(t, 0, res.ExitCode)
|
2018-01-16 16:35:04 +00:00
|
|
|
|
2020-06-03 13:37:14 +00:00
|
|
|
assert.Check(t, is.Equal(strconv.FormatInt(test.update, 10), strings.TrimSpace(res.Stdout())))
|
|
|
|
}
|
2018-01-16 16:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-11 11:28:13 +00:00
|
|
|
|
|
|
|
func TestUpdatePidsLimit(t *testing.T) {
|
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2020-02-18 09:43:56 +00:00
|
|
|
skip.If(t, testEnv.DaemonInfo.CgroupDriver == "none")
|
2017-04-11 11:28:13 +00:00
|
|
|
skip.If(t, !testEnv.DaemonInfo.PidsLimit)
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := setupTest(t)
|
2019-02-24 13:11:00 +00:00
|
|
|
apiClient := testEnv.APIClient()
|
2023-08-11 11:45:45 +00:00
|
|
|
oldAPIClient := request.NewAPIClient(t, client.WithVersion("1.24"))
|
2017-04-11 11:28:13 +00:00
|
|
|
|
|
|
|
intPtr := func(i int64) *int64 {
|
|
|
|
return &i
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
desc string
|
2019-02-24 13:11:00 +00:00
|
|
|
oldAPI bool
|
2019-02-24 15:26:37 +00:00
|
|
|
initial *int64
|
2017-04-11 11:28:13 +00:00
|
|
|
update *int64
|
|
|
|
expect int64
|
|
|
|
expectCg string
|
|
|
|
}{
|
|
|
|
{desc: "update from none", update: intPtr(32), expect: 32, expectCg: "32"},
|
2019-02-24 15:26:37 +00:00
|
|
|
{desc: "no change", initial: intPtr(32), expect: 32, expectCg: "32"},
|
|
|
|
{desc: "update lower", initial: intPtr(32), update: intPtr(16), expect: 16, expectCg: "16"},
|
|
|
|
{desc: "update on old api ignores value", oldAPI: true, initial: intPtr(32), update: intPtr(16), expect: 32, expectCg: "32"},
|
|
|
|
{desc: "unset limit with zero", initial: intPtr(32), update: intPtr(0), expect: 0, expectCg: "max"},
|
|
|
|
{desc: "unset limit with minus one", initial: intPtr(32), update: intPtr(-1), expect: 0, expectCg: "max"},
|
|
|
|
{desc: "unset limit with minus two", initial: intPtr(32), update: intPtr(-2), expect: 0, expectCg: "max"},
|
2017-04-11 11:28:13 +00:00
|
|
|
} {
|
2019-02-24 13:11:00 +00:00
|
|
|
c := apiClient
|
|
|
|
if test.oldAPI {
|
2023-08-11 11:45:45 +00:00
|
|
|
c = oldAPIClient
|
2019-02-24 13:11:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 11:28:13 +00:00
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.StartSpan(ctx, t)
|
2019-02-24 15:26:37 +00:00
|
|
|
// Using "network=host" to speed up creation (13.96s vs 6.54s)
|
2019-06-06 11:15:31 +00:00
|
|
|
cID := container.Run(ctx, t, apiClient, container.WithPidsLimit(test.initial), container.WithNetworkMode("host"))
|
2019-02-24 15:26:37 +00:00
|
|
|
|
2019-02-24 13:11:00 +00:00
|
|
|
_, err := c.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
|
2017-04-11 11:28:13 +00:00
|
|
|
Resources: containertypes.Resources{
|
|
|
|
PidsLimit: test.update,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2019-02-24 13:11:00 +00:00
|
|
|
inspect, err := c.ContainerInspect(ctx, cID)
|
2017-04-11 11:28:13 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, inspect.HostConfig.Resources.PidsLimit != nil)
|
2019-02-24 15:26:37 +00:00
|
|
|
assert.Equal(t, *inspect.HostConfig.Resources.PidsLimit, test.expect)
|
2017-04-11 11:28:13 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-06-03 13:37:14 +00:00
|
|
|
pidsFile := "/sys/fs/cgroup/pids/pids.max"
|
|
|
|
if testEnv.DaemonInfo.CgroupVersion == "2" {
|
|
|
|
pidsFile = "/sys/fs/cgroup/pids.max"
|
|
|
|
}
|
|
|
|
res, err := container.Exec(ctx, c, cID, []string{"cat", pidsFile})
|
2017-04-11 11:28:13 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.Len(res.Stderr(), 0))
|
|
|
|
|
|
|
|
out := strings.TrimSpace(res.Stdout())
|
|
|
|
assert.Equal(t, out, test.expectCg)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|