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