Browse code

Fix docker stats show wrong memory limit when do docker update

When a container create with -m 100m and then docker update other
cgroup settings such as --cpu-quota, the memory limit show by
docker stats will become the default value but not the 100m.

Signed-off-by: Lei Jitang <leijitang@huawei.com>

Lei Jitang authored on 2015/12/30 10:33:16
Showing 2 changed files
... ...
@@ -601,7 +601,7 @@ func (container *Container) UpdateContainer(hostConfig *container.HostConfig) er
601 601
 	// the command so we can update configs to the real world.
602 602
 	if container.IsRunning() {
603 603
 		container.Lock()
604
-		updateCommand(container.Command, resources)
604
+		updateCommand(container.Command, *cResources)
605 605
 		container.Unlock()
606 606
 	}
607 607
 
... ...
@@ -3,8 +3,11 @@
3 3
 package main
4 4
 
5 5
 import (
6
+	"encoding/json"
7
+	"fmt"
6 8
 	"strings"
7 9
 
10
+	"github.com/docker/docker/api/types"
8 11
 	"github.com/docker/docker/pkg/integration/checker"
9 12
 	"github.com/go-check/check"
10 13
 )
... ...
@@ -160,3 +163,34 @@ func (s *DockerSuite) TestUpdateKernelMemory(c *check.C) {
160 160
 	out, _ = dockerCmd(c, "exec", name, "cat", file)
161 161
 	c.Assert(strings.TrimSpace(out), checker.Equals, "104857600")
162 162
 }
163
+
164
+func (s *DockerSuite) TestUpdateStats(c *check.C) {
165
+	testRequires(c, DaemonIsLinux)
166
+	testRequires(c, memoryLimitSupport)
167
+	testRequires(c, cpuCfsQuota)
168
+	name := "foo"
169
+	dockerCmd(c, "run", "-d", "-ti", "--name", name, "-m", "500m", "busybox")
170
+
171
+	c.Assert(waitRun(name), checker.IsNil)
172
+
173
+	getMemLimit := func(id string) uint64 {
174
+		resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
175
+		c.Assert(err, checker.IsNil)
176
+		c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json")
177
+
178
+		var v *types.Stats
179
+		err = json.NewDecoder(body).Decode(&v)
180
+		c.Assert(err, checker.IsNil)
181
+		body.Close()
182
+
183
+		return v.MemoryStats.Limit
184
+	}
185
+	preMemLimit := getMemLimit(name)
186
+
187
+	dockerCmd(c, "update", "--cpu-quota", "2000", name)
188
+
189
+	curMemLimit := getMemLimit(name)
190
+
191
+	c.Assert(preMemLimit, checker.Equals, curMemLimit)
192
+
193
+}