update_linux_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "strconv"
  5. "strings"
  6. "testing"
  7. "time"
  8. containertypes "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/client"
  10. "github.com/docker/docker/integration/internal/container"
  11. "github.com/docker/docker/testutil/request"
  12. "gotest.tools/v3/assert"
  13. is "gotest.tools/v3/assert/cmp"
  14. "gotest.tools/v3/poll"
  15. "gotest.tools/v3/skip"
  16. )
  17. func TestUpdateMemory(t *testing.T) {
  18. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  19. skip.If(t, testEnv.DaemonInfo.CgroupDriver == "none")
  20. skip.If(t, !testEnv.DaemonInfo.MemoryLimit)
  21. skip.If(t, !testEnv.DaemonInfo.SwapLimit)
  22. defer setupTest(t)()
  23. client := testEnv.APIClient()
  24. ctx := context.Background()
  25. cID := container.Run(ctx, t, client, func(c *container.TestContainerConfig) {
  26. c.HostConfig.Resources = containertypes.Resources{
  27. Memory: 200 * 1024 * 1024,
  28. }
  29. })
  30. poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
  31. const (
  32. setMemory int64 = 314572800
  33. setMemorySwap int64 = 524288000
  34. )
  35. _, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
  36. Resources: containertypes.Resources{
  37. Memory: setMemory,
  38. MemorySwap: setMemorySwap,
  39. },
  40. })
  41. assert.NilError(t, err)
  42. inspect, err := client.ContainerInspect(ctx, cID)
  43. assert.NilError(t, err)
  44. assert.Check(t, is.Equal(setMemory, inspect.HostConfig.Memory))
  45. assert.Check(t, is.Equal(setMemorySwap, inspect.HostConfig.MemorySwap))
  46. memoryFile := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
  47. if testEnv.DaemonInfo.CgroupVersion == "2" {
  48. memoryFile = "/sys/fs/cgroup/memory.max"
  49. }
  50. res, err := container.Exec(ctx, client, cID,
  51. []string{"cat", memoryFile})
  52. assert.NilError(t, err)
  53. assert.Assert(t, is.Len(res.Stderr(), 0))
  54. assert.Equal(t, 0, res.ExitCode)
  55. assert.Check(t, is.Equal(strconv.FormatInt(setMemory, 10), strings.TrimSpace(res.Stdout())))
  56. // see ConvertMemorySwapToCgroupV2Value() for the convention:
  57. // https://github.com/opencontainers/runc/commit/c86be8a2c118ca7bad7bbe9eaf106c659a83940d
  58. if testEnv.DaemonInfo.CgroupVersion == "2" {
  59. res, err = container.Exec(ctx, client, cID,
  60. []string{"cat", "/sys/fs/cgroup/memory.swap.max"})
  61. assert.NilError(t, err)
  62. assert.Assert(t, is.Len(res.Stderr(), 0))
  63. assert.Equal(t, 0, res.ExitCode)
  64. assert.Check(t, is.Equal(strconv.FormatInt(setMemorySwap-setMemory, 10), strings.TrimSpace(res.Stdout())))
  65. } else {
  66. res, err = container.Exec(ctx, client, cID,
  67. []string{"cat", "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"})
  68. assert.NilError(t, err)
  69. assert.Assert(t, is.Len(res.Stderr(), 0))
  70. assert.Equal(t, 0, res.ExitCode)
  71. assert.Check(t, is.Equal(strconv.FormatInt(setMemorySwap, 10), strings.TrimSpace(res.Stdout())))
  72. }
  73. }
  74. func TestUpdateCPUQuota(t *testing.T) {
  75. skip.If(t, testEnv.DaemonInfo.CgroupDriver == "none")
  76. defer setupTest(t)()
  77. client := testEnv.APIClient()
  78. ctx := context.Background()
  79. cID := container.Run(ctx, t, client)
  80. for _, test := range []struct {
  81. desc string
  82. update int64
  83. }{
  84. {desc: "some random value", update: 15000},
  85. {desc: "a higher value", update: 20000},
  86. {desc: "a lower value", update: 10000},
  87. {desc: "unset value", update: -1},
  88. } {
  89. if testEnv.DaemonInfo.CgroupVersion == "2" {
  90. // On v2, specifying CPUQuota without CPUPeriod is currently broken:
  91. // https://github.com/opencontainers/runc/issues/2456
  92. // As a workaround we set them together.
  93. _, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
  94. Resources: containertypes.Resources{
  95. CPUQuota: test.update,
  96. CPUPeriod: 100000,
  97. },
  98. })
  99. assert.NilError(t, err)
  100. } else {
  101. _, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
  102. Resources: containertypes.Resources{
  103. CPUQuota: test.update,
  104. },
  105. })
  106. assert.NilError(t, err)
  107. }
  108. inspect, err := client.ContainerInspect(ctx, cID)
  109. assert.NilError(t, err)
  110. assert.Check(t, is.Equal(test.update, inspect.HostConfig.CPUQuota))
  111. if testEnv.DaemonInfo.CgroupVersion == "2" {
  112. res, err := container.Exec(ctx, client, cID,
  113. []string{"/bin/cat", "/sys/fs/cgroup/cpu.max"})
  114. assert.NilError(t, err)
  115. assert.Assert(t, is.Len(res.Stderr(), 0))
  116. assert.Equal(t, 0, res.ExitCode)
  117. quotaPeriodPair := strings.Fields(res.Stdout())
  118. quota := quotaPeriodPair[0]
  119. if test.update == -1 {
  120. assert.Check(t, is.Equal("max", quota))
  121. } else {
  122. assert.Check(t, is.Equal(strconv.FormatInt(test.update, 10), quota))
  123. }
  124. } else {
  125. res, err := container.Exec(ctx, client, cID,
  126. []string{"/bin/cat", "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"})
  127. assert.NilError(t, err)
  128. assert.Assert(t, is.Len(res.Stderr(), 0))
  129. assert.Equal(t, 0, res.ExitCode)
  130. assert.Check(t, is.Equal(strconv.FormatInt(test.update, 10), strings.TrimSpace(res.Stdout())))
  131. }
  132. }
  133. }
  134. func TestUpdatePidsLimit(t *testing.T) {
  135. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  136. skip.If(t, testEnv.DaemonInfo.CgroupDriver == "none")
  137. skip.If(t, !testEnv.DaemonInfo.PidsLimit)
  138. defer setupTest(t)()
  139. apiClient := testEnv.APIClient()
  140. oldAPIclient := request.NewAPIClient(t, client.WithVersion("1.24"))
  141. ctx := context.Background()
  142. intPtr := func(i int64) *int64 {
  143. return &i
  144. }
  145. for _, test := range []struct {
  146. desc string
  147. oldAPI bool
  148. initial *int64
  149. update *int64
  150. expect int64
  151. expectCg string
  152. }{
  153. {desc: "update from none", update: intPtr(32), expect: 32, expectCg: "32"},
  154. {desc: "no change", initial: intPtr(32), expect: 32, expectCg: "32"},
  155. {desc: "update lower", initial: intPtr(32), update: intPtr(16), expect: 16, expectCg: "16"},
  156. {desc: "update on old api ignores value", oldAPI: true, initial: intPtr(32), update: intPtr(16), expect: 32, expectCg: "32"},
  157. {desc: "unset limit with zero", initial: intPtr(32), update: intPtr(0), expect: 0, expectCg: "max"},
  158. {desc: "unset limit with minus one", initial: intPtr(32), update: intPtr(-1), expect: 0, expectCg: "max"},
  159. {desc: "unset limit with minus two", initial: intPtr(32), update: intPtr(-2), expect: 0, expectCg: "max"},
  160. } {
  161. c := apiClient
  162. if test.oldAPI {
  163. c = oldAPIclient
  164. }
  165. t.Run(test.desc, func(t *testing.T) {
  166. // Using "network=host" to speed up creation (13.96s vs 6.54s)
  167. cID := container.Run(ctx, t, apiClient, container.WithPidsLimit(test.initial), container.WithNetworkMode("host"))
  168. _, err := c.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
  169. Resources: containertypes.Resources{
  170. PidsLimit: test.update,
  171. },
  172. })
  173. assert.NilError(t, err)
  174. inspect, err := c.ContainerInspect(ctx, cID)
  175. assert.NilError(t, err)
  176. assert.Assert(t, inspect.HostConfig.Resources.PidsLimit != nil)
  177. assert.Equal(t, *inspect.HostConfig.Resources.PidsLimit, test.expect)
  178. ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
  179. defer cancel()
  180. pidsFile := "/sys/fs/cgroup/pids/pids.max"
  181. if testEnv.DaemonInfo.CgroupVersion == "2" {
  182. pidsFile = "/sys/fs/cgroup/pids.max"
  183. }
  184. res, err := container.Exec(ctx, c, cID, []string{"cat", pidsFile})
  185. assert.NilError(t, err)
  186. assert.Assert(t, is.Len(res.Stderr(), 0))
  187. out := strings.TrimSpace(res.Stdout())
  188. assert.Equal(t, out, test.expectCg)
  189. })
  190. }
  191. }