1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
//go:build !windows
package main
import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
"testing"
"time"
"github.com/creack/pty"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/testutil"
"github.com/docker/docker/testutil/request"
"gotest.tools/v3/assert"
)
func (s *DockerCLIUpdateSuite) TearDownTest(ctx context.Context, c *testing.T) {
s.ds.TearDownTest(ctx, c)
}
func (s *DockerCLIUpdateSuite) OnTimeout(c *testing.T) {
s.ds.OnTimeout(c)
}
func (s *DockerCLIUpdateSuite) TestUpdateRunningContainer(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, memoryLimitSupport)
const name = "test-update-container"
cli.DockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "top")
cli.DockerCmd(c, "update", "-m", "500M", name)
assert.Equal(c, inspectField(c, name, "HostConfig.Memory"), "524288000")
const file = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
out := cli.DockerCmd(c, "exec", name, "cat", file).Stdout()
assert.Equal(c, strings.TrimSpace(out), "524288000")
}
func (s *DockerCLIUpdateSuite) TestUpdateRunningContainerWithRestart(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, memoryLimitSupport)
const name = "test-update-container"
cli.DockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "top")
cli.DockerCmd(c, "update", "-m", "500M", name)
cli.DockerCmd(c, "restart", name)
assert.Equal(c, inspectField(c, name, "HostConfig.Memory"), "524288000")
const file = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
out := cli.DockerCmd(c, "exec", name, "cat", file).Stdout()
assert.Equal(c, strings.TrimSpace(out), "524288000")
}
func (s *DockerCLIUpdateSuite) TestUpdateStoppedContainer(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, memoryLimitSupport)
const name = "test-update-container"
const file = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
cli.DockerCmd(c, "run", "--name", name, "-m", "300M", "busybox", "cat", file)
cli.DockerCmd(c, "update", "-m", "500M", name)
assert.Equal(c, inspectField(c, name, "HostConfig.Memory"), "524288000")
out := cli.DockerCmd(c, "start", "-a", name).Stdout()
assert.Equal(c, strings.TrimSpace(out), "524288000")
}
func (s *DockerCLIUpdateSuite) TestUpdatePausedContainer(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, cpuShare)
const name = "test-update-container"
cli.DockerCmd(c, "run", "-d", "--name", name, "--cpu-shares", "1000", "busybox", "top")
cli.DockerCmd(c, "pause", name)
cli.DockerCmd(c, "update", "--cpu-shares", "500", name)
assert.Equal(c, inspectField(c, name, "HostConfig.CPUShares"), "500")
cli.DockerCmd(c, "unpause", name)
const file = "/sys/fs/cgroup/cpu/cpu.shares"
out := cli.DockerCmd(c, "exec", name, "cat", file).Stdout()
assert.Equal(c, strings.TrimSpace(out), "500")
}
func (s *DockerCLIUpdateSuite) TestUpdateWithUntouchedFields(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, memoryLimitSupport)
testRequires(c, cpuShare)
const name = "test-update-container"
cli.DockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "--cpu-shares", "800", "busybox", "top")
cli.DockerCmd(c, "update", "-m", "500M", name)
// Update memory and not touch cpus, `cpuset.cpus` should still have the old value
out := inspectField(c, name, "HostConfig.CPUShares")
assert.Equal(c, out, "800")
const file = "/sys/fs/cgroup/cpu/cpu.shares"
out = cli.DockerCmd(c, "exec", name, "cat", file).Stdout()
assert.Equal(c, strings.TrimSpace(out), "800")
}
func (s *DockerCLIUpdateSuite) TestUpdateContainerInvalidValue(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, memoryLimitSupport)
const name = "test-update-container"
cli.DockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "true")
out, _, err := dockerCmdWithError("update", "-m", "2M", name)
assert.ErrorContains(c, err, "")
expected := "Minimum memory limit allowed is 6MB"
assert.Assert(c, strings.Contains(out, expected))
}
func (s *DockerCLIUpdateSuite) TestUpdateContainerWithoutFlags(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, memoryLimitSupport)
const name = "test-update-container"
cli.DockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "true")
_, _, err := dockerCmdWithError("update", name)
assert.ErrorContains(c, err, "")
}
func (s *DockerCLIUpdateSuite) TestUpdateSwapMemoryOnly(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, memoryLimitSupport)
testRequires(c, swapMemorySupport)
const name = "test-update-container"
cli.DockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "--memory-swap", "500M", "busybox", "top")
cli.DockerCmd(c, "update", "--memory-swap", "600M", name)
assert.Equal(c, inspectField(c, name, "HostConfig.MemorySwap"), "629145600")
const file = "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
out := cli.DockerCmd(c, "exec", name, "cat", file).Stdout()
assert.Equal(c, strings.TrimSpace(out), "629145600")
}
func (s *DockerCLIUpdateSuite) TestUpdateInvalidSwapMemory(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, memoryLimitSupport)
testRequires(c, swapMemorySupport)
const name = "test-update-container"
cli.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
assert.ErrorContains(c, err, "")
// Update invalid swap memory with failure should not change HostConfig
assert.Equal(c, inspectField(c, name, "HostConfig.Memory"), "314572800")
assert.Equal(c, inspectField(c, name, "HostConfig.MemorySwap"), "524288000")
cli.DockerCmd(c, "update", "--memory-swap", "600M", name)
assert.Equal(c, inspectField(c, name, "HostConfig.MemorySwap"), "629145600")
const file = "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
out := cli.DockerCmd(c, "exec", name, "cat", file).Stdout()
assert.Equal(c, strings.TrimSpace(out), "629145600")
}
func (s *DockerCLIUpdateSuite) TestUpdateStats(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, memoryLimitSupport)
testRequires(c, cpuCfsQuota)
const name = "foo"
cli.DockerCmd(c, "run", "-d", "-ti", "--name", name, "-m", "500m", "busybox")
cli.WaitRun(c, name)
getMemLimit := func(id string) uint64 {
resp, body, err := request.Get(testutil.GetContext(c), fmt.Sprintf("/containers/%s/stats?stream=false", id))
assert.NilError(c, err)
assert.Equal(c, resp.Header.Get("Content-Type"), "application/json")
var v *types.Stats
err = json.NewDecoder(body).Decode(&v)
assert.NilError(c, err)
body.Close()
return v.MemoryStats.Limit
}
preMemLimit := getMemLimit(name)
cli.DockerCmd(c, "update", "--cpu-quota", "2000", name)
curMemLimit := getMemLimit(name)
assert.Equal(c, preMemLimit, curMemLimit)
}
func (s *DockerCLIUpdateSuite) TestUpdateMemoryWithSwapMemory(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, memoryLimitSupport)
testRequires(c, swapMemorySupport)
const name = "test-update-container"
cli.DockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "busybox", "top")
out, _, err := dockerCmdWithError("update", "--memory", "800M", name)
assert.ErrorContains(c, err, "")
assert.Assert(c, strings.Contains(out, "Memory limit should be smaller than already set memoryswap limit"))
cli.DockerCmd(c, "update", "--memory", "800M", "--memory-swap", "1000M", name)
}
func (s *DockerCLIUpdateSuite) TestUpdateNotAffectMonitorRestartPolicy(c *testing.T) {
testRequires(c, DaemonIsLinux, cpuShare)
id := cli.DockerCmd(c, "run", "-tid", "--restart=always", "busybox", "sh").Stdout()
id = strings.TrimSpace(id)
cli.DockerCmd(c, "update", "--cpu-shares", "512", id)
cpty, tty, err := pty.Open()
assert.NilError(c, err)
defer cpty.Close()
cmd := exec.Command(dockerBinary, "attach", id)
cmd.Stdin = tty
assert.NilError(c, cmd.Start())
defer cmd.Process.Kill()
_, err = cpty.Write([]byte("exit\n"))
assert.NilError(c, err)
assert.NilError(c, cmd.Wait())
// container should restart again and keep running
err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
assert.NilError(c, err)
cli.WaitRun(c, id)
}
func (s *DockerCLIUpdateSuite) TestUpdateWithNanoCPUs(c *testing.T) {
testRequires(c, cpuCfsQuota, cpuCfsPeriod)
const file1 = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
const file2 = "/sys/fs/cgroup/cpu/cpu.cfs_period_us"
out := cli.DockerCmd(c, "run", "-d", "--cpus", "0.5", "--name", "top", "busybox", "top").Stdout()
assert.Assert(c, strings.TrimSpace(out) != "")
out = cli.DockerCmd(c, "exec", "top", "sh", "-c", fmt.Sprintf("cat %s && cat %s", file1, file2)).Combined()
assert.Equal(c, strings.TrimSpace(out), "50000\n100000")
clt, err := client.NewClientWithOpts(client.FromEnv)
assert.NilError(c, err)
inspect, err := clt.ContainerInspect(testutil.GetContext(c), "top")
assert.NilError(c, err)
assert.Equal(c, inspect.HostConfig.NanoCPUs, int64(500000000))
out = inspectField(c, "top", "HostConfig.CpuQuota")
assert.Equal(c, out, "0", "CPU CFS quota should be 0")
out = inspectField(c, "top", "HostConfig.CpuPeriod")
assert.Equal(c, out, "0", "CPU CFS period should be 0")
out, _, err = dockerCmdWithError("update", "--cpu-quota", "80000", "top")
assert.ErrorContains(c, err, "")
assert.Assert(c, strings.Contains(out, "Conflicting options: CPU Quota cannot be updated as NanoCPUs has already been set"))
cli.DockerCmd(c, "update", "--cpus", "0.8", "top")
inspect, err = clt.ContainerInspect(testutil.GetContext(c), "top")
assert.NilError(c, err)
assert.Equal(c, inspect.HostConfig.NanoCPUs, int64(800000000))
out = inspectField(c, "top", "HostConfig.CpuQuota")
assert.Equal(c, out, "0", "CPU CFS quota should be 0")
out = inspectField(c, "top", "HostConfig.CpuPeriod")
assert.Equal(c, out, "0", "CPU CFS period should be 0")
out = cli.DockerCmd(c, "exec", "top", "sh", "-c", fmt.Sprintf("cat %s && cat %s", file1, file2)).Combined()
assert.Equal(c, strings.TrimSpace(out), "80000\n100000")
}
|