docker_cli_update_unix_test.go 9.6 KB

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