docker_cli_update_unix_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // +build !windows
  2. package main
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "github.com/docker/docker/pkg/integration/checker"
  8. "github.com/docker/engine-api/types"
  9. "github.com/go-check/check"
  10. )
  11. func (s *DockerSuite) TestUpdateRunningContainer(c *check.C) {
  12. testRequires(c, DaemonIsLinux)
  13. testRequires(c, memoryLimitSupport)
  14. name := "test-update-container"
  15. dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "top")
  16. dockerCmd(c, "update", "-m", "500M", name)
  17. memory := inspectField(c, name, "HostConfig.Memory")
  18. if memory != "524288000" {
  19. c.Fatalf("Got the wrong memory value, we got %d, expected 524288000(500M).", memory)
  20. }
  21. file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
  22. out, _ := dockerCmd(c, "exec", name, "cat", file)
  23. c.Assert(strings.TrimSpace(out), checker.Equals, "524288000")
  24. }
  25. func (s *DockerSuite) TestUpdateRunningContainerWithRestart(c *check.C) {
  26. testRequires(c, DaemonIsLinux)
  27. testRequires(c, memoryLimitSupport)
  28. name := "test-update-container"
  29. dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "top")
  30. dockerCmd(c, "update", "-m", "500M", name)
  31. dockerCmd(c, "restart", name)
  32. memory := inspectField(c, name, "HostConfig.Memory")
  33. if memory != "524288000" {
  34. c.Fatalf("Got the wrong memory value, we got %d, expected 524288000(500M).", memory)
  35. }
  36. file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
  37. out, _ := dockerCmd(c, "exec", name, "cat", file)
  38. c.Assert(strings.TrimSpace(out), checker.Equals, "524288000")
  39. }
  40. func (s *DockerSuite) TestUpdateStoppedContainer(c *check.C) {
  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. memory := inspectField(c, name, "HostConfig.Memory")
  48. if memory != "524288000" {
  49. c.Fatalf("Got the wrong memory value, we got %d, expected 524288000(500M).", memory)
  50. }
  51. out, _ := dockerCmd(c, "start", "-a", name)
  52. c.Assert(strings.TrimSpace(out), checker.Equals, "524288000")
  53. }
  54. func (s *DockerSuite) TestUpdatePausedContainer(c *check.C) {
  55. testRequires(c, DaemonIsLinux)
  56. testRequires(c, cpuShare)
  57. name := "test-update-container"
  58. dockerCmd(c, "run", "-d", "--name", name, "--cpu-shares", "1000", "busybox", "top")
  59. dockerCmd(c, "pause", name)
  60. dockerCmd(c, "update", "--cpu-shares", "500", name)
  61. out := inspectField(c, name, "HostConfig.CPUShares")
  62. if out != "500" {
  63. c.Fatalf("Got the wrong cpu shares value, we got %d, expected 500.", out)
  64. }
  65. dockerCmd(c, "unpause", name)
  66. file := "/sys/fs/cgroup/cpu/cpu.shares"
  67. out, _ = dockerCmd(c, "exec", name, "cat", file)
  68. c.Assert(strings.TrimSpace(out), checker.Equals, "500")
  69. }
  70. func (s *DockerSuite) TestUpdateWithUntouchedFields(c *check.C) {
  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. c.Assert(out, check.Equals, "800")
  80. file := "/sys/fs/cgroup/cpu/cpu.shares"
  81. out, _ = dockerCmd(c, "exec", name, "cat", file)
  82. c.Assert(strings.TrimSpace(out), checker.Equals, "800")
  83. }
  84. func (s *DockerSuite) TestUpdateContainerInvalidValue(c *check.C) {
  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. c.Assert(err, check.NotNil)
  91. expected := "Minimum memory limit allowed is 4MB"
  92. c.Assert(out, checker.Contains, expected)
  93. }
  94. func (s *DockerSuite) TestUpdateContainerWithoutFlags(c *check.C) {
  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. c.Assert(err, check.NotNil)
  101. }
  102. func (s *DockerSuite) TestUpdateKernelMemory(c *check.C) {
  103. testRequires(c, DaemonIsLinux)
  104. testRequires(c, kernelMemorySupport)
  105. name := "test-update-container"
  106. dockerCmd(c, "run", "-d", "--name", name, "--kernel-memory", "50M", "busybox", "top")
  107. _, _, err := dockerCmdWithError("update", "--kernel-memory", "100M", name)
  108. // Update kernel memory to a running container is not allowed.
  109. c.Assert(err, check.NotNil)
  110. out := inspectField(c, name, "HostConfig.KernelMemory")
  111. // Update kernel memory to a running container with failure should not change HostConfig
  112. if out != "52428800" {
  113. c.Fatalf("Got the wrong memory value, we got %d, expected 52428800(50M).", out)
  114. }
  115. dockerCmd(c, "stop", name)
  116. dockerCmd(c, "update", "--kernel-memory", "100M", name)
  117. dockerCmd(c, "start", name)
  118. out = inspectField(c, name, "HostConfig.KernelMemory")
  119. if out != "104857600" {
  120. c.Fatalf("Got the wrong memory value, we got %d, expected 104857600(100M).", out)
  121. }
  122. file := "/sys/fs/cgroup/memory/memory.kmem.limit_in_bytes"
  123. out, _ = dockerCmd(c, "exec", name, "cat", file)
  124. c.Assert(strings.TrimSpace(out), checker.Equals, "104857600")
  125. }
  126. func (s *DockerSuite) TestUpdateStats(c *check.C) {
  127. testRequires(c, DaemonIsLinux)
  128. testRequires(c, memoryLimitSupport)
  129. testRequires(c, cpuCfsQuota)
  130. name := "foo"
  131. dockerCmd(c, "run", "-d", "-ti", "--name", name, "-m", "500m", "busybox")
  132. c.Assert(waitRun(name), checker.IsNil)
  133. getMemLimit := func(id string) uint64 {
  134. resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
  135. c.Assert(err, checker.IsNil)
  136. c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json")
  137. var v *types.Stats
  138. err = json.NewDecoder(body).Decode(&v)
  139. c.Assert(err, checker.IsNil)
  140. body.Close()
  141. return v.MemoryStats.Limit
  142. }
  143. preMemLimit := getMemLimit(name)
  144. dockerCmd(c, "update", "--cpu-quota", "2000", name)
  145. curMemLimit := getMemLimit(name)
  146. c.Assert(preMemLimit, checker.Equals, curMemLimit)
  147. }