docker_cli_update_unix_test.go 9.3 KB

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