docker_cli_update_unix_test.go 9.3 KB

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