docker_cli_update_unix_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // +build !windows
  2. package main
  3. import (
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "os/exec"
  8. "strings"
  9. "time"
  10. "github.com/creack/pty"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/client"
  13. "github.com/docker/docker/internal/test/request"
  14. "github.com/docker/docker/pkg/parsers/kernel"
  15. "github.com/go-check/check"
  16. "gotest.tools/assert"
  17. )
  18. func (s *DockerSuite) TestUpdateRunningContainer(c *check.C) {
  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 *check.C) {
  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 *check.C) {
  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 *check.C) {
  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 *check.C) {
  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 *check.C) {
  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 4MB"
  87. assert.Assert(c, strings.Contains(out, expected))
  88. }
  89. func (s *DockerSuite) TestUpdateContainerWithoutFlags(c *check.C) {
  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) TestUpdateKernelMemory(c *check.C) {
  98. testRequires(c, DaemonIsLinux, kernelMemorySupport)
  99. name := "test-update-container"
  100. dockerCmd(c, "run", "-d", "--name", name, "--kernel-memory", "50M", "busybox", "top")
  101. dockerCmd(c, "update", "--kernel-memory", "100M", name)
  102. assert.Equal(c, inspectField(c, name, "HostConfig.KernelMemory"), "104857600")
  103. file := "/sys/fs/cgroup/memory/memory.kmem.limit_in_bytes"
  104. out, _ := dockerCmd(c, "exec", name, "cat", file)
  105. assert.Equal(c, strings.TrimSpace(out), "104857600")
  106. }
  107. func (s *DockerSuite) TestUpdateKernelMemoryUninitialized(c *check.C) {
  108. testRequires(c, DaemonIsLinux, kernelMemorySupport)
  109. isNewKernel := CheckKernelVersion(4, 6, 0)
  110. name := "test-update-container"
  111. dockerCmd(c, "run", "-d", "--name", name, "busybox", "top")
  112. _, _, err := dockerCmdWithError("update", "--kernel-memory", "100M", name)
  113. // Update kernel memory to a running container without kernel memory initialized
  114. // is not allowed before kernel version 4.6.
  115. if !isNewKernel {
  116. assert.ErrorContains(c, err, "")
  117. } else {
  118. assert.NilError(c, err)
  119. }
  120. dockerCmd(c, "pause", name)
  121. _, _, err = dockerCmdWithError("update", "--kernel-memory", "200M", name)
  122. if !isNewKernel {
  123. assert.ErrorContains(c, err, "")
  124. } else {
  125. assert.NilError(c, err)
  126. }
  127. dockerCmd(c, "unpause", name)
  128. dockerCmd(c, "stop", name)
  129. dockerCmd(c, "update", "--kernel-memory", "300M", name)
  130. dockerCmd(c, "start", name)
  131. assert.Equal(c, inspectField(c, name, "HostConfig.KernelMemory"), "314572800")
  132. file := "/sys/fs/cgroup/memory/memory.kmem.limit_in_bytes"
  133. out, _ := dockerCmd(c, "exec", name, "cat", file)
  134. assert.Equal(c, strings.TrimSpace(out), "314572800")
  135. }
  136. // GetKernelVersion gets the current kernel version.
  137. func GetKernelVersion() *kernel.VersionInfo {
  138. v, _ := kernel.ParseRelease(testEnv.DaemonInfo.KernelVersion)
  139. return v
  140. }
  141. // CheckKernelVersion checks if current kernel is newer than (or equal to)
  142. // the given version.
  143. func CheckKernelVersion(k, major, minor int) bool {
  144. return kernel.CompareKernelVersion(*GetKernelVersion(), kernel.VersionInfo{Kernel: k, Major: major, Minor: minor}) >= 0
  145. }
  146. func (s *DockerSuite) TestUpdateSwapMemoryOnly(c *check.C) {
  147. testRequires(c, DaemonIsLinux)
  148. testRequires(c, memoryLimitSupport)
  149. testRequires(c, swapMemorySupport)
  150. name := "test-update-container"
  151. dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "--memory-swap", "500M", "busybox", "top")
  152. dockerCmd(c, "update", "--memory-swap", "600M", name)
  153. assert.Equal(c, inspectField(c, name, "HostConfig.MemorySwap"), "629145600")
  154. file := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
  155. out, _ := dockerCmd(c, "exec", name, "cat", file)
  156. assert.Equal(c, strings.TrimSpace(out), "629145600")
  157. }
  158. func (s *DockerSuite) TestUpdateInvalidSwapMemory(c *check.C) {
  159. testRequires(c, DaemonIsLinux)
  160. testRequires(c, memoryLimitSupport)
  161. testRequires(c, swapMemorySupport)
  162. name := "test-update-container"
  163. dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "--memory-swap", "500M", "busybox", "top")
  164. _, _, err := dockerCmdWithError("update", "--memory-swap", "200M", name)
  165. // Update invalid swap memory should fail.
  166. // This will pass docker config validation, but failed at kernel validation
  167. assert.ErrorContains(c, err, "")
  168. // Update invalid swap memory with failure should not change HostConfig
  169. assert.Equal(c, inspectField(c, name, "HostConfig.Memory"), "314572800")
  170. assert.Equal(c, inspectField(c, name, "HostConfig.MemorySwap"), "524288000")
  171. dockerCmd(c, "update", "--memory-swap", "600M", name)
  172. assert.Equal(c, inspectField(c, name, "HostConfig.MemorySwap"), "629145600")
  173. file := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
  174. out, _ := dockerCmd(c, "exec", name, "cat", file)
  175. assert.Equal(c, strings.TrimSpace(out), "629145600")
  176. }
  177. func (s *DockerSuite) TestUpdateStats(c *check.C) {
  178. testRequires(c, DaemonIsLinux)
  179. testRequires(c, memoryLimitSupport)
  180. testRequires(c, cpuCfsQuota)
  181. name := "foo"
  182. dockerCmd(c, "run", "-d", "-ti", "--name", name, "-m", "500m", "busybox")
  183. assert.NilError(c, waitRun(name))
  184. getMemLimit := func(id string) uint64 {
  185. resp, body, err := request.Get(fmt.Sprintf("/containers/%s/stats?stream=false", id))
  186. assert.NilError(c, err)
  187. assert.Equal(c, resp.Header.Get("Content-Type"), "application/json")
  188. var v *types.Stats
  189. err = json.NewDecoder(body).Decode(&v)
  190. assert.NilError(c, err)
  191. body.Close()
  192. return v.MemoryStats.Limit
  193. }
  194. preMemLimit := getMemLimit(name)
  195. dockerCmd(c, "update", "--cpu-quota", "2000", name)
  196. curMemLimit := getMemLimit(name)
  197. assert.Equal(c, preMemLimit, curMemLimit)
  198. }
  199. func (s *DockerSuite) TestUpdateMemoryWithSwapMemory(c *check.C) {
  200. testRequires(c, DaemonIsLinux)
  201. testRequires(c, memoryLimitSupport)
  202. testRequires(c, swapMemorySupport)
  203. name := "test-update-container"
  204. dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "busybox", "top")
  205. out, _, err := dockerCmdWithError("update", "--memory", "800M", name)
  206. assert.ErrorContains(c, err, "")
  207. assert.Assert(c, strings.Contains(out, "Memory limit should be smaller than already set memoryswap limit"))
  208. dockerCmd(c, "update", "--memory", "800M", "--memory-swap", "1000M", name)
  209. }
  210. func (s *DockerSuite) TestUpdateNotAffectMonitorRestartPolicy(c *check.C) {
  211. testRequires(c, DaemonIsLinux, cpuShare)
  212. out, _ := dockerCmd(c, "run", "-tid", "--restart=always", "busybox", "sh")
  213. id := strings.TrimSpace(string(out))
  214. dockerCmd(c, "update", "--cpu-shares", "512", id)
  215. cpty, tty, err := pty.Open()
  216. assert.NilError(c, err)
  217. defer cpty.Close()
  218. cmd := exec.Command(dockerBinary, "attach", id)
  219. cmd.Stdin = tty
  220. assert.NilError(c, cmd.Start())
  221. defer cmd.Process.Kill()
  222. _, err = cpty.Write([]byte("exit\n"))
  223. assert.NilError(c, err)
  224. assert.NilError(c, cmd.Wait())
  225. // container should restart again and keep running
  226. err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
  227. assert.NilError(c, err)
  228. assert.NilError(c, waitRun(id))
  229. }
  230. func (s *DockerSuite) TestUpdateWithNanoCPUs(c *check.C) {
  231. testRequires(c, cpuCfsQuota, cpuCfsPeriod)
  232. file1 := "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
  233. file2 := "/sys/fs/cgroup/cpu/cpu.cfs_period_us"
  234. out, _ := dockerCmd(c, "run", "-d", "--cpus", "0.5", "--name", "top", "busybox", "top")
  235. assert.Assert(c, strings.TrimSpace(out) != "")
  236. out, _ = dockerCmd(c, "exec", "top", "sh", "-c", fmt.Sprintf("cat %s && cat %s", file1, file2))
  237. assert.Equal(c, strings.TrimSpace(out), "50000\n100000")
  238. clt, err := client.NewClientWithOpts(client.FromEnv)
  239. assert.NilError(c, err)
  240. inspect, err := clt.ContainerInspect(context.Background(), "top")
  241. assert.NilError(c, err)
  242. assert.Equal(c, inspect.HostConfig.NanoCPUs, int64(500000000))
  243. out = inspectField(c, "top", "HostConfig.CpuQuota")
  244. assert.Equal(c, out, "0", "CPU CFS quota should be 0")
  245. out = inspectField(c, "top", "HostConfig.CpuPeriod")
  246. assert.Equal(c, out, "0", "CPU CFS period should be 0")
  247. out, _, err = dockerCmdWithError("update", "--cpu-quota", "80000", "top")
  248. assert.ErrorContains(c, err, "")
  249. assert.Assert(c, strings.Contains(out, "Conflicting options: CPU Quota cannot be updated as NanoCPUs has already been set"))
  250. dockerCmd(c, "update", "--cpus", "0.8", "top")
  251. inspect, err = clt.ContainerInspect(context.Background(), "top")
  252. assert.NilError(c, err)
  253. assert.Equal(c, inspect.HostConfig.NanoCPUs, int64(800000000))
  254. out = inspectField(c, "top", "HostConfig.CpuQuota")
  255. assert.Equal(c, out, "0", "CPU CFS quota should be 0")
  256. out = inspectField(c, "top", "HostConfig.CpuPeriod")
  257. assert.Equal(c, out, "0", "CPU CFS period should be 0")
  258. out, _ = dockerCmd(c, "exec", "top", "sh", "-c", fmt.Sprintf("cat %s && cat %s", file1, file2))
  259. assert.Equal(c, strings.TrimSpace(out), "80000\n100000")
  260. }