docker_cli_update_unix_test.go 9.6 KB

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