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