2015-12-28 06:19:26 -05:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-09-01 15:14:59 -04:00
|
|
|
"context"
|
2015-12-29 20:33:16 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-12-16 20:57:05 -05:00
|
|
|
"os/exec"
|
2015-12-28 06:19:26 -05:00
|
|
|
"strings"
|
2016-12-16 20:57:05 -05:00
|
|
|
"time"
|
2015-12-28 06:19:26 -05:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-09-01 15:14:59 -04:00
|
|
|
"github.com/docker/docker/client"
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2018-04-17 04:22:04 -04:00
|
|
|
"github.com/docker/docker/internal/test/request"
|
2017-09-14 13:17:49 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers/kernel"
|
2015-12-28 06:19:26 -05:00
|
|
|
"github.com/go-check/check"
|
2016-12-30 04:49:36 -05:00
|
|
|
"github.com/kr/pty"
|
2015-12-28 06:19:26 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestUpdateRunningContainer(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
testRequires(c, memoryLimitSupport)
|
|
|
|
|
|
|
|
name := "test-update-container"
|
|
|
|
dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "top")
|
|
|
|
dockerCmd(c, "update", "-m", "500M", name)
|
|
|
|
|
2016-02-02 20:26:46 -05:00
|
|
|
c.Assert(inspectField(c, name, "HostConfig.Memory"), checker.Equals, "524288000")
|
2015-12-28 06:19:26 -05:00
|
|
|
|
|
|
|
file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
|
|
|
|
out, _ := dockerCmd(c, "exec", name, "cat", file)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "524288000")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestUpdateRunningContainerWithRestart(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
testRequires(c, memoryLimitSupport)
|
|
|
|
|
|
|
|
name := "test-update-container"
|
|
|
|
dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "top")
|
|
|
|
dockerCmd(c, "update", "-m", "500M", name)
|
|
|
|
dockerCmd(c, "restart", name)
|
|
|
|
|
2016-02-02 20:26:46 -05:00
|
|
|
c.Assert(inspectField(c, name, "HostConfig.Memory"), checker.Equals, "524288000")
|
2015-12-28 06:19:26 -05:00
|
|
|
|
|
|
|
file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
|
|
|
|
out, _ := dockerCmd(c, "exec", name, "cat", file)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "524288000")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestUpdateStoppedContainer(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
testRequires(c, memoryLimitSupport)
|
|
|
|
|
|
|
|
name := "test-update-container"
|
|
|
|
file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
|
|
|
|
dockerCmd(c, "run", "--name", name, "-m", "300M", "busybox", "cat", file)
|
|
|
|
dockerCmd(c, "update", "-m", "500M", name)
|
|
|
|
|
2016-02-02 20:26:46 -05:00
|
|
|
c.Assert(inspectField(c, name, "HostConfig.Memory"), checker.Equals, "524288000")
|
2015-12-28 06:19:26 -05:00
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "start", "-a", name)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "524288000")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestUpdatePausedContainer(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
testRequires(c, cpuShare)
|
|
|
|
|
|
|
|
name := "test-update-container"
|
|
|
|
dockerCmd(c, "run", "-d", "--name", name, "--cpu-shares", "1000", "busybox", "top")
|
|
|
|
dockerCmd(c, "pause", name)
|
|
|
|
dockerCmd(c, "update", "--cpu-shares", "500", name)
|
|
|
|
|
2016-02-02 20:26:46 -05:00
|
|
|
c.Assert(inspectField(c, name, "HostConfig.CPUShares"), checker.Equals, "500")
|
2015-12-28 06:19:26 -05:00
|
|
|
|
|
|
|
dockerCmd(c, "unpause", name)
|
|
|
|
file := "/sys/fs/cgroup/cpu/cpu.shares"
|
2016-02-02 20:26:46 -05:00
|
|
|
out, _ := dockerCmd(c, "exec", name, "cat", file)
|
2015-12-28 06:19:26 -05:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "500")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestUpdateWithUntouchedFields(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
testRequires(c, memoryLimitSupport)
|
|
|
|
testRequires(c, cpuShare)
|
|
|
|
|
|
|
|
name := "test-update-container"
|
|
|
|
dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "--cpu-shares", "800", "busybox", "top")
|
|
|
|
dockerCmd(c, "update", "-m", "500M", name)
|
|
|
|
|
|
|
|
// Update memory and not touch cpus, `cpuset.cpus` should still have the old value
|
2016-01-28 09:19:25 -05:00
|
|
|
out := inspectField(c, name, "HostConfig.CPUShares")
|
2015-12-28 06:19:26 -05:00
|
|
|
c.Assert(out, check.Equals, "800")
|
|
|
|
|
|
|
|
file := "/sys/fs/cgroup/cpu/cpu.shares"
|
|
|
|
out, _ = dockerCmd(c, "exec", name, "cat", file)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "800")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestUpdateContainerInvalidValue(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
testRequires(c, memoryLimitSupport)
|
|
|
|
|
|
|
|
name := "test-update-container"
|
|
|
|
dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "true")
|
|
|
|
out, _, err := dockerCmdWithError("update", "-m", "2M", name)
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
expected := "Minimum memory limit allowed is 4MB"
|
|
|
|
c.Assert(out, checker.Contains, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestUpdateContainerWithoutFlags(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
testRequires(c, memoryLimitSupport)
|
|
|
|
|
|
|
|
name := "test-update-container"
|
|
|
|
dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "true")
|
|
|
|
_, _, err := dockerCmdWithError("update", name)
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestUpdateKernelMemory(c *check.C) {
|
2016-03-31 22:32:46 -04:00
|
|
|
testRequires(c, DaemonIsLinux, kernelMemorySupport)
|
2015-12-28 06:19:26 -05:00
|
|
|
|
|
|
|
name := "test-update-container"
|
|
|
|
dockerCmd(c, "run", "-d", "--name", name, "--kernel-memory", "50M", "busybox", "top")
|
2016-07-11 20:07:24 -04:00
|
|
|
dockerCmd(c, "update", "--kernel-memory", "100M", name)
|
|
|
|
|
|
|
|
c.Assert(inspectField(c, name, "HostConfig.KernelMemory"), checker.Equals, "104857600")
|
|
|
|
|
|
|
|
file := "/sys/fs/cgroup/memory/memory.kmem.limit_in_bytes"
|
|
|
|
out, _ := dockerCmd(c, "exec", name, "cat", file)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "104857600")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestUpdateKernelMemoryUninitialized(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux, kernelMemorySupport)
|
|
|
|
|
2017-09-11 10:23:59 -04:00
|
|
|
isNewKernel := CheckKernelVersion(4, 6, 0)
|
2016-07-11 20:07:24 -04:00
|
|
|
name := "test-update-container"
|
|
|
|
dockerCmd(c, "run", "-d", "--name", name, "busybox", "top")
|
2015-12-28 06:19:26 -05:00
|
|
|
_, _, err := dockerCmdWithError("update", "--kernel-memory", "100M", name)
|
2016-08-02 01:17:20 -04:00
|
|
|
// Update kernel memory to a running container without kernel memory initialized
|
|
|
|
// is not allowed before kernel version 4.6.
|
|
|
|
if !isNewKernel {
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
} else {
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
2015-12-28 06:19:26 -05:00
|
|
|
|
2016-03-31 22:32:46 -04:00
|
|
|
dockerCmd(c, "pause", name)
|
2016-08-02 01:17:20 -04:00
|
|
|
_, _, err = dockerCmdWithError("update", "--kernel-memory", "200M", name)
|
|
|
|
if !isNewKernel {
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
} else {
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
2016-03-31 22:32:46 -04:00
|
|
|
dockerCmd(c, "unpause", name)
|
|
|
|
|
2015-12-28 06:19:26 -05:00
|
|
|
dockerCmd(c, "stop", name)
|
2016-08-02 01:17:20 -04:00
|
|
|
dockerCmd(c, "update", "--kernel-memory", "300M", name)
|
2015-12-28 06:19:26 -05:00
|
|
|
dockerCmd(c, "start", name)
|
|
|
|
|
2016-08-02 01:17:20 -04:00
|
|
|
c.Assert(inspectField(c, name, "HostConfig.KernelMemory"), checker.Equals, "314572800")
|
2015-12-28 06:19:26 -05:00
|
|
|
|
|
|
|
file := "/sys/fs/cgroup/memory/memory.kmem.limit_in_bytes"
|
2016-02-02 20:26:46 -05:00
|
|
|
out, _ := dockerCmd(c, "exec", name, "cat", file)
|
2016-08-02 01:17:20 -04:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "314572800")
|
2015-12-28 06:19:26 -05:00
|
|
|
}
|
2015-12-29 20:33:16 -05:00
|
|
|
|
2017-09-14 13:17:49 -04:00
|
|
|
// GetKernelVersion gets the current kernel version.
|
|
|
|
func GetKernelVersion() *kernel.VersionInfo {
|
|
|
|
v, _ := kernel.ParseRelease(testEnv.DaemonInfo.KernelVersion)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckKernelVersion checks if current kernel is newer than (or equal to)
|
|
|
|
// the given version.
|
|
|
|
func CheckKernelVersion(k, major, minor int) bool {
|
2017-10-03 12:09:16 -04:00
|
|
|
return kernel.CompareKernelVersion(*GetKernelVersion(), kernel.VersionInfo{Kernel: k, Major: major, Minor: minor}) >= 0
|
2017-09-14 13:17:49 -04:00
|
|
|
}
|
|
|
|
|
2016-02-24 00:36:47 -05:00
|
|
|
func (s *DockerSuite) TestUpdateSwapMemoryOnly(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
testRequires(c, memoryLimitSupport)
|
|
|
|
testRequires(c, swapMemorySupport)
|
|
|
|
|
|
|
|
name := "test-update-container"
|
|
|
|
dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "--memory-swap", "500M", "busybox", "top")
|
|
|
|
dockerCmd(c, "update", "--memory-swap", "600M", name)
|
|
|
|
|
|
|
|
c.Assert(inspectField(c, name, "HostConfig.MemorySwap"), checker.Equals, "629145600")
|
|
|
|
|
|
|
|
file := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
|
2016-02-24 01:23:48 -05:00
|
|
|
out, _ := dockerCmd(c, "exec", name, "cat", file)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "629145600")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestUpdateInvalidSwapMemory(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
testRequires(c, memoryLimitSupport)
|
|
|
|
testRequires(c, swapMemorySupport)
|
|
|
|
|
|
|
|
name := "test-update-container"
|
|
|
|
dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "--memory-swap", "500M", "busybox", "top")
|
|
|
|
_, _, err := dockerCmdWithError("update", "--memory-swap", "200M", name)
|
|
|
|
// Update invalid swap memory should fail.
|
|
|
|
// This will pass docker config validation, but failed at kernel validation
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
// Update invalid swap memory with failure should not change HostConfig
|
|
|
|
c.Assert(inspectField(c, name, "HostConfig.Memory"), checker.Equals, "314572800")
|
|
|
|
c.Assert(inspectField(c, name, "HostConfig.MemorySwap"), checker.Equals, "524288000")
|
|
|
|
|
|
|
|
dockerCmd(c, "update", "--memory-swap", "600M", name)
|
|
|
|
|
|
|
|
c.Assert(inspectField(c, name, "HostConfig.MemorySwap"), checker.Equals, "629145600")
|
|
|
|
|
|
|
|
file := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
|
2016-02-24 00:36:47 -05:00
|
|
|
out, _ := dockerCmd(c, "exec", name, "cat", file)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "629145600")
|
|
|
|
}
|
|
|
|
|
2015-12-29 20:33:16 -05:00
|
|
|
func (s *DockerSuite) TestUpdateStats(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
testRequires(c, memoryLimitSupport)
|
|
|
|
testRequires(c, cpuCfsQuota)
|
|
|
|
name := "foo"
|
|
|
|
dockerCmd(c, "run", "-d", "-ti", "--name", name, "-m", "500m", "busybox")
|
|
|
|
|
|
|
|
c.Assert(waitRun(name), checker.IsNil)
|
|
|
|
|
|
|
|
getMemLimit := func(id string) uint64 {
|
2017-03-06 10:35:27 -05:00
|
|
|
resp, body, err := request.Get(fmt.Sprintf("/containers/%s/stats?stream=false", id))
|
2015-12-29 20:33:16 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json")
|
|
|
|
|
|
|
|
var v *types.Stats
|
|
|
|
err = json.NewDecoder(body).Decode(&v)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
body.Close()
|
|
|
|
|
|
|
|
return v.MemoryStats.Limit
|
|
|
|
}
|
|
|
|
preMemLimit := getMemLimit(name)
|
|
|
|
|
|
|
|
dockerCmd(c, "update", "--cpu-quota", "2000", name)
|
|
|
|
|
|
|
|
curMemLimit := getMemLimit(name)
|
|
|
|
|
|
|
|
c.Assert(preMemLimit, checker.Equals, curMemLimit)
|
|
|
|
|
|
|
|
}
|
2016-08-08 06:36:03 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestUpdateMemoryWithSwapMemory(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
testRequires(c, memoryLimitSupport)
|
|
|
|
testRequires(c, swapMemorySupport)
|
|
|
|
|
|
|
|
name := "test-update-container"
|
|
|
|
dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "busybox", "top")
|
|
|
|
out, _, err := dockerCmdWithError("update", "--memory", "800M", name)
|
|
|
|
c.Assert(err, checker.NotNil)
|
|
|
|
c.Assert(out, checker.Contains, "Memory limit should be smaller than already set memoryswap limit")
|
|
|
|
|
|
|
|
dockerCmd(c, "update", "--memory", "800M", "--memory-swap", "1000M", name)
|
|
|
|
}
|
2016-12-16 20:57:05 -05:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestUpdateNotAffectMonitorRestartPolicy(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux, cpuShare)
|
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "run", "-tid", "--restart=always", "busybox", "sh")
|
|
|
|
id := strings.TrimSpace(string(out))
|
|
|
|
dockerCmd(c, "update", "--cpu-shares", "512", id)
|
|
|
|
|
|
|
|
cpty, tty, err := pty.Open()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer cpty.Close()
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
cmd.Stdin = tty
|
|
|
|
|
|
|
|
c.Assert(cmd.Start(), checker.IsNil)
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
_, err = cpty.Write([]byte("exit\n"))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
c.Assert(cmd.Wait(), checker.IsNil)
|
|
|
|
|
|
|
|
// container should restart again and keep running
|
|
|
|
err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(waitRun(id), checker.IsNil)
|
|
|
|
}
|
2017-02-18 01:04:37 -05:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestUpdateWithNanoCPUs(c *check.C) {
|
|
|
|
testRequires(c, cpuCfsQuota, cpuCfsPeriod)
|
|
|
|
|
|
|
|
file1 := "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
|
|
|
|
file2 := "/sys/fs/cgroup/cpu/cpu.cfs_period_us"
|
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--cpus", "0.5", "--name", "top", "busybox", "top")
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "exec", "top", "sh", "-c", fmt.Sprintf("cat %s && cat %s", file1, file2))
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "50000\n100000")
|
|
|
|
|
2019-01-03 16:49:00 -05:00
|
|
|
clt, err := client.NewClientWithOpts(client.FromEnv)
|
2017-09-01 15:14:59 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
inspect, err := clt.ContainerInspect(context.Background(), "top")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(inspect.HostConfig.NanoCPUs, checker.Equals, int64(500000000))
|
|
|
|
|
2017-02-18 01:04:37 -05:00
|
|
|
out = inspectField(c, "top", "HostConfig.CpuQuota")
|
|
|
|
c.Assert(out, checker.Equals, "0", check.Commentf("CPU CFS quota should be 0"))
|
|
|
|
out = inspectField(c, "top", "HostConfig.CpuPeriod")
|
|
|
|
c.Assert(out, checker.Equals, "0", check.Commentf("CPU CFS period should be 0"))
|
|
|
|
|
2017-09-01 15:14:59 -04:00
|
|
|
out, _, err = dockerCmdWithError("update", "--cpu-quota", "80000", "top")
|
2017-02-18 01:04:37 -05:00
|
|
|
c.Assert(err, checker.NotNil)
|
|
|
|
c.Assert(out, checker.Contains, "Conflicting options: CPU Quota cannot be updated as NanoCPUs has already been set")
|
|
|
|
|
2018-07-09 13:40:34 -04:00
|
|
|
dockerCmd(c, "update", "--cpus", "0.8", "top")
|
2017-09-01 15:14:59 -04:00
|
|
|
inspect, err = clt.ContainerInspect(context.Background(), "top")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(inspect.HostConfig.NanoCPUs, checker.Equals, int64(800000000))
|
|
|
|
|
2017-02-18 01:04:37 -05:00
|
|
|
out = inspectField(c, "top", "HostConfig.CpuQuota")
|
|
|
|
c.Assert(out, checker.Equals, "0", check.Commentf("CPU CFS quota should be 0"))
|
|
|
|
out = inspectField(c, "top", "HostConfig.CpuPeriod")
|
|
|
|
c.Assert(out, checker.Equals, "0", check.Commentf("CPU CFS period should be 0"))
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "exec", "top", "sh", "-c", fmt.Sprintf("cat %s && cat %s", file1, file2))
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "80000\n100000")
|
|
|
|
}
|